Skip to content

Commit

Permalink
Finished the time based puzzle unlocking
Browse files Browse the repository at this point in the history
  • Loading branch information
dlareau committed Feb 13, 2020
1 parent 1ed1283 commit 3398289
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
9 changes: 3 additions & 6 deletions huntserver/management/commands/runupdates.py
Expand Up @@ -5,7 +5,7 @@
from huntserver.models import Hunt, HintUnlockPlan


class Command(BaseCommand):
class RunUpdates(BaseCommand):
help = 'Runs all time related updates for the huntserver app'

def handle(self, *args, **options):
Expand Down Expand Up @@ -34,8 +34,5 @@ def handle(self, *args, **options):
if(diff_minutes >= 1):
new_points = curr_hunt.points_per_minute * diff_minutes
curr_hunt.team_set.all().update(num_unlock_points=F('num_unlock_points') + new_points)
# for team in curr_hunt.team_set.all():
# unlockable_puzzles = curr_hunt.puzzle_set.exclude()
# write function on puzzle that takes team and checks if it is/should be unlocked
# filter out all puzzles that can't possibly be unlocked because time then run the
# unlock function on each of them
for team in curr_hunt.team_set.all():
team.unlock_puzzles()
30 changes: 26 additions & 4 deletions huntserver/models.py
Expand Up @@ -376,11 +376,29 @@ def unlock_puzzles(self):
# See if the number of points is enough to unlock any given puzzle
puzzles = puzzles.difference(self.unlocked.all())
for puzzle in puzzles:
if(puzzle.num_required_to_unlock <= mapping[puzzle.puzzle_number]):
logger.info("Team %s unlocked puzzle %s" % (str(self.team_name),
s_unlock = (puzzle.num_required_to_unlock <= mapping[puzzle.puzzle_number])
p_unlock = (self.num_unlock_points >= puzzle.points_cost)

if(puzzle.unlock_type == Puzzle.SOLVES_UNLOCK and s_unlock):
logger.info("Team %s unlocked puzzle %s with solves" % (str(self.team_name),
str(puzzle.puzzle_id)))
Unlock.objects.create(team=self, puzzle=puzzle, time=timezone.now())
elif(puzzle.unlock_type == Puzzle.POINTS_UNLOCK and p_unlock):
logger.info("Team %s unlocked puzzle %s with points" % (str(self.team_name),
str(puzzle.puzzle_id)))
Unlock.objects.create(team=self, puzzle=puzzle, time=timezone.now())
elif(puzzle.unlock_type == Puzzle.EITHER_UNLOCK and (s_unlock or p_unlock)):
logger.info("Team %s unlocked puzzle %s with either" % (str(self.team_name),
str(puzzle.puzzle_id)))
Unlock.objects.create(team=self, puzzle=puzzle, time=timezone.now())
elif(puzzle.unlock_type == Puzzle.BOTH_UNLOCK and (s_unlock and p_unlock)):
logger.info("Team %s unlocked puzzle %s with both" % (str(self.team_name),
str(puzzle.puzzle_id)))
Unlock.objects.create(team=self, puzzle=puzzle, time=timezone.now())

# The way this works currently sucks, it should only be called once per solve
# Right now that one place is Submission.respond()
# It also only does # of solves based unlocks. Time based unlocks are done in run_updates
def unlock_hints(self):
num_solved = self.solved.count()
plans = self.hunt.hintunlockplan_set
Expand Down Expand Up @@ -497,8 +515,12 @@ def respond(self):
if(not self.puzzle.hunt.is_public):
if(self.puzzle not in self.team.solved.all()):
self.create_solve()
self.team.unlock_puzzles()
self.team.unlock_hints()
t = self.team
t.num_unlock_points = models.F('num_unlock_points') + self.puzzle.points_value
t.save()
t.refresh_from_db()
t.unlock_puzzles()
t.unlock_hints() # The one and only place to call unlock hints

# Check against regexes
for resp in self.puzzle.response_set.all():
Expand Down

0 comments on commit 3398289

Please sign in to comment.