reorganized everything, put launcher in main
This commit is contained in:
@@ -1,122 +1,269 @@
|
||||
from ChessTournament.models.models import Player, Tournament, Round, Match
|
||||
from ChessTournament.models.models import (Player, Tournament, Round, Match,
|
||||
MatchHistory)
|
||||
from ChessTournament.models.models import DATAPATH, PLAYERFILE, TOURNAMENTFILE
|
||||
from ChessTournament.views.menu import Menu
|
||||
from ChessTournament.views.base import View
|
||||
|
||||
from random import shuffle
|
||||
import os
|
||||
import json
|
||||
|
||||
|
||||
class Controller:
|
||||
class Save:
|
||||
def __init__(self):
|
||||
# loading models
|
||||
self.players_list = []
|
||||
self.score_list = []
|
||||
pass
|
||||
|
||||
def load_file(self, file):
|
||||
try:
|
||||
os.mkdir(DATAPATH)
|
||||
except FileExistsError:
|
||||
pass
|
||||
# test if file exists... could be done more gracefully
|
||||
try:
|
||||
with open(file, "r") as json_file:
|
||||
data_tmp = json.load(json_file)
|
||||
return data_tmp
|
||||
|
||||
except json.decoder.JSONDecodeError:
|
||||
print("Erreur de format sur le fichier")
|
||||
except FileNotFoundError:
|
||||
return False
|
||||
|
||||
def write_file(self, data_tmp, file):
|
||||
with open(file, "w") as json_file:
|
||||
json_file.write(json.dumps(data_tmp))
|
||||
return "Done."
|
||||
|
||||
|
||||
def player_write(self, player) -> bool:
|
||||
data_tmp = []
|
||||
if self.load_file(PLAYERFILE):
|
||||
data_tmp = (self.load_file(PLAYERFILE))
|
||||
data_tmp.append(player)
|
||||
self.write_file(data_tmp, PLAYERFILE)
|
||||
print("Joueur créé !")
|
||||
return True
|
||||
|
||||
def player_load(self) -> list:
|
||||
"""Load and create player from JSON file
|
||||
returns:
|
||||
list of Player"""
|
||||
if self.load_file(PLAYERFILE):
|
||||
data_tmp = self.load_file(PLAYERFILE)
|
||||
data_list = []
|
||||
for player in data_tmp:
|
||||
data_list.append(Player(name=player['name'],
|
||||
lastname=player['lastname'],
|
||||
birthdate=player['birthdate'],
|
||||
ine=player['ine']))
|
||||
|
||||
return data_list
|
||||
else:
|
||||
print("\n**** Pas de fichier joueur trouvé :/\n")
|
||||
|
||||
def tournament_write(self, tournament):
|
||||
data = {
|
||||
tournament.name: tournament.data()
|
||||
}
|
||||
print(data)
|
||||
|
||||
if self.load_file(TOURNAMENTFILE):
|
||||
data_tmp = self.load_file(TOURNAMENTFILE)
|
||||
data_tmp[tournament.name] = tournament.data()
|
||||
self.write_file(data_tmp, TOURNAMENTFILE)
|
||||
else:
|
||||
self.write_file(data, TOURNAMENTFILE)
|
||||
return True
|
||||
|
||||
def tournament_load(self):
|
||||
if self.load_file(TOURNAMENTFILE):
|
||||
data_tmp = self.load_file(TOURNAMENTFILE)
|
||||
return data_tmp
|
||||
else:
|
||||
print("\n**** Pas de fichier tournoi trouvé :/ \n")
|
||||
|
||||
|
||||
class Application:
|
||||
def __init__(self, tournament, save, view, menu):
|
||||
self.tournament = tournament
|
||||
self.save = save
|
||||
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]
|
||||
self.match_history = MatchHistory()
|
||||
self.view = view
|
||||
self.menu = menu
|
||||
|
||||
def sort_by_score(self):
|
||||
self.tournament.players_list.sort(key=lambda t: t.score, reverse = True)
|
||||
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)
|
||||
|
||||
tournament_details = self.view.prompt_for_tournament()
|
||||
self.tournament.name = tournament_details['name']
|
||||
self.tournament.location = tournament_details['location']
|
||||
self.tournament.date_start = tournament_details['date_start']
|
||||
self.tournament.date_end = tournament_details['date_end']
|
||||
self.tournament.description = tournament_details['description']
|
||||
self.tournament.total_round = tournament_details['total_round']
|
||||
if self.save.player_load():
|
||||
self.tournament.players_list = self.save.player_load()
|
||||
self.save.tournament_write(self.tournament)
|
||||
else:
|
||||
print("Placez un fichier joueur dans le répertoire data"
|
||||
"ou créez des nouveaux joueurs depuis le menu")
|
||||
print()
|
||||
self.menu_manager()
|
||||
|
||||
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):
|
||||
for each_round in range(1, self.tournament.total_round + 1):
|
||||
self.tournament.current_round += 1
|
||||
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.name = "Round " + str(each_round)
|
||||
self.view.prompt_for_round(self.round)
|
||||
# set round start time
|
||||
self.round.start_time = self.round.get_time()
|
||||
# create matches TODO : check from history
|
||||
self.round.match_list = self.create_match()
|
||||
# display matches
|
||||
self.view.display_matches(self.round.match_list)
|
||||
self.view.prompt_for_scores()
|
||||
self.round.end_time = self.round.get_time()
|
||||
self.view.input_scores(self.round.match_list)
|
||||
self.scores(self.round.match_list)
|
||||
self.sort_by_score()
|
||||
self.tournament.round_list.append(self.round.save())
|
||||
print("après maj", self.tournament.round_list)
|
||||
self.save.tournament_write(self.tournament)
|
||||
self.view.display_round_info(self.round)
|
||||
self.view.display_scores(self.tournament.players_list)
|
||||
|
||||
print("Le tournoi", self.tournament.name, "est terminé !")
|
||||
print("\nLe tournoi", self.tournament.name, "est terminé !\n")
|
||||
|
||||
def get_match_info(self, match_list):
|
||||
matches = []
|
||||
for i in match_list:
|
||||
matches.append(i.get_data())
|
||||
return matches
|
||||
|
||||
def check_match(self, match, match_history):
|
||||
for item in match_history:
|
||||
if match in item:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def create_match(self) -> list:
|
||||
"""Create match with two consecutive players
|
||||
|
||||
|
||||
|
||||
def create_match(self):
|
||||
"""Create match with two consecutive players. Check if match already happened in round
|
||||
|
||||
returns a round.match_list
|
||||
returns :
|
||||
list of Match
|
||||
"""
|
||||
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)
|
||||
match_list.append(match)
|
||||
return match_list
|
||||
|
||||
return self.round.match_list
|
||||
def scores(self, match_list) -> list:
|
||||
"""user asked to enter scores, update Player
|
||||
returns:
|
||||
list of tuples
|
||||
"""
|
||||
matches = []
|
||||
for match in match_list:
|
||||
count = match_list.index(match) + 1
|
||||
result = self.view.input_scores(match, count)
|
||||
if result in ("1", "2", "3"):
|
||||
if result == "1":
|
||||
match.player1.score += 1
|
||||
match.score1 = 1
|
||||
elif result == "2":
|
||||
match.player2.score += 1
|
||||
match.score2 = 1
|
||||
elif result == "3":
|
||||
match.player1.score += 0.5
|
||||
match.player2.score += 0.5
|
||||
match.score1 = match.score2 = 0.5
|
||||
matches.append(match.get_data())
|
||||
return matches
|
||||
|
||||
|
||||
|
||||
def run(self):
|
||||
def menu_manager(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.view.prompt_for_new_player()
|
||||
elif menu_choice == "1":
|
||||
print("c'est parti")
|
||||
self.create_tournament()
|
||||
self.run_tournament()
|
||||
self.view.display_winner(self.tournament.players_list)
|
||||
self.view.display_scores(self.tournament.players_list)
|
||||
while True:
|
||||
# Quit
|
||||
if menu_choice == "4":
|
||||
print("Bye")
|
||||
|
||||
# Rapports
|
||||
elif menu_choice == "3":
|
||||
rapport_choice = self.menu.items(2)
|
||||
|
||||
# Go back
|
||||
if rapport_choice == "4":
|
||||
self.menu_manager()
|
||||
|
||||
# Display players from file
|
||||
elif rapport_choice == "1":
|
||||
if self.save.player_load():
|
||||
self.view.display_players(self.save.player_load())
|
||||
input("?")
|
||||
|
||||
# Display list of tournaments
|
||||
elif rapport_choice == "2":
|
||||
if self.save.tournament_load():
|
||||
self.view.display_tournaments(self.save.tournament_load())
|
||||
input("?")
|
||||
|
||||
# display tournament's details
|
||||
elif rapport_choice == "3":
|
||||
temp = {}
|
||||
if self.save.tournament_load():
|
||||
temp = self.save.tournament_load()
|
||||
name = self.view.prompt_tournament_to_display(temp)
|
||||
if name in temp:
|
||||
self.view.display_tournament_detail(
|
||||
temp[name])
|
||||
else:
|
||||
self.view.display_error()
|
||||
|
||||
# create new player and save it in file
|
||||
elif menu_choice == "2":
|
||||
joueur = self.view.prompt_for_new_player()
|
||||
self.save.player_write(joueur)
|
||||
input("Retour ?")
|
||||
self.menu_manager()
|
||||
|
||||
# create new tournament
|
||||
elif menu_choice == "1":
|
||||
print("c'est parti")
|
||||
self.create_tournament()
|
||||
self.run_tournament()
|
||||
self.view.display_winner(self.tournament.players_list)
|
||||
self.view.display_scores(self.tournament.players_list)
|
||||
self.menu_manager()
|
||||
|
||||
|
||||
run = Controller()
|
||||
run.run()
|
||||
class CheckMatch:
|
||||
pass
|
||||
|
||||
|
||||
class MenuManager:
|
||||
pass
|
||||
|
||||
|
||||
class TournamentManager:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
class UserManager:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user