diff --git a/app.py b/app.py index 36d1c50..ed77024 100644 --- a/app.py +++ b/app.py @@ -222,7 +222,7 @@ def roster(world: dict, now: float) -> list[dict]: "fight": player["action"]["id"] if game.joinable(world, player) and game.offline_limit(world, player) > now else None, "party": (player["action"] or {}).get("party"), "order": (player["action"] or {}).get("order"), # the work order they're crafting - "garrison": player["garrison"], "donated": player["donated"], + "donated": player["donated"], "look": game.look_view(player), "seen": int(player["last_active"]), }) diff --git a/game.py b/game.py index 2d7d0d6..adbe151 100644 --- a/game.py +++ b/game.py @@ -1720,7 +1720,7 @@ RAID_TRAITS = ["lightly armoured, heavy on archers", "mounted and fast, few bows # --- world --------------------------------------------------------------- -WORLD_VERSION = 44 +WORLD_VERSION = 45 HOME_CITY = "guildhall" CITY_KEYS = ("events", "next_event_at", "next_minor_at", "next_hardship_at", "next_fortune_at", "caravan", "orders") TRAVEL_TIME = 12 * 60 # game seconds on the road between two towns @@ -2137,6 +2137,11 @@ def migrate(world: dict, now: float | None = None) -> World: city["buildings"].setdefault("outworks", {"level": 0, "damaged": False, "fund": {}, "fittings": []}) city["works_base"] = city["repelled"] world["version"] = 44 + if world["version"] < 45: + # Everyone stands guard unless they step down. + for player in world["players"].values(): + player["garrison"] = True + world["version"] = 45 return world @@ -2473,7 +2478,7 @@ def new_player(uid: str, name: str, cls: str, now: float) -> dict: "last_active": now, "overflow": "city", "bury": True, "perks": {"bank_slot": 0}, "manual": [], "patterns": [], "found": [], "eat_below": EAT_BELOW, "rented": 0, "boosts": [], "bought": None, "deeds": {}, "owned": [], "look": dict(DEFAULT_LOOK), - "garrison": False, + "garrison": True, # stands guard wherever they are, see on_guard() "at": HOME_CITY, "banks": {}, "travel": None, # banks: what you left in the towns you're not in "town_favour": {}, # likewise your favour: `favour` is what you hold in the town you're in "town_slots": {}, # bank slots bought with favour, each good only in its town @@ -4638,12 +4643,24 @@ def donate(world: dict, player: dict, item: str, qty: int, now: float) -> str | return None -def stand_guard(world: World, player: dict, join: bool, now: float) -> str | None: - """Join or leave this town's garrison. Whoever wasn't here when the gates closed stays out.""" +def _shut_out(world: World, player: dict) -> bool: + """Whoever wasn't here when the gates closed stays out of this threat.""" raid = upcoming_raid(world) - if join and not raid: - return "There's no raid to stand guard for" - if join and player.get("arrived", 0) > raid["fire_at"] - GATES_WARNING / speed(world): + return bool(raid) and player.get("arrived", 0) > raid["fire_at"] - GATES_WARNING / speed(world) + + +def on_guard(world: World, player: dict) -> bool: + """Standing guard is the default and follows you around; you only count where you stand, + and only if you've been around since the last threat.""" + if not player["garrison"] or player["at"] != world.here or player["travel"] or _shut_out(world, player): + return False + last = last_threat(world) + return not last or player["last_active"] >= last["resolved"] + + +def stand_guard(world: World, player: dict, join: bool, now: float) -> str | None: + """Step down from the garrison, or back into it.""" + if join and _shut_out(world, player): return "The gates have closed" player["garrison"] = bool(join) return None @@ -4651,7 +4668,7 @@ def stand_guard(world: World, player: dict, join: bool, now: float) -> str | Non def travel(world: World, player: dict, to: str, pouch: dict, now: float) -> str | None: """Set off for another town with what fits in a pouch. Work waits until you arrive, - but a shared fight or a work order stays behind, and so does your place in the garrison.""" + but a shared fight or a work order stays behind.""" if to not in world.doc["cities"]: return "No such town" if to == player["at"]: @@ -4666,7 +4683,6 @@ def travel(world: World, player: dict, to: str, pouch: dict, now: float) -> str current = player["action"] if current and (current.get("party") or "order" in current): drop_out(world, player, now, "Left for another town") - player["garrison"] = False for item, qty in pouch.items(): bank_take(player, item, qty) player["travel"] = {"from": player["at"], "to": to, "left": now, "arrive": now + TRAVEL_TIME / speed(world), "pouch": pouch} @@ -5013,7 +5029,6 @@ def depart_caravan(world: World, player: dict, convoy_id, now: float) -> str | N current = member["action"] if current and (current.get("party") or "order" in current): drop_out(source, member, now, "Left with a caravan") - member["garrison"] = False member["travel"] = {"from": convoy["from"], "to": convoy["to"], "left": now, "arrive": arrive, "pouch": {}, "convoy": convoy["id"]} fullness = _convoy_load(convoy) / (route["load"] * len(convoy["crew"])) convoy.update(status="road", left=now, arrive=arrive, midway=now + route["time"] / 2, odds=route["odds"] * fullness, rolled=False) @@ -5688,7 +5703,7 @@ def defence(world: dict, guards: int | None = None, dragon: bool = False) -> dic parts["reinforcements"] = round(sum(march_power(march) for march in helping), 1) garrison = [] for player in world["players"].values(): - if player["garrison"] and player["at"] == world.here: + if on_guard(world, player): power = combat_power(player) garrison.append({"id": player["id"], "name": player["name"], "power": round(power, 1)}) parts["garrison"] += power @@ -6317,9 +6332,6 @@ def _resolve_raid(world: dict, event: dict, now: float, rng: random.Random) -> N report["lines"] = lines event["report"] = report _pay_guards(world, report) - for player in world["players"].values(): - if player["at"] == world.here: - player["garrison"] = False city["rally"] = city["rallies"] = 0 city["devotion"] = 0 city["hired"] = 0 @@ -6383,9 +6395,6 @@ def _resolve_dragon(world: dict, event: dict, now: float, rng: random.Random) -> report["lines"] = lines event["report"] = report _pay_guards(world, report) - for player in world["players"].values(): - if player["at"] == world.here: - player["garrison"] = False city["rally"] = city["rallies"] = 0 city["devotion"] = 0 city["hired"] = 0 diff --git a/static/app.js b/static/app.js index d1df8fe..2afc422 100644 --- a/static/app.js +++ b/static/app.js @@ -2969,7 +2969,7 @@ function rosterCards(players) {
${rosterSub(p)} · ${esc(p.look.home)}${lastSeen(p)}
${p.total}total
-
${rosterDoing(p)}${p.garrison ? `On guard` : ""}${joinButton(p)} +
${rosterDoing(p)}${joinButton(p)} ${FAVOUR} ${fmt(p.donated)}
${skills}
`; }).join("")}`; @@ -2986,7 +2986,7 @@ function rosterTable(players) {
${rosterSub(p)}${lastSeen(p)}
${p.total} ${rosterTown(p)} -
${rosterDoing(p)}${p.garrison ? `🛡️` : ""}${joinButton(p)}
+
${rosterDoing(p)}${joinButton(p)}
${skills.map((s) => { const lvl = p.levels[s.id]; return `${lvl}`; @@ -3079,7 +3079,7 @@ function characterSheet(uid) {

${presence(p)}${esc(p.name)}

${cls.name} · total level ${total} · ${days === 1 ? "first day" : `${days} days`} in the city${lastSeen(p)}
-
${p.doing ? `${skillOf(p.skill).icon} ${esc(p.doing)}` : `Idle`}${p.garrison ? ` · On guard` : ""}
+
${p.doing ? `${skillOf(p.skill).icon} ${esc(p.doing)}` : `Idle`}
diff --git a/tests/test_game.py b/tests/test_game.py index 3bb5b41..ebd35b5 100644 --- a/tests/test_game.py +++ b/tests/test_game.py @@ -696,7 +696,17 @@ class EventTest(unittest.TestCase): self.assertTrue(raid["report"]["title"].startswith("Victory")) self.assertEqual(world["city"]["repelled"], 1) self.assertEqual(p["favour"], 25) - self.assertFalse(p["garrison"]) + self.assertTrue(p["garrison"]) # still on guard for the next one + + def test_garrison_needs_activity_since_the_last_threat(self): + world = world_with(("wil", "commoner")) + raid = self.announce(world) + p = world["players"]["wil"] + game.tick(world, raid["fire_at"], random.Random(1)) + p["last_active"] = raid["resolved"] - 1 + self.assertEqual(game.defence(world)["garrison"], []) + p["last_active"] = raid["resolved"] + 1 + self.assertEqual([g["id"] for g in game.defence(world)["garrison"]], ["wil"]) def test_defeat_culls_and_loots_banks_too(self): world = world_with(("fred", "commoner")) @@ -1450,6 +1460,7 @@ class FavourTest(unittest.TestCase): def test_drilling_takes_as_long_as_the_scouting(self): world, p = self.world, self.p + p["garrison"] = False # drilling earns combat XP, which would raise the garrison too self.assertEqual(game.start_action(world, p, "drill_militia", T0), "No raid to prepare for") world["next_event_at"] = T0 game.tick(world, T0, random.Random(2)) @@ -3702,12 +3713,20 @@ class KingdomTest(unittest.TestCase): def test_garrison_counts_only_where_you_stand(self): new = self.found() - self.a["garrison"] = True - before = game.defence(self.world)["parts"]["garrison"] - self.assertGreater(before, 0) - self.assertEqual(game.defence(new)["parts"]["garrison"], 0) - self.go(self.a, new.here) - self.assertFalse(self.a["garrison"]) + ids = lambda town: [g["id"] for g in game.defence(town)["garrison"]] + self.assertIn(self.a["id"], ids(self.world)) + self.assertNotIn(self.a["id"], ids(new)) + self.assertIsNone(game.travel(self.world, self.a, new.here, {}, T0)) + self.assertNotIn(self.a["id"], ids(self.world)) # on the road + game.process_player(self.world, self.a, self.a["travel"]["arrive"]) + self.assertIn(self.a["id"], ids(new)) + + def test_stepping_down_lasts_until_you_rejoin(self): + ids = lambda: [g["id"] for g in game.defence(self.world)["garrison"]] + self.assertIsNone(game.stand_guard(self.world, self.a, False, T0)) + self.assertNotIn(self.a["id"], ids()) + self.assertIsNone(game.stand_guard(self.world, self.a, True, T0)) # no raid needed to step back in + self.assertIn(self.a["id"], ids()) def test_gates_close_on_latecomers(self): new = self.found() @@ -3716,6 +3735,7 @@ class KingdomTest(unittest.TestCase): self.a["at"] = new.here self.a["arrived"] = T0 + 3600 - game.GATES_WARNING + 1 self.assertIn("gates", game.stand_guard(new, self.a, True, T0 + 3599)) + self.assertEqual(game.defence(new)["garrison"], []) # the default doesn't get you past the gates self.a["arrived"] = T0 self.assertIsNone(game.stand_guard(new, self.a, True, T0 + 3599)) self.assertTrue(self.a["garrison"]) @@ -4534,6 +4554,12 @@ class SiegeworksTest(unittest.TestCase): self.assertEqual(city["works_base"], 8) self.assertEqual(city["buildings"]["outworks"]["level"], 0) + def test_migration_puts_everyone_on_guard(self): + world = world_with(("fred", "builder")) + world["players"]["fred"]["garrison"] = False + world.doc["version"] = 44 + self.assertTrue(game.migrate(world.doc, T0)["players"]["fred"]["garrison"]) + if __name__ == "__main__": unittest.main()