The default rules and side for a new table are preferences in the panel, and the hall's selectors set the same
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jm2auWk6RP71CjaAb4FMoG
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
aa4f86bc56
commit
dcfbd38ae6
@@ -16,13 +16,10 @@
|
|||||||
let name = $state(playerName());
|
let name = $state(playerName());
|
||||||
let code = $state('');
|
let code = $state('');
|
||||||
/** The host's choices for the next table, from the game's declared options. */
|
/** The host's choices for the next table, from the game's declared options. */
|
||||||
// The host's last choices come back as the defaults; a regular does not re-pick them each visit.
|
// A new table starts from the preferences' table defaults; the hall's selectors set the same keys.
|
||||||
let options = $state<Record<string, string>>(
|
const options = $derived<Record<string, string>>(
|
||||||
Object.fromEntries((game.options ?? []).map((o) => [o.key, o.choices.some((c) => c.value === prefs[`table.${o.key}`]) ? String(prefs[`table.${o.key}`]) : o.default]))
|
Object.fromEntries((game.options ?? []).map((o) => [o.key, o.choices.some((c) => c.value === prefs[`table.${o.key}`]) ? String(prefs[`table.${o.key}`]) : o.default]))
|
||||||
);
|
);
|
||||||
$effect(() => {
|
|
||||||
for (const o of game.options ?? []) if (prefs[`table.${o.key}`] !== options[o.key]) setPref(`table.${o.key}`, options[o.key]);
|
|
||||||
});
|
|
||||||
let prefsOpen = $state(false);
|
let prefsOpen = $state(false);
|
||||||
|
|
||||||
/** The hall's still of the opening position, on whichever board the selector names. */
|
/** The hall's still of the opening position, on whichever board the selector names. */
|
||||||
@@ -192,7 +189,7 @@
|
|||||||
<div class="option-block">
|
<div class="option-block">
|
||||||
<label class="option">
|
<label class="option">
|
||||||
<span>{o.label}</span>
|
<span>{o.label}</span>
|
||||||
<select bind:value={options[o.key]}>
|
<select value={options[o.key]} onchange={(e) => setPref(`table.${o.key}`, e.currentTarget.value)}>
|
||||||
{#each o.choices as c (c.value)}<option value={c.value}>{c.label}</option>{/each}
|
{#each o.choices as c (c.value)}<option value={c.value}>{c.label}</option>{/each}
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
|
|||||||
+10
-3
@@ -21,7 +21,16 @@ export const KIT_PREFERENCES: Preference[] = [
|
|||||||
{ key: 'confirmMoves', label: 'Confirm a move before it is sent', kind: 'toggle', default: false }
|
{ key: 'confirmMoves', label: 'Confirm a move before it is sent', kind: 'toggle', default: false }
|
||||||
];
|
];
|
||||||
|
|
||||||
export const PREFERENCES: Preference[] = [...KIT_PREFERENCES, ...(game.preferences ?? [])];
|
/** Every table option is also a preference: the choice a new table starts with. The hall reads and writes the same keys. */
|
||||||
|
const TABLE_DEFAULTS: Preference[] = (game.options ?? []).map((o) => ({
|
||||||
|
key: `table.${o.key}`,
|
||||||
|
label: `${o.label}, for a new table`,
|
||||||
|
kind: 'choice',
|
||||||
|
choices: o.choices.map((c) => ({ value: c.value, label: c.label })),
|
||||||
|
default: o.default
|
||||||
|
}));
|
||||||
|
|
||||||
|
export const PREFERENCES: Preference[] = [...KIT_PREFERENCES, ...TABLE_DEFAULTS, ...(game.preferences ?? [])];
|
||||||
|
|
||||||
const KEY = 'hnefatafl:prefs';
|
const KEY = 'hnefatafl:prefs';
|
||||||
|
|
||||||
@@ -38,8 +47,6 @@ function load(): Record<string, boolean | string> {
|
|||||||
if (p.kind === 'toggle' && typeof v === 'boolean') base[p.key] = v;
|
if (p.kind === 'toggle' && typeof v === 'boolean') base[p.key] = v;
|
||||||
if (p.kind === 'choice' && typeof v === 'string' && p.choices?.some((c) => c.value === v)) base[p.key] = v;
|
if (p.kind === 'choice' && typeof v === 'string' && p.choices?.some((c) => c.value === v)) base[p.key] = v;
|
||||||
}
|
}
|
||||||
// The hall's last table options ride along under table.<key>, undeclared.
|
|
||||||
for (const [k, v] of Object.entries(saved)) if (k.startsWith('table.') && typeof v === 'string') base[k] = v;
|
|
||||||
} catch {
|
} catch {
|
||||||
// Without storage the defaults apply for this visit.
|
// Without storage the defaults apply for this visit.
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user