added new fields to KyuDanRanking that allow to pick up the calculation from the last state of the KyuDanRanking.

last_hanchan_date: it contains the start of the latest hanchan content for this players ranking.
wins_in_row: to save the currents wins in a row

Added option to calcuclate rankings until a given datetime.
This commit is contained in:
2017-11-19 16:14:59 +01:00
parent e6f2528a0e
commit d9e0d5596c
7 changed files with 189 additions and 85 deletions

View File

@@ -4,7 +4,7 @@
# werden dürfen.
from __future__ import division
from datetime import datetime, time
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
@@ -345,10 +345,10 @@ class KyuDanRanking(models.Model):
legacy_hanchan_count = models.PositiveIntegerField(default=0)
legacy_dan_points = models.PositiveIntegerField(default=0)
legacy_kyu_points = models.PositiveIntegerField(default=0)
wins_in_a_row = 0
wins_in_a_row = models.PositiveIntegerField(default=0)
last_hanchan_date = models.DateTimeField(blank=True, null=True)
objects = managers.KyuDanRankingManager()
class Meta(object):
ordering = ('-dan_points', 'dan', '-kyu_points')
verbose_name = _(u'Kyū/Dan Ranking')
@@ -436,28 +436,43 @@ class KyuDanRanking(models.Model):
else:
return reverse('player-kyu-score', args=[self.user.username])
def recalculate(self, hanchan_start=None):
def calculate(self, since=None, until=None, force_recalc=False):
"""
Fetches all valid Hanchans from this Player and recalculates his
Kyu/Dan Ranking.
"""
self.dan = None
self.dan_points = self.legacy_dan_points or 0
self.kyu = None
self.kyu_points = self.legacy_kyu_points or 0
self.hanchan_count = self.legacy_hanchan_count or 0
self.good_hanchans = 0
self.won_hanchans = 0
self.update_rank()
valid_hanchans = Hanchan.objects.confirmed_hanchans(user=self.user)
valid_hanchans = valid_hanchans.order_by('start')
if since and self.last_hanchan_date and since < self.last_hanchan_date:
force_recalc = True
if until and self.last_hanchan_date and until < self.last_hanchan_date:
force_recalc = True
if force_recalc:
# Setze alles auf die legacy Werte und berechne alles von neuem.
self.dan = None
self.dan_points = self.legacy_dan_points or 0
self.kyu = None
self.kyu_points = self.legacy_kyu_points or 0
self.hanchan_count = self.legacy_hanchan_count or 0
self.good_hanchans = 0
self.won_hanchans = 0
self.update_rank()
self.last_hanchan_date = None
if self.legacy_date:
since = timezone.make_aware(
datetime.combine(self.legacy_date, time(0, 0, 0)))
else:
since = None
else:
since = self.last_hanchan_date or self.legacy_date
LOGGER.info(
"recalculating Kyu/Dan points for %s since %s...",
self.user, str(hanchan_start)
"recalculating Kyu/Dan points for %(user)s since %(since)s...",
{'user': self.user, 'since': str(since)}
)
valid_hanchans = Hanchan.objects.confirmed_hanchans(
user=self.user).order_by('start')
if self.legacy_date:
valid_hanchans = valid_hanchans.filter(start__gt=self.legacy_date)
if since:
valid_hanchans = valid_hanchans.filter(start__gt=since)
if until:
valid_hanchans = valid_hanchans.filter(start__lte=until)
""" TODO: Hanchan Punkte nur neu berechnen wenn sie nach hachan_start
lagen. Es müssen aber alle durch die Schleife rennen, damit die Punkte
@@ -465,7 +480,7 @@ class KyuDanRanking(models.Model):
self.hanchan_count += valid_hanchans.count()
for hanchan in valid_hanchans:
hanchan.get_playerdata(self.user)
if hanchan_start and hanchan_start < hanchan.start:
if since and since < hanchan.start:
self.dan_points += hanchan.dan_points or 0
self.kyu_points += hanchan.kyu_points or 0
self.update_rank()
@@ -482,6 +497,7 @@ class KyuDanRanking(models.Model):
hanchan.save(recalculate=False)
self.won_hanchans += 1 if hanchan.placement == 1 else 0
self.good_hanchans += 1 if hanchan.placement == 2 else 0
self.last_hanchan_date = hanchan.start
LOGGER.debug(
'id: %(id)d, start: %(start)s, placement: %(placement)d, '
'score: %(score)d, kyu points: %(kyu_points)d, dan points: '
@@ -593,9 +609,9 @@ class SeasonRanking(models.Model):
def get_absolute_url(self):
return reverse('player-ladder-score', args=[self.user.username])
def recalculate(self):
def recalculate(self, until=None):
season_hanchans = Hanchan.objects.season_hanchans(
user=self.user, season=self.season)
user=self.user, season=self.season, until=until)
sum_placement = 0
sum_score = 0
self.placement = None