City work orders

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rope 2026-10-28 13:59:14 +00:00
parent 4b0724e6e5
commit e1d37ede87
4 changed files with 243 additions and 47 deletions

View file

@ -19,6 +19,7 @@ let draft = null; // a work order being written
let seenOrders = null; // order ids already known, so only new ones toast
let deliveredSeen = {}; // how much of each order had arrived
let earnedSeen = {}; // gold each order had paid you for reagents
let favourSeen = {}; // favour each city order had earned you
let pickedClass = null; // registration
let queueOpen = false; // the queue panel under the top bar
const tabs = {}; // skill -> selected recipe tab
@ -2242,7 +2243,8 @@ function buildingCard(key, b) {
const stores = Math.min(missing, gold ? city.treasury : city.stockpile[id] || 0);
const chip = costChip(gold ? COIN : item(id).icon, gold ? "Gold" : item(id).name, need, need - missing, !gold && id);
return `<div class="fund-row">${chip}<span class="btns">
${mine ? `<button class="btn sm gold" data-fund="${key}" data-item="${id}" data-source="bank" title="From your ${gold ? "purse" : "bank"}, earns favour">Give ${fmt(mine)}</button>` : ""}
${cityOrderButton(id, missing, key)}
${mine ?`<button class="btn sm gold" data-fund="${key}" data-item="${id}" data-source="bank" title="From your ${gold ? "purse" : "bank"}, earns favour">Give ${fmt(mine)}</button>` : ""}
${stores ? `<button class="btn sm" data-fund="${key}" data-item="${id}" data-source="city" title="From the city's ${gold ? "treasury" : "stockpile"}">${gold ? "Treasury" : "Stockpile"} ${fmt(stores)}</button>` : ""}
</span></div>`;
}).join("")}</div>`;
@ -2256,6 +2258,20 @@ function buildingCard(key, b) {
</div></div>${body}</div>`;
}
// A city work order for what a building still needs of an item, delivered into its fund (a fitting
// fitted): what's not in the stockpile or on order yet. Once it's all on order, the button shows those orders.
function cityOrderButton(id, missing, key) {
if (!C.order_recipes[id] && !C.order_raw[id]) return "";
const ordered = (S.orders || []).filter((o) => o.city && o.status === "open" && o.item === id)
.reduce((n, o) => n + o.qty - o.delivered, 0);
const want = Math.min(missing, (fundNeeds()[id]?.qty || 0) - ordered);
if (want > 0) {
return `<button class="btn sm" data-order-for="${id}" data-qty="${want}" data-for-fund="${key}"
title="${esc(`City work order: ${item(id).name} ×${fmt(want)} for the ${C.buildings[key].name.toLowerCase()}`)}">${glyph("claw-hammer", "#e3c060")}</button>`;
}
return ordered ? `<button class="btn sm" data-order-show title="${esc(`On order: ${fmt(ordered)}`)}">${ORDER_ICON()} ${fmt(ordered)}</button>` : "";
}
// Fittings Construction makes for this building: fitted, ready to fit, or waiting on a level.
function fittingRows(key, b) {
const rows = C.fittings.filter((f) => f.building === key).map((f) => {
@ -2270,7 +2286,7 @@ function fittingRows(key, b) {
state = mine || stores
? `${mine ? `<button class="btn sm gold" data-install="${f.id}" data-source="bank" title="From your bank, earns favour">Fit</button>` : ""}
${stores ? `<button class="btn sm" data-install="${f.id}" data-source="city" title="From the city's stockpile">Fit from stockpile</button>` : ""}`
: `<span class="muted small">Construction ${recipe.level}</span>`;
: `<span class="muted small">Construction ${recipe.level}</span>${cityOrderButton(f.id, 1, key)}`;
}
return `<div class="fitting-row"><span class="fit-ico">${it.icon}</span>
<span class="fitting-text"><b${goto(f.id)} title="${esc(gotoTip(f.id).trim())}">${esc(f.name)}</b> <span class="muted small">${esc(f.effect)}</span></span>
@ -2401,9 +2417,9 @@ function raidPrep(ev) {
const ORDER_ICON = () => glyph("scroll-unfurled", "#e3c060");
// An order you could pick up: open, not yours, not claimed by someone else, and your level is high enough.
// An order you could pick up: open, not yours (a city order is anyone's), not claimed by someone else, and your level is high enough.
function canTake(o) {
if (o.status !== "open" || o.by === S.uid) return false;
if (o.status !== "open" || o.by === S.uid && !o.city) return false;
if (o.kind === "craft" && o.claimed && o.claimed !== S.uid) return false;
return S.me.levels[o.skill] >= o.level;
}
@ -2414,17 +2430,21 @@ function orderNews(state) {
if (seenOrders) {
for (const o of orders) {
if (!seenOrders.has(o.id) && o.by !== uid && state.me && state.me.levels[o.skill] >= o.level && o.status === "open") {
toast(`📜 ${item(o.item).name}${o.qty > 1 ? ` ×${fmt(o.qty)}` : ""} · ${fmt(o.reward)} gold`, "level");
toast(`📜 ${o.city ? "🏘️ " : ""}${item(o.item).name}${o.qty > 1 ? ` ×${fmt(o.qty)}` : ""}${o.reward || !o.city ? ` · ${fmt(o.reward)} gold` : ""}`, "level");
}
const was = deliveredSeen[o.id];
if (o.by === uid && was !== undefined && o.delivered > was) toast(`📦 ${item(o.item).name} ×${fmt(o.delivered - was)}`, "good");
if (o.by === uid && !o.city && was !== undefined && o.delivered > was) toast(`📦 ${item(o.item).name} ×${fmt(o.delivered - was)}`, "good");
const got = (o.earned || {})[uid] || 0, had = earnedSeen[o.id];
if (had !== undefined && got > had) toast(`🤝 ${item(o.item).name} · +${fmt(got - had)} gold`, "good");
// Favour from your reagents in someone else's make: your own makes show in the summary.
const fav = Math.round((o.favour || {})[uid] || 0), favHad = favourSeen[o.id];
if (favHad !== undefined && fav > favHad && o.claimed !== uid) toast(`🏘️ ${item(o.item).name} · +${fmt(fav - favHad)} favour`, "good");
}
}
seenOrders = new Set(orders.map((o) => o.id));
deliveredSeen = Object.fromEntries(orders.map((o) => [o.id, o.delivered]));
earnedSeen = Object.fromEntries(orders.map((o) => [o.id, (o.earned || {})[uid] || 0]));
favourSeen = Object.fromEntries(orders.map((o) => [o.id, Math.round((o.favour || {})[uid] || 0)]));
}
// What one run of a recipe takes right now: a lit fire pays its log (the forge a coal too).
@ -2459,7 +2479,9 @@ function orderSlots(o) {
const give = Math.min(have, need - pool);
// What putting them in pays you once they're crafted: your value's slice of the reagents' share.
const share = o.by === S.uid ? 0 : Math.round(o.reward / o.qty * out * C.input_share * item(id).value / worth * give);
const fill = give ? `${tip} · +${fmt(give)}${share ? ` → ${fmt(share)} gold` : ""}` : `${tip} · 🎒 0${gotoTip(id)}`;
// On a city order they count as donated: their worth in favour.
const pays = [share ? `${fmt(share)} gold` : "", o.city ? `${fmt(item(id).value * give)} favour` : ""].filter(Boolean).join(" + ");
const fill = give ? `${tip} · +${fmt(give)}${pays ? ` → ${pays}` : ""}` : `${tip} · 🎒 0${gotoTip(id)}`;
// None to give: the slot links to where it's made instead.
const click = give ? `data-order-fill="${o.id}" data-item="${id}"` : sourceOf(id) ? goto(id) : "disabled";
return `<div class="ro-slot open"><button class="doll-slot empty-req" ${click} title="${esc(fill)}">${item(id).icon}</button>
@ -2475,34 +2497,45 @@ function orderCard(o) {
? ` · ${Object.entries(o.from).map(([uid, n]) => `${esc(playerName(uid))} ${fmt(n)}`).join(", ")}` : ""}</div>
<div class="bar"><span style="width:${o.delivered / o.qty * 100}%"></span></div>` : "";
let buttons = "", status = "";
// Your city order is still yours to cancel, and anyone's to fill: you too.
const cancel = mine && o.city ? `<button class="btn danger" data-order-cancel="${o.id}">Cancel</button>` : "";
if (o.status !== "open") {
status = `<span class="tag ${o.status === "done" ? "good" : ""}">${{ done: "Done", cancelled: "Cancelled", expired: "Expired" }[o.status]}</span>`;
if (mine && !o.held) buttons = `<button class="btn" data-order-remove="${o.id}">Remove</button>`;
} else if (mine) {
} else if (mine && !o.city) {
buttons = `<button class="btn danger" data-order-cancel="${o.id}">Cancel</button>`;
if (o.claimed) status = `<span class="tag">${esc(playerName(o.claimed))}</span>`;
} else if (o.kind === "gather") {
buttons = `<button class="btn primary" data-order-deliver="${o.id}" ${have ? "" : "disabled"}>Deliver ${fmt(Math.min(have, left))}</button>`;
buttons = `${cancel}<button class="btn primary" data-order-deliver="${o.id}" ${have ? "" : "disabled"}>Deliver ${fmt(Math.min(have, left))}</button>`;
} else if (o.claimed && o.claimed !== S.uid) {
status = `<span class="tag">${esc(playerName(o.claimed))}</span>`;
buttons = cancel;
} else {
const working = S.me.action?.order === o.id;
const why = short ? `Needs ${skill.name.toLowerCase()} ${o.level}` : S.me.blocked[o.action] && !working
&& !S.me.blocked[o.action].startsWith("Out of") ? S.me.blocked[o.action] : "";
buttons = `${o.claimed === S.uid ? `<button class="btn" data-order-drop="${o.id}">Drop</button>` : ""}
buttons = `${cancel}${o.claimed === S.uid || o.city && working ? `<button class="btn" data-order-drop="${o.id}">Drop</button>` : ""}
${have ? `<button class="btn" data-order-deliver="${o.id}">Deliver ${fmt(Math.min(have, left))}</button>` : ""}
${working ? `<span class="tag gold">Crafting</span>` : `<button class="btn primary" data-order-work="${o.id}" ${why ? `disabled title="${esc(why)}"` : ""}>Craft</button>`}`;
}
const reward = o.kind === "gather" ? `${fmt(Math.round(o.reward / o.qty))} each` : fmt(o.reward);
// A city order pays its worth in favour, shared by whoever's reagents and work went in.
const favour = o.city ? `<span class="item-chip ro-reward" title="Favour, shared">${FAVOUR} ${fmt(it.value * o.qty)}</span>` : "";
const favoured = Object.entries(o.favour || {}).filter(([, n]) => Math.round(n));
// A city order is worked by anyone, several at once: who's at it right now.
const crafting = o.city && o.status === "open" ? S.players.filter((p) => p.order === o.id) : [];
const city = o.fund ? C.buildings[o.fund].name : "City";
return `<div class="action order">
<div class="top"><div class="ico">${it.icon}</div><div>
<div class="name">${o.kind === "gather" ? "Wanted: " : ""}${esc(it.name)}${o.qty > 1 ? ` ×${fmt(o.qty)}` : ""}</div>
<div class="sub">${mine ? "Yours" : esc(playerName(o.by))} · ${ago(o.at)}${o.held ? ` · ${fmt(o.held)} waiting for bank space` : ""}</div></div>
<span class="ro-req ${short && !mine ? "bad" : ""}" title="${esc(`${skill.name} ${o.level}`)}">${skill.icon} ${o.level}</span></div>
<div class="sub">${o.city ? `🏘️ ${esc(city)} · ${mine ? "yours" : esc(playerName(o.by))}` : mine ? "Yours" : esc(playerName(o.by))} · ${ago(o.at)}${o.held ? ` · ${fmt(o.held)} waiting for bank space` : ""}</div></div>
<span class="ro-req ${short && (!mine || o.city) ? "bad" : ""}" title="${esc(`${skill.name} ${o.level}`)}">${skill.icon} ${o.level}</span></div>
${o.kind === "craft" ? `<div class="ro-slots">${orderSlots(o)}</div>` : ""}
${Object.keys(o.earned || {}).length ? `<div class="small muted">🤝 ${Object.entries(o.earned).map(([uid, n]) => `${esc(playerName(uid))} ${fmt(n)}`).join(", ")}</div>` : ""}
${favoured.length ? `<div class="small muted">${FAVOUR} ${favoured.map(([uid, n]) => `${esc(playerName(uid))} ${fmt(Math.round(n))}`).join(", ")}</div>` : ""}
${crafting.length ? `<div class="small muted" title="Crafting now">${skill.icon} ${crafting.map((p) => esc(p.name)).join(", ")}</div>` : ""}
${progress}
<div class="foot"><span class="item-chip ro-reward">${COIN} ${reward}</span>${status}<span class="btns">${buttons}</span></div>
<div class="foot">${o.reward || !o.city ? `<span class="item-chip ro-reward">${COIN} ${reward}</span>` : ""}${favour}${status}<span class="btns">${buttons}</span></div>
</div>`;
}
@ -2517,7 +2550,7 @@ function orderPicker(list) {
const q = (d.search || "").trim().toLowerCase();
const shown = q ? actions.filter(([id]) => item(id).name.toLowerCase().includes(q))
: inSkill.filter(([, a]) => !groups.length || a.group === d.group);
const others = S.players.filter((p) => p.id !== S.uid);
const others = S.players.filter((p) => p.id !== S.uid || d.city); // a city order you can make yourself
const tiles = shown.sort((x, y) => x[1].level - y[1].level).map(([id, a]) => {
const makers = others.filter((p) => p.levels[a.skill] >= a.level).length;
const tip = `${item(id).name} · ${skillOf(a.skill).name} ${a.level}${d.kind === "craft" ? ` · 👥 ${makers}` : ""}`;
@ -2531,12 +2564,18 @@ function orderPicker(list) {
<div class="swap-grid pick-grid">${tiles || `<span class="muted small">—</span>`}</div></div>`;
}
// What the draft can ask for: a city order only what the city has a use for.
function orderList() {
const list = draft.kind === "craft" ? C.order_recipes : C.order_raw;
return draft.city ? Object.fromEntries(Object.entries(list).filter(([id]) => C.city_uses[id].length)) : list;
}
function orderComposer() {
const d = draft, list = d.kind === "craft" ? C.order_recipes : C.order_raw;
const d = draft, list = orderList();
let slots = "", maker = "";
const a = d.item && C.actions.find((x) => x.id === list[d.item]);
if (a) {
const smiths = S.players.filter((p) => p.id !== S.uid && p.levels[a.skill] >= a.level).length;
const smiths = S.players.filter((p) => (p.id !== S.uid || d.city) && p.levels[a.skill] >= a.level).length;
maker = `<div class="small muted" title="${esc(`${skillOf(a.skill).name} ${a.level} · ${smiths} can make it`)}">${skillOf(a.skill).icon} ${a.level} · 👥 ${smiths}</div>`;
if (d.kind === "craft") {
const runs = Math.ceil(d.qty / a.outputs[d.item]), ins = runInputs(a);
@ -2551,20 +2590,21 @@ function orderComposer() {
}
}
const gold = d.kind === "craft" ? d.reward : d.reward * d.qty;
const why = !a ? "" : d.qty > C.order_max[d.kind] ? `Max ${fmt(C.order_max[d.kind])}`
: gold > S.me.gold ? `Gold ${fmt(S.me.gold)} / ${fmt(gold)}` : "";
const why = !a ? "" : gold > S.me.gold ? `Gold ${fmt(S.me.gold)} / ${fmt(gold)}` : "";
const fund = d.fund && `<div class="small muted" title="Delivered into its fund">🏘️ ${esc(C.buildings[d.fund].name)}</div>`;
return `<div class="panel compose">
<div class="tabs" style="margin:0 0 14px"><button class="tab ${d.kind === "craft" ? "on" : ""}" data-draft-kind="craft">Craft</button>
<button class="tab ${d.kind === "gather" ? "on" : ""}" data-draft-kind="gather">Materials</button></div>
<button class="tab ${d.kind === "gather" ? "on" : ""}" data-draft-kind="gather">Materials</button>
<button class="tab ${d.city ? "on" : ""}" data-draft-city title="For the city: goes to the stockpile, favour shared by whoever helps">🏘️ City</button></div>
${orderPicker(list)}
<div class="compose-grid">
<div>${a ? `<div class="ro-pick"><span class="doll-slot on">${item(d.item).icon}</span><div><b>${esc(item(d.item).name)}</b>${maker}</div></div>`
<div>${a ? `<div class="ro-pick"><span class="doll-slot on">${item(d.item).icon}</span><div><b>${esc(item(d.item).name)}</b>${maker}${fund || ""}</div></div>`
: `<div class="ro-pick"><span class="doll-slot empty-req"></span><span class="muted">—</span></div>`}
<div class="stepper">×<input type="number" min="1" max="${C.order_max[d.kind]}" value="${d.qty}" data-draft="qty">
<span class="muted small">max ${fmt(C.order_max[d.kind])}</span></div></div>
<div class="stepper">×<input type="number" min="1" value="${d.qty}" data-draft="qty"></div></div>
<div>${slots ? `<div class="ro-slots big">${slots}</div>` : ""}</div>
<div><div class="reward-input">${COIN}<input type="number" min="0" value="${d.reward}" data-draft="reward">
${d.kind === "gather" ? `<span class="muted small">each · ${fmt(gold)}</span>` : ""}</div>
${d.city && a ? `<div class="small muted" title="Favour, shared">${FAVOUR} ${fmt(item(d.item).value * d.qty)}</div>` : ""}
<div class="btns"><button class="btn primary" data-order-post ${a && !why ? "" : "disabled"}>Post</button>
${why ? `<span class="why">${esc(why)}</span>` : ""}</div></div>
</div></div>`;
@ -3045,11 +3085,25 @@ document.addEventListener("click", (e) => {
if ("queueSkip" in d) return act("/api/queue/skip", {});
if ("queueClear" in d) return act("/api/queue/clear", {});
if (d.join) { tagging = true; return act("/api/join", { id: d.join }); }
if (d.orderFor) {
// From a town square shortfall: a city order for that item and amount, ready to post.
draft = { kind: C.order_recipes[d.orderFor] ? "craft" : "gather", item: "", qty: Number(d.qty), provide: [], reward: 0, city: true };
pickOrderItem(d.orderFor);
draft.fund = d.forFund;
return go("orders");
}
if ("orderShow" in d) { orderTab = "open"; return go("orders"); }
if ("orderNew" in d) {
draft = draft ? null : { kind: "craft", item: "", qty: 1, provide: [], reward: 0 };
return render();
}
if (d.draftKind) { draft = { kind: d.draftKind, item: "", qty: 1, provide: [], reward: 0 }; clearSearch(); return render(); }
if (d.draftKind) { draft = { kind: d.draftKind, item: "", qty: 1, provide: [], reward: 0, city: draft.city }; clearSearch(); return render(); }
if ("draftCity" in d) {
draft.city = !draft.city;
if (!draft.city) draft.fund = null;
pickOrderItem(draft.item); // keeps it if the city can use it; a city order starts at no gold
return render();
}
if (d.draftSkill) { draft.skill = d.draftSkill; draft.search = ""; clearSearch(); return render(); }
if (d.draftGroup) { draft.group = d.draftGroup; return render(); }
if (d.draftItem) {
@ -3067,7 +3121,7 @@ document.addEventListener("click", (e) => {
// A fire lit since picking may now pay a reagent that was ticked.
const a = draft.kind === "craft" && C.actions.find((x) => x.id === C.order_recipes[draft.item]);
const provide = a ? draft.provide.filter((id) => runInputs(a)[id]) : draft.provide;
const body = { kind: draft.kind, item: draft.item, qty: draft.qty, provide, reward: draft.reward };
const body = { kind: draft.kind, item: draft.item, qty: draft.qty, provide, reward: draft.reward, city: !!draft.city, fund: draft.fund || null };
return api("/api/order", body).then((state) => { draft = null; orderTab = "yours"; accept(state); }).catch((err) => toast(err.message, "bad"));
}
if (d.otab) { orderTab = d.otab; return render(); }
@ -3150,11 +3204,12 @@ function clearSearch() {
}
function pickOrderItem(id) {
const list = draft.kind === "craft" ? C.order_recipes : C.order_raw;
const list = orderList();
if (id !== draft.item) draft.fund = null; // the building asked for that item, not this one
draft.item = list[id] ? id : "";
draft.provide = [];
// A starting price: what the item's worth, each.
draft.reward = draft.item ? item(draft.item).value * (draft.kind === "craft" ? draft.qty : 1) : 0;
// A starting price: what the item's worth, each. The city pays in favour instead.
draft.reward = draft.item && !draft.city ? item(draft.item).value * (draft.kind === "craft" ? draft.qty : 1) : 0;
}
document.addEventListener("input", (e) => {
@ -3182,7 +3237,6 @@ document.addEventListener("change", (e) => {
} else {
const before = draft.qty;
draft[key] = Math.max(key === "qty" ? 1 : 0, Math.floor(Number(e.target.value) || 0));
if (key === "qty") draft.qty = Math.min(draft.qty, C.order_max[draft.kind]);
// A crafting order's reward is the total: it follows the quantity at the same price each.
if (key === "qty" && draft.kind === "craft") draft.reward = Math.round(draft.reward / before * draft.qty);
if (key === "qty" && draft.kind === "craft" && draft.item) {