mirror of
https://github.com/rommapp/romm.git
synced 2026-12-04 21:02:21 +01:00
The scheduled Trunk Check (all files) run fails on 79 prose-check issues that predate the linter: clipped 'X, else Y' chains, 'internal id' in API descriptions, and clipped 'Left out, ...' conditionals. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
2.5 KiB
Python
98 lines
2.5 KiB
Python
from typing import Annotated, Dict, TypedDict
|
|
|
|
from fastapi import Query, Request, status
|
|
|
|
from decorators.auth import protected_route
|
|
from handler.auth.constants import Scope
|
|
from handler.auth.dependencies import (
|
|
can_access,
|
|
get_permissions,
|
|
visible_rom_or_404,
|
|
)
|
|
from handler.database import db_rom_handler
|
|
from handler.netplay_handler import NetplayRoom, netplay_handler
|
|
from models.permission import PermAction, PermEntity
|
|
from utils.router import APIRouter
|
|
|
|
router = APIRouter(
|
|
prefix="/netplay",
|
|
tags=["netplay"],
|
|
)
|
|
|
|
|
|
DEFAULT_MAX_PLAYERS = 4
|
|
|
|
|
|
def _get_owner_player_name(room: NetplayRoom) -> str:
|
|
owner_sid = room.get("owner")
|
|
if not owner_sid:
|
|
return "Unknown"
|
|
|
|
return next(
|
|
(
|
|
p["player_name"]
|
|
for p in room["players"].values()
|
|
if p["socketId"] == owner_sid
|
|
),
|
|
"Unknown",
|
|
)
|
|
|
|
|
|
def _is_room_open(room: NetplayRoom, game_id: int) -> bool:
|
|
if len(room["players"]) >= room["max_players"]:
|
|
return False
|
|
return str(room["game_id"]) == str(game_id)
|
|
|
|
|
|
class RoomsResponse(TypedDict):
|
|
room_name: str
|
|
current: int
|
|
max: int
|
|
player_name: str
|
|
hasPassword: bool
|
|
|
|
|
|
@protected_route(
|
|
router.get,
|
|
"/list",
|
|
[Scope.ROMS_READ],
|
|
responses={
|
|
status.HTTP_404_NOT_FOUND: {
|
|
"description": "No rom the caller can see has this ID."
|
|
}
|
|
},
|
|
)
|
|
async def get_rooms(
|
|
request: Request,
|
|
game_id: Annotated[
|
|
int,
|
|
Query(
|
|
description=(
|
|
"The ID of the rom. A rom that is unknown or hidden returns 404."
|
|
)
|
|
),
|
|
],
|
|
) -> Dict[str, RoomsResponse]:
|
|
"""List the open netplay rooms for a game. The list is empty when the caller cannot use emulation."""
|
|
visible_rom_or_404(request, db_rom_handler.get_rom_visibility(game_id), game_id)
|
|
|
|
# Empty rather than 403, like the streaming joinable list.
|
|
if not can_access(get_permissions(request), PermEntity.EMULATION, PermAction.READ):
|
|
return {}
|
|
|
|
netplay_rooms = await netplay_handler.get_all()
|
|
|
|
open_rooms: Dict[str, RoomsResponse] = {
|
|
session_id: RoomsResponse(
|
|
room_name=room["room_name"],
|
|
current=len(room["players"]),
|
|
max=room["max_players"],
|
|
player_name=_get_owner_player_name(room),
|
|
hasPassword=bool(room["password"]),
|
|
)
|
|
for session_id, room in netplay_rooms.items()
|
|
if _is_room_open(room, game_id)
|
|
}
|
|
|
|
return open_rooms
|