1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
from math import floor
from csv import reader
from pathlib import Path
from dataclasses import dataclass
@dataclass
class Player:
name: str
skill: int
status: bool
@dataclass
class Team:
player1: Player
player2: Player
@dataclass
class Game:
team1: Team
team2: Team
class PlayerList:
def __init__(self, location: Path = "players.csv"):
with open(location, newline="") as file:
data_reader = reader(file)
next(data_reader, None)
self.players = [
(
Player(
name=row[0],
skill=row[1],
status=(row[2] == "True"),
)
)
for row in data_reader
]
def get_player(self, player_request: str):
return [player for player in self.players if player.name == player_request]
def get_active_players(self):
return [player for player in self.players if player.status]
def toggle_player(self, player_request: Player):
self.players = [
(
Player(
name=player.name,
skill=player.skill,
status=not player.status,
)
if player.name == player_request
else player
)
for player in self.players
]
def __str__(self):
display_string = ""
for player in self.players:
display_string += player.__str__() + "\n"
return display_string
@dataclass
class GameList:
courts: int = 8
def create_games(self, players: list[Player]):
possible_games = floor(len(players) / 4)
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])
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
|