Diverse Umbauarbeiten für das neue Ranking.

This commit is contained in:
2017-12-22 10:51:20 +01:00
parent b20b988e5d
commit 1fdf88c6d2
6 changed files with 243 additions and 62 deletions

View File

@@ -347,9 +347,12 @@ class KyuDanRanking(models.Model):
hanchan_count = models.PositiveIntegerField(default=0)
legacy_date = models.DateField(blank=True, null=True)
legacy_dan = models.PositiveSmallIntegerField(blank=True, null=True)
legacy_dan_points = models.PositiveIntegerField(default=0)
legacy_kyu_points = models.PositiveIntegerField(default=0)
legacy_hanchan_count = models.PositiveIntegerField(default=0)
legacy_dan_points = models.PositiveIntegerField(blank=True, null=True)
legacy_kyu = models.PositiveSmallIntegerField(blank=True, null=True)
legacy_kyu_points = models.PositiveIntegerField(blank=True, null=True)
legacy_hanchan_count = models.PositiveIntegerField(blank=True, null=True)
legacy_good_hanchans = models.PositiveIntegerField(blank=True, null=True)
legacy_won_hanchans = models.PositiveIntegerField(blank=True, null=True)
wins_in_a_row = models.PositiveIntegerField(default=0)
last_hanchan_date = models.DateTimeField(blank=True, null=True)
objects = managers.KyuDanRankingManager()
@@ -371,12 +374,15 @@ class KyuDanRanking(models.Model):
das er einen Dan Rang aufsteigt. Dies wird als Kommentar abgespeichert,
um es besser nachvollziehen zu können.
"""
if self.dan and hanchan.placement == 1:
if not self.dan or not settings.DAN_3_WINS_IN_A_ROW:
return
if hanchan.placement == 1:
self.wins_in_a_row += 1
else:
self.wins_in_a_row = 0
return
if self.wins_in_a_row >= 3 and self.dan < 9:
if self.dan and self.wins_in_a_row >= 3 and self.dan < 9:
LOGGER.info(
'adding bonuspoints for 3 wins in a row for %s', self.user)
new_dan_rank = self.dan + 1
@@ -397,6 +403,7 @@ class KyuDanRanking(models.Model):
bonus_points, new_dan_rank)
self.dan_points += bonus_points
self.wins_in_a_row = 0
self.update_rank()
def append_tournament_bonuspoints(self, hanchan):
"""
@@ -455,18 +462,17 @@ class KyuDanRanking(models.Model):
# Setze alles auf die legacy Werte und berechne alles von neuem.
self.dan = self.legacy_dan
self.dan_points = self.legacy_dan_points or 0
self.kyu = None
self.max_dan_points = self.dan_points
self.kyu = self.legacy_kyu
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.good_hanchans = self.legacy_good_hanchans or 0
self.won_hanchans = self.legacy_won_hanchans or 0
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
self.update_rank()
since = timezone.make_aware(datetime.combine(
self.legacy_date,
time(23, 59, 59))) if self.legacy_date else None
elif self.last_hanchan_date:
since = self.last_hanchan_date
elif self.legacy_date:
@@ -481,40 +487,27 @@ class KyuDanRanking(models.Model):
valid_hanchans = valid_hanchans.filter(start__gt=since)
if until:
valid_hanchans = valid_hanchans.filter(start__lte=until)
self.hanchan_count += valid_hanchans.count()
for hanchan in valid_hanchans:
self.hanchan_count += 1
hanchan.get_playerdata(self.user)
if since and hanchan.start < since:
print(hanchan, "<", since, "no recalc")
LOGGER.debug(hanchan, "<", since, "no recalc")
self.dan_points += hanchan.dan_points or 0
self.kyu_points += hanchan.kyu_points or 0
self.update_rank()
else:
hanchan.bonus_points = 0
hanchan.player_comment = u""
hanchan.player_comment = ""
self.update_hanchan_points(hanchan)
if hanchan.event.mahjong_tournament:
self.append_tournament_bonuspoints(hanchan)
self.update_rank()
self.append_3_in_a_row_bonuspoints(hanchan)
self.update_rank()
hanchan.update_playerdata(self.user)
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: '
'%(dan_points)d, bonus points: %(bonus_points)d',
{'id': hanchan.pk, 'start': hanchan.start,
'placement': hanchan.placement,
'score': hanchan.game_score,
'kyu_points': hanchan.kyu_points or 0,
'dan_points': hanchan.dan_points or 0,
'bonus_points': hanchan.bonus_points or 0}
)
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
self.save(force_update=True)
def update_hanchan_points(self, hanchan):
@@ -572,25 +565,27 @@ class KyuDanRanking(models.Model):
self.kyu_points += hanchan.kyu_points
def update_rank(self):
print(self.user, self.dan, self.kyu)
old_dan = self.dan
if settings.DAN_ALLOW_DROP_DOWN and (self.dan or self.dan_points > 0):
self.dan = max((dan for min_points, dan in settings.DAN_RANKS if
self.dan_points > min_points))
elif self.dan or self.dan_points > 0:
self.dan = max((dan for min_points, dan in settings.DAN_RANKS if
self.max_dan_points > min_points))
# Update Dan ranking:
if self.dan or self.dan_points > 0:
if settings.DAN_ALLOW_DROP_DOWN:
self.dan = max((dan for min_points, dan in settings.DAN_RANKS
if self.dan_points > min_points))
else:
self.max_dan_points = max(self.max_dan_points, self.dan_points)
self.dan = max((dan for min_points, dan in settings.DAN_RANKS
if self.max_dan_points > min_points))
# jump from Kyu to Dan
elif self.kyu_points > 50:
self.dan = 1
self.dan_points = 0
self.kyu = None
self.kyu_points = 0
self.wins_in_a_row = 0
# update Kyu ranking_
else:
print(self, self.kyu_points)
self.kyu = max((kyu for min_points, kyu in settings.KYU_RANKS if
self.kyu_points > min_points))
self.wins_in_a_row = 0 if self.dan > old_dan else self.wins_in_a_row
self.kyu = min((kyu for min_points, kyu in settings.KYU_RANKS
if self.kyu_points > min_points))
class SeasonRanking(models.Model):