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..516764f --- /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 = max(200 - int(log(nb_solves)*8.5)*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..354f131 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 = max(200 - int(log(nb_solves)*8.5)*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()})