SRNI-2005/ctf
0
1from __future__ import division # Use floating point for math calculations2 3import math4 5from CTFd.models import Solves6from CTFd.utils.modes import get_model7 8 9def get_solve_count(challenge):10 Model = get_model()11 12 solve_count = (13 Solves.query.join(Model, Solves.account_id == Model.id)14 .filter(15 Solves.challenge_id == challenge.id,16 Model.hidden == False,17 Model.banned == False,18 )19 .count()20 )21 return solve_count22 23 24def linear(challenge):25 solve_count = get_solve_count(challenge)26 27 # If the solve count is 0 we shouldn't manipulate the solve count to28 # let the math update back to normal29 if solve_count != 0:30 # We subtract -1 to allow the first solver to get max point value31 solve_count -= 132 33 value = challenge.initial - (challenge.decay * solve_count)34 35 value = math.ceil(value)36 37 if value < challenge.minimum:38 value = challenge.minimum39 40 return value41 42 43def logarithmic(challenge):44 solve_count = get_solve_count(challenge)45 46 # If the solve count is 0 we shouldn't manipulate the solve count to47 # let the math update back to normal48 if solve_count != 0:49 # We subtract -1 to allow the first solver to get max point value50 solve_count -= 151 52 # Handle situations where admins have entered a 0 decay53 # This is invalid as it can cause a division by zero54 if challenge.decay == 0:55 challenge.decay = 156 57 # It is important that this calculation takes into account floats.58 # Hence this file uses from __future__ import division59 value = (60 ((challenge.minimum - challenge.initial) / (challenge.decay**2))61 * (solve_count**2)62 ) + challenge.initial63 64 value = math.ceil(value)65 66 if value < challenge.minimum:67 value = challenge.minimum68 69 return value70 71 72DECAY_FUNCTIONS = {73 "linear": linear,74 "logarithmic": logarithmic,75}76 