Skip to content

Commit

Permalink
Added hint lockout functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Dillon Lareau committed Feb 5, 2020
1 parent 32544f4 commit fa8f9db
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
13 changes: 6 additions & 7 deletions huntserver/hunt_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,14 @@ def puzzle_hint(request, puzzle_id):
"""
puzzle = get_object_or_404(Puzzle, puzzle_id__iexact=puzzle_id)
team = team_from_user_hunt(request.user, puzzle.hunt)
if(team is None):
return render(request, 'access_error.html', {'reason': "team"})

if request.method == 'POST':
# If the hunt isn't public and you aren't signed in, please stop...
if(team is None):
return HttpResponse('fail')
# Can't request a hint if there aren't any left
if(team.num_available_hints == 0):
return HttpResponseForbidden()

# Normal answer responses for a signed in user in an ongoing hunt
form = HintRequestForm(request.POST)
if form.is_valid():
h = Hint.objects.create(request=form.cleaned_data['request'], puzzle=puzzle, team=team,
Expand All @@ -316,8 +317,6 @@ def puzzle_hint(request, puzzle_id):

# Will return HTML rows for all submissions the user does not yet have
elif request.is_ajax():
if(team is None):
return HttpResponseNotFound('access denied')

# Find which objects the user hasn't seen yet and render them to HTML
last_date = datetime.strptime(request.GET.get("last_date"), '%Y-%m-%dT%H:%M:%S.%fZ')
Expand All @@ -336,7 +335,7 @@ def puzzle_hint(request, puzzle_id):
return HttpResponse(json.dumps(context))

else:
if(team is None or puzzle not in team.unlocked.all()):
if(puzzle not in team.unlocked.all()):
return render(request, 'access_error.html', {'reason': "puzzle"})

form = HintRequestForm()
Expand Down
11 changes: 6 additions & 5 deletions huntserver/management/commands/runupdates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class Command(BaseCommand):
def handle(self, *args, **options):
# Check hints
curr_hunt = Hunt.objects.get(is_current_hunt=True)
if(not curr_hunt.is_open):
return
num_min = (timezone.now() - curr_hunt.start_date).seconds / 60
for hup in curr_hunt.hintunlockplan_set.exclude(unlock_type=HintUnlockPlan.SOLVES_UNLOCK):
if((hup.unlock_type == hup.TIMED_UNLOCK
and hup.num_triggered < 1
and num_min > hup.unlock_parameter)
or (hup.unlock_type == hup.INTERVAL_UNLOCK
and num_min / hup.unlock_parameter < hup.num_triggered)):
if((hup.unlock_type == hup.TIMED_UNLOCK and
hup.num_triggered < 1 and num_min > hup.unlock_parameter)
or (hup.unlock_type == hup.INTERVAL_UNLOCK and
num_min / hup.unlock_parameter < hup.num_triggered)):
curr_hunt.team_set.all().update(num_available_hints=F('num_available_hints') + 1)
hup.num_triggered = hup.num_triggered + 1
hup.save()
11 changes: 11 additions & 0 deletions huntserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ def short_name(self):
def has_waiting_messages(self):
return max(self.last_received_message - self.last_seen_message, 0)

def hints_open_for_puzzle(self, puzzle):
if(self.num_available_hints > 0 or self.hint_set.count() > 0):
try:
unlock = Unlock.objects.get(team=self, puzzle=puzzle)
except Unlock.DoesNotExist:
return False

return (timezone.now() - unlock.time).total_seconds() > 60 * self.hunt.hint_lockout
else:
return False

def __str__(self):
return str(self.person_set.count()) + " (" + self.location + ") " + self.team_name

Expand Down
12 changes: 8 additions & 4 deletions huntserver/templates/puzzle.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "hunt_base.html" %}
{% load hunt_tags %}
{% block title %}Puzzle - {{ puzzle.puzzle_name }}{% endblock title %}

{% block includes %}
Expand Down Expand Up @@ -39,10 +40,13 @@ <h3><a href='{{ PROTECTED_URL }}solutions/{{ puzzle.puzzle_id }}_sol.pdf'>
<h3 style="float: right;">Published solution not available</h3>
{% endif %}
{% else %}
<br>
<h3><a href='/hints/{{ puzzle.puzzle_id }}/'>
Click here to request a hint
</a></h3>
{% hints_open team puzzle as show_hint_link %}
{% if show_hint_link %}
<br>
<h3><a href='/hints/{{ puzzle.puzzle_id }}/'>
Click here to request a hint
</a></h3>
{% endif %}
{% endif %}
</div>
{% if puzzle.hunt.is_public or not puzzle in team.solved.all %}
Expand Down
7 changes: 7 additions & 0 deletions huntserver/templatetags/hunt_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def contact_email(context):
return settings.CONTACT_EMAIL


@register.simple_tag()
def hints_open(team, puzzle):
if(team is None or puzzle is None):
return False
return team.hints_open_for_puzzle(puzzle)


@register.simple_tag(takes_context=True)
def shib_login_url(context, entityID, next_path):
if(context['request'].is_secure()):
Expand Down

0 comments on commit fa8f9db

Please sign in to comment.