fix(economy): handle db_error in commands, dedupe cooldowns, single leaderboard scan
Three robustness/perf fixes from the review:
1. db_error UX: economy core returns {"ok": False, "reason": "db_error"} on a
PocketBase outage, but no handler expected it - deferred commands hung on
"thinking..." and others showed a misleading "you're broke". Added a shared
reply_db_error helper (commands/_replies.py) + S.ERR["db_error"], and wired a
db_error branch into every handler that can receive it (daily/work/beg/crime/
rob/give/buy/roulette/slots/blackjack/heist/fish/prestige/vanity/consumables/
request-funding). Also fixed a latent KeyError in vs-bot RPS that read
res["balance"] without checking res["ok"].
2. Deduped the item->cooldown mapping that was copied in do_daily/do_work/do_beg,
do_fish_start, _maybe_remind and _restore_reminders. Single source of truth:
store.ITEM_COOLDOWNS + effective_cooldown(cmd, items).
3. /leaderboard did six full-collection scans (one per tab). Added
get_all_leaderboards() which scans once and builds all six views in memory.
Tests: effective_cooldown cases, and get_all_leaderboards matches the individual
queries + scans the collection exactly once.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VbAVsrZuYesea99mPMmPT
This commit is contained in:
@@ -80,3 +80,32 @@ async def get_leaderboard_fish(top_n: int | None = 10) -> list[tuple[str, int]]:
|
||||
reverse=True,
|
||||
)
|
||||
return result if top_n is None else result[:top_n]
|
||||
|
||||
|
||||
async def get_all_leaderboards() -> dict[str, list[tuple]]:
|
||||
"""Build every leaderboard view from a SINGLE collection scan.
|
||||
|
||||
/leaderboard shows six tabs; calling each get_leaderboard_* separately would
|
||||
read the whole collection six times. This reads once and sorts in memory,
|
||||
returning the same tuple shapes the individual functions produce (unbounded -
|
||||
the command paginates)."""
|
||||
records = await pb_client.list_all_records()
|
||||
users = [r for r in records if r.get("user_id")]
|
||||
|
||||
def desc(keyfn) -> list[dict]:
|
||||
return sorted(users, key=keyfn, reverse=True)
|
||||
|
||||
return {
|
||||
"coins": [(r["user_id"], r.get("balance", 0))
|
||||
for r in desc(lambda r: r.get("balance", 0))],
|
||||
"exp": [(r["user_id"], r.get("exp", 0), get_level(r.get("exp", 0)))
|
||||
for r in desc(lambda r: r.get("exp", 0))],
|
||||
"season": [(r["user_id"], r.get("season_total_exp", 0), r.get("prestige_level", 0))
|
||||
for r in desc(lambda r: r.get("season_total_exp", 0))],
|
||||
"prestige": [(r["user_id"], r.get("prestige_level", 0), r.get("prestige_points", 0))
|
||||
for r in desc(lambda r: (r.get("prestige_level", 0), r.get("prestige_points", 0)))],
|
||||
"wagered": [(r["user_id"], r.get("total_wagered", 0))
|
||||
for r in desc(lambda r: r.get("total_wagered", 0))],
|
||||
"fish": [(r["user_id"], r.get("total_fish_caught", 0))
|
||||
for r in desc(lambda r: r.get("total_fish_caught", 0))],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user