feat(room): use participated user count as participant count for playlist room
This commit is contained in:
@@ -59,9 +59,10 @@ class ItemAttemptsCount(AsyncAttrs, ItemAttemptsCountBase, table=True):
|
|||||||
self.attempts = sum(score.attempts for score in playlist_scores)
|
self.attempts = sum(score.attempts for score in playlist_scores)
|
||||||
self.total_score = sum(score.total_score for score in playlist_scores)
|
self.total_score = sum(score.total_score for score in playlist_scores)
|
||||||
self.pp = sum(score.score.pp for score in playlist_scores)
|
self.pp = sum(score.score.pp for score in playlist_scores)
|
||||||
self.completed = len([score for score in playlist_scores if score.score.passed])
|
passed_scores = [score for score in playlist_scores if score.score.passed]
|
||||||
|
self.completed = len(passed_scores)
|
||||||
self.accuracy = (
|
self.accuracy = (
|
||||||
sum(score.score.accuracy for score in playlist_scores) / self.completed if self.completed > 0 else 0.0
|
sum(score.score.accuracy for score in passed_scores) / self.completed if self.completed > 0 else 0.0
|
||||||
)
|
)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
await session.refresh(self)
|
await session.refresh(self)
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ from sqlmodel import (
|
|||||||
Relationship,
|
Relationship,
|
||||||
SQLModel,
|
SQLModel,
|
||||||
col,
|
col,
|
||||||
|
func,
|
||||||
select,
|
select,
|
||||||
)
|
)
|
||||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
@@ -118,15 +119,35 @@ class RoomResp(RoomBase):
|
|||||||
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 = []
|
resp.recent_participants = []
|
||||||
for recent_participant in await session.exec(
|
if room.category == RoomCategory.REALTIME:
|
||||||
select(RoomParticipatedUser)
|
query = (
|
||||||
.where(
|
select(RoomParticipatedUser)
|
||||||
RoomParticipatedUser.room_id == room.id,
|
.where(
|
||||||
col(RoomParticipatedUser.left_at).is_(None),
|
RoomParticipatedUser.room_id == room.id,
|
||||||
|
col(RoomParticipatedUser.left_at).is_(None),
|
||||||
|
)
|
||||||
|
.limit(8)
|
||||||
|
.order_by(col(RoomParticipatedUser.joined_at).desc())
|
||||||
)
|
)
|
||||||
.limit(8)
|
else:
|
||||||
.order_by(col(RoomParticipatedUser.joined_at).desc())
|
query = (
|
||||||
):
|
select(RoomParticipatedUser)
|
||||||
|
.where(
|
||||||
|
RoomParticipatedUser.room_id == room.id,
|
||||||
|
)
|
||||||
|
.limit(8)
|
||||||
|
.order_by(col(RoomParticipatedUser.joined_at).desc())
|
||||||
|
)
|
||||||
|
resp.participant_count = (
|
||||||
|
await session.exec(
|
||||||
|
select(func.count())
|
||||||
|
.select_from(RoomParticipatedUser)
|
||||||
|
.where(
|
||||||
|
RoomParticipatedUser.room_id == room.id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).first() or 0
|
||||||
|
for recent_participant in await session.exec(query):
|
||||||
resp.recent_participants.append(
|
resp.recent_participants.append(
|
||||||
await UserResp.from_db(
|
await UserResp.from_db(
|
||||||
await recent_participant.awaitable_attrs.user,
|
await recent_participant.awaitable_attrs.user,
|
||||||
|
|||||||
Reference in New Issue
Block a user