-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
192 lines (157 loc) · 7.34 KB
/
Copy pathcontroller.py
File metadata and controls
192 lines (157 loc) · 7.34 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
from datetime import datetime
from typing import TYPE_CHECKING
from models.match import Match
from models.player import Player
from models.round import Round
from models.tournament import Tournament
from views.view import View
if TYPE_CHECKING:
from router import Router
class Controller:
def __init__(self, router: "Router"):
self._router = router
self.view = View(self)
@property
def router(self):
return self._router
@property
def app(self):
return self._router.app
def index(self, context):
# Display menu #1 and return choice menu between player index or Tournament index
context = self.view.render_view(context)
return context
def index_player(self, context):
# Display players and return choice menu between new player , update ranking player, menu index
context["players"] = Player.list_of_players_by_alphabetic_sort(self.app.players)
context = self.view.render_view(context)
return context
def add_player(self, context):
# with view asks for the entry of the information of the new player and return save message with index_player
context = self.view.render_view(context)
if "player" in context:
player = Player.dict_to_object(self.app, context["player"])
player.save()
context.pop("player")
return context
def update_ranking_player(self, context):
# with view asks for the new ranking of the player and return save message with index_player
if "player_id" in context:
context["player"] = Player.find_player_by_identifier(self.app, int(context["player_id"]))
context = self.view.render_view(context)
if "player" in context:
player = context["player"]
player.save()
context.pop('player')
return context
def index_tournament(self, context):
# Display tournaments and return choice menu between new tournament, display tournament, menu index
context["tournaments"] = self.app.tournaments
context = self.view.render_view(context)
return context
def add_tournament(self, context):
# with view asks for the entry of the information of the new tournament and return save message with index tournament
context = self.view.render_view(context)
if "tournament" in context:
tournament = Tournament.dict_to_object(self.app, context["tournament"])
context.pop("tournament")
tournament.save()
return context
def display_tournament(self, context):
# display tournament, return menu choice between start_round, add player, save result match
if "tournament_id" in context:
context["tournament"] = Tournament.find_tournament_by_identifier(self.app, int(context["tournament_id"]))
context.pop('tournament_id')
context["tournament_players"] = []
for player_id in context["tournament"].players:
context["tournament_players"].append(Player.find_player_by_identifier(self.app, player_id))
context = self.view.render_view(context)
return context
def add_player_into_tournament(self, context):
if "tournament" in context:
tournament = context["tournament"]
players = [
player
for player in Player.list_of_players_by_alphabetic_sort(self.app.players)
if player.identifier not in tournament.players
]
context["players"] = players
context = self.view.render_view(context)
if "player_ids" in context:
players_ids_list = context["player_ids"].split(",")
context.pop("player_ids")
for player_id in players_ids_list:
player_id = int(player_id.strip())
if player_id not in tournament.players:
tournament.add_player(player_id)
tournament.save()
else:
context["route"] = self.router.HOMEPAGE_ID
return context
def start_round(self, context):
if "tournament" in context:
tournament = context["tournament"]
context.pop('tournament')
tournament.start_round()
tournament.save()
context['tournament_id'] = tournament.identifier
context["route_id"] = self.router.DISPLAY_TOURNAMENT_ID
return context
def save_round(self, context):
if "tournament" in context:
tournament = context["tournament"]
if "match" and "match_result" in context:
match = Match.find_match_by_tuple(tournament.active_round, context["match"])
if context["match_result"] == "1":
match.set_player_one_is_winner()
elif context["match_result"] == "2":
match.set_player_two_is_winner()
else:
match.set_match_equality()
if match.round.state == Round.STATE_STOP:
now_date = datetime.now()
match.round.end_date = now_date.strftime("%d/%m/%Y - %H:%M")
tournament.save()
context["route_id"] = self.router.DISPLAY_TOURNAMENT_ID
return context
def report_index(self, context):
context = self.view.render_view(context)
return context
def report_player(self, context):
if context["sorted_by"] == 'surname':
players = Player.list_of_players_by_alphabetic_sort(self.app.players)
else:
players = Player.list_of_players_by_ranking_sort(self.app.players)
context.pop("sorted_by")
context["players"] = players
context = self.view.render_view(context)
return context
def report_tournament(self, context):
context["tournaments"] = self.app.tournaments
context = self.view.render_view(context)
return context
def report_tournament_rounds(self, context):
tournament_id = context["tournament_id"]
context.pop("tournament_id")
context["tournament"] = Tournament.find_tournament_by_identifier(self.app, int(tournament_id))
context = self.view.render_view(context)
return context
def report_tournament_matchs(self, context):
tournament_id = context["tournament_id"]
context.pop("tournament_id")
context["tournament"] = Tournament.find_tournament_by_identifier(self.app, int(tournament_id))
context = self.view.render_view(context)
return context
def report_tournament_players(self, context):
tournament_id = context["tournament_id"]
context.pop("tournament_id")
context["tournament"] = Tournament.find_tournament_by_identifier(self.app, int(tournament_id))
context["tournament_players"] = []
for player_id in context["tournament"].players:
context["tournament_players"].append(Player.find_player_by_identifier(self.app, player_id))
if context["sorted_by"] == 'surname':
context["tournament_players"] = Player.list_of_players_by_alphabetic_sort(context["tournament_players"])
else:
context["tournament_players"] = Player.list_of_players_by_ranking_sort(context["tournament_players"])
context = self.view.render_view(context)
return context