diff --git a/game_flow.py b/game_flow.py index 3c9efb3..af5ee8f 100644 --- a/game_flow.py +++ b/game_flow.py @@ -96,7 +96,23 @@ class PlayerWebSocketHandler(WebSocketHandler): await handler.connect() return handler + def __init__(self, custom_id: str, label: str): + super().__init__(custom_id, label) + self.ticket = None + self.match_id = None + async def on_message(self, msg): + if "matchmaker_matched" in msg: + match_id = msg["matchmaker_matched"]["match_id"] + print(f"[{self.label}] āœ… Match found: {match_id}") + await self.join_match(match_id) + return + + if "matchmaker_ticket" in msg: + self.ticket = msg["matchmaker_ticket"]["ticket"] + print(f"[{self.label}] āœ… Received ticket: {self.ticket}") + return + if "match_data" not in msg: print(f"[{self.label}] {msg}") return @@ -124,9 +140,8 @@ class PlayerWebSocketHandler(WebSocketHandler): "matchmaker_add": { "min_count": 2, "max_count": 2, - "string_properties": { - "mode": mode - } + "string_properties": {"mode": mode}, + "query": f"+mode:{mode}" } })) print(f"[{self.label}] Searching match for mode={mode}...") @@ -151,6 +166,7 @@ class PlayerWebSocketHandler(WebSocketHandler): async def join_match(self, match_id: str): await self.ws.send(json.dumps({"match_join": {"match_id": match_id}})) print(f"[{self.label}] Joined match: {match_id}") + self.match_id = match_id # ---------- Gameplay ---------- async def send_move(self, match_id: str, row: int, col: int): diff --git a/match_making_flow.py b/match_making_flow.py index 545e478..4387939 100644 --- a/match_making_flow.py +++ b/match_making_flow.py @@ -1,8 +1,9 @@ import asyncio -from game_flow import PlayerWebSocketHandler +import random +from game_flow import PlayerWebSocketHandler, TEST_SCENARIOS -async def simulate_matchmaking(num_players: int = 6, mode: str = "classic"): +async def simulate_matchmaking(num_players: int = 6): print(f"\nšŸŽ® Spawning {num_players} players...\n") # 1) Login + WebSocket connect @@ -21,12 +22,20 @@ async def simulate_matchmaking(num_players: int = 6, mode: str = "classic"): await asyncio.sleep(0.3) - # 3) Queue all players in matchmaking - print(f"\nšŸŽÆ Queuing players for mode={mode}...\n") + # āœ… 3) Split evenly between classic & blitz + half = num_players // 2 + assignments = ["classic"] * half + ["blitz"] * (num_players - half) + + # Optional — shuffle for realism + random.shuffle(assignments) + + print("\nšŸŽÆ Queuing players:") + for p, mode in zip(players, assignments): + print(f" - {p.label} -> {mode}") await asyncio.gather(*[ p.join_matchmaking(mode) - for p in players + for p, mode in zip(players, assignments) ]) print("\nāœ… All players queued — waiting for matches...\n")