Update lio.py
This commit is contained in:
@@ -279,25 +279,38 @@ async def _verify_room_password(db: Database, room_id: int, provided_password: s
|
|||||||
|
|
||||||
|
|
||||||
async def _add_or_update_participant(db: Database, room_id: int, user_id: int) -> None:
|
async def _add_or_update_participant(db: Database, room_id: int, user_id: int) -> None:
|
||||||
"""Add a user as participant or update existing participation."""
|
"""添加用户为参与者或更新现有参与记录。"""
|
||||||
# Check if user already has a participation record
|
# 检查用户是否已有活跃的参与记录
|
||||||
existing_result = await db.execute(
|
existing_result = await db.execute(
|
||||||
select(RoomParticipatedUser.id).where(
|
select(RoomParticipatedUser.id).where(
|
||||||
RoomParticipatedUser.room_id == room_id,
|
RoomParticipatedUser.room_id == room_id,
|
||||||
RoomParticipatedUser.user_id == user_id
|
RoomParticipatedUser.user_id == user_id,
|
||||||
).order_by(desc(RoomParticipatedUser.joined_at))
|
col(RoomParticipatedUser.left_at).is_(None)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
existing_id = existing_result.scalar_one_or_none()
|
existing_ids = existing_result.scalars().all() # 获取所有匹配的ID
|
||||||
|
|
||||||
if existing_id:
|
if existing_ids:
|
||||||
# Update existing participation record using SQLAlchemy update
|
# 如果存在多条记录,清理重复项,只保留最新的一条
|
||||||
|
if len(existing_ids) > 1:
|
||||||
|
print(f"警告:用户 {user_id} 在房间 {room_id} 中发现 {len(existing_ids)} 条活跃参与记录")
|
||||||
|
|
||||||
|
# 将除第一条外的所有记录标记为已离开(清理重复记录)
|
||||||
|
for extra_id in existing_ids[1:]:
|
||||||
|
await db.execute(
|
||||||
|
update(RoomParticipatedUser)
|
||||||
|
.where(col(RoomParticipatedUser.id) == extra_id)
|
||||||
|
.values(left_at=utcnow())
|
||||||
|
)
|
||||||
|
|
||||||
|
# 更新剩余的活跃参与记录(刷新加入时间)
|
||||||
await db.execute(
|
await db.execute(
|
||||||
update(RoomParticipatedUser)
|
update(RoomParticipatedUser)
|
||||||
.where(col(RoomParticipatedUser.id) == existing_id)
|
.where(col(RoomParticipatedUser.id) == existing_ids[0])
|
||||||
.values(left_at=None, joined_at=utcnow())
|
.values(joined_at=utcnow())
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# Create new participation record
|
# 创建新的参与记录
|
||||||
participant = RoomParticipatedUser(room_id=room_id, user_id=user_id)
|
participant = RoomParticipatedUser(room_id=room_id, user_id=user_id)
|
||||||
db.add(participant)
|
db.add(participant)
|
||||||
|
|
||||||
@@ -406,7 +419,7 @@ async def add_user_to_room(
|
|||||||
print(f"Successfully added user {user_id} to room {room_id}")
|
print(f"Successfully added user {user_id} to room {room_id}")
|
||||||
return {"success": True}
|
return {"success": True}
|
||||||
|
|
||||||
""" except HTTPException:
|
""" except HTTPException:
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error adding user to room: {str(e)}")
|
print(f"Error adding user to room: {str(e)}")
|
||||||
@@ -425,36 +438,36 @@ async def remove_user_from_room(
|
|||||||
timestamp: str = "",
|
timestamp: str = "",
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""Remove a user from a multiplayer room."""
|
"""Remove a user from a multiplayer room."""
|
||||||
try:
|
""" try: """
|
||||||
# Verify request signature
|
# Verify request signature
|
||||||
body = await request.body()
|
body = await request.body()
|
||||||
if not verify_request_signature(request, timestamp, body):
|
if not verify_request_signature(request, timestamp, body):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
detail="Invalid request signature"
|
detail="Invalid request signature"
|
||||||
)
|
|
||||||
|
|
||||||
# Mark user as left using SQLAlchemy update statement
|
|
||||||
await db.execute(
|
|
||||||
update(RoomParticipatedUser)
|
|
||||||
.where(
|
|
||||||
col(RoomParticipatedUser.room_id) == room_id,
|
|
||||||
col(RoomParticipatedUser.user_id) == user_id,
|
|
||||||
col(RoomParticipatedUser.left_at).is_(None)
|
|
||||||
)
|
|
||||||
.values(left_at=utcnow())
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update participant count
|
# Mark user as left using SQLAlchemy update statement
|
||||||
await _update_room_participant_count(db, room_id)
|
await db.execute(
|
||||||
|
update(RoomParticipatedUser)
|
||||||
await db.commit()
|
.where(
|
||||||
return {"success": True}
|
col(RoomParticipatedUser.room_id) == room_id,
|
||||||
|
col(RoomParticipatedUser.user_id) == user_id,
|
||||||
|
col(RoomParticipatedUser.left_at).is_(None)
|
||||||
|
)
|
||||||
|
.values(left_at=utcnow())
|
||||||
|
)
|
||||||
|
|
||||||
except HTTPException:
|
# Update participant count
|
||||||
|
await _update_room_participant_count(db, room_id)
|
||||||
|
|
||||||
|
await db.commit()
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
""" except HTTPException:
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
detail=f"Failed to remove user from room: {str(e)}"
|
detail=f"Failed to remove user from room: {str(e)}"
|
||||||
)
|
) """
|
||||||
Reference in New Issue
Block a user