From 2f6362360b26f382ef2451769840f7f9d780e049 Mon Sep 17 00:00:00 2001 From: Danhia Date: Thu, 3 Feb 2022 20:27:55 +0100 Subject: [PATCH 1/2] added dynamic scoring for permanent platform, let's hope nothing breaks --- src/ctfs/management/__init__.py | 0 src/ctfs/management/commands/__init__.py | 0 .../management/commands/actualize_points.py | 28 +++++++++++++++++++ src/ctfs/views.py | 20 +++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 src/ctfs/management/__init__.py create mode 100644 src/ctfs/management/commands/__init__.py create mode 100644 src/ctfs/management/commands/actualize_points.py diff --git a/src/ctfs/management/__init__.py b/src/ctfs/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/ctfs/management/commands/__init__.py b/src/ctfs/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/ctfs/management/commands/actualize_points.py b/src/ctfs/management/commands/actualize_points.py new file mode 100644 index 0000000..39656e4 --- /dev/null +++ b/src/ctfs/management/commands/actualize_points.py @@ -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() \ No newline at end of file diff --git a/src/ctfs/views.py b/src/ctfs/views.py index b7f1bc8..e1680ef 100644 --- a/src/ctfs/views.py +++ b/src/ctfs/views.py @@ -5,6 +5,8 @@ from .models import Category, CTF, CTF_flags from .forms import submit_flag from accounts.models import UserProfileInfo from django.utils.translation import get_language +from math import log +from accounts.models import UserProfileInfo def get_description_by_lang(ctf): lang = get_language() @@ -19,6 +21,23 @@ def get_description_by_lang(ctf): ret = ctf.description_ru 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): cat = get_object_or_404(Category, slug=cat_slug) 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.score += ctf_info.points 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()}) else: return render(request, 'ctfs/ctf_info.html', { 'ctf' : ctf_info, 'solved_list': solved_list, 'failed': True, 'description': description, 'date': timezone.now()}) From d5e2554602010041644515cbf1ecc0cc8cc5bc94 Mon Sep 17 00:00:00 2001 From: Danhia Date: Thu, 3 Feb 2022 21:02:55 +0100 Subject: [PATCH 2/2] hotfix for dynamic scoring --- src/ctfs/management/commands/actualize_points.py | 2 +- src/ctfs/views.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ctfs/management/commands/actualize_points.py b/src/ctfs/management/commands/actualize_points.py index 39656e4..516764f 100644 --- a/src/ctfs/management/commands/actualize_points.py +++ b/src/ctfs/management/commands/actualize_points.py @@ -15,7 +15,7 @@ class Command(BaseCommand): nb_solves = len(solves) if nb_solves > 0: - new_points = 200 - int(log(nb_solves)*8.5)*5 + new_points = max(200 - int(log(nb_solves)*8.5)*5, 5) else: new_points = 200 diff --git a/src/ctfs/views.py b/src/ctfs/views.py index e1680ef..354f131 100644 --- a/src/ctfs/views.py +++ b/src/ctfs/views.py @@ -27,7 +27,7 @@ def actualize_points(ctf): solves = CTF_flags.objects.filter(ctf=ctf) nb_solves = len(solves) - new_points = 200 - int(log(nb_solves)*8.5)*5 + new_points = max(200 - int(log(nb_solves)*8.5)*5, 5) if new_points != ctf.points: diff = ctf.points - new_points