Quick matchmaking API
This commit is contained in:
50
app.py
50
app.py
@@ -4,6 +4,7 @@ import random
|
|||||||
import re
|
import re
|
||||||
import string
|
import string
|
||||||
import uuid
|
import uuid
|
||||||
|
from collections import deque
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
@@ -240,6 +241,55 @@ async def new_room(request):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
quick_queue: deque[str]
|
||||||
|
quick_map: dict[str, Room]
|
||||||
|
lock = asyncio.Lock()
|
||||||
|
|
||||||
|
|
||||||
|
@app.POST("/quick")
|
||||||
|
async def quick_match(request: Request):
|
||||||
|
global quick_queue, quick_map
|
||||||
|
data = parse(request.body)
|
||||||
|
async with lock:
|
||||||
|
if (
|
||||||
|
data
|
||||||
|
and len(data) == 1
|
||||||
|
and "queue_id" in data
|
||||||
|
and data["queue_id"] in quick_queue
|
||||||
|
):
|
||||||
|
first = None
|
||||||
|
second = None
|
||||||
|
position: int = 0
|
||||||
|
# UPDATE LOGIC
|
||||||
|
while not second and position < len(quick_queue):
|
||||||
|
if quick_queue[position] not in quick_map:
|
||||||
|
if not first:
|
||||||
|
first = quick_queue[position]
|
||||||
|
else:
|
||||||
|
second = quick_queue[position]
|
||||||
|
position += 1
|
||||||
|
if first and second:
|
||||||
|
room = Room()
|
||||||
|
k = room_key()
|
||||||
|
rooms[k] = room
|
||||||
|
quick_map[first] = room
|
||||||
|
quick_map[second] = room
|
||||||
|
|
||||||
|
qid = data["queue_id"]
|
||||||
|
if qid in quick_map:
|
||||||
|
return JSONResponse(request, {"room_id": quick_map[qid]})
|
||||||
|
else:
|
||||||
|
return JSONResponse(
|
||||||
|
request, {}
|
||||||
|
) # Client handles empty as continue to wait
|
||||||
|
|
||||||
|
else:
|
||||||
|
qid = str(uuid.uuid4())
|
||||||
|
quick_queue.append(qid)
|
||||||
|
return JSONResponse(request, {"queue_id": qid})
|
||||||
|
|
||||||
|
|
||||||
@app.GET("/")
|
@app.GET("/")
|
||||||
async def home(request):
|
async def home(request):
|
||||||
return render(request, "home.html")
|
return render(request, "home.html")
|
||||||
|
|||||||
Reference in New Issue
Block a user