-
Notifications
You must be signed in to change notification settings - Fork 38
refactor(server): extract shop + inject get_progression_content into the seam (R3) #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Cosmetics shop (spec 010) — buy/equip avatars & themes with earned currency. | ||
|
|
||
| Extracted verbatim from ``server.py`` (R3); edits: ``@app`` -> ``@router``, | ||
| ``meta_db`` -> ``appstate.meta_db``, ``_get_progression_content()`` -> | ||
| ``appstate.get_progression_content()`` (the accessor is injected into the seam; | ||
| its lazy content cache stays in server.py). | ||
| """ | ||
|
|
||
| from fastapi import APIRouter | ||
| from fastapi.responses import JSONResponse | ||
|
|
||
| import appstate | ||
| from reqfields import _clean_str | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| @router.get("/api/shop") | ||
| def api_shop(): | ||
| content = appstate.get_progression_content() | ||
| owned = appstate.meta_db.get_owned_items() | ||
| equipped = appstate.meta_db.get_equipped() | ||
| items = [ | ||
| {**item, "owned": iid in owned, "equipped": equipped.get(item["slot"]) == iid} | ||
| for iid, item in sorted(content["shop"].items()) | ||
| ] | ||
| return {"items": items, "wallet": appstate.meta_db.get_wallet()} | ||
|
|
||
|
|
||
| @router.post("/api/shop/buy") | ||
| def api_shop_buy(data: dict): | ||
| """Spend Decibels on a cosmetic. Atomic: balance check + spend + ownership | ||
| in one transaction. Decibels are earned by playing only — never purchasable.""" | ||
| item_id = _clean_str(data.get("item_id")) | ||
| item = appstate.get_progression_content()["shop"].get(item_id) | ||
| if not item: | ||
| return JSONResponse({"error": f"unknown item: {item_id!r}"}, status_code=400) | ||
| status, wallet = appstate.meta_db.buy_shop_item(item) | ||
| if status == "owned": | ||
| return JSONResponse({"error": "already owned", "wallet": wallet}, status_code=409) | ||
| if status == "insufficient": | ||
| return JSONResponse({"error": "insufficient balance", "wallet": wallet}, status_code=402) | ||
| return {"ok": True, "item_id": item_id, "wallet": wallet} | ||
|
|
||
|
|
||
| @router.post("/api/shop/equip") | ||
| def api_shop_equip(data: dict): | ||
| """Equip an owned cosmetic into its slot. Body: {slot, item_id|null} | ||
| (null unequips, restoring the default look).""" | ||
| import progression as progression_mod | ||
| slot = _clean_str(data.get("slot")) | ||
| if slot not in progression_mod.SHOP_SLOTS: | ||
| return JSONResponse({"error": f"slot must be one of {sorted(progression_mod.SHOP_SLOTS)}"}, status_code=400) | ||
| item_id = data.get("item_id") | ||
| if item_id is not None: | ||
| item_id = _clean_str(item_id) | ||
| item = appstate.get_progression_content()["shop"].get(item_id) | ||
| if not item or item["slot"] != slot: | ||
| return JSONResponse({"error": f"unknown item for slot {slot}: {item_id!r}"}, status_code=400) | ||
| if item_id not in appstate.meta_db.get_owned_items(): | ||
| return JSONResponse({"error": "item not owned"}, status_code=403) | ||
| return {"ok": True, "equipped": appstate.meta_db.equip_item(slot, item_id)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.