Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@
Column("create_time", TIMESTAMP, nullable=False),
Column("update_time", TIMESTAMP, nullable=False),
Column("user_agent", String),
Column("last_login", TIMESTAMP)
Column("last_login", TIMESTAMP),
Column("avatar_id", Integer, ForeignKey("avatars_list.id")),
)

leaderboard = Table(
Expand Down
10 changes: 10 additions & 0 deletions server/lobbyconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ async def command_avatar(self, message):
)
self.player.avatar = None

new_avatar_id = row.id if avatar_url is not None else None
if avatar_url is not None:
await conn.execute(
avatars.update().where(
Expand All @@ -972,6 +973,15 @@ async def command_avatar(self, message):
"url": avatar_url,
"tooltip": row.tooltip
}
# Mirror the selection to login.avatar_id so reads via the new
# authoritative column stay consistent with the legacy flag.
await conn.execute(
t_login.update().where(
t_login.c.id == self.player.id
).values(
avatar_id=new_avatar_id
)
)
self.player_service.mark_dirty(self.player)
else:
raise KeyError("invalid action")
Expand Down
12 changes: 10 additions & 2 deletions server/player_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import TYPE_CHECKING, ClassVar, Optional, ValuesView

import aiocron
from sqlalchemy import and_, select
from sqlalchemy import and_, func, select

import server.metrics as metrics
from server.config import config
Expand Down Expand Up @@ -90,6 +90,9 @@ async def fetch_player_data(self, player: Player) -> None:
)
player.user_groups = {row.technical_name for row in result}

# Avatar lookup: `login.avatar_id` is the new authoritative FK,
# but for backwards compatibility we still fall back to the
# legacy `avatars.selected = 1` row if `avatar_id` is null.
sql = select(
avatars_list.c.url,
avatars_list.c.tooltip,
Expand All @@ -105,7 +108,12 @@ async def fetch_player_data(self, player: Player) -> None:
avatars.c.selected == 1
)
)
.outerjoin(avatars_list)
.outerjoin(
avatars_list,
onclause=avatars_list.c.id == func.coalesce(
login.c.avatar_id, avatars.c.idAvatar
)
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
).where(login.c.id == player.id) # yapf: disable

result = await conn.execute(sql)
Expand Down
Loading