feat(multiplayer,playlist): show host & renect participants
This commit is contained in:
@@ -26,6 +26,7 @@ from .playlists import Playlist, PlaylistResp
|
|||||||
from .pp_best_score import PPBestScore
|
from .pp_best_score import PPBestScore
|
||||||
from .relationship import Relationship, RelationshipResp, RelationshipType
|
from .relationship import Relationship, RelationshipResp, RelationshipType
|
||||||
from .room import Room, RoomResp
|
from .room import Room, RoomResp
|
||||||
|
from .room_participated_user import RoomParticipatedUser
|
||||||
from .score import (
|
from .score import (
|
||||||
MultiplayerScores,
|
MultiplayerScores,
|
||||||
Score,
|
Score,
|
||||||
@@ -69,6 +70,7 @@ __all__ = [
|
|||||||
"RelationshipResp",
|
"RelationshipResp",
|
||||||
"RelationshipType",
|
"RelationshipType",
|
||||||
"Room",
|
"Room",
|
||||||
|
"RoomParticipatedUser",
|
||||||
"RoomResp",
|
"RoomResp",
|
||||||
"Score",
|
"Score",
|
||||||
"ScoreAround",
|
"ScoreAround",
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
|
|
||||||
from app.database.playlist_attempts import PlaylistAggregateScore
|
from app.database.playlist_attempts import PlaylistAggregateScore
|
||||||
|
from app.database.room_participated_user import RoomParticipatedUser
|
||||||
from app.models.model import UTCBaseModel
|
from app.models.model import UTCBaseModel
|
||||||
from app.models.multiplayer_hub import ServerMultiplayerRoom
|
from app.models.multiplayer_hub import ServerMultiplayerRoom
|
||||||
from app.models.room import (
|
from app.models.room import (
|
||||||
@@ -15,6 +16,7 @@ from app.models.room import (
|
|||||||
from .lazer_user import User, UserResp
|
from .lazer_user import User, UserResp
|
||||||
from .playlists import Playlist, PlaylistResp
|
from .playlists import Playlist, PlaylistResp
|
||||||
|
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncAttrs
|
||||||
from sqlmodel import (
|
from sqlmodel import (
|
||||||
BigInteger,
|
BigInteger,
|
||||||
Column,
|
Column,
|
||||||
@@ -23,6 +25,8 @@ from sqlmodel import (
|
|||||||
ForeignKey,
|
ForeignKey,
|
||||||
Relationship,
|
Relationship,
|
||||||
SQLModel,
|
SQLModel,
|
||||||
|
col,
|
||||||
|
select,
|
||||||
)
|
)
|
||||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
@@ -51,10 +55,9 @@ class RoomBase(SQLModel, UTCBaseModel):
|
|||||||
auto_start_duration: int
|
auto_start_duration: int
|
||||||
status: RoomStatus
|
status: RoomStatus
|
||||||
# TODO: channel_id
|
# TODO: channel_id
|
||||||
# recent_participants: list[User]
|
|
||||||
|
|
||||||
|
|
||||||
class Room(RoomBase, table=True):
|
class Room(AsyncAttrs, RoomBase, table=True):
|
||||||
__tablename__ = "rooms" # pyright: ignore[reportAssignmentType]
|
__tablename__ = "rooms" # pyright: ignore[reportAssignmentType]
|
||||||
id: int = Field(default=None, primary_key=True, index=True)
|
id: int = Field(default=None, primary_key=True, index=True)
|
||||||
host_id: int = Field(
|
host_id: int = Field(
|
||||||
@@ -80,13 +83,14 @@ class RoomResp(RoomBase):
|
|||||||
difficulty_range: RoomDifficultyRange | None = None
|
difficulty_range: RoomDifficultyRange | None = None
|
||||||
current_playlist_item: PlaylistResp | None = None
|
current_playlist_item: PlaylistResp | None = None
|
||||||
current_user_score: PlaylistAggregateScore | None = None
|
current_user_score: PlaylistAggregateScore | None = None
|
||||||
|
recent_participants: list[UserResp] = Field(default_factory=list)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def from_db(
|
async def from_db(
|
||||||
cls,
|
cls,
|
||||||
room: Room,
|
room: Room,
|
||||||
|
session: AsyncSession,
|
||||||
include: list[str] = [],
|
include: list[str] = [],
|
||||||
session: AsyncSession | None = None,
|
|
||||||
user: User | None = None,
|
user: User | None = None,
|
||||||
) -> "RoomResp":
|
) -> "RoomResp":
|
||||||
resp = cls.model_validate(room.model_dump())
|
resp = cls.model_validate(room.model_dump())
|
||||||
@@ -113,8 +117,27 @@ class RoomResp(RoomBase):
|
|||||||
resp.playlist_item_stats = stats
|
resp.playlist_item_stats = stats
|
||||||
resp.difficulty_range = difficulty_range
|
resp.difficulty_range = difficulty_range
|
||||||
resp.current_playlist_item = resp.playlist[-1] if resp.playlist else None
|
resp.current_playlist_item = resp.playlist[-1] if resp.playlist else None
|
||||||
|
resp.recent_participants = []
|
||||||
if "current_user_score" in include and user and session:
|
for recent_participant in await session.exec(
|
||||||
|
select(RoomParticipatedUser)
|
||||||
|
.where(
|
||||||
|
RoomParticipatedUser.room_id == room.id,
|
||||||
|
col(RoomParticipatedUser.left_at).is_(None),
|
||||||
|
)
|
||||||
|
.limit(8)
|
||||||
|
.order_by(col(RoomParticipatedUser.joined_at).desc())
|
||||||
|
):
|
||||||
|
resp.recent_participants.append(
|
||||||
|
await UserResp.from_db(
|
||||||
|
await recent_participant.awaitable_attrs.user,
|
||||||
|
session,
|
||||||
|
include=["statistics"],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
resp.host = await UserResp.from_db(
|
||||||
|
await room.awaitable_attrs.host, session, include=["statistics"]
|
||||||
|
)
|
||||||
|
if "current_user_score" in include and user:
|
||||||
resp.current_user_score = await PlaylistAggregateScore.from_db(
|
resp.current_user_score = await PlaylistAggregateScore.from_db(
|
||||||
room.id, user.id, session
|
room.id, user.id, session
|
||||||
)
|
)
|
||||||
|
|||||||
39
app/database/room_participated_user.py
Normal file
39
app/database/room_participated_user.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
from datetime import UTC, datetime
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncAttrs
|
||||||
|
from sqlmodel import (
|
||||||
|
BigInteger,
|
||||||
|
Column,
|
||||||
|
DateTime,
|
||||||
|
Field,
|
||||||
|
ForeignKey,
|
||||||
|
Relationship,
|
||||||
|
SQLModel,
|
||||||
|
)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .lazer_user import User
|
||||||
|
from .room import Room
|
||||||
|
|
||||||
|
|
||||||
|
class RoomParticipatedUser(AsyncAttrs, SQLModel, table=True):
|
||||||
|
__tablename__ = "room_participated_users" # pyright: ignore[reportAssignmentType]
|
||||||
|
|
||||||
|
id: int | None = Field(
|
||||||
|
default=None, sa_column=Column(BigInteger, primary_key=True, autoincrement=True)
|
||||||
|
)
|
||||||
|
room_id: int = Field(sa_column=Column(ForeignKey("rooms.id"), nullable=False))
|
||||||
|
user_id: int = Field(
|
||||||
|
sa_column=Column(BigInteger, ForeignKey("lazer_users.id"), nullable=False)
|
||||||
|
)
|
||||||
|
joined_at: datetime = Field(
|
||||||
|
sa_column=Column(DateTime(timezone=True), nullable=False),
|
||||||
|
default=datetime.now(UTC),
|
||||||
|
)
|
||||||
|
left_at: datetime | None = Field(
|
||||||
|
sa_column=Column(DateTime(timezone=True), nullable=True), default=None
|
||||||
|
)
|
||||||
|
|
||||||
|
room: "Room" = Relationship()
|
||||||
|
user: "User" = Relationship()
|
||||||
@@ -10,6 +10,7 @@ from app.database.multiplayer_event import MultiplayerEvent, MultiplayerEventRes
|
|||||||
from app.database.playlist_attempts import ItemAttemptsCount, ItemAttemptsResp
|
from app.database.playlist_attempts import ItemAttemptsCount, ItemAttemptsResp
|
||||||
from app.database.playlists import Playlist, PlaylistResp
|
from app.database.playlists import Playlist, PlaylistResp
|
||||||
from app.database.room import Room, RoomBase, RoomResp
|
from app.database.room import Room, RoomBase, RoomResp
|
||||||
|
from app.database.room_participated_user import RoomParticipatedUser
|
||||||
from app.database.score import Score
|
from app.database.score import Score
|
||||||
from app.dependencies.database import get_db, get_redis
|
from app.dependencies.database import get_db, get_redis
|
||||||
from app.dependencies.fetcher import get_fetcher
|
from app.dependencies.fetcher import get_fetcher
|
||||||
@@ -44,13 +45,13 @@ async def get_all_rooms(
|
|||||||
for room in db_rooms:
|
for room in db_rooms:
|
||||||
if category == "realtime":
|
if category == "realtime":
|
||||||
if room.id in MultiplayerHubs.rooms:
|
if room.id in MultiplayerHubs.rooms:
|
||||||
resp_list.append(await RoomResp.from_db(room))
|
resp_list.append(await RoomResp.from_db(room, db))
|
||||||
elif category is not None:
|
elif category is not None:
|
||||||
if category == room.category:
|
if category == room.category:
|
||||||
resp_list.append(await RoomResp.from_db(room))
|
resp_list.append(await RoomResp.from_db(room, db))
|
||||||
else:
|
else:
|
||||||
if room.id not in MultiplayerHubs.rooms:
|
if room.id not in MultiplayerHubs.rooms:
|
||||||
resp_list.append(await RoomResp.from_db(room))
|
resp_list.append(await RoomResp.from_db(room, db))
|
||||||
return resp_list
|
return resp_list
|
||||||
|
|
||||||
|
|
||||||
@@ -73,6 +74,30 @@ class APIUploadedRoom(RoomBase):
|
|||||||
playlist: list[Playlist]
|
playlist: list[Playlist]
|
||||||
|
|
||||||
|
|
||||||
|
async def _participate_room(
|
||||||
|
room_id: int, user_id: int, db_room: Room, session: AsyncSession
|
||||||
|
):
|
||||||
|
participated_user = (
|
||||||
|
await session.exec(
|
||||||
|
select(RoomParticipatedUser).where(
|
||||||
|
RoomParticipatedUser.room_id == room_id,
|
||||||
|
RoomParticipatedUser.user_id == user_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).first()
|
||||||
|
if participated_user is None:
|
||||||
|
participated_user = RoomParticipatedUser(
|
||||||
|
room_id=room_id,
|
||||||
|
user_id=user_id,
|
||||||
|
joined_at=datetime.now(UTC),
|
||||||
|
)
|
||||||
|
session.add(participated_user)
|
||||||
|
else:
|
||||||
|
participated_user.left_at = None
|
||||||
|
participated_user.joined_at = datetime.now(UTC)
|
||||||
|
db_room.participant_count += 1
|
||||||
|
|
||||||
|
|
||||||
@router.post("/rooms", tags=["room"], response_model=APICreatedRoom)
|
@router.post("/rooms", tags=["room"], response_model=APICreatedRoom)
|
||||||
async def create_room(
|
async def create_room(
|
||||||
room: APIUploadedRoom,
|
room: APIUploadedRoom,
|
||||||
@@ -91,6 +116,7 @@ async def create_room(
|
|||||||
db.add(db_room)
|
db.add(db_room)
|
||||||
await db.commit()
|
await db.commit()
|
||||||
await db.refresh(db_room)
|
await db.refresh(db_room)
|
||||||
|
await _participate_room(db_room.id, user_id, db_room, db)
|
||||||
|
|
||||||
playlist: list[Playlist] = []
|
playlist: list[Playlist] = []
|
||||||
# 处理 APIUploadedRoom 里的 playlist 字段
|
# 处理 APIUploadedRoom 里的 playlist 字段
|
||||||
@@ -106,7 +132,7 @@ async def create_room(
|
|||||||
await db.refresh(db_room)
|
await db.refresh(db_room)
|
||||||
db_room.playlist = playlist
|
db_room.playlist = playlist
|
||||||
await db.refresh(db_room)
|
await db.refresh(db_room)
|
||||||
created_room = APICreatedRoom.model_validate(await RoomResp.from_db(db_room))
|
created_room = APICreatedRoom.model_validate(await RoomResp.from_db(db_room, db))
|
||||||
created_room.error = ""
|
created_room.error = ""
|
||||||
return created_room
|
return created_room
|
||||||
|
|
||||||
@@ -143,10 +169,10 @@ async def delete_room(room: int, db: AsyncSession = Depends(get_db)):
|
|||||||
async def add_user_to_room(room: int, user: int, db: AsyncSession = Depends(get_db)):
|
async def add_user_to_room(room: int, user: int, db: AsyncSession = Depends(get_db)):
|
||||||
db_room = (await db.exec(select(Room).where(Room.id == room))).first()
|
db_room = (await db.exec(select(Room).where(Room.id == room))).first()
|
||||||
if db_room is not None:
|
if db_room is not None:
|
||||||
db_room.participant_count += 1
|
await _participate_room(room, user, db_room, db)
|
||||||
await db.commit()
|
await db.commit()
|
||||||
await db.refresh(db_room)
|
await db.refresh(db_room)
|
||||||
resp = await RoomResp.from_db(db_room)
|
resp = await RoomResp.from_db(db_room, db)
|
||||||
await db.refresh(db_room)
|
await db.refresh(db_room)
|
||||||
for item in db_room.playlist:
|
for item in db_room.playlist:
|
||||||
resp.playlist.append(await PlaylistResp.from_db(item, ["beatmap"]))
|
resp.playlist.append(await PlaylistResp.from_db(item, ["beatmap"]))
|
||||||
@@ -161,6 +187,16 @@ async def remove_user_from_room(
|
|||||||
):
|
):
|
||||||
db_room = (await db.exec(select(Room).where(Room.id == room))).first()
|
db_room = (await db.exec(select(Room).where(Room.id == room))).first()
|
||||||
if db_room is not None:
|
if db_room is not None:
|
||||||
|
participated_user = (
|
||||||
|
await db.exec(
|
||||||
|
select(RoomParticipatedUser).where(
|
||||||
|
RoomParticipatedUser.room_id == room,
|
||||||
|
RoomParticipatedUser.user_id == user,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).first()
|
||||||
|
if participated_user is not None:
|
||||||
|
participated_user.left_at = datetime.now(UTC)
|
||||||
db_room.participant_count -= 1
|
db_room.participant_count -= 1
|
||||||
await db.commit()
|
await db.commit()
|
||||||
return None
|
return None
|
||||||
@@ -286,7 +322,7 @@ async def get_room_events(
|
|||||||
room = (await db.exec(select(Room).where(Room.id == room_id))).first()
|
room = (await db.exec(select(Room).where(Room.id == room_id))).first()
|
||||||
if room is None:
|
if room is None:
|
||||||
raise HTTPException(404, "Room not found")
|
raise HTTPException(404, "Room not found")
|
||||||
room_resp = await RoomResp.from_db(room)
|
room_resp = await RoomResp.from_db(room, db)
|
||||||
|
|
||||||
users = await db.exec(select(User).where(col(User.id).in_(user_ids)))
|
users = await db.exec(select(User).where(col(User.id).in_(user_ids)))
|
||||||
user_resps = [await UserResp.from_db(user, db) for user in users]
|
user_resps = [await UserResp.from_db(user, db) for user in users]
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ from app.database.lazer_user import User
|
|||||||
from app.database.multiplayer_event import MultiplayerEvent
|
from app.database.multiplayer_event import MultiplayerEvent
|
||||||
from app.database.playlists import Playlist
|
from app.database.playlists import Playlist
|
||||||
from app.database.relationship import Relationship, RelationshipType
|
from app.database.relationship import Relationship, RelationshipType
|
||||||
|
from app.database.room_participated_user import RoomParticipatedUser
|
||||||
from app.dependencies.database import engine, get_redis
|
from app.dependencies.database import engine, get_redis
|
||||||
from app.dependencies.fetcher import get_fetcher
|
from app.dependencies.fetcher import get_fetcher
|
||||||
from app.exception import InvokeException
|
from app.exception import InvokeException
|
||||||
@@ -253,6 +254,32 @@ class MultiplayerHub(Hub[MultiplayerClientState]):
|
|||||||
self.add_to_group(client, self.group_id(room_id))
|
self.add_to_group(client, self.group_id(room_id))
|
||||||
await server_room.match_type_handler.handle_join(user)
|
await server_room.match_type_handler.handle_join(user)
|
||||||
await self.event_logger.player_joined(room_id, user.user_id)
|
await self.event_logger.player_joined(room_id, user.user_id)
|
||||||
|
|
||||||
|
async with AsyncSession(engine) as session:
|
||||||
|
async with session.begin():
|
||||||
|
if (
|
||||||
|
participated_user := (
|
||||||
|
await session.exec(
|
||||||
|
select(RoomParticipatedUser).where(
|
||||||
|
RoomParticipatedUser.room_id == room_id,
|
||||||
|
RoomParticipatedUser.user_id == client.user_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).first()
|
||||||
|
) is None:
|
||||||
|
participated_user = RoomParticipatedUser(
|
||||||
|
room_id=room_id,
|
||||||
|
user_id=client.user_id,
|
||||||
|
)
|
||||||
|
session.add(participated_user)
|
||||||
|
else:
|
||||||
|
participated_user.left_at = None
|
||||||
|
participated_user.joined_at = datetime.now(UTC)
|
||||||
|
|
||||||
|
room = await session.get(Room, room_id)
|
||||||
|
if room is None:
|
||||||
|
raise InvokeException("Room does not exist in database")
|
||||||
|
room.participant_count += 1
|
||||||
return room
|
return room
|
||||||
|
|
||||||
async def ChangeBeatmapAvailability(
|
async def ChangeBeatmapAvailability(
|
||||||
@@ -846,6 +873,24 @@ class MultiplayerHub(Hub[MultiplayerClientState]):
|
|||||||
self.group_id(room.room.room_id), "UserLeft", user
|
self.group_id(room.room.room_id), "UserLeft", user
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async with AsyncSession(engine) as session:
|
||||||
|
async with session.begin():
|
||||||
|
participated_user = (
|
||||||
|
await session.exec(
|
||||||
|
select(RoomParticipatedUser).where(
|
||||||
|
RoomParticipatedUser.room_id == room.room.room_id,
|
||||||
|
RoomParticipatedUser.user_id == user.user_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).first()
|
||||||
|
if participated_user is not None:
|
||||||
|
participated_user.left_at = datetime.now(UTC)
|
||||||
|
|
||||||
|
db_room = await session.get(Room, room.room.room_id)
|
||||||
|
if db_room is None:
|
||||||
|
raise InvokeException("Room does not exist in database")
|
||||||
|
db_room.participant_count -= 1
|
||||||
|
|
||||||
target_store = self.state.get(user.user_id)
|
target_store = self.state.get(user.user_id)
|
||||||
if target_store:
|
if target_store:
|
||||||
target_store.room_id = 0
|
target_store.room_id = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user