added dynamic scoring for permanent platform, let's hope nothing breaks

This commit is contained in:
Danhia 2022-02-03 20:27:55 +01:00
parent e806b23812
commit f8eef0ef3d
4 changed files with 48 additions and 0 deletions

View File

View File

View File

@ -0,0 +1,28 @@
from collections import defaultdict
from django.core.management.base import BaseCommand, CommandError
from accounts.models import UserProfileInfo
from ctfs.models import CTF_flags, CTF
from math import log
class Command(BaseCommand):
help = 'Actualize challenges points based on number of solves'
def handle(self, *args, **options):
challenges = CTF.objects.filter(event=None, disabled=False).exclude(category__name="-Intro-")
for ctf in challenges:
solves = CTF_flags.objects.filter(ctf=ctf)
nb_solves = len(solves)
if nb_solves > 0:
new_points = 200 - int(log(nb_solves)*8.5)*5
else:
new_points = 200
if new_points != ctf.points:
diff = ctf.points - new_points
ctf.points = new_points
ctf.save()
for s in solves:
s.user.userprofileinfo.score -= diff
s.user.userprofileinfo.save()

View File

@ -5,6 +5,8 @@ from .models import Category, CTF, CTF_flags
from .forms import submit_flag from .forms import submit_flag
from accounts.models import UserProfileInfo from accounts.models import UserProfileInfo
from django.utils.translation import get_language from django.utils.translation import get_language
from math import log
from accounts.models import UserProfileInfo
def get_description_by_lang(ctf): def get_description_by_lang(ctf):
lang = get_language() lang = get_language()
@ -19,6 +21,23 @@ def get_description_by_lang(ctf):
ret = ctf.description_ru ret = ctf.description_ru
return ret return ret
def actualize_points(ctf):
if ctf.category.name == "-Intro-":
return
solves = CTF_flags.objects.filter(ctf=ctf)
nb_solves = len(solves)
new_points = 200 - int(log(nb_solves)*8.5)*5
if new_points != ctf.points:
diff = ctf.points - new_points
ctf.points = new_points
ctf.save()
for s in solves:
s.user.userprofileinfo.score -= diff
s.user.userprofileinfo.save()
def category(request, cat_slug): def category(request, cat_slug):
cat = get_object_or_404(Category, slug=cat_slug) cat = get_object_or_404(Category, slug=cat_slug)
ctfs = CTF.objects.filter(category=cat, event=None, disabled=False).order_by('points') ctfs = CTF.objects.filter(category=cat, event=None, disabled=False).order_by('points')
@ -46,6 +65,7 @@ def ctf(request, cat_slug, ctf_slug):
profil.last_submission_date = timezone.now() profil.last_submission_date = timezone.now()
profil.score += ctf_info.points profil.score += ctf_info.points
profil.save() profil.save()
actualize_points(ctf_info)
return render(request, 'ctfs/ctf_info.html', { 'ctf' : ctf_info, 'solved_list': solved_list, 'valitated': True, 'description': description, 'date': timezone.now()}) return render(request, 'ctfs/ctf_info.html', { 'ctf' : ctf_info, 'solved_list': solved_list, 'valitated': True, 'description': description, 'date': timezone.now()})
else: else:
return render(request, 'ctfs/ctf_info.html', { 'ctf' : ctf_info, 'solved_list': solved_list, 'failed': True, 'description': description, 'date': timezone.now()}) return render(request, 'ctfs/ctf_info.html', { 'ctf' : ctf_info, 'solved_list': solved_list, 'failed': True, 'description': description, 'date': timezone.now()})