Spaces:
Paused
Paused
feat: add matchmaking service - 4 durrs per room every 30s in test mode
Browse files- app.py +3 -0
- services/matchmaking.py +52 -0
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from models import *
|
|
| 7 |
from routes import lobby, telemetry, websocket
|
| 8 |
from games import registry, assigner
|
| 9 |
from games.loader import GameLoader
|
|
|
|
| 10 |
|
| 11 |
# Configure heavy logging - output to stdout so HF captures it
|
| 12 |
logging.basicConfig(
|
|
@@ -41,6 +42,8 @@ with app.app_context():
|
|
| 41 |
# Start test mode if enabled
|
| 42 |
if Config.TEST_MODE:
|
| 43 |
logger.warning("TEST_MODE ENABLED - auto-assign and DURRRR bots active")
|
|
|
|
|
|
|
| 44 |
# Test mode setup happens per-request in lobby.index
|
| 45 |
|
| 46 |
@app.route("/inference")
|
|
|
|
| 7 |
from routes import lobby, telemetry, websocket
|
| 8 |
from games import registry, assigner
|
| 9 |
from games.loader import GameLoader
|
| 10 |
+
from services.matchmaking import MatchmakingService
|
| 11 |
|
| 12 |
# Configure heavy logging - output to stdout so HF captures it
|
| 13 |
logging.basicConfig(
|
|
|
|
| 42 |
# Start test mode if enabled
|
| 43 |
if Config.TEST_MODE:
|
| 44 |
logger.warning("TEST_MODE ENABLED - auto-assign and DURRRR bots active")
|
| 45 |
+
MatchmakingService.start(app, interval=30, bots_per_room=4)
|
| 46 |
+
logger.info("MATCHMAKING_STARTED interval=30s bots_per_room=4")
|
| 47 |
# Test mode setup happens per-request in lobby.index
|
| 48 |
|
| 49 |
@app.route("/inference")
|
services/matchmaking.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
import time
|
| 3 |
+
import logging
|
| 4 |
+
from extensions import db
|
| 5 |
+
from models import User, Room, RoomMembership, ChatMessage
|
| 6 |
+
from services.rooms import RoomService
|
| 7 |
+
from services.test_players import TestMode
|
| 8 |
+
from config import Config
|
| 9 |
+
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class MatchmakingService:
|
| 14 |
+
_thread = None
|
| 15 |
+
_stop = threading.Event()
|
| 16 |
+
|
| 17 |
+
@staticmethod
|
| 18 |
+
def start(app, interval=30, bots_per_room=4):
|
| 19 |
+
"""Background loop: every `interval` seconds, create a room with `bots_per_room` test bots."""
|
| 20 |
+
if MatchmakingService._thread and MatchmakingService._thread.is_alive():
|
| 21 |
+
return
|
| 22 |
+
MatchmakingService._stop.clear()
|
| 23 |
+
|
| 24 |
+
def _loop():
|
| 25 |
+
logger.info("MATCHMAKING_LOOP_START interval=%s bots_per_room=%s", interval, bots_per_room)
|
| 26 |
+
while not MatchmakingService._stop.is_set():
|
| 27 |
+
time.sleep(interval)
|
| 28 |
+
if MatchmakingService._stop.is_set():
|
| 29 |
+
break
|
| 30 |
+
try:
|
| 31 |
+
with app.app_context():
|
| 32 |
+
bots = TestMode.create_test_players(bots_per_room)
|
| 33 |
+
room = RoomService.create_room(bots[0].id, name=f"Match Room {int(time.time())}")
|
| 34 |
+
for u in bots[1:]:
|
| 35 |
+
RoomService.join_room(room.id, u.id)
|
| 36 |
+
TestMode.start_chat_loop(app, room.id)
|
| 37 |
+
logger.warning("MATCHMAKING_ASSIGNED room=%s bots=%s", room.id, len(bots))
|
| 38 |
+
except Exception as e:
|
| 39 |
+
logger.exception("MATCHMAKING_ERROR: %s", e)
|
| 40 |
+
try:
|
| 41 |
+
db.session.rollback()
|
| 42 |
+
except Exception:
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
t = threading.Thread(target=_loop, daemon=True, name="matchmaking-loop")
|
| 46 |
+
t.start()
|
| 47 |
+
MatchmakingService._thread = t
|
| 48 |
+
logger.info("MATCHMAKING_THREAD_STARTED")
|
| 49 |
+
|
| 50 |
+
@staticmethod
|
| 51 |
+
def stop():
|
| 52 |
+
MatchmakingService._stop.set()
|