diff --git a/app.py b/app.py index ceb2b1f..af2972a 100644 --- a/app.py +++ b/app.py @@ -56,7 +56,8 @@ class Store: db.execute("CREATE TABLE IF NOT EXISTS world (id INTEGER PRIMARY KEY CHECK (id = 1), data TEXT NOT NULL)") row = db.execute("SELECT data FROM world WHERE id = 1").fetchone() if row: - self.world = game.migrate(json.loads(row[0])) + world = json.loads(row[0]) + self.world = game.migrate(world, time.time() + world["config"].get("clock_offset", 0)) self.world["config"]["speed"] = speed else: self.world = game.new_world(time.time(), speed) diff --git a/game.py b/game.py index fac4b93..aa5a373 100644 --- a/game.py +++ b/game.py @@ -409,8 +409,9 @@ def new_world(now: float, speed: float = 1.0) -> dict: } -def migrate(world: dict) -> dict: +def migrate(world: dict, now: float | None = None) -> dict: """Bring a saved world up to the current shape. Never loses anything.""" + now = now or world["city"]["last_tick"] if world.get("version", 1) < 2: city = world["city"] for building in city["buildings"].values(): @@ -419,7 +420,6 @@ def migrate(world: dict) -> dict: city.setdefault("tier", 0) world["version"] = 2 if world["version"] < 3: - now = world["city"]["last_tick"] for player in world["players"].values(): for skill in SKILL_IDS: player["xp"].setdefault(skill, 0) @@ -430,7 +430,7 @@ def migrate(world: dict) -> dict: player.setdefault("hp_at", now) world["version"] = 3 if world["version"] < 4: - _sell_useless_stock(world, world["city"]["last_tick"]) + _sell_useless_stock(world, now) world["version"] = 4 return world