server can now send the current state of the tournament

This commit is contained in:
2024-10-19 22:54:24 +02:00
parent dc2073f6f2
commit 7e57980c01
6 changed files with 34 additions and 24 deletions

View File

@ -3,10 +3,10 @@
# ::: :::::::: # # ::: :::::::: #
# Bot.py :+: :+: :+: # # Bot.py :+: :+: :+: #
# +:+ +:+ +:+ # # +:+ +:+ +:+ #
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/05 03:54:20 by tomoron #+# #+# # # Created: 2024/10/19 18:29:36 by tomoron #+# #+# #
# Updated: 2024/10/16 14:10:20 by tomoron ### ########.fr # # Updated: 2024/10/19 22:41:25 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -30,7 +30,7 @@ class Bot(Player):
self.objective = {"pos":0, "up": False} self.objective = {"pos":0, "up": False}
self.skin = 0 self.skin = 0
self.goal = 0 self.goal = 0
if(gameSettings.noBotMovement): if(GameSettings.BotMovement):
asyncio.create_task(self.updateLoop()) asyncio.create_task(self.updateLoop())
asyncio.create_task(self.goToObjectiveLoop()) asyncio.create_task(self.goToObjectiveLoop())
print("I am a bot, boop boop beep boop") print("I am a bot, boop boop beep boop")

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# # # Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
# Updated: 2024/10/15 13:38:03 by tomoron ### ########.fr # # Updated: 2024/10/19 22:27:23 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -30,16 +30,16 @@ class Game:
@multimethod @multimethod
def __init__(self, p1 , p2 , tournamentCode): def __init__(self, p1 , p2 , tournamentCode):
self.initAttributes() self.initAttributes()
self.p1 = p1
self.p2 = p2
if(isinstance(p1, Bot) and isinstance(p2, Bot)): if(isinstance(p1, Bot) and isinstance(p2, Bot)):
self.winner=1 self.winner=1
self.pWinner=p1 self.pWinner=p1
return return
elif(isinstance(p2,Bot)): elif(isinstance(p2,Bot)):
p2,p1 = p1, p2 self.p2,self.p1 = self.p1, self.p2
self.withBot = isinstance(p1,Bot) self.withBot = isinstance(self.p1,Bot)
self.tournamentCode = tournamentCode self.tournamentCode = tournamentCode
self.p1 = p1
self.p2 = p2
p1.setGame(self) p1.setGame(self)
p2.setGame(self) p2.setGame(self)
print("game created with ", p1.socket.username, "vs", p2.socket.username) print("game created with ", p1.socket.username, "vs", p2.socket.username)

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/06 16:33:56 by tomoron #+# #+# # # Created: 2024/10/06 16:33:56 by tomoron #+# #+# #
# Updated: 2024/10/16 14:12:50 by tomoron ### ########.fr # # Updated: 2024/10/19 22:41:38 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -46,4 +46,4 @@ class GameSettings:
maxScore = 2 maxScore = 2
maxPlayerSpeed = 6 maxPlayerSpeed = 6
noBotMovement = False BotMovement = True

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/04 17:17:07 by tomoron #+# #+# # # Created: 2024/10/04 17:17:07 by tomoron #+# #+# #
# Updated: 2024/10/15 19:01:39 by tomoron ### ########.fr # # Updated: 2024/10/19 21:53:02 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -25,6 +25,7 @@ class Tournament:
def __init__(self, socket, nbBot, skin, goal): def __init__(self, socket, nbBot, skin, goal):
self.messages = [] self.messages = []
self.players = [] self.players = []
self.history = []
self.nbBot = nbBot self.nbBot = nbBot
self.end = False self.end = False
self.genCode() self.genCode()
@ -50,7 +51,7 @@ class Tournament:
players = [] players = []
for x in range(len(self.players)): for x in range(len(self.players)):
players.append({"id":x,"username":self.players[x].socket.username, "pfp":self.players[x].socket.pfp}) players.append({"id":x,"username":self.players[x].socket.username, "pfp":self.players[x].socket.pfp})
socket.sync_send("tournament",{"action":5, "players":players, "messages" : self.messages}) socket.sync_send("tournament",{"action":5, "players":players, "messages" : self.messages, "history": self.history})
def sendMessage(self, socket, message): def sendMessage(self, socket, message):
self.messages.append({"username":socket.username, "message":message}) self.messages.append({"username":socket.username, "message":message})
@ -108,17 +109,18 @@ class Tournament:
else: else:
right = self.createGames(players, level + 1) right = self.createGames(players, level + 1)
left = self.createGames(players, level + 1) left = self.createGames(players, level + 1)
return(TournamentGame(left, right, self.code)) return(TournamentGame(left, right, self))
def start(self): def start(self):
self.started = True self.started = True
self.finalGame = self.createGames(self.players.copy()) self.finalGame = self.createGames(self.players.copy())
asyncio.create_task(self.tournamentLoop()) asyncio.create_task(self.tournamentLoop())
def addHistory(self, p1, p2, p1Win):
self.broadcast({"action":6, "p1" : p1, "p2" : p2, "p1Win": p1Win})
self.history.append({"p1": p1, "p2" : p2, "p1Win":p1Win})
async def tournamentLoop(self): async def tournamentLoop(self):
while self.finalGame.winner == None: while self.finalGame.winner == None:
await asyncio.sleep(1) await asyncio.sleep(1)
print("tournament done, winner is ", self.finalGame.winner.socket.username) print("tournament done, winner is ", self.finalGame.winner.socket.username)

View File

@ -3,8 +3,7 @@
# ::: :::::::: # # ::: :::::::: #
# TournamentGame.py :+: :+: :+: # # TournamentGame.py :+: :+: :+: #
# +:+ +:+ +:+ # # +:+ +:+ +:+ #
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # +#+#+#+#+#+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/12 22:49:00 by tomoron #+# #+# # # Created: 2024/10/12 22:49:00 by tomoron #+# #+# #
# Updated: 2024/10/15 13:36:34 by tomoron ### ########.fr # # Updated: 2024/10/15 13:36:34 by tomoron ### ########.fr #
# # # #
@ -14,10 +13,10 @@ from .Game import Game
import asyncio import asyncio
class TournamentGame: class TournamentGame:
def __init__(self, left, right, code): def __init__(self, left, right, tournament):
self.game = None self.game = None
self.winner = None self.winner = None
self.code = code self.tournament = tournament
self.right = right self.right = right
self.left = left self.left = left
asyncio.create_task(self.loop()) asyncio.create_task(self.loop())
@ -27,11 +26,11 @@ class TournamentGame:
r = None r = None
print("start new game") print("start new game")
if(isinstance(self.left,TournamentGame)): if(isinstance(self.left,TournamentGame)):
self.game = Game(self.left.winner, self.right.winner, self.code) self.game = Game(self.left.winner, self.right.winner, self.tournament.code)
l = self.left.winner l = self.left.winner
r = self.right.winner r = self.right.winner
else: else:
self.game = Game(self.left, self.right, self.code) self.game = Game(self.left, self.right, self.tournament.code)
l = self.left l = self.left
r = self.right r = self.right
l.socket.sync_send("tournament", { l.socket.sync_send("tournament", {
@ -66,5 +65,8 @@ class TournamentGame:
else: else:
if(self.game.winner != None): if(self.game.winner != None):
print("game ended, winner is", self.game.pWinner.socket.username) print("game ended, winner is", self.game.pWinner.socket.username)
p1Id = self.tournament.playerFromSocket(self.game.p1.socket)
p2Id = self.tournament.playerFromSocket(self.game.p2.socket)
self.tournament.addHistory(p1Id, p2Id, self.game.winner == 1)
self.winner = self.game.pWinner self.winner = self.game.pWinner
await asyncio.sleep(1) await asyncio.sleep(1)

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/01 13:16:39 by edbernar #+# #+# # # Created: 2024/10/01 13:16:39 by edbernar #+# #+# #
# Updated: 2024/10/14 21:41:33 by edbernar ### ########.fr # # Updated: 2024/10/19 22:54:14 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -45,6 +45,12 @@ from .tournamentActions.fetchAllData import fetchAllData
# 5 : allData : gives tournament data to the client # 5 : allData : gives tournament data to the client
# - players : [{id, username, pfp}, ...] # - players : [{id, username, pfp}, ...]
# - messages : [{username, message}] # - messages : [{username, message}]
# - history : [{p1, p2, p1Win})]
#
# 6 : gameUpdate : when a tournament match ends
# - p1 : id of the player 1
# - p2 : id of the player 2
# - p1Win : true/false
#client actions (actions sent by the client) : #client actions (actions sent by the client) :