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:
@@ -3,12 +3,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from datetime import timedelta
|
||||
|
||||
from ..pb_client import DatabaseError
|
||||
from .store import (
|
||||
COOLDOWNS, _cooldown_remaining, _commit, _is_jailed, _locked_by, _now,
|
||||
_prestige_mult, _txn, get_user,
|
||||
_cooldown_remaining, _commit, _is_jailed, _locked_by, _now,
|
||||
_prestige_mult, _txn, effective_cooldown, get_user,
|
||||
)
|
||||
|
||||
|
||||
@@ -81,7 +80,7 @@ async def do_fish_start(user_id: int) -> dict:
|
||||
if jail := _is_jailed(user):
|
||||
return {"ok": False, "reason": "jailed", "remaining": jail}
|
||||
|
||||
fish_cd = timedelta(seconds=90) if "ussipurk" in user["items"] else COOLDOWNS["fish"]
|
||||
fish_cd = effective_cooldown("fish", user["items"])
|
||||
if cd := _cooldown_remaining(user, "fish", override_cd=fish_cd):
|
||||
return {"ok": False, "reason": "cooldown", "remaining": cd}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user