Skip to content

Commit e8dee9c

Browse files
authored
Merge pull request #29 from ghlearn/arilivigni-no-tie-scoring
Never tie scores: reachable 5,000 rubric, score_taken, board-first serving
2 parents ff984b2 + f3aed12 commit e8dee9c

25 files changed

Lines changed: 1364 additions & 297 deletions

‎.github/extensions/commit-and-sip/booth-engine.mjs‎

Lines changed: 233 additions & 13 deletions
Large diffs are not rendered by default.

‎.github/extensions/commit-and-sip/booth-panel.mjs‎

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,28 @@ export class BoothPanel {
3030
// strand the station. Reconcile instead of trusting the cursor: a run that is
3131
// gone or already finished releases it, and the next attendee can start.
3232
async currentRun() {
33-
let state;
34-
try {
35-
state = await this.engine.get(this.runId);
36-
} catch (error) {
37-
if (error?.code !== "run_missing") throw error;
38-
this.runId = null;
39-
return null;
40-
}
41-
if (state.phase === "complete") {
42-
this.runId = null;
43-
return null;
33+
for (;;) {
34+
const runId = this.runId;
35+
if (!runId) return null;
36+
let state;
37+
try {
38+
state = await this.engine.get(runId);
39+
} catch (error) {
40+
if (error?.code !== "run_missing") throw error;
41+
}
42+
if (this.runId !== runId) continue;
43+
if (!state || state.phase === "complete") {
44+
this.runId = null;
45+
return null;
46+
}
47+
return state;
4448
}
45-
return state;
4649
}
4750

48-
// The idle screen polls every few seconds, and that is the only thing still
49-
// running once an attendee has handed over. So it also starts a retry of
50-
// any publication still owed, at most every 30 seconds, without waiting for
51-
// it: the screen never slows down for the network. The interval is kept on
52-
// the engine every station shares, not on this panel: several stations'
53-
// idle polls, staggered, would otherwise start a sweep every few seconds.
51+
// Screens poll every few seconds, including after hand-over. A poll also
52+
// starts a retry of any publication still owed, at most every 30 seconds,
53+
// without waiting for it. The interval is shared across this booth's
54+
// stations so staggered polls do not start extra sweeps.
5455
sweepInBackground(now = Date.now()) {
5556
if (now - (this.engine.lastPublicationSweep ?? -Infinity) < SWEEP_INTERVAL_MS) return;
5657
this.engine.lastPublicationSweep = now;
@@ -93,21 +94,30 @@ export class BoothPanel {
9394
}
9495
if (this.runId) await this.currentRun();
9596
requireValue(this.runId, "not_started", "Start an order at this station first.", 409);
96-
const state = await this.engine.dispatch(this.runId, action, input);
97-
// Serving commits locally first; publishing to the event leaderboard is a
98-
// separate step that must not be able to fail or slow the attendee's
99-
// submission. It runs in the background: the screen answers at once with
100-
// the drink "still being confirmed", and polls until the service answers.
101-
if (action === "submit_name") {
102-
this.backgroundPublish = this.engine.publish(this.runId).catch(() => {});
103-
return this.decorate(state);
97+
// With an event leaderboard, submit_name answers only once the board has
98+
// admitted the drink (and it is served) or said why not, so there is no
99+
// background publish left to start here.
100+
const runId = this.runId;
101+
let state;
102+
try {
103+
state = await this.engine.dispatch(runId, action, input);
104+
} catch (error) {
105+
if (this.runId !== runId || !(await this.currentRun()) || this.runId !== runId) return this.get();
106+
throw error;
104107
}
108+
if (this.runId !== runId) return this.get();
105109
// Completing hands the station to the next attendee. The run itself stays
106110
// finished in the store; only this panel's cursor is released.
107111
if (action === "complete") {
108112
this.runId = null;
109113
return { phase: "idle", ...(await this.engine.house()), justCompleted: state.handle };
110114
}
111-
return this.decorate(state);
115+
if (state.phase === "complete") {
116+
this.runId = null;
117+
return this.get();
118+
}
119+
const current = await this.currentRun();
120+
if (!current || this.runId !== runId) return this.get();
121+
return this.decorate(current);
112122
}
113123
}

‎.github/extensions/commit-and-sip/canvas.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function boothCanvasDefinition({ engine, guarded = fn => fn(), reportErro
1818
placement: { enum: PLACEMENTS }
1919
},
2020
required: ["name"], additionalProperties: false
21-
}, "Add the attendee's invented drink to the house menu and score it. A declared mascot or placement is checked against the name."],
21+
}, "Score the attendee's invented drink and add it to the house menu. With an event leaderboard configured, it is served only once the board admits it: a score another drink holds (score_taken) or an unreachable board (leaderboard_unreachable) keeps the attendee naming. A declared mascot or placement is checked against the name."],
2222
complete: [empty, "Finish this attendee and clear the station for the next one. The drink stays on the menu and leaderboard."],
2323
refresh: [empty, "Read the current station state, house menu, and leaderboard."]
2424
};

‎.github/extensions/commit-and-sip/renderer/booth.html‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ <h1>Invent your drink.</h1>
5050
<button id="name-submit" class="primary" type="submit">Add it to the menu</button>
5151
</fieldset>
5252
</form>
53+
<p id="name-checking" class="notice" role="status" hidden>Checking your drink and adding it to the menu...</p>
5354
<p id="name-problem" class="notice error-notice" role="alert" hidden></p>
55+
<p id="name-board-feedback" class="notice error-notice" role="status" hidden></p>
5456
</section>
5557

5658
<!-- Scored and on the menu. -->

‎.github/extensions/commit-and-sip/renderer/booth.js‎

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
const VIEWS = ["idle", "naming", "served"];
66
let busy = false;
77
let phase = null;
8-
// The drink is published in the background, so the served screen keeps
9-
// checking while the outcome is still open: pending, or failed and due for
10-
// a retry (the panel retries on its poll). Confirmed and rejected are final.
11-
let confirming = false;
8+
let backgroundLoading = false;
9+
let revision = 0;
10+
let nameErrorCode = null;
1211

1312
function fail(message) {
1413
$("booth-error-message").textContent = message;
@@ -36,7 +35,10 @@
3635
let data;
3736
try { data = await response.json(); }
3837
catch { throw new Error(`The booth returned an unreadable response (HTTP ${response.status}). Refresh the connection.`); }
39-
if (!response.ok) throw new Error(data.error?.message || `The booth returned HTTP ${response.status}. Refresh the connection.`);
38+
if (!response.ok) throw Object.assign(
39+
new Error(data.error?.message || `The booth returned HTTP ${response.status}. Refresh the connection.`),
40+
{ code: data.error?.code }
41+
);
4042
return data;
4143
} catch (error) {
4244
if (error.name === "AbortError") throw new Error("The booth took too long to respond. Refresh the connection before trying again; your drink may already have been saved.");
@@ -97,6 +99,14 @@
9799

98100
function fillNaming(state) {
99101
$("naming-handle").textContent = state.handle;
102+
const feedback = state.admissionFeedback;
103+
$("name-board-feedback").hidden = !feedback;
104+
$("name-board-feedback").textContent = feedback ? `Earlier attempt: ${feedback.message}` : "";
105+
if (feedback && (nameErrorCode === "leaderboard_unreachable"
106+
|| (nameErrorCode === feedback.code && $("name-problem").textContent === feedback.message))) {
107+
$("name-problem").hidden = true;
108+
nameErrorCode = null;
109+
}
100110
}
101111

102112
function fillServed(state) {
@@ -163,7 +173,6 @@
163173

164174
function render(state) {
165175
phase = state.phase === "complete" ? "served" : state.phase;
166-
confirming = state.phase === "served" && ["pending", "failed"].includes(state.sync?.state) && !state.removed;
167176
for (const view of VIEWS) $(`view-${view}`).hidden = view !== phase;
168177
fillMenu(state);
169178
if (phase === "naming") fillNaming(state);
@@ -173,13 +182,30 @@
173182
? `Thanks, ${state.justCompleted}. The counter is clear and ready for the next barista.`
174183
: "Step up and invent a coffee for the house menu.";
175184
$("name-problem").hidden = true;
185+
$("name-board-feedback").hidden = true;
186+
nameErrorCode = null;
176187
$("pick-name").value = "";
177188
$("name-form").reset();
178189
}
179190
}
180191

181192
async function load(explicit = true) {
182193
if (busy) return;
194+
if (!explicit) {
195+
if (backgroundLoading) return;
196+
backgroundLoading = true;
197+
const current = revision;
198+
try {
199+
const state = await request("/api/state");
200+
if (!busy && current === revision) render(state);
201+
} catch (error) {
202+
if (!busy && current === revision) fail(error.message);
203+
} finally {
204+
backgroundLoading = false;
205+
}
206+
return;
207+
}
208+
revision += 1;
183209
busy = true;
184210
controls();
185211
try {
@@ -196,6 +222,7 @@
196222

197223
async function act(action, input = {}) {
198224
if (busy) return;
225+
revision += 1;
199226
busy = true;
200227
controls();
201228
try {
@@ -219,6 +246,12 @@
219246
event.preventDefault();
220247
if (busy || !$("name-form").reportValidity()) return;
221248
$("name-problem").hidden = true;
249+
$("name-board-feedback").hidden = true;
250+
nameErrorCode = null;
251+
// A booth with an event leaderboard serves only once the board admits the
252+
// drink, so the answer can take a few seconds. Say what is happening.
253+
$("name-checking").hidden = false;
254+
revision += 1;
222255
busy = true;
223256
controls();
224257
try {
@@ -230,16 +263,18 @@
230263
// with what they typed rather than sending them back to the counter.
231264
$("name-problem").textContent = error.message;
232265
$("name-problem").hidden = false;
266+
nameErrorCode = error.code ?? null;
233267
} finally {
268+
$("name-checking").hidden = true;
234269
busy = false;
235270
controls();
236271
}
237272
});
238273

239274
void load();
240275
window.setInterval(() => {
241-
// Keep the menu and leaderboard current between attendees without
242-
// interrupting anyone mid-typing.
243-
if (!document.hidden && (phase === "idle" || confirming)) void load(false);
276+
// Reconcile background settlements and staff closures without disabling
277+
// or resetting the naming form while someone is typing.
278+
if (!document.hidden && VIEWS.includes(phase)) void load(false);
244279
}, 5000);
245280
})();

‎.github/extensions/commit-and-sip/services/booth-menu.mjs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,21 @@ export function findDrink(menu, id) {
2121
return menu.find(entry => entry.id === id) ?? null;
2222
}
2323

24-
export function addDrink(menu, { rawName, rules, runId, handle, choice = {}, removedIds = [], now = new Date().toISOString() }) {
24+
// The attendee's way forward when their score is taken: a different name
25+
// almost always lands on a different total, because Letter value moves in
26+
// single points.
27+
export const SCORE_TAKEN_MESSAGE = (name, score, where = "the house menu") =>
28+
`${name} scored ${score}, but another drink on ${where} already has that score, and no two drinks may share one. ` +
29+
"Tweak your name (add, swap, or respell a word) and submit again.";
30+
31+
// Every check a drink must pass and the entry it would become, without adding
32+
// it. A booth with an event leaderboard serves only once the board admits the
33+
// drink, so it prepares here, asks the board, and pushes afterwards.
34+
//
35+
// `uniqueScore` refuses a score another drink on this menu already holds.
36+
// Booths use it even with an event leaderboard because their menus may still
37+
// contain drinks served while publishing was disabled.
38+
export function prepareDrink(menu, { rawName, rules, runId, handle, choice = {}, removedIds = [], uniqueScore = false, now = new Date().toISOString() }) {
2539
requireValue(Array.isArray(menu), "menu_invalid", "The house menu is unavailable. Ask booth staff to restore it.");
2640
requireValue(Array.isArray(removedIds), "menu_invalid", "The removal record is unavailable. Ask booth staff to restore it.");
2741
requireValue(typeof runId === "string" && runId.length > 0, "invalid_run", "A run ID is required to add a drink.", 400);
@@ -59,6 +73,13 @@ export function addDrink(menu, { rawName, rules, runId, handle, choice = {}, rem
5973
const drink = buildAttendeeOrder(rawName, rules, runId);
6074
const entry = { ...drink, breakdown, createdAt: now, example: false, handle, mascot, placement, runId, score };
6175
requireValue(entry.id === id, "menu_invalid", "The drink ID changed between checks. Try the name again.");
76+
requireValue(!uniqueScore || !menu.some(item => !item.example && item.score === score),
77+
"score_taken", SCORE_TAKEN_MESSAGE(name, score), 409);
78+
return entry;
79+
}
80+
81+
export function addDrink(menu, options) {
82+
const entry = prepareDrink(menu, options);
6283
menu.push(entry);
6384
return entry;
6485
}
@@ -95,7 +116,9 @@ export function removeDrink(menu, removals, id, { removedBy, reason, now = new D
95116
}
96117

97118
// Every drink gets its own place, so an event cannot end in a tie for first.
98-
// An equal score goes to the drink that got there first: on the event board
119+
// New drinks never share a score (the leaderboard service, or a standalone
120+
// booth, refuses a taken one), but a menu or board kept from before that rule
121+
// can still hold a tie. An equal score goes to the drink that got there first: on the event board
99122
// that is when the service admitted it, at a booth when the booth served it.
100123
// The ID only separates two drinks stamped in the same millisecond, so the
101124
// order never depends on the order the menu happens to be stored in. A row

‎.github/extensions/commit-and-sip/services/leaderboard.mjs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ import { requireValue } from "../domain.mjs";
33

44
// The booth is authoritative for its own menu; the event leaderboard is a
55
// separate service that no booth can see into. Everything here is a client
6-
// seam: the local entry is always durable first, and a submission that fails
7-
// can never cost an attendee the drink they invented or the score they earned.
6+
// seam. A new drink is served only once the board admits it (see submitName in
7+
// booth-engine.mjs); the sync states below track drinks already on the menu:
8+
// ones served before this booth was configured, sent again by a rebuild, or
9+
// left pending by a booth build that served first.
810
export const SYNC_STATES = ["disabled", "pending", "confirmed", "failed", "rejected"];
911

1012
// Refusals that retrying cannot change: another attendee already holds the
11-
// name, staff took it down, or both handles this publication can use (its
12-
// own and its canonical form, the same two on every retry) are taken. They
13+
// name, staff took it down, both handles this publication can use (its
14+
// own and its canonical form, the same two on every retry) are taken, or
15+
// another drink already holds its score. They
1316
// become "rejected" and are never sent again, not even by a rebuild.
1417
// Otherwise, after the service lost its data, the booth that lost a clash
1518
// could publish first and take the name from the attendee who legitimately
1619
// holds it, and a handle clash would be resent forever.
17-
export const TERMINAL_REJECTIONS = ["duplicate_drink", "handle_taken", "unavailable_drink"];
20+
export const TERMINAL_REJECTIONS = ["duplicate_drink", "handle_taken", "score_taken", "unavailable_drink"];
1821

1922
// A random value minted once per publication and kept with the run, so every
2023
// retry of one publication carries the same token. It lets the service tell a

‎.github/extensions/commit-and-sip/services/name-score.mjs‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import { validateCoffeeName } from "./coffee-name.mjs";
55
// so the same name always scores the same, a rank can be recomputed server-side
66
// from stored names, and staff can explain any score at the booth from the
77
// returned breakdown. No model call is involved; this is a rubric, not a judge.
8+
//
9+
// Every category maximum can be earned by one valid name at the same time
10+
// (a test proves it), so 5,000 is a real ceiling, not a sum on paper. Letter
11+
// value pays in single points, so scores are not stuck on steps of 50 and two
12+
// different names rarely land on the same total. Rarely is not never: a
13+
// finite range cannot give every name its own score, so the leaderboard and a
14+
// standalone booth refuse a drink whose score another drink already holds.
815
export const MAX_SCORE = 5000;
916

1017
// The seeded house drinks are worked examples shown to attendees. They are never
@@ -44,8 +51,8 @@ function alliterationPoints(name, mascot) {
4451

4552
function craftPoints(name) {
4653
const found = new Set(tokens(name).filter(token => COFFEE_TERMS.includes(token)));
47-
if (found.size >= 2) return { points: 900, found: [...found] };
48-
return { points: found.size === 1 ? 600 : 0, found: [...found] };
54+
if (found.size >= 2) return { points: 800, found: [...found] };
55+
return { points: found.size === 1 ? 500 : 0, found: [...found] };
4956
}
5057

5158
function wordplayPoints(name, mascot) {
@@ -55,25 +62,26 @@ function wordplayPoints(name, mascot) {
5562
// A blend fuses the mascot into a longer word, such as "Monachino", wherever
5663
// that word sits in the name.
5764
if (list.some(token => token !== mascot && token.includes(mascot))) {
58-
points += 450;
65+
points += 400;
5966
signals.push("blend");
6067
}
6168
// Internal rhyme: two words sharing a three-letter ending.
6269
const endings = list.filter(token => token.length >= 3).map(token => token.slice(-3));
6370
if (new Set(endings).size < endings.length) {
64-
points += 450;
71+
points += 400;
6572
signals.push("rhyme");
6673
}
67-
return { points: Math.min(points, 900), signals };
74+
return { points: Math.min(points, 800), signals };
6875
}
6976

70-
// Two to four words is the house shape. This band is deliberately flat so a
77+
// Two to five words is the house shape. This band is deliberately flat, and
78+
// wide enough to hold a mascot, two coffee terms and two invented words, so a
7179
// richer name is not penalised for having room to be inventive.
7280
function economyPoints(name) {
7381
const count = words(name).length;
74-
if (count >= 2 && count <= 4) return 600;
75-
if (count === 5) return 350;
76-
return count === 1 ? 250 : 150;
82+
if (count >= 2 && count <= 5) return 500;
83+
if (count === 6) return 300;
84+
return count === 1 ? 200 : 100;
7785
}
7886

7987
// Same unbroken-text rule validateCoffeeName uses, so crew credit never exceeds
@@ -95,7 +103,17 @@ function rarityPoints(name, mascots) {
95103
const inventive = new Set(tokens(name).filter(token =>
96104
!mascots.some(mascot => token.includes(mascot))
97105
&& !COFFEE_TERMS.includes(token) && !COMMON_WORDS.includes(token)));
98-
return { points: Math.min(inventive.size, 3) * 200, inventive: [...inventive] };
106+
return { points: Math.min(inventive.size, 2) * 250, inventive: [...inventive] };
107+
}
108+
109+
// Each letter is worth its place in the alphabet (A=1 ... Z=26), capped. The
110+
// only category that pays in single points, so it is what keeps two different
111+
// names off the same total. Digits, spaces and punctuation are worth nothing.
112+
const LETTER_VALUE_MAX = 400;
113+
function letterPoints(name) {
114+
const total = [...name.toLowerCase()].reduce((sum, char) =>
115+
sum + (char >= "a" && char <= "z" ? char.charCodeAt(0) - 96 : 0), 0);
116+
return Math.min(total, LETTER_VALUE_MAX);
99117
}
100118

101119
export function scoreCoffeeName(raw, rules) {
@@ -110,11 +128,12 @@ export function scoreCoffeeName(raw, rules) {
110128
const breakdown = [
111129
{ label: "Valid house name", max: 600, points: 600 },
112130
{ label: "Alliteration", max: 800, points: alliterationPoints(name, mascot) },
113-
{ label: "Coffee craft", max: 900, points: craft.points, detail: craft.found },
114-
{ label: "Wordplay", max: 900, points: play.points, detail: play.signals },
115-
{ label: "House shape", max: 600, points: economyPoints(name) },
131+
{ label: "Coffee craft", max: 800, points: craft.points, detail: craft.found },
132+
{ label: "Wordplay", max: 800, points: play.points, detail: play.signals },
133+
{ label: "House shape", max: 500, points: economyPoints(name) },
116134
{ label: "Mascot crew", max: 600, points: crewPoints(crew), detail: crew },
117-
{ label: "Invention", max: 600, points: rarity.points, detail: rarity.inventive },
135+
{ label: "Invention", max: 500, points: rarity.points, detail: rarity.inventive },
136+
{ label: "Letter value", max: LETTER_VALUE_MAX, points: letterPoints(name) },
118137
];
119138
const total = breakdown.reduce((sum, part) => sum + part.points, 0);
120139
// Clamped so a rubric change can never emit an out-of-range competition score.

0 commit comments

Comments
 (0)