-
Notifications
You must be signed in to change notification settings - Fork 0
/
onlinePlay.py
102 lines (90 loc) · 3.34 KB
/
onlinePlay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import asyncio
from websockets.server import serve
from websockets.sync.client import connect
import logicaSemaforo as s
import json
def mkWebSocket(ip, port):
try:
with connect("ws://"+ip+":"+port) as websocket:
websocket.send("semáforo")
message = websocket.recv()
if message == "semáforo indeed":
return websocket
else:
return False
except ConnectionRefusedError:
return False
with open("config.json", "r") as configFile:
config = json.load(configFile)
ip = config["ip"]
port = config["port"]
def newGame(name, avatar, public):
with connect("ws://"+ip+":"+port) as ws:
jsonS = json.dumps(("newGame", name, avatar, public))
ws.send(jsonS)
message = ws.recv()
content = json.loads(message)
if content[0] == "newGameCreated":
return "newGameCreated", content[1]
else:
return "error", content
def waitForGame(gameID, name, playerNumber, avatar=None):
with connect("ws://"+ip+":"+port) as ws:
jsonS = json.dumps(("checkGame", gameID, name, playerNumber, avatar))
ws.send(jsonS)
message = ws.recv()
content = json.loads(message)
if content[0] == "gameExists, ONEPLAYER":
return content[0], None
elif content[0] == "gameExists, TWOPLAYERS":
jsonS = json.dumps(("sendGameData", gameID))
ws.send(jsonS)
message = ws.recv()
gameData = json.loads(message)
return content[0], gameData
elif content[0] == "gameExists, PLAYERADDED":
print("jogo encontrado")
return content[0], None
elif content[0] == "gameDoesNotExist":
print("jogo não encontrado")
return "Error", None
else:
print("erro")
return "Error", None
def listGames():
with connect("ws://"+ip+":"+port) as ws:
jsonS = json.dumps(("listGames",))
ws.send(jsonS)
message = ws.recv()
content = json.loads(message)
if content[0] == "openGames":
return content[0], content[1]
elif content[0] == "noOpenGames":
return content[0], None
else:
return "Error", None
def sendPlay(player, play, gameData):
with connect("ws://"+ip+":"+port) as ws:
jsonS = json.dumps(("sendPlay", gameData["onlineGameID"], player, play, gameData))
ws.send(jsonS)
jsonS2 = json.dumps(("sendGameData", gameData["onlineGameID"]))
ws.send(jsonS2)
message = ws.recv()
updatedData = json.loads(message)
if updatedData["history"][-1][-1] == gameData["history"][-1][-1]:
return "ok"
else:
return("jogada não aceite " + str(gameData) + str(updatedData))
def getPlay(gameData):
gameID = gameData["onlineGameID"]
with connect("ws://"+ip+":"+port) as ws:
jsonS = json.dumps(("sendGameData", gameID))
ws.send(jsonS)
message = ws.recv()
receivedGameData = json.loads(message)
if len(receivedGameData["history"]) > len(gameData["history"]):
gameData = s.play(gameData, receivedGameData["history"][-1][1])
if gameData["board"] == receivedGameData["board"]:
return receivedGameData
else:
return False