adapt folders to MVC

This commit is contained in:
yann 2025-01-31 09:43:02 +01:00
parent 471b2c9692
commit eff56ad6d6
6 changed files with 36 additions and 38 deletions

View File

@ -3,19 +3,16 @@ import json
from models.player import Player from models.player import Player
class Participant(UserDict): class Participants(UserDict):
"""Dict of players and score attending a tournament """Dict of players and score attending a tournament
takes tournament's name and list of object Player takes tournament's name and list of object Player
returns dict with player: score""" returns dict with player: score"""
def __init__(self, player_list = None): def __init__(self, player_list): #player_list FOR TEST ; to feed when creating object
#self.tournament #self.tournament
self.player_list = player_list self.player_list = player_list
self.identifiant = ()
self.data = {} self.data = {}
self.PLAYERS_FILE = "./data/players/player_list.json" self.PLAYERS_FILE = "./data/players/player_list.json" #FOR TEST
# initiate list
def create_participant_from_list(self, players): def create_participant_from_list(self, players):
for item in players: for item in players:

View File

@ -1,4 +0,0 @@
class ScoreList(list):
"""Player tuple (name, lastname) and score"""
def __init__(self):

View File

@ -1,4 +1,4 @@
from models.participant import Participant from models.participant import Participants
from models.turn import Turn from models.turn import Turn
@ -25,7 +25,4 @@ class Tournament:
self.description = "Pas encore de description" self.description = "Pas encore de description"
self.turn_list = [] self.turn_list = []
def run_turns(self):
pass
#if self.current_turn == "Round 1":

View File

@ -1,5 +1,5 @@
from random import choice, shuffle from random import choice, shuffle
from models.participant import Participant from models.participant import Participants
from models.match import Match from models.match import Match
class Turn: class Turn:
@ -15,35 +15,25 @@ class Turn:
self.match_result = [] self.match_result = []
self.player_list = [] self.player_list = []
def create_player_list(self): #not used for now
"""name list from dict"""
for player in self.participants:
self.player_list.append([player[0], player[1]])
def ramble_player_list(self): def ramble_player_list(self):
"""shuffle player's list""" """shuffle player's list"""
return shuffle(self.player_list) return shuffle(self.player_list)
def sort_scores(self, player_list):
"""order players on score"""
def score(couple):
return couple[1]
return sorted(player_list, key=score)
def sort_players_by_score(self): def sort_players_by_score(self):
"""orders dict on value and returns sorted list"""
return sorted(self.participants.items(), key=lambda t: t[1]) return sorted(self.participants.items(), key=lambda t: t[1])
def create_match(self): def create_match(self):
print("Liste des joueurs: ", self.player_list) print("Liste des joueurs: ", self.player_list)
j = 0 j = 0
k = 0
for i in range(0, len(self.player_list), 2): for i in range(0, len(self.player_list), 2):
j += 1 j += 1
match = Match(self.player_list[i][0], self.player_list[i+1][0]) match = Match(self.player_list[i][0], self.player_list[i+1][0])
match.name = "match" + str(j) match.name = "match" + str(j)
while match in self.match_history:
if match in self.match_history: # If match has already been made, choose the next player k += 1# If match has already been made, choose the next player
match = Match(self.player_list[i][0], self.player_list[i+2][0]) match = Match(self.player_list[i][0], self.player_list[i+k][0])
self.match_list.append(match) self.match_list.append(match)
else: else:
self.match_list.append(match) self.match_list.append(match)

View File

@ -6,4 +6,7 @@ class View:
def prompt_for_scores(self): def prompt_for_scores(self):
print() print()
input("Saisir les scores ?") input("Saisir les scores ?")
return True return True
def display_winner(self, participants):
pass

29
vrac.py
View File

@ -1,4 +1,4 @@
from models.participant import Participant from models.participant import Participants
from models.player import Player from models.player import Player
from models.match import Match from models.match import Match
from models.turn import Turn from models.turn import Turn
@ -78,7 +78,7 @@ player_list = [joueur1, joueur2, joueur3, joueur4, joueur5, joueur6]
#print(name_from_chess_id("JF78739", player_list)) #print(name_from_chess_id("JF78739", player_list))
def test2(player_list): def test2(player_list):
# create new participants object (dict from list)... # create new participants object (dict from list)...
participants = Participant("Tournoi de cajou", player_list) participants = Participants("Tournoi de cajou", player_list)
# display the dict # display the dict
print("print(participants) : ", participants.create_participant_from_list()) print("print(participants) : ", participants.create_participant_from_list())
print(participants.data) print(participants.data)
@ -117,15 +117,21 @@ def test(player_list):
def test3(): def test3():
# initialization # initialization
participants = Participant() participants = Participants(player_list)
participants.get_players_from_file() #load dict from file participants.get_players_from_file() #load dict from file
view = View() view = View()
tour = Turn(participants) turn_nb = 1
tournoi1 = Tournament("Tournoi de Cajou", participants) tournoi1 = Tournament("Tournoi de Cajou", participants)
def run_turn(turn_nb): def run_turn(turn_nb):
tour = Turn(participants.data, name = "Round"+str(turn_nb)) tour = Turn(participants.data, name = "Round"+str(turn_nb))
print("Commençons le", tour.name)
if turn_nb == 1:
tour.player_list = tour.sort_players_by_score()
else:
tour.player_list = tour.sort_players_by_score()
tour.create_match() tour.create_match()
print(f"La liste des matchs pour le {tour.name} est :\n {tour.match_list}") print(f"La liste des matchs pour le {tour.name} est :\n {tour.match_list}")
view.prompt_for_scores() view.prompt_for_scores()
@ -133,11 +139,20 @@ def test3():
print("Save \n", tour.name, tour.match_result) print("Save \n", tour.name, tour.match_result)
tournoi1.turn_list.append([tour.name, tour.match_result]) tournoi1.turn_list.append([tour.name, tour.match_result])
while i < tournoi1.total_turn: def display_winner(participants):
if tounoi1.current_turn == 1: base =
for i in participants:
if participants[i]
print("Début du", tournoi1.name, "!")
while turn_nb < tournoi1.total_turn:
tournoi1.current_turn = turn_nb
run_turn(turn_nb)
turn_nb += 1
run_turn(turn_nb) print("\nLe", tournoi1.name, "est terminé.\n")
print("Scores finaux:\n", participants.data)
print("liste des tours:\n", tournoi1.turn_list)
#for i in range(1, tournoi1.total_turn+1): #for i in range(1, tournoi1.total_turn+1):
#tour = Turn(participants, name = "Round"+str(i)) #tour = Turn(participants, name = "Round"+str(i))