fixes for game flow and MOVE logs
This commit is contained in:
52
game_flow.py
52
game_flow.py
@@ -10,6 +10,14 @@ WS = "ws://127.0.0.1:7350"
|
|||||||
SERVER_KEY = "defaultkey"
|
SERVER_KEY = "defaultkey"
|
||||||
|
|
||||||
|
|
||||||
|
def print_board(board):
|
||||||
|
# board = [["X","",""], ["","O",""], ["","",""]]
|
||||||
|
symbols = lambda c: c if c else " "
|
||||||
|
rows = [" | ".join(symbols(c) for c in row) for row in board]
|
||||||
|
separator = "\n---------\n"
|
||||||
|
print("\n" + separator.join(rows) + "\n")
|
||||||
|
|
||||||
|
|
||||||
# ---------- Auth ----------
|
# ---------- Auth ----------
|
||||||
|
|
||||||
def login(custom_id: str) -> str:
|
def login(custom_id: str) -> str:
|
||||||
@@ -22,7 +30,6 @@ def login(custom_id: str) -> str:
|
|||||||
)
|
)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
body = r.json()
|
body = r.json()
|
||||||
# print("LOGIN:", body)
|
|
||||||
return body["token"]
|
return body["token"]
|
||||||
|
|
||||||
|
|
||||||
@@ -40,11 +47,39 @@ async def listener(ws, label: str):
|
|||||||
while True:
|
while True:
|
||||||
raw = await ws.recv()
|
raw = await ws.recv()
|
||||||
data = json.loads(raw)
|
data = json.loads(raw)
|
||||||
print(f"[{label}] {data}")
|
if "match_data" not in data:
|
||||||
|
print(f"[{label}] {data}")
|
||||||
|
continue
|
||||||
|
md = data["match_data"]
|
||||||
|
parse_match_data(md, label)
|
||||||
except websockets.exceptions.ConnectionClosedOK:
|
except websockets.exceptions.ConnectionClosedOK:
|
||||||
print(f"[{label}] WebSocket closed cleanly")
|
print(f"[{label}] WebSocket closed cleanly")
|
||||||
|
return
|
||||||
except websockets.exceptions.ConnectionClosedError as e:
|
except websockets.exceptions.ConnectionClosedError as e:
|
||||||
print(f"[{label}] WebSocket closed with error: {e}")
|
print(f"[{label}] WebSocket closed with error: {e}")
|
||||||
|
return
|
||||||
|
|
||||||
|
def parse_match_data(md, label: str):
|
||||||
|
op = int(md.get("op_code", 0))
|
||||||
|
|
||||||
|
# decode base64 payload
|
||||||
|
payload_json = base64.b64decode(md["data"]).decode()
|
||||||
|
payload = json.loads(payload_json)
|
||||||
|
|
||||||
|
# OpMove → just log move
|
||||||
|
if op == 1:
|
||||||
|
print(f"[{label}] MOVE -> {payload}")
|
||||||
|
return
|
||||||
|
|
||||||
|
# OpState → pretty-print board
|
||||||
|
if op == 2:
|
||||||
|
print(f"\n[{label}] GAME STATE")
|
||||||
|
print_board(payload["board"])
|
||||||
|
print(f"Turn={payload['turn']} Winner={payload['winner']}\n")
|
||||||
|
return
|
||||||
|
|
||||||
|
# other opcodes
|
||||||
|
raise RuntimeError(f"[{label}] UNKNOWN OPCODE {op}: {payload}")
|
||||||
|
|
||||||
|
|
||||||
async def send_move(ws, match_id: str, row: int, col: int):
|
async def send_move(ws, match_id: str, row: int, col: int):
|
||||||
@@ -75,13 +110,12 @@ async def main():
|
|||||||
ws1 = await connect(token1)
|
ws1 = await connect(token1)
|
||||||
ws2 = await connect(token2)
|
ws2 = await connect(token2)
|
||||||
|
|
||||||
# 3) Create match from P1
|
# 3) Create a match from P1
|
||||||
await ws1.send(json.dumps({"match_create": {}}))
|
await ws1.send(json.dumps({"match_create": {}}))
|
||||||
raw = await ws1.recv()
|
raw = await ws1.recv()
|
||||||
print("RAW:", raw)
|
|
||||||
msg = json.loads(raw)
|
msg = json.loads(raw)
|
||||||
match_id = msg["match"]["match_id"]
|
match_id = msg["match"]["match_id"]
|
||||||
print("Match:", match_id) # keep the dot; it's part of the ID
|
print("Match:", match_id)
|
||||||
|
|
||||||
# 4) Only P2 explicitly joins (creator is auto-joined)
|
# 4) Only P2 explicitly joins (creator is auto-joined)
|
||||||
await ws2.send(json.dumps({"match_join": {"match_id": match_id}}))
|
await ws2.send(json.dumps({"match_join": {"match_id": match_id}}))
|
||||||
@@ -96,19 +130,19 @@ async def main():
|
|||||||
# 6) Play a quick winning game for P1 (X)
|
# 6) Play a quick winning game for P1 (X)
|
||||||
# P1: (0,0)
|
# P1: (0,0)
|
||||||
await send_move(ws1, match_id, 0, 0)
|
await send_move(ws1, match_id, 0, 0)
|
||||||
await asyncio.sleep(0.3)
|
await asyncio.sleep(1.5)
|
||||||
|
|
||||||
# P2: (1,1)
|
# P2: (1,1)
|
||||||
await send_move(ws2, match_id, 1, 1)
|
await send_move(ws2, match_id, 1, 1)
|
||||||
await asyncio.sleep(0.3)
|
await asyncio.sleep(1.5)
|
||||||
|
|
||||||
# P1: (0,1)
|
# P1: (0,1)
|
||||||
await send_move(ws1, match_id, 0, 1)
|
await send_move(ws1, match_id, 0, 1)
|
||||||
await asyncio.sleep(0.3)
|
await asyncio.sleep(1.5)
|
||||||
|
|
||||||
# P2: (2,2)
|
# P2: (2,2)
|
||||||
await send_move(ws2, match_id, 2, 2)
|
await send_move(ws2, match_id, 2, 2)
|
||||||
await asyncio.sleep(0.3)
|
await asyncio.sleep(1.5)
|
||||||
|
|
||||||
# P1: (0,2) -> X wins by top row
|
# P1: (0,2) -> X wins by top row
|
||||||
await send_move(ws1, match_id, 0, 2)
|
await send_move(ws1, match_id, 0, 2)
|
||||||
|
|||||||
Reference in New Issue
Block a user