139 lines
4.8 KiB
Python
139 lines
4.8 KiB
Python
from ChessTournament.models.models import Player, Tournament, Round, Match
|
|
from ChessTournament.views.menu import Menu
|
|
from ChessTournament.views.base import View
|
|
|
|
from random import shuffle
|
|
|
|
|
|
class Controller:
|
|
def __init__(self):
|
|
# loading models
|
|
self.players_list = []
|
|
self.score_list = []
|
|
self.match = Match()
|
|
self.tournament = Tournament()
|
|
|
|
# loading views
|
|
self.view = View()
|
|
self.menu = Menu()
|
|
|
|
# for test
|
|
joueur1 = Player("Bob", "Durand", "25/12/1995", "EF34924")
|
|
joueur2 = Player("Joe", "Bidjoba", "02/01/2001", "QS42622")
|
|
joueur3 = Player("Jeanine", "Mequesurton", "25/12/1995", "AB20022")
|
|
joueur4 = Player("Jean-Pierre", "Quiroul", "15/09/1992", "JF78739")
|
|
joueur5 = Player("René", "Nuphard", "25/12/1995", "ED22230")
|
|
joueur6 = Player("Sophie", "Fonfec", "24/05/1999", "EE49948")
|
|
|
|
self.liste = [joueur1, joueur2, joueur3, joueur4, joueur5, joueur6]
|
|
|
|
def sort_by_score(self):
|
|
self.tournament.players_list.sort(key=lambda t: t.score, reverse = True)
|
|
|
|
def shuffle_players(self):
|
|
return shuffle(self.tournament.players_list)
|
|
|
|
def create_tournament(self):
|
|
print("Nouveau tournoi ! \n")
|
|
self.tournament.name = input("Nom du tournoi ? : ")
|
|
self.tournament.location = input("Lieu du tournoi : ")
|
|
self.tournament.date_start = input("date de début (jj/mm/aaaa) : ")
|
|
self.tournament.date_end = input("date de fin (jj/mm/aaaa) : ")
|
|
self.tournament.description = input("Description ? : ")
|
|
#self.tournament.players_list = input("Liste des joueurs : ")
|
|
self.tournament.players_list = self.liste
|
|
|
|
total_round = input("Nombre de tours ? (4 par défaut) : ") or 4
|
|
if total_round != 4:
|
|
self.tournament.total_round = int(total_round)
|
|
|
|
|
|
def run_tournament(self):
|
|
input("Prêt à lancer le premier round ?\n")
|
|
#print("tour", self.tournament.current_round, round1.start_time)
|
|
print("Liste des joueurs : ", self.tournament.players_list)
|
|
shuffle(self.tournament.players_list)
|
|
|
|
for i in range(1, self.tournament.total_round):
|
|
self.round = Round()
|
|
self.round.name = "Round " + str(i)
|
|
self.tournament.current_round = self.round.name
|
|
#pour chaque tour :
|
|
# set le temps de début
|
|
# créer les matchs
|
|
# afficher les matchs
|
|
# attendre la saisie des scores
|
|
# ajouter le tour à la liste des tours du tournoi
|
|
|
|
self.round.start_time = self.round.get_time()
|
|
self.round.match_list = self.create_match()
|
|
|
|
print("Liste des joueurs, après tri : ", self.tournament.players_list)
|
|
print("Liste des matchs du Round : ", self.round.match_list)
|
|
#self.view.prompt_for_scores()
|
|
print("saisir les scores :")
|
|
self.view.input_scores(self.round.match_list)
|
|
# append self.tournament.round_list with [round, match_list]
|
|
self.sort_by_score()
|
|
|
|
print("scores du round :\n")
|
|
for i in self.tournament.players_list:
|
|
print(i.ine, " : ", i.score)
|
|
|
|
input("Continuer ?")
|
|
|
|
|
|
def create_match(self):
|
|
"""Create match with two consecutive players. Check if match already happened in round
|
|
|
|
returns a round.match_list
|
|
"""
|
|
j = 0
|
|
k = 0
|
|
print(self.tournament.players_list)
|
|
for i in range(0, len(self.tournament.players_list), 2):
|
|
j += 1
|
|
match_name = "match" + str(j)
|
|
match = Match()
|
|
match.player1 = self.tournament.players_list[i]
|
|
match.player2 = self.tournament.players_list[i+1]
|
|
self.round.match_list.append(match)
|
|
|
|
return self.round.match_list
|
|
|
|
def record_new_player(self):
|
|
print("Enregistrez un nouveau joueur :\n")
|
|
self.lastname = input("Nom de famille ? : ")
|
|
self.name = input("Prénom ? : ")
|
|
self.birthdate = input("Date de naissance (jj/mm/aaaa) ? : ")
|
|
#self.birthdate = self.check_date()
|
|
self.ine = input("Identifiant National d'Echecs (ine) ? : ")
|
|
#self.ine = self.test_ine()
|
|
return {"Nom": self.lastname, "Prénom": self.name, "Date de naissance": self.birthdate, "INE": self.ine}
|
|
|
|
|
|
def run(self):
|
|
menu_choice = self.menu.items(1)
|
|
if menu_choice == "4":
|
|
print("Bye")
|
|
elif menu_choice == "3":
|
|
self.menu.items(2)
|
|
elif menu_choice == "2":
|
|
self.record_new_player()
|
|
elif menu_choice == "1":
|
|
print("c'est parti")
|
|
self.create_tournament()
|
|
self.run_tournament()
|
|
print("Tournoi terminé.\n Les scores sont :\n")
|
|
for i in self.tournament.players_list:
|
|
print(i.ine, " : ", i.score)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
run = Controller()
|
|
run.run()
|
|
|