/
controller.py
916 lines (872 loc) · 32.9 KB
/
controller.py
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
import collections
import json
from datetime import datetime
from tinydb import Query, TinyDB
import input_validators
from model.match import Match
from model.player import Player
from model.round import Round
from model.tournament import Tournament
class Controller:
def __init__(
self,
select,
table,
warning,
tournament_table=TinyDB("db.json").table("tournament"),
player_table=TinyDB("db.json").table("player"),
round_table=TinyDB("db.json").table("round"),
match_table=TinyDB("db.json").table("match"),
):
self.select = select
self.table = table
self.warning = warning
self.tournament_table = tournament_table
self.player_table = player_table
self.round_table = round_table
self.match_table = match_table
def run(self):
"""
Run the code by displaying welcome message and maim menu
"""
self.warning.welcome_message()
self.main_menu()
def main_menu(self):
"""
Display main menu
"""
while True:
response = self.select.main_menu()
if input_validators.is_valid_main_menu_response(response):
break
if response == "1":
return self.manage_tournament()
if response == "2":
return self.create_player_main_menu()
if response == "3":
return self.change_ranking_menu()
if response == "4":
return self.tournaments_menu()
if response == "5":
return self.players_menu()
if response == "6":
return self.quit()
def tournaments_menu(self):
"""
Display tournaments menu
"""
response = self.table.tournaments()
while True:
response = self.select.tournament_menu()
if input_validators.is_valid_tournament_menu_response(response):
break
if response == "1":
return self.choice_tournament_for_print_players()
if response == "2":
return self.choice_tournament_for_print_rounds()
if response == "3":
return self.choice_tournament_for_print_matchs()
if response == "4":
return self.change_ranking_menu()
if response == "5":
return self.main_menu()
if response == "6":
return self.quit()
def end_tournament_menu(self):
"""
Display end round manu
"""
while True:
response = self.select.end_tournament_menu()
if input_validators.is_valid_end_tournament_menu_response(response):
break
if response == "1":
return self.change_ranking()
if response == "2":
return self.main_menu()
if response == "3":
return self.quit()
def choice_tournament_for_print_players(self):
"""
Display a menu to choice wich manner to display players for a tournament
Choices : ranking or alphabetical order
"""
if not self.tournament_table:
self.warning.no_tournament()
else:
while True:
choice_tournament = self.select.choose_tournament_for_print_players()
if input_validators.is_valid_id_tournament(choice_tournament):
break
while True:
choice_manner = self.select.print_players_menu()
if input_validators.is_valid_print_players_menu_response(choice_manner):
break
if choice_manner == "1":
self.table.players_by_ranking_of_tournament(choice_tournament)
else:
self.table.players_by_alphabetical_order_of_tournament(
choice_tournament
)
self.tournaments_menu()
def choice_tournament_for_print_rounds(self):
"""
Display a menu to choice wich tournament to display rounds
"""
if not self.tournament_table:
self.warning.no_tournament()
else:
while True:
choice_tournament = self.select.choose_tournament_for_print_rounds()
if input_validators.is_valid_id_tournament(choice_tournament):
break
self.table.rounds_of_tournament(choice_tournament)
self.tournaments_menu()
def choice_tournament_for_print_matchs(self):
"""
Display a menu to choice wich tournament to display matchs
"""
if not self.tournament_table:
self.warning.no_tournament()
else:
while True:
choice_tournament = self.select.choose_tournament_for_print_matchs()
if input_validators.is_valid_id_tournament(choice_tournament):
break
self.table.matchs_of_tournament(choice_tournament)
self.tournaments_menu()
def players_menu(self):
"""
Display players menu
"""
while True:
response = self.select.players_menu()
if input_validators.is_valid_players_menu_response(response):
break
if response == "1":
return self.players_by_ranking()
if response == "2":
return self.players_by_alphabetical_order()
if response == "3":
return self.change_ranking_menu()
if response == "4":
return self.main_menu()
if response == "5":
return self.quit()
def update_scores(self, match):
"""
Update score of both Match and Player table
Args:
match: match to update
"""
while True:
response = self.select.write_score_menu(match)
if input_validators.is_valid_write_score_menu_response(response):
break
Match.update_score_match(self, match, response, self.match_table)
Player.update_score_player(self, match, response, self.player_table)
def players_by_ranking(self):
"""
Display players by ranking table
"""
players = Player.read_players(self)
response = self.table.players_by_ranking(players)
while True:
response = self.select.players_by_ranking_menu()
if input_validators.is_valid_players_by_ranking_menu_response(response):
break
if response == "1":
return self.players_by_alphabetical_order()
if response == "2":
return self.change_ranking_menu()
if response == "3":
return self.main_menu()
if response == "4":
return self.quit()
def players_by_alphabetical_order(self):
"""
Display players by alphabetical order table
"""
response = self.table.players_by_alphabetical_order()
while True:
response = self.select.players_by_alphabetical_order_menu()
if input_validators.is_valid_players_by_alphabetical_order_menu_response(
response
):
break
if response == "1":
return self.players_by_ranking()
if response == "2":
return self.change_ranking_menu()
if response == "3":
return self.main_menu()
if response == "4":
return self.quit()
def change_ranking_menu(self):
"""
Display change ranking menu
"""
if not self.player_table:
self.warning.no_player()
self.main_menu()
else:
self.table.players_by_id()
while True:
response = self.select.ranking_menu()
if input_validators.is_valid_ranking_menu_response(response):
break
if response == "1":
return self.change_ranking()
if response == "2":
return self.main_menu()
def choice_create_next_round(self):
"""
Display a menu to let the user choice if he wants to create next round
"""
while True:
response = self.select.next_round_menu()
if input_validators.is_valid_next_round_menu_response(response):
break
if response == "1":
if self.is_new_tournament():
return self.create_first_round()
else:
return self.create_round()
if response == "2":
return self.change_ranking()
if response == "3":
return self.main_menu()
def choice_end_round(self):
"""
Display a menu to let the user choice if he wants to end current round
"""
while True:
response = self.select.end_round_menu()
if input_validators.is_valid_end_round_menu_response(response):
break
if response == "1":
return self.end_round()
if response == "2":
return self.main_menu()
def manage_tournament(self):
"""
Allow to create tournament if not already exist and first and other rounds
"""
if (
Tournament.tournament_table_is_empty(self.tournament_table) is True
or Tournament.status_tournament_is_finished(self.tournament_table) is True
):
new_tournament = Tournament.create_tournament()
serialized_tournament = vars(new_tournament)
self.tournament_table.insert(serialized_tournament)
self.add_players_to_tournament()
self.choice_create_next_round()
elif (
Tournament.tournament_table_is_empty(self.tournament_table) is not True
and self.round_table.all() == []
) or self.round_table.all()[-1]["status_round"] == "finished":
self.choice_create_next_round()
else:
self.choice_end_round()
def add_players_to_tournament(self):
"""
Add players to tournament. Initialize their score to 0.
"""
number_of_player = 0
list_of_players_added = []
list_of_remaining_players = Player.list_of_remaining_players(
self, self.player_table
)
while number_of_player < 8:
self.table.remaining_players(list_of_remaining_players)
self.warning.remaining_players_to_add(number_of_player)
while True:
response = self.select.add_player_create_tournament_menu()
if input_validators.is_valid_add_player_create_tournament_menu_response(
response
):
break
if response == "1":
if not self.player_table or list_of_remaining_players == []:
self.warning.no_player()
continue
while True:
existed_player_id = int(
self.choice_player_for_add_player_to_a_tournament()
)
if input_validators.is_valid_id_player(existed_player_id) is True:
break
list_of_players_added.append(existed_player_id)
elif response == "2":
new_player_id = self.create_player_in_tournament()
existed_player_id = new_player_id
list_of_players_added.append(new_player_id)
number_of_player += 1
contains_duplicates = any(
list_of_players_added.count(element) > 1
for element in list_of_players_added
)
if contains_duplicates:
list_of_players_added.pop(-1)
number_of_player -= 1
self.warning.add_a_player_several_time()
continue
self.tournament_table.update(
{"players": list_of_players_added},
doc_ids=[self.tournament_table.all()[-1].doc_id],
)
Player.initialize_score(self, self.player_table, self.tournament_table)
for player in list_of_remaining_players:
if player[0] == existed_player_id:
list_of_remaining_players.remove(player)
def create_player_main_menu(self):
"""
Create player
"""
new_player = Player.create_player()
serialized_player = vars(new_player)
self.player_table.insert(serialized_player)
return self.main_menu()
def create_player_in_tournament(self):
"""
Create player
Returns:
new_player_id (int): id of the new player
"""
new_player = Player.create_player()
serialized_player = vars(new_player)
self.player_table.insert(serialized_player)
new_player_id = self.player_table.all()[-1].doc_id
return new_player_id
def get_input_new_ranking(self):
"""
Get the input of the user when change ranking of a player
Returns:
new_ranking (int): new_ranking choice by the user
"""
while True:
id_player = int(self.select.change_ranking())
if input_validators.is_valid_id_player(id_player):
break
new_ranking = int(
input(
"Enter a new ranking for "
+ str(
self.player_table.all()[id_player - 1]["first_name"]
+ " "
+ str(self.player_table.all()[id_player - 1]["last_name"] + "\n")
)
)
)
self.player_table.update(
{"ranking": new_ranking},
doc_ids=[self.player_table.all()[id_player - 1].doc_id],
)
return new_ranking
def check_same_ranking(self):
"""
Check if the new ranking selected for a new player is already choosen
Returns:
Contains_duplicates (bool): depend on if the new ranking is already choosen
"""
players_ranking = [player["ranking"] for player in self.player_table]
return any(players_ranking.count(element) > 1 for element in players_ranking)
def change_ranking(self):
"""
Change ranking of a player
"""
new_ranking = self.get_input_new_ranking()
contains_duplicates = self.check_same_ranking()
while contains_duplicates is True:
self.table.players_by_id()
self.warning.players_same_ranking(new_ranking)
new_ranking = self.get_input_new_ranking()
contains_duplicates = self.check_same_ranking()
self.change_ranking_menu()
def is_new_tournament(self):
"""
Check if a tournament is a new one
"""
if (
self.round_table.all() == []
or self.round_table.all()[-1]["current_round"] == 4
):
return True
def list_of_players_by_ranking(self):
"""
Create a list of players sorted by ranking
Returns:
list_of_players_by_ranking (list): list of all the players of a tournament
"""
list_ranking_player = [
self.player_table.get(doc_id=player)
for player in self.tournament_table.all()[-1]["players"]
]
return Player.sort_list_of_players_by_ranking(self, list_ranking_player)
def list_of_players_by_score(self):
"""
Create a list of players sorted by score
Returns:
list_of_players_by_score (list): list of all the players of a tournament
"""
list_score_player = [
self.player_table.get(doc_id=player)
for player in self.tournament_table.all()[-1]["players"]
]
return Player.sort_list_of_players_by_score(self, list_score_player)
def list_of_all_matchs_of_a_tournament(self):
"""
Create a list of all matchs of a tournament
Returns:
all_matchs (list): list of all matchs of a tournament
"""
all_matchs = []
for rounds in self.tournament_table.all()[-1]["rounds"]:
for matchs in self.round_table.all()[rounds - 1]["list_of_match"]:
all_matchs.append(
[
self.match_table.all()[matchs - 1]["player_1"],
self.match_table.all()[matchs - 1]["player_2"],
]
)
return all_matchs
def end_round(self):
"""
End a round by updating scores, and status_round and end sate of round table
"""
for j in range(-4, 0):
self.update_scores(self.match_table.all()[j])
self.table.players_by_score()
self.round_table.update(
{"status_round": "finished"},
doc_ids=[self.round_table.all()[-1].doc_id],
)
self.round_table.update(
{
"end_date": json.dumps(
datetime.now().strftime("%d/%m/%Y %H:%M:%S"), default=str
)
},
doc_ids=[self.round_table.all()[-1].doc_id],
)
if self.round_table.all()[-1]["current_round"] == 4:
self.end_tournament()
else:
return self.manage_tournament()
def create_first_round(self):
new_round = Round.create_round()
serialized_round = vars(new_round)
new_matchs_id = []
self.round_table.insert(serialized_round)
list_of_players_by_ranking = self.list_of_players_by_ranking()
Play = Query()
for i in range(4):
new_match = Match.create_match(
self.player_table.get(
Play.ranking == list_of_players_by_ranking[0][i]["ranking"]
).doc_id,
self.player_table.get(
Play.ranking == list_of_players_by_ranking[1][i]["ranking"]
).doc_id,
)
serialized_match = vars(new_match)
self.match_table.insert(serialized_match)
new_matchs_id.append(self.match_table.all()[-1].doc_id)
list_of_rounds = self.tournament_table.all()[-1]["rounds"]
list_of_rounds.append(self.round_table.all()[-1].doc_id)
self.round_table.update(
{"list_of_match": new_matchs_id},
doc_ids=[self.round_table.all()[-1].doc_id],
)
self.tournament_table.update(
{"rounds": list_of_rounds},
doc_ids=[self.tournament_table.all()[-1].doc_id],
)
self.warning.round_create(str(self.round_table.all()[-1]["current_round"]))
self.table.matchs()
return self.manage_tournament()
def is_permuted_match_existed(
self, list_all_matchs, list_sorted_players, ind_1, ind_2
):
"""
Check if a permuted match has already been played.
Args:
list_all_matchs (list): list of all matchs already played in the tournament
list_sorted_players (list): list of players sorted by score
ind_1 (int): index of the player 1 to permute
ind_2 (int): index of the player 2 to permute
"""
if (
self.is_match_already_play(
list_all_matchs,
list_sorted_players[ind_1].doc_id,
list_sorted_players[ind_2].doc_id,
)
is True
):
return True
def create_round(self):
"""
Create a round. Update tournament and round table
"""
new_round = Round.create_round()
serialized_round = vars(new_round)
new_matchs_id = []
serialized_round["current_round"] = (
self.round_table.all()[-1]["current_round"] + 1
)
serialized_round["name"] = "Round " + str(serialized_round["current_round"])
self.round_table.insert(serialized_round)
list_of_players_by_score = self.list_of_players_by_score()
all_matchs_of_a_tournament = self.list_of_all_matchs_of_a_tournament()
Play = Query()
list_matchs = []
j = 0
try:
for _ in range(4):
k = 1
while self.is_match_already_play(
all_matchs_of_a_tournament,
self.player_table.get(
Play.ranking == list_of_players_by_score[j]["ranking"]
).doc_id,
self.player_table.get(
Play.ranking == list_of_players_by_score[j + 1]["ranking"]
).doc_id,
):
self.is_match_already_play(
all_matchs_of_a_tournament,
self.player_table.get(
Play.ranking == list_of_players_by_score[j]["ranking"]
).doc_id,
self.player_table.get(
Play.ranking
== list_of_players_by_score[j + 1 + k]["ranking"]
).doc_id,
)
(
list_of_players_by_score[j + 1],
list_of_players_by_score[j + 1 + k],
) = (
list_of_players_by_score[j + 1 + k],
list_of_players_by_score[j + 1],
)
k += 1
list_matchs.append(
(
self.player_table.get(
Play.ranking == list_of_players_by_score[j]["ranking"]
).doc_id,
self.player_table.get(
Play.ranking == list_of_players_by_score[j + 1]["ranking"]
).doc_id,
)
)
j += 2
except IndexError:
list_matchs.pop(-1)
if (
self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -1, -3
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -2, -4
)
is not True
):
list_matchs.append(
(
list_of_players_by_score[-1].doc_id,
list_of_players_by_score[-3].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-2].doc_id,
list_of_players_by_score[-4].doc_id,
)
)
elif (
self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -1, -4
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -2, -3
)
is not True
):
list_matchs.append(
(
list_of_players_by_score[-1].doc_id,
list_of_players_by_score[-4].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-2].doc_id,
list_of_players_by_score[-3].doc_id,
)
)
elif (
self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -1, -3
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -2, -5
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -4, -6
)
is not True
):
list_matchs.pop(-1)
list_matchs.append(
(
list_of_players_by_score[-1].doc_id,
list_of_players_by_score[-3].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-2].doc_id,
list_of_players_by_score[-5].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-4].doc_id,
list_of_players_by_score[-6].doc_id,
)
)
elif (
self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -1, -4
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -2, -5
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -3, -6
)
is not True
):
list_matchs.pop(-1)
list_matchs.append(
(
list_of_players_by_score[-1].doc_id,
list_of_players_by_score[-4].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-2].doc_id,
list_of_players_by_score[-5].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-3].doc_id,
list_of_players_by_score[-6].doc_id,
)
)
elif (
self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -2, -3
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -1, -5
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -4, -6
)
is not True
):
list_matchs.pop(-1)
list_matchs.append(
(
list_of_players_by_score[-2].doc_id,
list_of_players_by_score[-3].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-1].doc_id,
list_of_players_by_score[-5].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-4].doc_id,
list_of_players_by_score[-6].doc_id,
)
)
elif (
self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -2, -4
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -1, -5
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -3, -6
)
is not True
):
list_matchs.pop(-1)
list_matchs.append(
(
list_of_players_by_score[-2].doc_id,
list_of_players_by_score[-4].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-1].doc_id,
list_of_players_by_score[-5].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-3].doc_id,
list_of_players_by_score[-6].doc_id,
)
)
elif (
self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -1, -5
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -2, -6
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -3, -4
)
is not True
):
list_matchs.pop(-1)
list_matchs.append(
(
list_of_players_by_score[-1].doc_id,
list_of_players_by_score[-5].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-2].doc_id,
list_of_players_by_score[-6].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-3].doc_id,
list_of_players_by_score[-4].doc_id,
)
)
elif (
self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -1, -6
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -2, -5
)
is not True
and self.is_permuted_match_existed(
all_matchs_of_a_tournament, list_of_players_by_score, -3, -4
)
is not True
):
list_matchs.pop(-1)
list_matchs.append(
(
list_of_players_by_score[-1].doc_id,
list_of_players_by_score[-6].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-2].doc_id,
list_of_players_by_score[-5].doc_id,
)
)
list_matchs.append(
(
list_of_players_by_score[-3].doc_id,
list_of_players_by_score[-4].doc_id,
)
)
for match in list_matchs:
new_match = Match.create_match(
match[0],
match[1],
)
serialized_match = vars(new_match)
self.match_table.insert(serialized_match)
new_matchs_id.append(self.match_table.all()[-1].doc_id)
list_of_rounds = self.tournament_table.all()[-1]["rounds"]
list_of_rounds.append(self.round_table.all()[-1].doc_id)
self.round_table.update(
{"list_of_match": new_matchs_id},
doc_ids=[self.round_table.all()[-1].doc_id],
)
self.tournament_table.update(
{"rounds": list_of_rounds},
doc_ids=[self.tournament_table.all()[-1].doc_id],
)
self.warning.round_create(str(self.round_table.all()[-1]["current_round"]))
self.table.matchs()
return self.manage_tournament()
def choice_player_for_add_player_to_a_tournament(self):
"""
Choice a player in the list of all the players in the database
Returns:
choice_player (int): id a the player choosen
"""
return self.select.add_player()
def is_match_already_play(self, list_match, id1, id2):
"""
Check if a match has already been played
Args:
list_match (list): lists of couple of player's id
id1: id of the player one
id2: id of the player two
"""
for match in list_match:
if collections.Counter([id1, id2]) == collections.Counter(match):
return True
def end_tournament(self):
"""
End a tournament by updating status and end date of tournament table and displaying winner
"""
self.warning.tournament_winner()
self.tournament_table.update(
{
"end_date": json.dumps(
datetime.now().strftime("%d/%m/%Y %H:%M:%S"), default=str
)
},
doc_ids=[self.tournament_table.all()[-1].doc_id],
)
self.tournament_table.update(
{"status_tournament": "finished"},
doc_ids=[self.tournament_table.all()[-1].doc_id],
)
self.end_tournament_menu()
def quit(self):
"""
Close the program
"""
self.warning.quit()
return True