Skip to content

Commit

Permalink
Add balance field to Groups.standings
Browse files Browse the repository at this point in the history
  • Loading branch information
kostrykin committed Feb 1, 2024
1 parent 1a2ea39 commit 0093ea1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ <h2>{{ stage | stage_name:forloop.counter }} <small class="text-muted">Current S
<th scope="col">Attendee</th>
<th scope="col">Matches</th>
<th scope="col">Wins</th>
<th scope="col">Losses</th>
<th scope="col">Ties</th>
<th scope="col">Balance</th>
<th scope="col">Points</th>
</tr>
</thead>
Expand All @@ -154,8 +154,8 @@ <h2>{{ stage | stage_name:forloop.counter }} <small class="text-muted">Current S
<td>{{ row.participant }}</td>
<td>{{ row.matches }}</td>
<td>{{ row.win_count }}</td>
<td>{{ row.loss_count }}</td>
<td>{{ row.draw_count }}</td>
<td>{{ row.balance }}</td>
<td>{{ row.points }}</td>
</tr>

Expand Down
7 changes: 5 additions & 2 deletions tournaments/tournaments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def create_division_schedule(participants, with_returns = False):


def get_stats(participant, filters = dict()):
row = dict(participant = participant, win_count = 0, loss_count = 0, draw_count = 0, matches = 0)
row = dict(participant = participant, win_count = 0, loss_count = 0, draw_count = 0, matches = 0, balance = 0)
for fixture in participant.fixtures1.filter(**filters) | participant.fixtures2.filter(**filters):

# Only account for confirmed scores.
Expand All @@ -427,6 +427,9 @@ def get_stats(participant, filters = dict()):
elif scores[0] < scores[1]:
row['loss_count'] += 1

# Account score balance.
row['balance'] += scores[0] - scores[1]

return row


Expand Down Expand Up @@ -473,7 +476,7 @@ def standings(self):
standings = list()
for group in self.groups_info:
group_standings = [self.get_standings(participant) for participant in self.tournament.participants.filter(id__in = group)]
group_standings.sort(key = lambda row: (row['points'], row['matches'], row['participant'].id), reverse = True)
group_standings.sort(key = lambda row: (row['points'], row['balance'], row['matches'], row['participant'].id), reverse = True)
standings.append(group_standings)
return standings

Expand Down
49 changes: 37 additions & 12 deletions tournaments/tournaments/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
{
'participant': User.objects.get(id = 3),
Expand All @@ -413,6 +414,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
{
'participant': User.objects.get(id = 1),
Expand All @@ -421,6 +423,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
],
[
Expand All @@ -431,6 +434,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
{
'participant': User.objects.get(id = 2),
Expand All @@ -439,6 +443,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
],
]
Expand All @@ -456,22 +461,25 @@ def test_standings(self):
'draw_count': 0,
'points': 3,
'matches': 1,
'balance': 1,
},
{
'participant': User.objects.get(id = 3),
'participant': User.objects.get(id = 1),
'win_count': 0,
'loss_count': 1,
'loss_count': 0,
'draw_count': 0,
'points': 0,
'matches': 1,
'matches': 0,
'balance': 0,
},
{
'participant': User.objects.get(id = 1),
'participant': User.objects.get(id = 3),
'win_count': 0,
'loss_count': 0,
'loss_count': 1,
'draw_count': 0,
'points': 0,
'matches': 0,
'matches': 1,
'balance': -1,
},
],
[
Expand All @@ -482,6 +490,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
{
'participant': User.objects.get(id = 2),
Expand All @@ -490,6 +499,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
],
]
Expand All @@ -507,6 +517,7 @@ def test_standings(self):
'draw_count': 1,
'points': 4,
'matches': 2,
'balance': 1,
},
{
'participant': User.objects.get(id = 1),
Expand All @@ -515,6 +526,7 @@ def test_standings(self):
'draw_count': 1,
'points': 1,
'matches': 1,
'balance': 0,
},
{
'participant': User.objects.get(id = 3),
Expand All @@ -523,6 +535,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 1,
'balance': -1,
},
],
[
Expand All @@ -533,6 +546,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
{
'participant': User.objects.get(id = 2),
Expand All @@ -541,6 +555,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
],
]
Expand All @@ -552,20 +567,22 @@ def test_standings(self):
expected_standings = [
[
{
'participant': User.objects.get(id = 5),
'participant': User.objects.get(id = 1),
'win_count': 1,
'loss_count': 0,
'draw_count': 1,
'points': 4,
'matches': 2,
'balance': 3,
},
{
'participant': User.objects.get(id = 1),
'participant': User.objects.get(id = 5),
'win_count': 1,
'loss_count': 0,
'draw_count': 1,
'points': 4,
'matches': 2,
'balance': 1,
},
{
'participant': User.objects.get(id = 3),
Expand All @@ -574,6 +591,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 2,
'balance': -4,
},
],
[
Expand All @@ -584,6 +602,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
{
'participant': User.objects.get(id = 2),
Expand All @@ -592,6 +611,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 0,
'balance': 0,
},
],
]
Expand All @@ -602,20 +622,22 @@ def test_standings(self):
expected_standings = [
[
{
'participant': User.objects.get(id = 5),
'participant': User.objects.get(id = 1),
'win_count': 1,
'loss_count': 0,
'draw_count': 1,
'points': 4,
'matches': 2,
'balance': 3,
},
{
'participant': User.objects.get(id = 1),
'participant': User.objects.get(id = 5),
'win_count': 1,
'loss_count': 0,
'draw_count': 1,
'points': 4,
'matches': 2,
'balance': 1,
},
{
'participant': User.objects.get(id = 3),
Expand All @@ -624,6 +646,7 @@ def test_standings(self):
'draw_count': 0,
'points': 0,
'matches': 2,
'balance': -4,
},
],
[
Expand All @@ -634,6 +657,7 @@ def test_standings(self):
'draw_count': 1,
'points': 1,
'matches': 1,
'balance': 0,
},
{
'participant': User.objects.get(id = 2),
Expand All @@ -642,6 +666,7 @@ def test_standings(self):
'draw_count': 1,
'points': 1,
'matches': 1,
'balance': 0,
},
],
]
Expand All @@ -653,11 +678,11 @@ def test_placements(self):
mode = self.test_standings()
expected_placements = [
[
User.objects.get(id = 5),
User.objects.get(id = 1),
User.objects.get(id = 4),
],
[
User.objects.get(id = 1),
User.objects.get(id = 5),
User.objects.get(id = 2),
],
[
Expand Down

0 comments on commit 0093ea1

Please sign in to comment.