forked from retoor/devplacepy
feat: add Code Farm cooperative idle game with plot-based crop system and pub/sub notifications
Implement a Farmville-style cooperative idle game mounted at `/game` with member-only play and public farm viewing. The data layer is pure and timestamp-driven with no background tick: plot states are derived from `ready_at` vs current time, never stored. Key features include plantable software projects (shell script through kernel) that build over real time, harvest for coins and XP, CI tier upgrades for faster builds, plot purchasing with doubling cost, watering mechanics for friends' builds, daily quests, and prestige system. All game endpoints negotiate HTML or JSON and return full farm state for single-request client refresh. Pub/sub notifications broadcast farm updates on `public.game.farm.{username}` topics. Database schema adds `game_farms`, `game_plots`, and `game_quests` tables with appropriate indexes.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from fastapi import Request
|
||||
|
||||
from devplacepy.database import get_table
|
||||
from devplacepy.services.game import store
|
||||
from devplacepy.services.pubsub import publish
|
||||
|
||||
|
||||
def farm_topic(username: str) -> str:
|
||||
return f"public.game.farm.{username}"
|
||||
|
||||
|
||||
async def notify_farm(username: str) -> None:
|
||||
if not username:
|
||||
return
|
||||
try:
|
||||
await publish(farm_topic(username), {"kind": "update", "username": username})
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def owner_by_username(username: str) -> dict | None:
|
||||
return get_table("users").find_one(username=username)
|
||||
|
||||
|
||||
def state_payload(user: dict, viewer: dict | None = None) -> dict:
|
||||
farm = store.ensure_farm(user["uid"])
|
||||
return store.serialize_farm(farm, viewer=viewer or user, owner=user)
|
||||
|
||||
|
||||
def game_seo(request: Request, title: str, description: str) -> dict:
|
||||
from devplacepy.seo import base_seo_context
|
||||
|
||||
return base_seo_context(
|
||||
request,
|
||||
title=title,
|
||||
description=description,
|
||||
robots="noindex,follow",
|
||||
breadcrumbs=[
|
||||
{"name": "Home", "url": "/feed"},
|
||||
{"name": "Code Farm", "url": "/game"},
|
||||
],
|
||||
)
|
||||
Reference in New Issue
Block a user