### 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.
56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
import asyncio
|
|
import random
|
|
from game_flow import PlayerWebSocketHandler, TEST_SCENARIOS
|
|
|
|
|
|
async def simulate_matchmaking(num_players: int = 6):
|
|
print(f"\n🎮 Spawning {num_players} players...\n")
|
|
|
|
# 1) Login + WebSocket connect
|
|
players = await asyncio.gather(*[
|
|
PlayerWebSocketHandler.setup_player(f"player_{i}")
|
|
for i in range(num_players)
|
|
])
|
|
|
|
print("\n✅ All players authenticated + connected\n")
|
|
|
|
# 2) Start listeners BEFORE matchmaking
|
|
for p in players:
|
|
p.start_listener()
|
|
|
|
print("\n👂 WebSocket listeners active\n")
|
|
|
|
await asyncio.sleep(0.3)
|
|
|
|
# ✅ 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, mode in zip(players, assignments)
|
|
])
|
|
|
|
print("\n✅ All players queued — waiting for matches...\n")
|
|
|
|
# 4) Allow enough time for Nakama matchmaker to group players
|
|
await asyncio.sleep(12)
|
|
|
|
# 5) Cleanup
|
|
print("\n🧹 Closing player connections...\n")
|
|
await asyncio.gather(*[p.close() for p in players])
|
|
|
|
print("\n🏁 Matchmaking simulation complete\n")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(simulate_matchmaking(6))
|