Limit winning streaks to 5 wins

This commit is contained in:
Peter J. Holzer 2021-08-29 10:45:27 +02:00
parent ea0655dc77
commit b7373d2b98
1 changed files with 10 additions and 0 deletions

View File

@ -15,11 +15,15 @@ def compare(request):
if "left" not in request.session: if "left" not in request.session:
left_pic = random_pic() left_pic = random_pic()
request.session["left"] = left_pic.id request.session["left"] = left_pic.id
request.session["left_count"] = 0
if "right" not in request.session: if "right" not in request.session:
right_pic = random_pic() right_pic = random_pic()
request.session["right"] = right_pic.id request.session["right"] = right_pic.id
request.session["right_count"] = 0
left_pic = models.Picture.objects.get(id=request.session["left"]) left_pic = models.Picture.objects.get(id=request.session["left"])
right_pic = models.Picture.objects.get(id=request.session["right"]) right_pic = models.Picture.objects.get(id=request.session["right"])
request.session["left_count"] += 1
request.session["right_count"] += 1
context = { context = {
"left": left_pic, "left": left_pic,
"right": right_pic, "right": right_pic,
@ -48,6 +52,9 @@ def left_wins(request):
right.elo -= adjust right.elo -= adjust
right.save() right.save()
log.info("after: left=%f, right=%f", left.elo, right.elo) log.info("after: left=%f, right=%f", left.elo, right.elo)
log.info("counts: left=%d, right=%d", request.session["left_count"], request.session["right_count"])
if request.session["left_count"] >= 5:
del request.session["left"]
del request.session["right"] del request.session["right"]
return HttpResponseRedirect(reverse('compare')) return HttpResponseRedirect(reverse('compare'))
@ -65,6 +72,9 @@ def right_wins(request):
right.elo += adjust right.elo += adjust
right.save() right.save()
log.info("after: left=%f, right=%f", left.elo, right.elo) log.info("after: left=%f, right=%f", left.elo, right.elo)
log.info("counts: left=%d, right=%d", request.session["left_count"], request.session["right_count"])
if request.session["right_count"] >= 5:
del request.session["right"]
del request.session["left"] del request.session["left"]
return HttpResponseRedirect(reverse('compare')) return HttpResponseRedirect(reverse('compare'))