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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
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 Court:
number: int
status: bool
@dataclass
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 Player:
name: str
skill: int = 0
status: bool = True
class PlayerList:
def __init__(self, location: Path = "players.csv"):
players = []
with open(location, newline="") as file:
data_reader = reader(file)
next(data_reader, None)
for row in data_reader:
player = Player(name=row[0], skill=row[1], status=(row[2] == "True"))
players.append(player)
players.sort(key=attrgetter("name"))
self.players = players
def get_player(self, player_request: str):
players = [player for player in self.players if player.name == player_request]
players.sort(key=attrgetter("name"))
return players
def get_active_players(self):
players = [player for player in self.players if player.status]
players.sort(key=attrgetter("name"))
return players
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 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)
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:
def clear(self, court_list: CourtList):
courts = 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, 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
)
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
|