World: one city among many under cities, no gameplay change
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7dc780352c
commit
11e4d98d49
3 changed files with 100 additions and 7 deletions
2
app.py
2
app.py
|
|
@ -67,7 +67,7 @@ class Store:
|
||||||
return time.time() + self.world["config"]["clock_offset"]
|
return time.time() + self.world["config"]["clock_offset"]
|
||||||
|
|
||||||
def save(self) -> None:
|
def save(self) -> None:
|
||||||
data = json.dumps(self.world, separators=(",", ":"))
|
data = json.dumps(self.world.doc, separators=(",", ":"))
|
||||||
with closing(sqlite3.connect(self.path)) as db, db:
|
with closing(sqlite3.connect(self.path)) as db, db:
|
||||||
db.execute("INSERT INTO world (id, data) VALUES (1, ?) ON CONFLICT (id) DO UPDATE SET data = excluded.data",
|
db.execute("INSERT INTO world (id, data) VALUES (1, ?) ON CONFLICT (id) DO UPDATE SET data = excluded.data",
|
||||||
(data,))
|
(data,))
|
||||||
|
|
|
||||||
63
game.py
63
game.py
|
|
@ -10,6 +10,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
|
from collections.abc import MutableMapping
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
import combat
|
import combat
|
||||||
|
|
@ -1683,11 +1684,57 @@ RAID_TRAITS = ["lightly armoured, heavy on archers", "mounted and fast, few bows
|
||||||
|
|
||||||
# --- world ---------------------------------------------------------------
|
# --- world ---------------------------------------------------------------
|
||||||
|
|
||||||
WORLD_VERSION = 38
|
WORLD_VERSION = 39
|
||||||
|
HOME_CITY = "guildhall"
|
||||||
|
CITY_KEYS = ("events", "next_event_at", "next_minor_at", "next_hardship_at", "next_fortune_at", "caravan")
|
||||||
|
|
||||||
|
|
||||||
def new_world(now: float, speed: float = 1.0) -> dict:
|
class World(MutableMapping):
|
||||||
return {
|
"""The saved doc as seen from one city. `world["city"]` and CITY_KEYS are that city's own;
|
||||||
|
everything else is shared by the whole kingdom. `doc` is what gets saved."""
|
||||||
|
|
||||||
|
def __init__(self, doc: dict, here: str = HOME_CITY):
|
||||||
|
self.doc = doc
|
||||||
|
self.here = here
|
||||||
|
|
||||||
|
def at(self, here: str) -> World:
|
||||||
|
return World(self.doc, here)
|
||||||
|
|
||||||
|
def _holder(self, key: str) -> dict:
|
||||||
|
return self.doc["cities"][self.here] if key in CITY_KEYS else self.doc
|
||||||
|
|
||||||
|
def __getitem__(self, key: str):
|
||||||
|
return self.doc["cities"][self.here] if key == "city" else self._holder(key)[key]
|
||||||
|
|
||||||
|
def __setitem__(self, key: str, value) -> None:
|
||||||
|
if key == "city":
|
||||||
|
self.doc["cities"][self.here] = value
|
||||||
|
else:
|
||||||
|
self._holder(key)[key] = value
|
||||||
|
|
||||||
|
def __delitem__(self, key: str) -> None:
|
||||||
|
del self._holder(key)[key]
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
city = self.doc["cities"][self.here]
|
||||||
|
yield "city"
|
||||||
|
yield from (key for key in CITY_KEYS if key in city)
|
||||||
|
yield from self.doc
|
||||||
|
|
||||||
|
def __len__(self) -> int:
|
||||||
|
return sum(1 for _key in self)
|
||||||
|
|
||||||
|
|
||||||
|
def _nested(doc: dict) -> World:
|
||||||
|
"""Move a flat doc's one city, and what belongs to it, under `cities`."""
|
||||||
|
city = doc.pop("city")
|
||||||
|
city.update({key: doc.pop(key) for key in CITY_KEYS if key in doc})
|
||||||
|
doc["cities"] = {HOME_CITY: city}
|
||||||
|
return World(doc)
|
||||||
|
|
||||||
|
|
||||||
|
def new_world(now: float, speed: float = 1.0) -> World:
|
||||||
|
return _nested({
|
||||||
"version": WORLD_VERSION,
|
"version": WORLD_VERSION,
|
||||||
"created": now,
|
"created": now,
|
||||||
"config": {"speed": speed, "clock_offset": 0},
|
"config": {"speed": speed, "clock_offset": 0},
|
||||||
|
|
@ -1722,11 +1769,13 @@ def new_world(now: float, speed: float = 1.0) -> dict:
|
||||||
"orders": [], "next_order_id": 1,
|
"orders": [], "next_order_id": 1,
|
||||||
"parties": {}, "next_party_id": 1,
|
"parties": {}, "next_party_id": 1,
|
||||||
"caravan": None, "next_caravan_id": 1,
|
"caravan": None, "next_caravan_id": 1,
|
||||||
}
|
})
|
||||||
|
|
||||||
|
|
||||||
def migrate(world: dict, now: float | None = None) -> dict:
|
def migrate(world: dict, now: float | None = None) -> World:
|
||||||
"""Bring a saved world up to the current shape. Never loses anything."""
|
"""Bring a saved world up to the current shape. Never loses anything."""
|
||||||
|
if "cities" in world and not isinstance(world, World):
|
||||||
|
world = World(world)
|
||||||
now = now or world["city"]["last_tick"]
|
now = now or world["city"]["last_tick"]
|
||||||
if world.get("version", 1) < 2:
|
if world.get("version", 1) < 2:
|
||||||
city = world["city"]
|
city = world["city"]
|
||||||
|
|
@ -1961,7 +2010,9 @@ def migrate(world: dict, now: float | None = None) -> dict:
|
||||||
# The crucible: a fifth fire, for the jeweller's silver and gold.
|
# The crucible: a fifth fire, for the jeweller's silver and gold.
|
||||||
world["city"]["fires"].setdefault("crucible", new_fire(now))
|
world["city"]["fires"].setdefault("crucible", new_fire(now))
|
||||||
world["version"] = 38
|
world["version"] = 38
|
||||||
return world
|
if world["version"] < 39:
|
||||||
|
world["version"] = 39
|
||||||
|
return world if isinstance(world, World) else _nested(world)
|
||||||
|
|
||||||
|
|
||||||
def _planks_to_frames(world: dict) -> None:
|
def _planks_to_frames(world: dict) -> None:
|
||||||
|
|
|
||||||
|
|
@ -3552,6 +3552,48 @@ class TownsfolkTest(unittest.TestCase):
|
||||||
self.assertIn("the hearths", game.city_uses("kindling"))
|
self.assertIn("the hearths", game.city_uses("kindling"))
|
||||||
|
|
||||||
|
|
||||||
|
class WorldShapeTest(unittest.TestCase):
|
||||||
|
def test_a_new_world_keeps_its_city_under_cities(self):
|
||||||
|
world = game.new_world(T0)
|
||||||
|
self.assertEqual(list(world.doc["cities"]), [game.HOME_CITY])
|
||||||
|
self.assertNotIn("city", world.doc)
|
||||||
|
self.assertIs(world["city"], world.doc["cities"][game.HOME_CITY])
|
||||||
|
self.assertIs(world["events"], world["city"]["events"])
|
||||||
|
self.assertNotIn("events", world.doc)
|
||||||
|
|
||||||
|
def test_a_flat_save_moves_under_cities_and_loses_nothing(self):
|
||||||
|
world = game.new_world(T0)
|
||||||
|
city = world.doc["cities"].pop(game.HOME_CITY)
|
||||||
|
moved = {key: city.pop(key) for key in game.CITY_KEYS}
|
||||||
|
moved["events"].append({"id": 7, "kind": "raid", "status": "done"})
|
||||||
|
flat = {**world.doc, **moved, "city": city, "version": 38}
|
||||||
|
del flat["cities"]
|
||||||
|
loaded = game.migrate(json.loads(json.dumps(flat)), T0)
|
||||||
|
self.assertEqual(loaded["version"], game.WORLD_VERSION)
|
||||||
|
self.assertEqual(loaded["events"], moved["events"])
|
||||||
|
self.assertEqual(loaded["city"]["population"], city["population"])
|
||||||
|
for key in (*game.CITY_KEYS, "city"):
|
||||||
|
self.assertNotIn(key, loaded.doc)
|
||||||
|
|
||||||
|
def test_a_saved_world_loads_back_the_same(self):
|
||||||
|
world = game.new_world(T0)
|
||||||
|
saved = json.dumps(world.doc)
|
||||||
|
self.assertEqual(json.dumps(game.migrate(json.loads(saved), T0).doc), saved)
|
||||||
|
|
||||||
|
def test_each_city_sees_its_own_state_and_shares_the_rest(self):
|
||||||
|
world = game.new_world(T0)
|
||||||
|
second = json.loads(json.dumps(world["city"]))
|
||||||
|
second["population"] = 5.0
|
||||||
|
world.doc["cities"]["coast"] = second
|
||||||
|
coast = world.at("coast")
|
||||||
|
self.assertEqual(coast["city"]["population"], 5.0)
|
||||||
|
self.assertEqual(world["city"]["population"], 20.0)
|
||||||
|
coast["next_event_at"] = 1.0
|
||||||
|
self.assertNotEqual(world["next_event_at"], 1.0)
|
||||||
|
coast["next_event_id"] = 99
|
||||||
|
self.assertEqual(world["next_event_id"], 99)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue