summaryrefslogtreecommitdiff
path: root/model.py
diff options
context:
space:
mode:
authorKaran Jayachandra <karan.jayachandra@nxp.com>2024-10-04 18:39:27 +0200
committerKaran Jayachandra <karan.jayachandra@nxp.com>2024-10-04 18:39:27 +0200
commit39a01979a470e836da1128503e4587cb76eeae33 (patch)
treefb77504a341af38242572de7d72758da943cd938 /model.py
parent88d7300021cd3963f2d6a6202609b0f6e956cdab (diff)
Added the randomization and the persistent memory
Diffstat (limited to 'model.py')
-rw-r--r--model.py30
1 files changed, 20 insertions, 10 deletions
diff --git a/model.py b/model.py
index ef6e762..4a25400 100644
--- a/model.py
+++ b/model.py
@@ -1,6 +1,7 @@
from math import floor
from csv import reader
from pathlib import Path
+from random import sample
from dataclasses import dataclass
@@ -67,24 +68,33 @@ class PlayerList:
return display_string
+TOTAL_PLAYERS_PER_COURT = 4
+DUMMY_PLAYER = Player(name="-", skill=0, status=True)
+DUMMY_TEAM = Team(player1=DUMMY_PLAYER, player2=DUMMY_PLAYER)
+DUMMY_GAME = Game(DUMMY_TEAM, DUMMY_TEAM)
+
+
@dataclass
class GameList:
courts: int = 8
+ def __post_init__(self):
+ self.games = [DUMMY_GAME for _ in range(self.courts)]
+
def create_games(self, players: list[Player]):
- possible_games = floor(len(players) / 4)
+ possible_games = floor(len(players) / TOTAL_PLAYERS_PER_COURT)
+ chosen_players = sample(players, possible_games * TOTAL_PLAYERS_PER_COURT)
+ clumped_players = [
+ chosen_players[i : i + TOTAL_PLAYERS_PER_COURT]
+ for i in range(0, len(chosen_players), TOTAL_PLAYERS_PER_COURT)
+ ]
games = []
- for i in range(possible_games):
- team1 = Team(player1=players[4 * i + 0], player2=players[4 * i + 1])
- team2 = Team(player1=players[4 * i + 2], player2=players[4 * i + 3])
+ for clump in clumped_players:
+ team1 = Team(player1=clump[0], player2=clump[1])
+ team2 = Team(player1=clump[2], player2=clump[3])
game = Game(team1, team2)
games.append(game)
empty_courts = (
self.courts - possible_games if self.courts > possible_games else 0
)
- for i in range(empty_courts):
- dummy_player = Player(name="-", skill=0, status=True)
- dummy_team = Team(player1=dummy_player, player2=dummy_player)
- game = Game(dummy_team, dummy_team)
- games.append(game)
- return games
+ self.games = games + [DUMMY_GAME for _ in range(empty_courts)]