From 39a01979a470e836da1128503e4587cb76eeae33 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Fri, 4 Oct 2024 18:39:27 +0200 Subject: Added the randomization and the persistent memory --- model.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'model.py') 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)] -- cgit v1.3.1