fix(multiplayer): fail to fetch for multiplayer

This commit is contained in:
MingxuanGame
2025-08-08 12:07:48 +00:00
parent 5bf733a94e
commit dd7b8a14cd
3 changed files with 32 additions and 8 deletions

View File

@@ -19,6 +19,7 @@ from typing import (
from app.database.beatmap import Beatmap from app.database.beatmap import Beatmap
from app.dependencies.database import engine from app.dependencies.database import engine
from app.dependencies.fetcher import get_fetcher
from app.exception import InvokeException from app.exception import InvokeException
from .mods import APIMod from .mods import APIMod
@@ -518,8 +519,11 @@ class MultiplayerQueue:
raise InvokeException("Freestyle items cannot have allowed mods") raise InvokeException("Freestyle items cannot have allowed mods")
async with AsyncSession(engine) as session: async with AsyncSession(engine) as session:
fetcher = await get_fetcher()
async with session: async with session:
beatmap = await session.get(Beatmap, item.beatmap_id) beatmap = await Beatmap.get_or_fetch(
session, fetcher, bid=item.beatmap_id
)
if beatmap is None: if beatmap is None:
raise InvokeException("Beatmap not found") raise InvokeException("Beatmap not found")
if item.beatmap_checksum != beatmap.checksum: if item.beatmap_checksum != beatmap.checksum:
@@ -543,10 +547,11 @@ class MultiplayerQueue:
raise InvokeException("Freestyle items cannot have allowed mods") raise InvokeException("Freestyle items cannot have allowed mods")
async with AsyncSession(engine) as session: async with AsyncSession(engine) as session:
fetcher = await get_fetcher()
async with session: async with session:
beatmap = await session.get(Beatmap, item.beatmap_id) beatmap = await Beatmap.get_or_fetch(
if beatmap is None: session, fetcher, bid=item.beatmap_id
raise InvokeException("Beatmap not found") )
if item.beatmap_checksum != beatmap.checksum: if item.beatmap_checksum != beatmap.checksum:
raise InvokeException("Checksum mismatch") raise InvokeException("Checksum mismatch")

View File

@@ -77,6 +77,7 @@ async def batch_get_beatmaps(
b_ids: list[int] = Query(alias="ids[]", default_factory=list), b_ids: list[int] = Query(alias="ids[]", default_factory=list),
current_user: User = Depends(get_current_user), current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db), db: AsyncSession = Depends(get_db),
fetcher: Fetcher = Depends(get_fetcher),
): ):
if not b_ids: if not b_ids:
# select 50 beatmaps by last_updated # select 50 beatmaps by last_updated
@@ -86,9 +87,27 @@ async def batch_get_beatmaps(
) )
).all() ).all()
else: else:
beatmaps = ( beatmaps = list(
await db.exec(select(Beatmap).where(col(Beatmap.id).in_(b_ids)).limit(50)) (
).all() await db.exec(
select(Beatmap).where(col(Beatmap.id).in_(b_ids)).limit(50)
)
).all()
)
not_found_beatmaps = [
bid for bid in b_ids if bid not in [bm.id for bm in beatmaps]
]
beatmaps.extend(
beatmap
for beatmap in await asyncio.gather(
*[
Beatmap.get_or_fetch(db, fetcher, bid=bid)
for bid in not_found_beatmaps
],
return_exceptions=True,
)
if isinstance(beatmap, Beatmap)
)
return BatchGetResp( return BatchGetResp(
beatmaps=[ beatmaps=[

View File

@@ -211,7 +211,7 @@ class MultiplayerHub(Hub[MultiplayerClientState]):
raise InvokeException( raise InvokeException(
"Failed to fetch beatmap, please retry later" "Failed to fetch beatmap, please retry later"
) )
await Playlist.add_to_db(item, db_room.id, session) await Playlist.add_to_db(item, room.room_id, session)
server_room = ServerMultiplayerRoom( server_room = ServerMultiplayerRoom(
room=room, room=room,