-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
294 lines (254 loc) · 11.1 KB
/
controller.py
File metadata and controls
294 lines (254 loc) · 11.1 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
controller.py
A module controlling the flow and logic of the chess tournament management system.
Manages user interactions, tournament creation, round management, and report generation.
Directory Structure:
data/
players/ - Directory for storing player data (players.json).
tournaments/ - Directory for storing tournament data (e.g., EUChessTour.json).
Usage:
Run the script to start the interactive menu:
$ python3 main.py
Classes:
Controller: Manages the application logic and user interface interactions.
Functions:
run(): Entry point to start the application loop.
Author:
Mathieu Patoz
Updated: March 06, 2025
"""
from model import Player, Tournament, Database
from view import View
import os
class Controller:
"""A class managing the flow and logic of the chess tournament application.
Attributes:
db (Database): The database handler for player and tournament data.
tournament (Tournament): The current tournament being managed.
players (list): List of all Player objects.
view (View): The view handler for user interface operations.
Methods:
create_tournament(): Create a new tournament and save it.
add_players(): Add a new player to the database.
update_global_scores(): Update global player scores from the current tournament.
manage_rounds(): Manage the rounds of the current tournament.
manage_existing_tournament(): Manage an existing unfinished tournament.
generate_reports(): Generate and display reports.
run(): Run the main application loop.
"""
def __init__(self):
"""Initialize the Controller with database and view.
Initializes the database, loads existing players, and sets up the view.
"""
self.db = Database()
self.tournament = None
self.players = self.db.load_players()
self.view = View()
def create_tournament(self):
"""Create a new tournament and save it.
Prompts the user for tournament details and initializes it with players from the
database.
Returns:
None
"""
name, location, start_date, description = self.view.get_tournament_info()
self.tournament = Tournament(name, location, start_date, description)
self.tournament.add_players_from_database(self.players)
self.db.save_tournament(self.tournament)
self.view.display_message(
f"Tournament '{name}' created successfully with "
f"{len(self.tournament.players)} players!"
)
def add_players(self):
"""Add a new player to the database.
Prompts the user for player details and saves the new player.
Returns:
None
"""
firstname, lastname, birthdate, national_id = self.view.get_player_info()
player = Player(firstname, lastname, birthdate, national_id)
self.players.append(player)
self.db.save_players(self.players)
self.view.display_message("Player added successfully!")
def update_global_scores(self):
"""Update global player scores based on the current tournament's scores.
Increments the global score of each player based on their tournament performance.
Returns:
None
"""
if self.tournament:
for tour_player in self.tournament.players:
global_player = next(
(p for p in self.players if p.national_id == tour_player.national_id), None
)
if global_player:
global_player.score += tour_player.score
self.db.save_players(self.players)
def manage_rounds(self):
"""Manage the rounds of the current tournament.
Handles starting, finishing, and ending rounds interactively.
Returns:
None
"""
if not self.tournament:
self.view.display_message("Please create a tournament first!")
return
if not self.tournament.players:
self.view.display_message(
"No players registered for the tournament! "
"Add players first."
)
return
while True:
choice = self.view.display_round_management_menu()
if choice == "1":
if self.tournament.start_round():
self.db.save_tournament(self.tournament)
self.view.display_message(
f"Round {self.tournament.current_round} "
"started!"
)
else:
self.db.save_tournament(self.tournament)
self.view.display_message(
"No pairs could be generated for this round "
"or the tournament is complete!"
)
elif choice == "2":
if self.tournament.finish_round():
current_round = self.tournament.rounds[-1]
for match in current_round.matches:
if not match.is_finished:
self.view.display_message(
f"Match: {match.players[0][0].lastname}, "
f"{match.players[0][0].firstname} vs "
f"{match.players[1][0].lastname}, "
f"{match.players[1][0].firstname}"
)
winner_idx = self.view.get_match_result(
match.players[0][0],
match.players[1][0]
)
match.set_result(winner_idx)
for player, score in match.players:
player.score += score
self.db.save_tournament(self.tournament)
self.update_global_scores()
self.view.display_message(
f"Round {self.tournament.current_round} "
"finished!"
)
else:
self.view.display_message("No round to finish!")
elif choice == "3":
if self.view.confirm_end_tournament():
if self.tournament.end_tournament():
self.db.save_tournament(self.tournament)
self.update_global_scores()
self.view.display_message(
f"Tournament '{self.tournament.name}' "
f"ended on {self.tournament.end_date}!"
)
else:
self.view.display_message(
"Cannot end tournament: it must have "
"completed all rounds or already ended."
)
elif choice == "4":
break
else:
self.view.display_message("Invalid option, try again.")
def manage_existing_tournament(self):
"""Manage an existing unfinished tournament.
Loads and allows management of an unfinished tournament.
Returns:
None
"""
tournaments = []
for filename in os.listdir(self.db.tournaments_dir):
if filename.endswith('.json'):
tournament_name = filename[:-5]
tournament = self.db.load_tournament(tournament_name)
if tournament:
tournaments.append(tournament)
unfinished_tournaments = self.view.display_unfinished_tournaments(tournaments)
if unfinished_tournaments is None:
self.view.display_message("No unfinished tournaments available.")
return
try:
choice = int(
input("Select an unfinished tournament by number (or 0 to cancel): ")
)
if choice == 0:
self.view.display_message("Returning to main menu.")
return
if 1 <= choice <= len(unfinished_tournaments):
self.tournament = unfinished_tournaments[choice - 1]
self.db.save_tournament(self.tournament)
self.view.display_message(f"Loaded tournament: {self.tournament.name}")
self.manage_rounds()
else:
self.view.display_message("Invalid choice, returning to main menu.")
except ValueError:
self.view.display_message("Please enter a number, returning to main menu.")
def generate_reports(self):
"""Generate and display reports.
Provides options to list players, tournaments, or detailed tournament info.
Returns:
None
"""
if not self.players and not self.tournament:
self.view.display_message("No data available for reports!")
return
while True:
choice = self.view.display_reports_menu()
if choice == "1":
self.view.display_players(self.players)
elif choice == "2":
tournaments = []
for filename in os.listdir(self.db.tournaments_dir):
if filename.endswith('.json'):
tournament_name = filename[:-5]
tournament = self.db.load_tournament(tournament_name)
if tournament:
tournaments.append(tournament)
self.view.display_tournaments(tournaments)
elif choice == "3":
tournaments = []
for filename in os.listdir(self.db.tournaments_dir):
if filename.endswith('.json'):
tournament_name = filename[:-5]
tournament = self.db.load_tournament(tournament_name)
if tournament:
tournaments.append(tournament)
selected_tournament = self.view.select_tournament(tournaments)
if selected_tournament:
self.view.display_tournament_details(selected_tournament)
elif choice == "4":
break
else:
self.view.display_message("Invalid option, try again.")
def run(self):
"""Run the main application loop.
Starts the interactive menu and processes user choices.
Returns:
None
"""
self.view = View()
while True:
choice = self.view.display_menu()
if choice == "1":
self.add_players()
elif choice == "2":
self.create_tournament()
elif choice == "3":
self.manage_existing_tournament()
elif choice == "4":
self.manage_rounds()
elif choice == "5":
self.generate_reports()
elif choice == "6":
self.view.display_message("Goodbye!")
break
else:
self.view.display_message("Invalid option, try again.")