refactor with MVC folder; tests in vrac.py for now

This commit is contained in:
2025-01-30 17:14:46 +01:00
parent 78cdc9028e
commit 471b2c9692
24 changed files with 467 additions and 70 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

27
models/match.py Normal file
View File

@@ -0,0 +1,27 @@
from models.player import Player
class Match:
"""Get two players
print a string with both ids
return a tuple of list player, score
"""
def __init__(self, player1, player2):
self.name = None
self.player1 = player1
self.player2 = player2
self.score1 = 0
self.score2 = 0
self.data = ([self.player1, self.score1], [self.player2, self.score2])
def __str__(self):
return f"[{self.player1}, {self.player2}]" #pretty print for prompt
def __repr__(self):
#return ([self.player1, self.score1], [self.player2, self.score2])
return str(self)
def update(self):
"""Update tuple when attributs have change"""
self.data = ([self.player1, self.score1], [self.player2, self.score2])
return self.data

49
models/participant.py Normal file
View File

@@ -0,0 +1,49 @@
from collections import UserDict
import json
from models.player import Player
class Participant(UserDict):
"""Dict of players and score attending a tournament
takes tournament's name and list of object Player
returns dict with player: score"""
def __init__(self, player_list = None):
#self.tournament
self.player_list = player_list
self.identifiant = ()
self.data = {}
self.PLAYERS_FILE = "./data/players/player_list.json"
# initiate list
def create_participant_from_list(self, players):
for item in players:
self.data[item.chess_id] = 0
return self.data
def get_list_from_file(self):
with open(self.PLAYERS_FILE) as file:
self.data = json.load(file)
def get_players_from_file(self):
"""create a Player list from the json file
uses file in current folder
return a list of object Player
"""
players = []
data = {}
with open(self.PLAYERS_FILE) as file:
data = json.load(file)
for i in data:
players.append(
Player(name=data[i][0], lastname=data[i][1], birthdate=data[i][2], gender=data[i][3], chess_id=i))
# print(data[i][0])
j = + 1
return self.create_participant_from_list(players)
def ask_for_new_participant(self):
pass

20
models/player.py Normal file
View File

@@ -0,0 +1,20 @@
import json
from datetime import datetime
class Player:
"""Player from the club"""
def __init__(self, name, lastname, birthdate, gender, chess_id=None):
self.name = name
self.lastname = lastname
self.birthdate = birthdate
self.gender = gender
self.chess_id = chess_id
def __str__(self):
"""Used in print"""
# return f"{self.name} {self.lastname}, né le {self.birthdate}, genre: {self.gender}"
return self.chess_id
def __repr__(self):
return str(self)

4
models/scorelist.py Normal file
View File

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

31
models/tournament.py Normal file
View File

@@ -0,0 +1,31 @@
from models.participant import Participant
from models.turn import Turn
class Tournament:
"""A competition with players and turns
takes player_list
"""
def __init__(self,
name,
participants,
location = "Club",
date_start = "today",
date_end = 'today',
current_turn = 1,
total_turn = 4 ):
self.name = name
self.participants = participants
self.location = location
self.date_start = date_start
self.date_end = date_end
self.total_turn = total_turn
self.current_turn = current_turn
self.description = "Pas encore de description"
self.turn_list = []
def run_turns(self):
pass
#if self.current_turn == "Round 1":

74
models/turn.py Normal file
View File

@@ -0,0 +1,74 @@
from random import choice, shuffle
from models.participant import Participant
from models.match import Match
class Turn:
"""Round for tournament
has name, dict of participant (object)
"""
def __init__(self, participants, name="Round 1"):
self.name = name
self.participants = participants
self.match_history = []
self.match_list = []
self.match_result = []
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):
"""shuffle player's 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):
return sorted(self.participants.items(), key=lambda t: t[1])
def create_match(self):
print("Liste des joueurs: ", self.player_list)
j = 0
for i in range(0, len(self.player_list), 2):
j += 1
match = Match(self.player_list[i][0], self.player_list[i+1][0])
match.name = "match" + str(j)
if match in self.match_history: # If match has already been made, choose the next player
match = Match(self.player_list[i][0], self.player_list[i+2][0])
self.match_list.append(match)
else:
self.match_list.append(match)
#print(match)
self.match_history.append([self.name, self.match_list])
return self.match_list
# if i.index
def input_scores(self):
for match in self.match_list:
print(match.name)
self.result = input(f"Vainqueur du {match.name} : 1.{match.player1}, 2.{match.player2}, 3.nul\n ? ")
if self.result == "1":
self.participants[match.player1] += 1
match.score1 += 1
if self.result == "2":
self.participants[match.player2] += 1
match.score2 += 1
if self.result == "3":
self.participants[match.player1] += 0.5
match.score1 += 0.5
self.participants[match.player2] += 0.5
match.score2 += 0.5
match.update() # update match then save it at the end of the turn
self.match_result.append(match.data)
return self.match_result