-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatchmaking.py
More file actions
279 lines (235 loc) · 10.2 KB
/
matchmaking.py
File metadata and controls
279 lines (235 loc) · 10.2 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""ELO-based matchmaking queue with periodic matching.
Players join a per-category queue (keyboard / voice) with their ELO rating.
A background task periodically scans for pairs within an ELO threshold.
The threshold widens over time so no player waits indefinitely.
When matched, a room is auto-created with both controllers set and
status transitioned to ``fighting``. Clients poll for match results.
"""
from __future__ import annotations
import asyncio
import time
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from elo import EloManager
from room_manager import RoomManager
# ─────────────────────────────────────────────
# Constants
# ─────────────────────────────────────────────
MATCH_INTERVAL = 3.0 # seconds between match attempts
STALE_THRESHOLD = 60.0 # seconds without refresh → prune
MATCH_EXPIRY = 60.0 # seconds before unclaimed match result expires
DEFAULT_ELO_THRESHOLD = 100
THRESHOLD_WIDEN_AMOUNT = 50
THRESHOLD_WIDEN_INTERVAL = 10 # seconds
class MatchmakingTask:
"""Background matchmaking engine — matches players by ELO within a category."""
def __init__(self, room_manager: RoomManager, elo_manager: EloManager) -> None:
self._room_manager = room_manager
self._elo_manager = elo_manager
self._task: asyncio.Task[None] | None = None
self._stopped = False
# In-memory state
self._entries: dict[str, dict[str, Any]] = {} # player_id → entry
self._matches: dict[str, dict[str, Any]] = {} # player_id → match result
# ── Queue operations ──────────────────────────
async def join(
self,
player_id: str,
category: str,
controller: str,
elo: float,
user_id: str = "",
name: str = "",
) -> None:
"""Add a player to the matchmaking queue."""
now = time.monotonic()
self._entries[player_id] = {
"category": category,
"controller": controller,
"elo": elo,
"user_id": user_id,
"name": name,
"joined_at": now,
"refreshed_at": now,
}
await self._room_manager.matchmaking_join(category, player_id, elo)
async def cancel(self, player_id: str) -> bool:
"""Remove a player from the queue + any pending match. Returns True if was queued."""
entry = self._entries.pop(player_id, None)
self._matches.pop(player_id, None)
if entry:
await self._room_manager.matchmaking_leave(entry["category"], player_id)
return True
return False
def refresh(self, player_id: str) -> None:
"""Mark a player as still active (called on status poll)."""
entry = self._entries.get(player_id)
if entry:
entry["refreshed_at"] = time.monotonic()
def get_status(self, player_id: str) -> dict[str, Any]:
"""Return the player's current matchmaking status."""
match = self._matches.get(player_id)
if match:
return {
"status": "matched",
"roomCode": match["roomCode"],
"playerNum": match["playerNum"],
"playerId": match["playerId"],
"opponentName": match["opponentName"],
}
entry = self._entries.get(player_id)
if not entry:
return {"status": "not_queued"}
now = time.monotonic()
wait_time = now - entry["joined_at"]
queue_count = sum(
1 for e in self._entries.values()
if e["category"] == entry["category"]
)
threshold = self._threshold(wait_time)
return {
"status": "searching",
"waitTime": round(wait_time),
"queueSize": queue_count,
"threshold": threshold,
"category": entry["category"],
}
# ── Matching algorithm ────────────────────────
def _threshold(self, wait_time: float) -> int:
"""ELO threshold: starts at 100, widens by 50 every 10 seconds."""
return DEFAULT_ELO_THRESHOLD + int(wait_time / THRESHOLD_WIDEN_INTERVAL) * THRESHOLD_WIDEN_AMOUNT
async def try_match(self) -> list[tuple[str, str]]:
"""Scan the queue and match closest-ELO pairs. Returns matched (pid1, pid2) pairs."""
matched_pairs: list[tuple[str, str]] = []
# Group by category
by_cat: dict[str, list[tuple[str, dict[str, Any]]]] = {}
for pid, entry in list(self._entries.items()):
cat = entry["category"]
by_cat.setdefault(cat, []).append((pid, entry))
for _category, players in by_cat.items():
if len(players) < 2:
continue
# Sort by ELO for efficient closest-pair matching
players.sort(key=lambda x: x[1]["elo"])
matched_pids: set[str] = set()
for i in range(len(players)):
pid1, entry1 = players[i]
if pid1 in matched_pids:
continue
now = time.monotonic()
wait1 = now - entry1["joined_at"]
thresh1 = self._threshold(wait1)
best: tuple[str, dict[str, Any]] | None = None
best_diff = float("inf")
for j in range(i + 1, len(players)):
pid2, entry2 = players[j]
if pid2 in matched_pids:
continue
diff = abs(entry1["elo"] - entry2["elo"])
wait2 = now - entry2["joined_at"]
thresh2 = self._threshold(wait2)
# Use the wider threshold (more generous for longer-waiting player)
threshold = max(thresh1, thresh2)
if diff <= threshold and diff < best_diff:
best = (pid2, entry2)
best_diff = diff
if best is not None:
pid2, entry2 = best
matched_pids.add(pid1)
matched_pids.add(pid2)
await self._create_match(pid1, entry1, pid2, entry2)
matched_pairs.append((pid1, pid2))
# Housekeeping
self._prune_stale()
self._prune_expired_matches()
return matched_pairs
async def _create_match(
self,
pid1: str,
entry1: dict[str, Any],
pid2: str,
entry2: dict[str, Any],
) -> str:
"""Create a room for a matched pair. Returns the room code."""
room = await self._room_manager.create_room(pid1)
code = room["code"]
await self._room_manager.join_room(code, pid2)
await self._room_manager.transition_status(code, "selecting")
await self._room_manager.set_controller(code, 1, entry1["controller"])
await self._room_manager.set_controller(code, 2, entry2["controller"])
await self._room_manager.transition_status(code, "fighting")
now = time.monotonic()
self._matches[pid1] = {
"roomCode": code,
"playerNum": 1,
"playerId": pid1,
"opponentName": entry2.get("name") or "Opponent",
"matched_at": now,
}
self._matches[pid2] = {
"roomCode": code,
"playerNum": 2,
"playerId": pid2,
"opponentName": entry1.get("name") or "Opponent",
"matched_at": now,
}
# Remove from queue
self._entries.pop(pid1, None)
self._entries.pop(pid2, None)
await self._room_manager.matchmaking_leave(entry1["category"], pid1)
await self._room_manager.matchmaking_leave(entry2["category"], pid2)
print(f"[matchmaking] Matched {pid1} vs {pid2} → room {code}")
return code
# ── Housekeeping ──────────────────────────────
def _prune_stale(self) -> list[str]:
"""Remove entries that haven't been refreshed within STALE_THRESHOLD."""
now = time.monotonic()
stale: list[str] = []
for pid in list(self._entries.keys()):
if now - self._entries[pid]["refreshed_at"] > STALE_THRESHOLD:
stale.append(pid)
self._entries.pop(pid)
return stale
def _prune_expired_matches(self) -> list[str]:
"""Remove match results that weren't picked up within MATCH_EXPIRY."""
now = time.monotonic()
expired: list[str] = []
for pid in list(self._matches.keys()):
if now - self._matches[pid]["matched_at"] > MATCH_EXPIRY:
expired.append(pid)
self._matches.pop(pid)
return expired
# ── Lifecycle ─────────────────────────────────
def start(self) -> None:
"""Start the periodic matching task."""
if self._task is not None:
return
self._stopped = False
self._task = asyncio.create_task(self._run())
print(f"[matchmaking] Started (interval={MATCH_INTERVAL}s)")
async def stop(self) -> None:
"""Stop the matching task."""
self._stopped = True
if self._task is not None:
self._task.cancel()
try:
await self._task
except asyncio.CancelledError:
pass
self._task = None
print("[matchmaking] Stopped")
async def _run(self) -> None:
"""Background loop — tries to match players at regular intervals."""
try:
while not self._stopped:
await asyncio.sleep(MATCH_INTERVAL)
if self._stopped:
break
try:
pairs = await self.try_match()
if pairs:
print(f"[matchmaking] Matched {len(pairs)} pair(s)")
except Exception as e:
print(f"[matchmaking] Error: {type(e).__name__}: {e}")
except asyncio.CancelledError:
pass