Favour per town; bank slot prices restart in each town

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rope 2026-11-04 17:14:40 +00:00
parent 22635504e8
commit a4c9977e5c
4 changed files with 146 additions and 28 deletions

10
app.py
View file

@ -121,8 +121,8 @@ def player_view(world: dict, player: dict, now: float) -> dict:
"boosts": {ab: [game.ITEMS[i]["name"] for i in player["equip"].values() if i and ab in game.ITEMS[i].get("mods", {})]
for ab in me["mods"]}}
view["health_potion"] = game.health_potion(player)
view["perk_costs"] = {perk: game.perk_cost(player, perk) for perk in player["perks"]}
view["rent_cost"] = game.rent_cost(player)
view["perk_costs"] = {perk: game.perk_cost(world, player, perk) for perk in player["perks"]}
view["rent_cost"] = game.rent_cost(world, player)
view["caravan"] = game.caravan_view(world, player, now)
view["titles"] = game.titles(player)
view["achievements"] = game.achievements(player)
@ -212,7 +212,7 @@ def roster(world: dict, now: float) -> list[dict]:
if key in ("max_hit", "accuracy", "evasion", "crit", "interval", "block", "damage")},
"armour": game.armour(player), "reduction": round(game.damage_reduction(player), 3),
"ward": game.magic_ward(player), "ward_reduction": round(game.reduction(game.magic_ward(player)), 3),
"favour": player["favour"],
"favour": game.favour_saved(player),
"id": player["id"], "name": player["name"], "cls": player["cls"],
"total": sum(game.skill_level(player, skill) for skill in game.SKILL_IDS),
"levels": {skill: game.skill_level(player, skill) for skill in game.SKILL_IDS},
@ -490,7 +490,7 @@ class Handler(BaseHTTPRequestHandler):
if path == "/api/wages":
return game.pay_wages(world, player, qty if qty == "all" or isinstance(qty, int) else -1, str(body.get("source")), now)
if path == "/api/rent":
return game.rent_slot(player)
return game.rent_slot(world, player)
if path == "/api/buy":
skill = body.get("skill")
return game.buy_ware(world, player, str(body.get("ware")), str(skill) if skill else None, now)
@ -531,7 +531,7 @@ class Handler(BaseHTTPRequestHandler):
if path == "/api/plot":
return game.plant_second_plot(world, player, body.get("id") or None)
if path == "/api/perk":
return game.buy_perk(player, str(body.get("perk")))
return game.buy_perk(world, player, str(body.get("perk")))
if path == "/api/pin":
return game.toggle_pin(player, str(body.get("skill")))
if path == "/api/track":

60
game.py
View file

@ -1684,7 +1684,7 @@ RAID_TRAITS = ["lightly armoured, heavy on archers", "mounted and fast, few bows
# --- world ---------------------------------------------------------------
WORLD_VERSION = 41
WORLD_VERSION = 42
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
@ -2066,6 +2066,13 @@ def migrate(world: dict, now: float | None = None) -> World:
# The war council, and guards on the march.
world.doc.update(marches=[], council=[], next_march_id=1)
world["version"] = 41
if world["version"] < 42:
# Favour belongs to the town it was earned in. All of it so far counts as the first town's.
for player in world["players"].values():
player.update(town_favour={}, town_slots={}, town_rented={})
if player["at"] != HOME_CITY:
player["town_favour"][HOME_CITY], player["favour"] = player["favour"], 0
world["version"] = 42
return world
@ -2404,6 +2411,9 @@ def new_player(uid: str, name: str, cls: str, now: float) -> dict:
"rented": 0, "boosts": [], "bought": None, "deeds": {}, "owned": [], "look": dict(DEFAULT_LOOK),
"garrison": False,
"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
"town_rented": {}, # and those rented with gold
"donated": 0,
"gave": {}, # item (or "gold"): {qty, worth, favour} given to the city, see credit()
"ledger": [], # what you did, see note() and track()
@ -2418,7 +2428,8 @@ def skill_level(player: dict, skill: str) -> int:
def bank_slots(world: dict, player: dict) -> int:
return (BASE_SLOTS + 3 * building_level(world, "storehouse") + TIER_SLOTS * world["city"]["tier"]
+ player["perks"]["bank_slot"] + player["rented"] + fitted(world, "shelving") + int(gear_bonus(player, "slots")))
+ player["perks"]["bank_slot"] + player["town_slots"].get(world.here, 0)
+ player["rented"] + player["town_rented"].get(world.here, 0) + fitted(world, "shelving") + int(gear_bonus(player, "slots")))
def stack_size(world: dict, player: dict | None = None) -> int:
@ -4419,7 +4430,11 @@ def _to_city(world: dict, order: dict, giver: dict, qty: int, now: float, used:
if not helper:
continue
for reagent, n in items.items():
favour[uid] = favour.get(uid, 0) + credit(helper, reagent, n, rate, ITEMS[reagent]["value"] * n * scale)
earned = credit(helper, reagent, n, rate, ITEMS[reagent]["value"] * n * scale)
favour[uid] = favour.get(uid, 0) + earned
if helper["at"] != world.here: # earned here, though they've moved on
helper["favour"] -= earned
helper["town_favour"][world.here] = helper["town_favour"].get(world.here, 0) + earned
note(world, helper, now, "Donated", reagent, n, label=label)
favour[giver["id"]] = favour.get(giver["id"], 0) + credit(giver, item, qty, rate, worth - pooled * scale)
note(world, giver, now, "Donated", item, qty, label=label)
@ -4596,7 +4611,9 @@ def arrive(world: World, player: dict) -> None:
"""Off the road: into this town's bank, pouch unpacked, and back to work."""
trip, player["travel"] = player["travel"], None
player["banks"][player["at"]] = player["bank"]
player["town_favour"][player["at"]] = player["favour"]
player["at"], player["bank"] = trip["to"], player["banks"].pop(trip["to"], {})
player["favour"] = player["town_favour"].pop(trip["to"], 0)
player["arrived"] = trip["arrive"]
world = here(world, player)
for item, qty in trip["pouch"].items():
@ -5757,7 +5774,7 @@ def _loot_banks(world: World, event: dict, share: float, now: float) -> bool:
looted = False
for player in world["players"].values():
bank = player["bank"] if player["at"] == world.here else player["banks"].get(world.here, {})
locked = (player["rented"] + gear_bonus(player, "slots")) / max(1, bank_slots(world, player))
locked = (player["rented"] + player["town_rented"].get(world.here, 0) + gear_bonus(player, "slots")) / max(1, bank_slots(world, player))
for item in list(bank):
gone = int(bank[item] * share * BANK_LOOT * (1 - min(1, locked)))
if gone:
@ -6615,16 +6632,19 @@ RENT_BASE = 1000 # gold for the first rented bank slot...
RENT_GROWTH = 1.35 # ...and each one after costs this much more
def rent_cost(player: dict) -> int:
return int(round(RENT_BASE * RENT_GROWTH ** player["rented"] / 50) * 50)
def rent_cost(world: World, player: dict) -> int:
"""Each slot rented in a town costs more there. The first town's carries on from the
slots rented before they became per town, which stay good everywhere."""
rented = player["town_rented"].get(world.here, 0) + (player["rented"] if world.here == HOME_CITY else 0)
return int(round(RENT_BASE * RENT_GROWTH ** rented / 50) * 50)
def rent_slot(player: dict) -> str | None:
cost = rent_cost(player)
def rent_slot(world: World, player: dict) -> str | None:
cost = rent_cost(world, player)
if player["gold"] < cost:
return f"Needs {cost:,} gold"
player["gold"] -= cost
player["rented"] += 1
player["town_rented"][world.here] = player["town_rented"].get(world.here, 0) + 1
return None
@ -7095,20 +7115,28 @@ def spend_favour(world: dict, player: dict, kind: str, now: float) -> str | None
return None
def perk_cost(player: dict, perk: str) -> int:
def perk_cost(world: World, player: dict, perk: str) -> int:
"""A bank slot's price doubles with each bought in this town. The first town's carries on from
the slots bought before they became per town, which stay good everywhere."""
if perk == "bank_slot":
return 500 * 2 ** player["perks"]["bank_slot"]
bought = player["town_slots"].get(world.here, 0)
return 500 * 2 ** (bought + (player["perks"]["bank_slot"] if world.here == HOME_CITY else 0))
raise KeyError(perk)
def buy_perk(player: dict, perk: str) -> str | None:
def favour_saved(player: dict) -> float:
"""Your favour across every town."""
return player["favour"] + sum(player["town_favour"].values())
def buy_perk(world: World, player: dict, perk: str) -> str | None:
if perk not in player["perks"]:
return "No such perk"
cost = perk_cost(player, perk)
cost = perk_cost(world, player, perk)
if player["favour"] < cost:
return f"Needs {cost:,} favour"
player["favour"] -= cost
player["perks"][perk] += 1
player["town_slots"][world.here] = player["town_slots"].get(world.here, 0) + 1
return None
@ -7398,6 +7426,10 @@ ORDER_RECIPES, ORDER_RAW = _order_sources()
# What changed, shown on the Patch notes page. Newest first; every shipped change adds a
# line here (new / balance / fix), and a new release a new entry on top.
PATCH_NOTES = [
{"date": "2026-11-04", "title": "Favour per town", "notes": [
("new", "Favour is earned and spent per town. What you've saved so far stays in Guildhall."),
("new", "Bank slots: favour price starts at 500 and gold at 1,000 in each town, rising there. New ones work only in that town. Ones you already own work everywhere."),
]},
{"date": "2026-11-04", "title": "War council", "notes": [
("new", "War council on the Kingdom page: propose sending guards to another town. 1h vote: a majority of active players settles it, else the votes cast. Tie: they stay."),
("new", "Guards march 20h with their kit and wage, stand one fight, then march home. Lost fight: some die. Their posts stay empty while they're away."),

View file

@ -769,7 +769,7 @@ function sidebar() {
<div class="me">
<div class="who">${esc(me.name)}</div>
<div class="cls">${cls.icon} ${cls.name} · ${HEART} <span data-live-hp>${fmt(me.hp)}</span>/${me.max_hp}</div>
<div class="purse"><span class="gold" title="Gold">${COIN} ${fmt(me.gold)}</span><span class="favour" title="Favour: earned by donating">${FAVOUR} ${fmt(me.favour)}</span></div>
<div class="purse"><span class="gold" title="Gold">${COIN} ${fmt(me.gold)}</span><span class="favour" title="Favour in this town: earned by donating here, spent here">${FAVOUR} ${fmt(me.favour)}</span></div>
</div>
${pinnedNav(me, doing)}
<div class="navhead">City</div>
@ -2146,15 +2146,16 @@ function goldPanel() {
function strongboxPanel() {
const me = S.me;
return `<div class="panel"><h2>Strongbox${me.rented ? ` · ${me.rented} rented` : ""}</h2>
return `<div class="panel"><h2>Strongbox${me.rented + (me.town_rented[me.at] || 0) ? ` · ${me.rented + (me.town_rented[me.at] || 0)} rented` : ""}</h2>
<div class="btns"><button class="btn gold" data-rent ${me.gold < me.rent_cost ? "disabled" : ""}>+1 bank slot (${COIN} ${fmt(me.rent_cost)})</button></div>
<p class="small muted">Yours for good. Each costs 35% more.</p></div>`;
<p class="small muted">Yours for good, in this town. Each costs 35% more.</p></div>`;
}
function favourPanel() {
const me = S.me;
return `<div class="panel"><h2>Favour with the council</h2>
<p class="favour">${FAVOUR} ${fmt(me.favour)} favour · ${fmt(me.donated)} given in total</p>
<p class="favour">${FAVOUR} ${fmt(me.favour)} favour in ${esc(townName(me.at))} · ${fmt(me.donated)} given in total</p>
${Object.entries(me.town_favour).filter(([, n]) => Math.round(n)).map(([id, n]) => `<p class="small muted">${FAVOUR} ${fmt(n)} in ${esc(townName(id))}</p>`).join("")}
<div class="btns"><button class="btn" data-perk="bank_slot" ${me.favour < me.perk_costs.bank_slot ? "disabled" : ""}>+1 bank slot (${fmt(me.perk_costs.bank_slot)} favour)</button></div>
<p class="small muted">More to spend on in <button class="linkish" data-nav="council">Council &amp; threats</button>.</p></div>`;
}
@ -2979,7 +2980,7 @@ function characterSheet(uid) {
${mine ? `<div class="stat"><div class="k">Prayer points</div><div class="v">${fmt(p.pp)}</div><div class="note">${p.prayers.length ? p.prayers.map((id) => esc(C.prayers.find((x) => x.id === id).name)).join(", ") : "No prayers active"}</div></div>` : ""}
<div class="stat"><div class="k">Armour</div><div class="v">${p.armour}</div><div class="note">${Math.round(p.reduction * 100)}% less physical</div></div>
<div class="stat"><div class="k">Ward</div><div class="v">${p.ward}</div><div class="note">${Math.round(p.ward_reduction * 100)}% less magic</div></div>
<div class="stat"><div class="k">Given to the city</div><div class="v favour">${fmt(p.donated)}</div><div class="note">${fmt(p.favour)} favour to spend</div></div>
<div class="stat"><div class="k">Given to the city</div><div class="v favour">${fmt(p.donated)}</div><div class="note">${fmt(p.favour)} favour saved</div></div>
${mine ? `<div class="stat"><div class="k">Purse</div><div class="v gold">${fmt(p.gold)}</div><div class="note">${p.slots_used} / ${p.slots} bank slots</div></div>` : ""}
</div>
<div class="cols">

View file

@ -2156,13 +2156,13 @@ class GoldTest(unittest.TestCase):
def test_rented_slots_cost_more_each_time(self):
before = game.bank_slots(self.world, self.ann)
self.assertEqual([game.rent_cost({"rented": n}) for n in range(4)], [1000, 1350, 1800, 2450])
self.assertIsNone(game.rent_slot(self.ann))
self.assertIsNone(game.rent_slot(self.ann))
self.assertEqual([game.rent_cost(self.world, {"rented": n, "town_rented": {}}) for n in range(4)], [1000, 1350, 1800, 2450])
self.assertIsNone(game.rent_slot(self.world, self.ann))
self.assertIsNone(game.rent_slot(self.world, self.ann))
self.assertEqual(game.bank_slots(self.world, self.ann), before + 2)
self.assertEqual(self.ann["gold"], 200_000 - 2350)
self.ann["gold"] = 0
self.assertEqual(game.rent_slot(self.ann), "Needs 1,800 gold")
self.assertEqual(game.rent_slot(self.world, self.ann), "Needs 1,800 gold")
def test_caravan_comes_on_the_prep_day_and_sells_once_per_visit(self):
self.world["next_event_at"] = T0
@ -4041,6 +4041,91 @@ class KingdomTest(unittest.TestCase):
threat["recon"] = 10**6
self.assertIn("main", game.event_view(world, threat, threat["fire_at"] - 3600))
def test_favour_stays_in_the_town_it_was_earned_in(self):
new = self.found()
self.a["favour"] = 1000
arrive = self.go(self.a, new.here)
self.assertEqual(self.a["favour"], 1000) # still yours until you leave the road
game.process_player(self.world, self.a, arrive)
self.assertEqual(self.a["favour"], 0)
self.assertEqual(self.a["town_favour"][game.HOME_CITY], 1000)
self.a["favour"] = 40
back = self.go(self.a, game.HOME_CITY, now=arrive)
game.process_player(self.world, self.a, back)
self.assertEqual(self.a["favour"], 1000)
self.assertEqual(self.a["town_favour"][new.here], 40)
self.assertEqual(game.favour_saved(self.a), 1040)
def test_you_cant_spend_another_towns_favour(self):
new = self.found()
self.a["favour"], self.a["at"] = 0, new.here
self.a["town_favour"][game.HOME_CITY] = 10**6
self.assertIn("favour", game.buy_perk(new, self.a, "bank_slot"))
def test_favour_slot_prices_restart_per_town(self):
new = self.found()
self.a["perks"]["bank_slot"] = 8
self.a["at"] = new.here
self.assertEqual(game.perk_cost(self.world, self.a, "bank_slot"), 128000)
self.assertEqual(game.perk_cost(new, self.a, "bank_slot"), 500)
self.a["favour"] = 1500
self.assertIsNone(game.buy_perk(new, self.a, "bank_slot"))
self.assertEqual(game.perk_cost(new, self.a, "bank_slot"), 1000)
self.assertEqual(self.a["favour"], 1000)
self.assertEqual(game.perk_cost(self.world, self.a, "bank_slot"), 128000)
def test_new_favour_slots_work_only_in_their_town_old_ones_everywhere(self):
new = self.found()
self.a["perks"]["bank_slot"] = 8
here, there = game.bank_slots(new, self.a), game.bank_slots(self.world, self.a)
self.a["favour"], self.a["at"] = 500, new.here
game.buy_perk(new, self.a, "bank_slot")
self.assertEqual(game.bank_slots(new, self.a), here + 1)
self.assertEqual(game.bank_slots(self.world, self.a), there)
self.assertGreaterEqual(here, 8 + game.BASE_SLOTS)
def test_a_helper_who_moved_on_earns_favour_where_the_order_was(self):
new = self.found()
self.a["at"] = new.here
order = {"item": "stone", "favour": {}}
game._to_city(self.world, order, self.b, 5, T0, {"a": {"stone": 5}})
self.assertGreater(self.a["town_favour"][game.HOME_CITY], 0)
self.assertEqual(self.a["favour"], 0)
def test_saved_favour_counts_as_the_first_towns(self):
new = self.found()
doc = json.loads(json.dumps(self.world.doc))
doc["version"] = 41
for player in doc["players"].values():
del player["town_favour"], player["town_slots"], player["town_rented"]
doc["players"]["a"].update(favour=700, at=new.here)
doc["players"]["b"].update(favour=300)
world = game.migrate(doc, T0)
a, b = world["players"]["a"], world["players"]["b"]
self.assertEqual((a["favour"], a["town_favour"][game.HOME_CITY]), (0, 700))
self.assertEqual((b["favour"], b["town_favour"]), (300, {}))
def test_gold_slot_prices_restart_per_town(self):
new = self.found()
self.a["rented"], self.a["gold"], self.a["at"] = 12, 5000, new.here
expensive = game.rent_cost(self.world, self.a)
self.assertEqual(game.rent_cost(new, self.a), game.RENT_BASE)
here, there = game.bank_slots(new, self.a), game.bank_slots(self.world, self.a)
self.assertIsNone(game.rent_slot(new, self.a))
self.assertEqual(self.a["gold"], 4000)
self.assertGreater(game.rent_cost(new, self.a), game.RENT_BASE)
self.assertEqual(game.rent_cost(self.world, self.a), expensive)
self.assertEqual(game.bank_slots(new, self.a), here + 1)
self.assertEqual(game.bank_slots(self.world, self.a), there)
def test_slots_rented_in_a_town_shield_its_bank(self):
new = self.found()
self.a["at"] = new.here
self.a["bank"] = {"stone": 1000}
self.a["town_rented"][new.here] = game.bank_slots(new, self.a) # half the slots
game._loot_banks(new, {"slot": 5}, 0.4, T0)
self.assertEqual(self.a["bank"]["stone"], 900)
def test_old_saves_gain_towns(self):
doc = json.loads(json.dumps(self.world.doc))
doc["version"] = 39