diff options
| author | Karan Jayachandra <karan.jayachandra@nxp.com> | 2024-10-11 16:59:45 +0200 |
|---|---|---|
| committer | Karan Jayachandra <karan.jayachandra@nxp.com> | 2024-10-11 16:59:45 +0200 |
| commit | 0db0ff2f1c712273ac48cb859c24253b1efa8384 (patch) | |
| tree | b92c4bd847ca94ad5cdf5a0b85576328484ff061 /model.py | |
| parent | 39a01979a470e836da1128503e4587cb76eeae33 (diff) | |
First working version with court selection
Diffstat (limited to 'model.py')
| -rw-r--r-- | model.py | 129 |
1 files changed, 99 insertions, 30 deletions
@@ -2,26 +2,47 @@ from math import floor from csv import reader from pathlib import Path from random import sample +from operator import attrgetter from dataclasses import dataclass @dataclass -class Player: - name: str - skill: int +class Court: + number: int status: bool @dataclass -class Team: - player1: Player - player2: Player +class CourtList: + total: int + + def __post_init__(self): + self.courts = [Court(i + 1, True) for i in range(self.total)] + self.active = self.total + + def get_court(self, court_number: int): + return [court for court in self.courts if court.number == court_number] + + def toggle_court(self, court_number: int): + self.courts = [ + ( + Court( + number=court_number, + status=not court.status, + ) + if court_number == court.number + else court + ) + for court in self.courts + ] + self.active = sum([court.status for court in self.courts]) @dataclass -class Game: - team1: Team - team2: Team +class Player: + name: str + skill: int = 0 + status: bool = True class PlayerList: @@ -68,33 +89,81 @@ class PlayerList: return display_string -TOTAL_PLAYERS_PER_COURT = 4 -DUMMY_PLAYER = Player(name="-", skill=0, status=True) +@dataclass +class Team: + player1: Player + player2: Player + + +@dataclass +class Game: + court: Court + team1: Team + team2: Team + + +DUMMY_PLAYER = Player(name="-") DUMMY_TEAM = Team(player1=DUMMY_PLAYER, player2=DUMMY_PLAYER) -DUMMY_GAME = Game(DUMMY_TEAM, DUMMY_TEAM) + + +def create_dummy_games(courts: list[Court]): + return [Game(court, DUMMY_TEAM, DUMMY_TEAM) for court in courts] + + +RESERVED_PLAYER = Player(name="Reserved") +RESERVED_TEAM = Team(player1=RESERVED_PLAYER, player2=RESERVED_PLAYER) + + +def create_reserved_games(courts: list[Court]): + return [Game(court, RESERVED_TEAM, RESERVED_TEAM) for court in courts] + + +PLAYER_PER_COURT = 4 + + +def create_single_game(court: Court, players: list[Player]): + team1 = Team(player1=players[0], player2=players[1]) + team2 = Team(player1=players[2], player2=players[3]) + return Game(court, team1, team2) + + +def create_possible_games(court_list: list[Court], players: list[Player]): + current_players = sample(players, len(court_list) * PLAYER_PER_COURT) + clumped_players = [ + current_players[i : i + PLAYER_PER_COURT] + for i in range(0, len(current_players), PLAYER_PER_COURT) + ] + games = [ + create_single_game(court, clump) + for court, clump in zip(court_list, clumped_players) + ] + return games @dataclass class GameList: - courts: int = 8 + court_list: CourtList def __post_init__(self): - self.games = [DUMMY_GAME for _ in range(self.courts)] + courts = self.court_list.courts + active_courts = [court for court in courts if court.status] + inactive_courts = [court for court in courts if not court.status] + dummy_games = create_dummy_games(active_courts) + reserved_games = create_reserved_games(inactive_courts) + games = dummy_games + reserved_games + games.sort(key=attrgetter("court.number")) + self.games = games - def create_games(self, players: list[Player]): - 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 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 + def create_games(self, court_list: CourtList, player_list: PlayerList): + active_players = player_list.get_active_players() + active_courts = [court for court in court_list.courts if court.status] + inactive_courts = [court for court in court_list.courts if not court.status] + possible_games = floor(len(active_players) / PLAYER_PER_COURT) + active_games = create_possible_games( + active_courts[:possible_games], active_players ) - self.games = games + [DUMMY_GAME for _ in range(empty_courts)] + dummy_games = create_dummy_games(active_courts[possible_games:]) + reserved_games = create_reserved_games(inactive_courts) + games = active_games + dummy_games + reserved_games + games.sort(key=attrgetter("court.number")) + self.games = games |
