Improve client matchmaking flow with ticket handling, auto-join, and mode distribution

### PlayerWebSocketHandler updates
- Track `ticket` and `match_id` per player instance
- Handle `matchmaker_ticket` messages and store ticket
- Handle `matchmaker_matched` and automatically join created match
- Enhance matchmaking debug output
- Update join_matchmaking() to include mode-based string_properties + query

### Matchmaking simulation improvements
- Evenly distribute players between "classic" and "blitz" modes
- Randomize assignment order to simulate real queue behavior
- Log player→mode mapping for visibility during tests

Example:
  player_0 -> classic
  player_3 -> blitz
  player_5 -> classic

Client test harness now accurately reflects multi-mode matchmaking behavior.
This commit is contained in:
2025-11-26 17:11:20 +05:30
parent eb35ccd180
commit 10058710fb
2 changed files with 33 additions and 8 deletions

View File

@@ -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):