Add game's Watchers

This commit is contained in:
nemunaire 2020-03-15 11:25:36 +01:00
parent 0f482aade5
commit 9bbcedeb59

View File

@ -44,6 +44,7 @@ def readSocket(socket, string):
s = socket.recv(1024).decode().strip()
return s.split("\n")[-1].strip()
watchers = []
while True:
try:
connP1, addrP1 = s.accept()
@ -51,25 +52,32 @@ while True:
connP1.send("Hey! Welcome to Opale game. What's your name?\n> ".encode())
print('Asking Player1 name...')
nameP1 = connP1.recv(1024).decode().strip()
connP1.send(("Welcome \033[93m%s\033[0m! We are waiting for a second player...\n" % nameP1).encode())
connP1.send("\n\nWould you like to load a saved game? (leave blank and press ENTER to load a new game)\n> ".encode())
if nameP1 == "watcher":
print("new Watcher")
watchers.append(connP1)
connP1.send(("Welcome! You'll watch futher games. Wait for one... :)\n").encode())
continue
else:
connP1.send(("Welcome \033[93m%s\033[0m! We are waiting for a second player...\n" % nameP1).encode())
connP2, addrP2 = s.accept()
print('Connected with ' + addrP2[0] + ':' + str(addrP2[1]))
connP2.send("Hey! Welcome to Opale game. What's your name?\n> ".encode())
print('Asking Player2 name...')
nameP2 = ""
while len(nameP2) == 0 or nameP2 == nameP1:
if nameP2 == nameP1:
connP2.send("Name conflict! Please choose another name: ".encode())
nameP2 = connP2.recv(1024).decode().strip()
connP2.send(("Welcome \033[93m%s\033[0m! We are ready to play. You'll fight against \033[93m%s\033[0m!\n" % (nameP2, nameP1)).encode())
connP1.send(("\nYou'll fight against \033[93m%s\033[0m" % nameP2).encode())
connP1.send("\n\nWould you like to load a saved game? (leave blank and press ENTER to load a new game)\n> ".encode())
saved_games = [k for k in games.keys() if (games[k].player1.name == nameP1 and games[k].player2.name == nameP2) or (games[k].player1.name == nameP2 and games[k].player2.name == nameP1)]
if len(saved_games):
connP1.send(("\nHere is a list of saved games together: %s\n" % saved_games).encode())
connP2, addrP2 = s.accept()
print('Connected with ' + addrP2[0] + ':' + str(addrP2[1]))
connP2.send("Hey! Welcome to Opale game. What's your name?\n> ".encode())
print('Asking Player2 name...')
nameP2 = ""
while len(nameP2) == 0 or nameP2 == nameP1:
if nameP2 == nameP1:
connP2.send("Name conflict! Please choose another name: ".encode())
nameP2 = connP2.recv(1024).decode().strip()
connP2.send(("Welcome \033[93m%s\033[0m! We are ready to play. You'll fight against \033[93m%s\033[0m!\n" % (nameP2, nameP1)).encode())
connP1.send(("\nYou'll fight against \033[93m%s\033[0m" % nameP2).encode())
saved_games = [k for k in games.keys() if (games[k].player1.name == nameP1 and games[k].player2.name == nameP2) or (games[k].player1.name == nameP2 and games[k].player2.name == nameP1)]
if len(saved_games):
connP1.send(("\nHere is a list of saved games together: %s\n" % saved_games).encode())
choice = "sentinel"
erchoice = ""
@ -111,11 +119,24 @@ while True:
game.player1.input = functools.partial(readSocket, connP1)
game.player2.input = functools.partial(readSocket, connP2)
def printWatchers(*args):
for w in watchers:
try:
writeSocket(w, *args)
except:
try:
w.close()
except:
pass
watchers.remove(w)
def printBoth(*args):
game.player1.print(*args)
game.player2.print(*args)
printWatchers(*args)
def play_turn():
printWatchers("\n\033[1mCurrent player:\033[0m \033[93m%s\033[0m \033[95m%s\033[0m" % (game.current_player.name, "DRAGON" if game.current_player.dragonPetrified else ""))
game.current_player.print("\n\033[1mCurrent player:\033[0m \033[93m%s\033[0m \033[95m%s\033[0m" % (game.current_player.name, "DRAGON" if game.current_player.dragonPetrified else ""))
game.current_partner.print("\n\033[1mCurrent player:\033[0m \033[96m%s\033[0m \033[95m%s\033[0m" % (game.current_player.name, "DRAGON" if game.current_player.dragonPetrified else ""))
@ -124,6 +145,7 @@ while True:
game.current_player.print("\033[1mHere is your hand currently:\033[0m %s" % {i+1: game.current_player.hand[i] for i in range(len(game.current_player.hand))})
game.current_partner.print("\033[1mHere is your hand currently:\033[0m %s" % {i+1: game.current_partner.hand[i] for i in range(len(game.current_partner.hand))})
game.current_partner.print("\n =============:[ Please wait until \033[1;96m" + game.current_player.name + "\033[0m plays... :-) ]:=============\r",None)
printWatchers("\n =============:[ Please wait until \033[1;96m" + game.current_player.name + "\033[0m plays... :-) ]:=============\r",None)
icards = game.current_player.input("\nChoose one or more card to play: ")
pcards = map(lambda x: int(x), icards.split(" "))
good = True
@ -138,19 +160,8 @@ while True:
if good:
try:
curRound = game.board.roundCity
hasDragon = game.current_player.dragonPetrified
game.play_round(*cards)
printBoth(chr(27) + "[2J")
game.current_player.print("\n\033[93m%s\033[0m plays: %s\n" % (game.current_partner.name, cards))
game.current_partner.print("\nYou played: %s\n" % (cards))
if game.current_partner.dragonPetrified != hasDragon:
game.current_player.print("\n\033[1;93m%s\033[95m takes the petrified dragon!\033[0m\n" % game.current_partner.name)
game.current_partner.print("\n\033[95mYou take the petrified dragon!\033[0m\n")
if curRound != game.board.roundCity and game.board.roundCity < 3:
game.current_player.print("\n\033[1;93m%s\033[96m discovers a new lost city! \033[1m%d/%d\033[0m\n" % (game.current_partner.name, game.board.roundCity, 3))
game.current_partner.print("\n\033[96mYou discover a new lost city! \033[1m%d/%d\033[0m\n" % (game.board.roundCity, 3))
break
return cards
except Exception as e:
game.current_player.print("\033[91m%s\033[0m" % e)
@ -170,14 +181,32 @@ while True:
game.player2.input = ip2
game.player1.print = pp1
game.player2.print = pp2
curRound = game.board.roundCity
hasDragon = game.current_player.dragonPetrified
try:
play_turn()
cards = play_turn()
except BrokenPipeError as e:
raise e
except Exception as e:
raise e
game.current_player.print("\033[91mSorry, something goes wrong: '%s'.\033[0m" % e)
pass
printBoth(chr(27) + "[2J")
game.current_player.print("\n\033[93m%s\033[0m plays: %s\n" % (game.current_partner.name, cards))
printWatchers("\n\033[93m%s\033[0m plays: %s\n" % (game.current_partner.name, cards))
game.current_partner.print("\nYou played: %s\n" % (cards))
if game.current_partner.dragonPetrified != hasDragon:
game.current_player.print("\n\033[1;93m%s\033[95m takes the petrified dragon!\033[0m\n" % game.current_partner.name)
printWatchers("\n\033[1;93m%s\033[95m takes the petrified dragon!\033[0m\n" % game.current_partner.name)
game.current_partner.print("\n\033[95mYou take the petrified dragon!\033[0m\n")
if curRound != game.board.roundCity and game.board.roundCity < 3:
game.current_player.print("\n\033[1;93m%s\033[96m discovers a new lost city! \033[1m%d/%d\033[0m\n" % (game.current_partner.name, game.board.roundCity, 3))
printWatchers("\n\033[1;93m%s\033[96m discovers a new lost city! \033[1m%d/%d\033[0m\n" % (game.current_partner.name, game.board.roundCity, 3))
game.current_partner.print("\n\033[96mYou discover a new lost city! \033[1m%d/%d\033[0m\n" % (game.board.roundCity, 3))
if game.player1.cantpioche:
printBoth("\n\033[1;93m%s n'a plus de pioche ! Last round\033[0m" % game.player1.name)
elif game.player2.cantpioche: