server can now send the current state of the tournament
This commit is contained in:
@ -3,10 +3,10 @@
|
||||
# ::: :::::::: #
|
||||
# Bot.py :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/10/05 03:54:20 by tomoron #+# #+# #
|
||||
# Updated: 2024/10/16 14:10:20 by tomoron ### ########.fr #
|
||||
# Created: 2024/10/19 18:29:36 by tomoron #+# #+# #
|
||||
# Updated: 2024/10/19 22:41:25 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -30,7 +30,7 @@ class Bot(Player):
|
||||
self.objective = {"pos":0, "up": False}
|
||||
self.skin = 0
|
||||
self.goal = 0
|
||||
if(gameSettings.noBotMovement):
|
||||
if(GameSettings.BotMovement):
|
||||
asyncio.create_task(self.updateLoop())
|
||||
asyncio.create_task(self.goToObjectiveLoop())
|
||||
print("I am a bot, boop boop beep boop")
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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
|
||||
def __init__(self, p1 , p2 , tournamentCode):
|
||||
self.initAttributes()
|
||||
self.p1 = p1
|
||||
self.p2 = p2
|
||||
if(isinstance(p1, Bot) and isinstance(p2, Bot)):
|
||||
self.winner=1
|
||||
self.pWinner=p1
|
||||
return
|
||||
elif(isinstance(p2,Bot)):
|
||||
p2,p1 = p1, p2
|
||||
self.withBot = isinstance(p1,Bot)
|
||||
self.p2,self.p1 = self.p1, self.p2
|
||||
self.withBot = isinstance(self.p1,Bot)
|
||||
self.tournamentCode = tournamentCode
|
||||
self.p1 = p1
|
||||
self.p2 = p2
|
||||
p1.setGame(self)
|
||||
p2.setGame(self)
|
||||
print("game created with ", p1.socket.username, "vs", p2.socket.username)
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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
|
||||
|
||||
maxPlayerSpeed = 6
|
||||
noBotMovement = False
|
||||
BotMovement = True
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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):
|
||||
self.messages = []
|
||||
self.players = []
|
||||
self.history = []
|
||||
self.nbBot = nbBot
|
||||
self.end = False
|
||||
self.genCode()
|
||||
@ -50,7 +51,7 @@ class Tournament:
|
||||
players = []
|
||||
for x in range(len(self.players)):
|
||||
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):
|
||||
self.messages.append({"username":socket.username, "message":message})
|
||||
@ -108,17 +109,18 @@ class Tournament:
|
||||
else:
|
||||
right = 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):
|
||||
self.started = True
|
||||
self.finalGame = self.createGames(self.players.copy())
|
||||
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):
|
||||
while self.finalGame.winner == None:
|
||||
await asyncio.sleep(1)
|
||||
print("tournament done, winner is ", self.finalGame.winner.socket.username)
|
||||
|
||||
|
||||
|
||||
|
@ -3,8 +3,7 @@
|
||||
# ::: :::::::: #
|
||||
# TournamentGame.py :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/10/12 22:49:00 by tomoron #+# #+# #
|
||||
# Updated: 2024/10/15 13:36:34 by tomoron ### ########.fr #
|
||||
# #
|
||||
@ -14,10 +13,10 @@ from .Game import Game
|
||||
import asyncio
|
||||
|
||||
class TournamentGame:
|
||||
def __init__(self, left, right, code):
|
||||
def __init__(self, left, right, tournament):
|
||||
self.game = None
|
||||
self.winner = None
|
||||
self.code = code
|
||||
self.tournament = tournament
|
||||
self.right = right
|
||||
self.left = left
|
||||
asyncio.create_task(self.loop())
|
||||
@ -27,11 +26,11 @@ class TournamentGame:
|
||||
r = None
|
||||
print("start new game")
|
||||
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
|
||||
r = self.right.winner
|
||||
else:
|
||||
self.game = Game(self.left, self.right, self.code)
|
||||
self.game = Game(self.left, self.right, self.tournament.code)
|
||||
l = self.left
|
||||
r = self.right
|
||||
l.socket.sync_send("tournament", {
|
||||
@ -66,5 +65,8 @@ class TournamentGame:
|
||||
else:
|
||||
if(self.game.winner != None):
|
||||
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
|
||||
await asyncio.sleep(1)
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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
|
||||
# - players : [{id, username, pfp}, ...]
|
||||
# - 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) :
|
||||
|
Reference in New Issue
Block a user