diff options
| author | Karan Jayachandra <karan.jayachandra@nxp.com> | 2024-10-04 18:39:27 +0200 |
|---|---|---|
| committer | Karan Jayachandra <karan.jayachandra@nxp.com> | 2024-10-04 18:39:27 +0200 |
| commit | 39a01979a470e836da1128503e4587cb76eeae33 (patch) | |
| tree | fb77504a341af38242572de7d72758da943cd938 | |
| parent | 88d7300021cd3963f2d6a6202609b0f6e956cdab (diff) | |
Added the randomization and the persistent memory
| -rw-r--r-- | app.py | 5 | ||||
| -rw-r--r-- | model.py | 30 |
2 files changed, 23 insertions, 12 deletions
@@ -36,9 +36,10 @@ def home(): @app.route("/new-games", methods=["POST"]) def get_new_games(): + GAME_GENERATOR.create_games(PLAYER_LIST.get_active_players()) return render_template( "games.j2", - games=GAME_GENERATOR.create_games(PLAYER_LIST.get_active_players()), + games=GAME_GENERATOR.games, ) @@ -46,7 +47,7 @@ def get_new_games(): def get_list_of_games(): return render_template( "games.j2", - games=GAME_GENERATOR.create_games(PLAYER_LIST.get_active_players()), + games=GAME_GENERATOR.games, ) @@ -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)] |
