Fish. Feed them. They will die if the community doesnt feed them. They will die. Each fish has a timer. They will super die.
Dead = upside down, still saveable for 30 seconds. Super dead = gone. Skeleton. Ghost. Tombstone. No revive. You had one job.
Backend status: checking. If backend is off, this tank is local demo. Fish still die. That part is real.
Last fed time is real. If you leave and come back in 5 minutes, the timers kept going. That is the point. They do not pause for you.
Each fish has last_fed_at. Frontend shows time left = interval - (now - last_fed_at). Backend is the truth. If backend is off, localStorage is the truth. If nobody feeds, last_fed_at gets old, fish die, then super die. No cron needed, just math.
# File: /opt/tinyfiles/aquarium_api.py
import json, sqlite3, time
from http.server import BaseHTTPRequestHandler, HTTPServer
DB="/opt/tinyfiles/aquarium.db"
FISH=[
{"id":1,"name":"Bloop","emoji":"🐟","interval":120},
{"id":2,"name":"Gerald","emoji":"🐡","interval":150},
{"id":3,"name":"Tiny","emoji":"🐠","interval":90},
{"id":4,"name":"Laser","emoji":"🦈","interval":180},
{"id":5,"name":"Moth","emoji":"🐙","interval":135},
]
def init():
c=sqlite3.connect(DB); c.execute("CREATE TABLE IF NOT EXISTS fish (id INTEGER PRIMARY KEY, name TEXT, emoji TEXT, interval INTEGER, last_fed_at REAL, fed_by TEXT, feeds INTEGER DEFAULT 0, status TEXT DEFAULT 'alive')")
for f in FISH:
c.execute("INSERT OR IGNORE INTO fish (id,name,emoji,interval,last_fed_at,feeds) VALUES (?,?,?,?,?,0)", (f["id"],f["name"],f["emoji"],f["interval"],time.time()))
c.execute("CREATE TABLE IF NOT EXISTS feed_log (id INTEGER PRIMARY KEY AUTOINCREMENT, fish_id INTEGER, feeder TEXT, created_at REAL)")
c.commit(); c.close()
class H(BaseHTTPRequestHandler):
def _cors(self):
self.send_header("Access-Control-Allow-Origin","*"); self.send_header("Access-Control-Allow-Methods","GET, POST, OPTIONS"); self.send_header("Access-Control-Allow-Headers","Content-Type")
def do_OPTIONS(self): self.send_response(204); self._cors(); self.end_headers()
def do_GET(self):
if self.path.startswith("/api/aquarium/fish"):
c=sqlite3.connect(DB); c.row_factory=sqlite3.Row; rows=c.execute("SELECT * FROM fish").fetchall(); logs=c.execute("SELECT * FROM feed_log ORDER BY id DESC LIMIT 20").fetchall()
body=json.dumps({"fish":[dict(r) for r in rows],"log":[dict(r) for r in logs]}).encode()
self.send_response(200); self._cors(); self.send_header("Content-Type","application/json"); self.send_header("Content-Length",str(len(body))); self.end_headers(); self.wfile.write(body)
else: self.send_response(404); self.end_headers()
def do_POST(self):
if self.path.startswith("/api/aquarium/feed"):
n=int(self.headers.get("Content-Length",0)); j=json.loads(self.rfile.read(n).decode() or "{}")
fid=j.get("fish_id"); feeder=(j.get("feeder") or "anonymous")[:100]
c=sqlite3.connect(DB)
if fid=="all":
c.execute("UPDATE fish SET last_fed_at=?, fed_by=?, feeds=feeds+1, status='alive' WHERE status != 'superdead'", (time.time(),feeder))
for f in FISH: c.execute("INSERT INTO feed_log (fish_id,feeder,created_at) VALUES (?,?,?)", (f["id"],feeder,time.time()))
else:
c.execute("UPDATE fish SET last_fed_at=?, fed_by=?, feeds=feeds+1, status='alive' WHERE id=? AND status != 'superdead'", (time.time(),feeder,fid))
c.execute("INSERT INTO feed_log (fish_id,feeder,created_at) VALUES (?,?,?)", (fid,feeder,time.time()))
c.commit(); c.close()
out=json.dumps({"ok":True}).encode(); self.send_response(200); self._cors(); self.send_header("Content-Type","application/json"); self.send_header("Content-Length",str(len(out))); self.end_headers(); self.wfile.write(out)
else: self.send_response(404); self.end_headers()
def log_message(self,*a): pass
init(); print("aquarium api on 127.0.0.1:5004"); HTTPServer(("127.0.0.1",5004),H).serve_forever()
# nginx
location /api/aquarium/ { proxy_pass http://127.0.0.1:5004/; proxy_set_header Host $host; }
# Timer math, frontend or backend:
# time_left = interval - (now - last_fed_at)
# time_left > 50% = happy
# 25% to 50% = hungry
# 0 to 25% = starving
# time_left <= 0 = dead, dead_for = now - (last_fed_at + interval)
# dead_for > 30 = super dead, status = superdead, no revive
# Feed dead within 30s = revive, last_fed_at = now, status = alive
# Feed superdead = nothing. It is gone.