dynamic scoring for events, i'm pretty sure everything is gonna break

This commit is contained in:
Danhia 2022-02-12 19:56:46 +01:00
parent 9f2d78ac33
commit aabba3ea53
4 changed files with 46 additions and 3 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 3.1.5 on 2022-02-12 18:27
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('events', '0007_event_auto_match'),
]
operations = [
migrations.AddField(
model_name='event',
name='dynamic',
field=models.BooleanField(default=False),
),
]

View File

@ -16,6 +16,7 @@ class Event(models.Model):
slug = models.SlugField(max_length=55)
team_size = models.PositiveIntegerField(default=1)
auto_match = models.BooleanField(default=False)
dynamic = models.BooleanField(default=False)
def __str__(self):
return self.name

View File

@ -29,7 +29,7 @@
{% elif errorform == True %}
<p>{% trans "Error while processing your request. (Invalid Form)" %}</p>
{% elif notsub == True %}
<span class="message error-msg">{% trans "Error: you're not registered to this event, so you can't register scores, fucking logic." %}</span>
<span class="message error-msg">{% trans "You must register to the event before submitting flags." %}</span>
{% elif noteam == True %}
<span class="message error-msg">{% trans "This is a team event, please create or join a team before submitting flags." %}</span>
{% if ctf.ctf_url %}

View File

@ -7,6 +7,7 @@ from ctfs.models import CTF, CTF_flags, Category
from django.utils.translation import get_language
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from math import log
def get_description_by_lang(ctf):
lang = get_language()
@ -21,6 +22,24 @@ def get_description_by_lang(ctf):
ret = ctf.description_ru
return ret
def actualize_points(ctf):
solves = CTF_flags.objects.filter(ctf=ctf)
nb_solves = len(solves)
new_points = max(50 - int(log(nb_solves)*2.5)*5, 5)
if new_points != ctf.points:
diff = ctf.points - new_points
ctf.points = new_points
ctf.save()
for s in solves:
player = EventPlayer.objects.get(event=ctf.event, user=s.user)
player.score -= diff
player.save()
if player.team:
player.team.score -= diff
player.team.save()
# Create your views here.
def events(request):
list_events = Event.objects.filter().order_by('-end_date', 'start_date')
@ -115,6 +134,7 @@ def event(request, event_slug):
def submit_event_flag(request, event_slug, chall_slug):
ev = get_object_or_404(Event, slug=event_slug)
response = redirect('events:event_chall_info', event_slug=event_slug, chall_slug=chall_slug)
flagged = False
if timezone.now() >= ev.end_date:
response['Location'] += '?EventIsOver=1'
@ -126,8 +146,10 @@ def submit_event_flag(request, event_slug, chall_slug):
response['Location'] += '?ChallengeNotFound=1'
return response
flagged = False
player = EventPlayer.objects.get(user=request.user, event=ev)
try:
player = EventPlayer.objects.get(event=ev, user=request.user)
except:
player = None
if player:
if ev.team_size > 1 and player.team is None:
@ -160,6 +182,8 @@ def submit_event_flag(request, event_slug, chall_slug):
player.team.last_submission_date = timezone.now()
player.team.score += ctf_info.points
player.team.save()
if ev.dynamic:
actualize_points(ctf_info)
response['Location'] += '?Congrat=1'
return response
else: