@hadoku/platform-wire 0.1.112 → 0.1.114
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/room-settings.d.ts +146 -0
- package/dist/room-settings.js +126 -0
- package/package.json +7 -3
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A room's settings, as a game declares them: each field's kind, default, label and lock, once,
|
|
3
|
+
* read by the server (what it stores, what a PATCH may change, and when) and by the page (the
|
|
4
|
+
* settings panel draws every field from the same definition). Knows no game.
|
|
5
|
+
*
|
|
6
|
+
* const BOMBERMAN_SETTINGS = defineRoomSettings({
|
|
7
|
+
* mode: choice(['classic', 'roguelike', 'sandbox'], 'classic', { label: 'Mode', lock: LIVE }),
|
|
8
|
+
* pickTimer: toggle(true, { label: 'Pick timer', lock: LIVE }),
|
|
9
|
+
* });
|
|
10
|
+
*
|
|
11
|
+
* ## Locks
|
|
12
|
+
*
|
|
13
|
+
* A field's `lock` names WHEN it cannot change, never why: `LIVE` ("while a match is live") is the
|
|
14
|
+
* kit's own, which a relay game's server answers (`@hadoku/game-server`'s `startRelayGame`). Any
|
|
15
|
+
* other name is a group the hosting room type answers itself (Magic's `event` and `rules`). A field
|
|
16
|
+
* with no lock changes whenever the leader likes.
|
|
17
|
+
*
|
|
18
|
+
* ## Refusals
|
|
19
|
+
*
|
|
20
|
+
* Every refusal names the field and what it must be (`settings.mode must be one of: classic, …`),
|
|
21
|
+
* and never echoes a value. A setting nobody declared is refused, never dropped.
|
|
22
|
+
*/
|
|
23
|
+
import { z } from 'zod';
|
|
24
|
+
/** The kit's own lock: a field that changes in the lobby only, never while a match is live. */
|
|
25
|
+
export declare const LIVE = "live";
|
|
26
|
+
/** What every field has. `T` is the value as it is stored, sent and read. */
|
|
27
|
+
interface FieldBase<T> {
|
|
28
|
+
/** A new room's value, and a stored room's when it has none. */
|
|
29
|
+
default: T;
|
|
30
|
+
/** What the panel calls it. */
|
|
31
|
+
label: string;
|
|
32
|
+
/** When it cannot change (see the header). Absent: whenever. */
|
|
33
|
+
lock?: string;
|
|
34
|
+
/** What a value must be, as a refusal says it after the field's name. */
|
|
35
|
+
must: string;
|
|
36
|
+
schema: z.ZodType<T>;
|
|
37
|
+
}
|
|
38
|
+
/** On or off. */
|
|
39
|
+
export interface ToggleField extends FieldBase<boolean> {
|
|
40
|
+
kind: 'toggle';
|
|
41
|
+
}
|
|
42
|
+
/** One of a closed list, drawn as a row of cards. */
|
|
43
|
+
export interface ChoiceField<T extends string = string> extends FieldBase<T> {
|
|
44
|
+
kind: 'choice';
|
|
45
|
+
choices: readonly {
|
|
46
|
+
value: T;
|
|
47
|
+
label: string;
|
|
48
|
+
}[];
|
|
49
|
+
}
|
|
50
|
+
/** A number in `[min, max]`, in steps of `step`. */
|
|
51
|
+
export interface NumberField extends FieldBase<number> {
|
|
52
|
+
kind: 'number';
|
|
53
|
+
min: number;
|
|
54
|
+
max: number;
|
|
55
|
+
step: number;
|
|
56
|
+
}
|
|
57
|
+
/** Anything else (Magic's pick timer, an object). The panel cannot draw it: the game draws its own row. */
|
|
58
|
+
export interface CustomField<T = unknown> extends FieldBase<T> {
|
|
59
|
+
kind: 'custom';
|
|
60
|
+
}
|
|
61
|
+
export type SettingField = ToggleField | ChoiceField | NumberField | CustomField;
|
|
62
|
+
export type SettingFields = Readonly<Record<string, SettingField>>;
|
|
63
|
+
/** The settings object a set of fields declares. */
|
|
64
|
+
export type SettingsOf<F extends SettingFields> = {
|
|
65
|
+
[K in keyof F]: F[K] extends FieldBase<infer T> ? T : never;
|
|
66
|
+
};
|
|
67
|
+
interface FieldOptions {
|
|
68
|
+
label: string;
|
|
69
|
+
lock?: string;
|
|
70
|
+
}
|
|
71
|
+
export declare function toggle(fallback: boolean, opts: FieldOptions): ToggleField;
|
|
72
|
+
/** `labels` names each value for the panel; a value with none is shown as itself. */
|
|
73
|
+
export declare function choice<const T extends string>(values: readonly [T, ...T[]], fallback: NoInfer<T>, opts: FieldOptions & {
|
|
74
|
+
labels?: Readonly<Partial<Record<T, string>>>;
|
|
75
|
+
}): ChoiceField<T>;
|
|
76
|
+
/** A whole number when `step` is (the default, 1); otherwise any number in range. */
|
|
77
|
+
export declare function bounded(range: {
|
|
78
|
+
min: number;
|
|
79
|
+
max: number;
|
|
80
|
+
step?: number;
|
|
81
|
+
}, fallback: number, opts: FieldOptions): NumberField;
|
|
82
|
+
/** A value of any shape `schema` admits; `must` says what that is. */
|
|
83
|
+
export declare function custom<T>(schema: z.ZodType<T>, fallback: T, opts: FieldOptions & {
|
|
84
|
+
must: string;
|
|
85
|
+
}): CustomField<T>;
|
|
86
|
+
/** The whole settings object's schema: a strict zod object, so a game may `.partial()` or `.omit()` it. */
|
|
87
|
+
export type SettingsSchema<F extends SettingFields> = z.ZodObject<{
|
|
88
|
+
[K in keyof F]: F[K]['schema'];
|
|
89
|
+
}, z.core.$strict>;
|
|
90
|
+
export type PatchResult<S> = {
|
|
91
|
+
ok: true;
|
|
92
|
+
patch: Partial<S>;
|
|
93
|
+
} | {
|
|
94
|
+
ok: false;
|
|
95
|
+
error: string;
|
|
96
|
+
};
|
|
97
|
+
export type ApplyResult<S> = {
|
|
98
|
+
ok: true;
|
|
99
|
+
next: S;
|
|
100
|
+
changed: (keyof S & string)[];
|
|
101
|
+
} | {
|
|
102
|
+
ok: false;
|
|
103
|
+
reason: string;
|
|
104
|
+
};
|
|
105
|
+
export interface RoomSettingsSpec<F extends SettingFields = SettingFields> {
|
|
106
|
+
readonly fields: F;
|
|
107
|
+
/** In declaration order: the panel's rows. */
|
|
108
|
+
readonly keys: readonly (keyof F & string)[];
|
|
109
|
+
/** The whole settings object, every field required and nothing else. */
|
|
110
|
+
readonly schema: SettingsSchema<F>;
|
|
111
|
+
/** A new room's settings, a fresh copy each call. */
|
|
112
|
+
defaults(): SettingsOf<F>;
|
|
113
|
+
/** A PATCH's `settings`: some of the fields, each valid; at least one; nothing undeclared. */
|
|
114
|
+
parsePatch(raw: unknown): PatchResult<SettingsOf<F>>;
|
|
115
|
+
/**
|
|
116
|
+
* The settings a room file holds, every missing field at its default and every retired one
|
|
117
|
+
* dropped. Null when it is not settings at all (a field undeclared, or a value its field refuses):
|
|
118
|
+
* the caller quarantines the room. `undefined` (a room written before it had settings) is the defaults.
|
|
119
|
+
*/
|
|
120
|
+
readStored(raw: unknown): SettingsOf<F> | null;
|
|
121
|
+
/**
|
|
122
|
+
* `patch` over `current`. Only the fields whose value actually moves are checked against their
|
|
123
|
+
* lock, in declaration order; the first lock `lockedBy` answers non-null refuses the whole patch.
|
|
124
|
+
*/
|
|
125
|
+
apply(current: SettingsOf<F>, patch: Partial<SettingsOf<F>>, lockedBy: (lock: string) => string | null): ApplyResult<SettingsOf<F>>;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Any game's settings as the kit reads them: the fields by name, the values as plain records. Every
|
|
129
|
+
* `RoomSettingsSpec` is one, so a server or a panel takes this and a game passes its own spec.
|
|
130
|
+
*/
|
|
131
|
+
export interface AnyRoomSettings {
|
|
132
|
+
readonly fields: SettingFields;
|
|
133
|
+
readonly keys: readonly string[];
|
|
134
|
+
defaults(): Record<string, unknown>;
|
|
135
|
+
parsePatch(raw: unknown): PatchResult<Record<string, unknown>>;
|
|
136
|
+
readStored(raw: unknown): Record<string, unknown> | null;
|
|
137
|
+
apply(current: Record<string, unknown>, patch: Record<string, unknown>, lockedBy: (lock: string) => string | null): ApplyResult<Record<string, unknown>>;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* A game's settings. `retired` names fields a room file may still hold from an older build: read
|
|
141
|
+
* and dropped, so the room still loads.
|
|
142
|
+
*/
|
|
143
|
+
export declare function defineRoomSettings<const F extends SettingFields>(fields: F, opts?: {
|
|
144
|
+
retired?: readonly string[];
|
|
145
|
+
}): RoomSettingsSpec<F>;
|
|
146
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A room's settings, as a game declares them: each field's kind, default, label and lock, once,
|
|
3
|
+
* read by the server (what it stores, what a PATCH may change, and when) and by the page (the
|
|
4
|
+
* settings panel draws every field from the same definition). Knows no game.
|
|
5
|
+
*
|
|
6
|
+
* const BOMBERMAN_SETTINGS = defineRoomSettings({
|
|
7
|
+
* mode: choice(['classic', 'roguelike', 'sandbox'], 'classic', { label: 'Mode', lock: LIVE }),
|
|
8
|
+
* pickTimer: toggle(true, { label: 'Pick timer', lock: LIVE }),
|
|
9
|
+
* });
|
|
10
|
+
*
|
|
11
|
+
* ## Locks
|
|
12
|
+
*
|
|
13
|
+
* A field's `lock` names WHEN it cannot change, never why: `LIVE` ("while a match is live") is the
|
|
14
|
+
* kit's own, which a relay game's server answers (`@hadoku/game-server`'s `startRelayGame`). Any
|
|
15
|
+
* other name is a group the hosting room type answers itself (Magic's `event` and `rules`). A field
|
|
16
|
+
* with no lock changes whenever the leader likes.
|
|
17
|
+
*
|
|
18
|
+
* ## Refusals
|
|
19
|
+
*
|
|
20
|
+
* Every refusal names the field and what it must be (`settings.mode must be one of: classic, …`),
|
|
21
|
+
* and never echoes a value. A setting nobody declared is refused, never dropped.
|
|
22
|
+
*/
|
|
23
|
+
import { z } from 'zod';
|
|
24
|
+
/** The kit's own lock: a field that changes in the lobby only, never while a match is live. */
|
|
25
|
+
export const LIVE = 'live';
|
|
26
|
+
export function toggle(fallback, opts) {
|
|
27
|
+
return { kind: 'toggle', default: fallback, must: 'must be a boolean', schema: z.boolean(), ...opts };
|
|
28
|
+
}
|
|
29
|
+
/** `labels` names each value for the panel; a value with none is shown as itself. */
|
|
30
|
+
export function choice(values, fallback, opts) {
|
|
31
|
+
const { labels, ...rest } = opts;
|
|
32
|
+
return {
|
|
33
|
+
kind: 'choice',
|
|
34
|
+
default: fallback,
|
|
35
|
+
choices: values.map((value) => ({ value, label: labels?.[value] ?? value })),
|
|
36
|
+
must: `must be one of: ${values.join(', ')}`,
|
|
37
|
+
schema: z.enum(values),
|
|
38
|
+
...rest,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** A whole number when `step` is (the default, 1); otherwise any number in range. */
|
|
42
|
+
export function bounded(range, fallback, opts) {
|
|
43
|
+
const step = range.step ?? 1;
|
|
44
|
+
const whole = Number.isInteger(step);
|
|
45
|
+
const base = whole ? z.int() : z.number();
|
|
46
|
+
return {
|
|
47
|
+
kind: 'number',
|
|
48
|
+
default: fallback,
|
|
49
|
+
min: range.min,
|
|
50
|
+
max: range.max,
|
|
51
|
+
step,
|
|
52
|
+
must: `must be ${whole ? 'an integer' : 'a number'} ${range.min}–${range.max}`,
|
|
53
|
+
schema: base.min(range.min).max(range.max),
|
|
54
|
+
...opts,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** A value of any shape `schema` admits; `must` says what that is. */
|
|
58
|
+
export function custom(schema, fallback, opts) {
|
|
59
|
+
return { kind: 'custom', default: fallback, schema, ...opts };
|
|
60
|
+
}
|
|
61
|
+
/** Values are JSON (they are stored and sent), so equal JSON is the same value. */
|
|
62
|
+
function same(a, b) {
|
|
63
|
+
return a === b || JSON.stringify(a) === JSON.stringify(b);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A game's settings. `retired` names fields a room file may still hold from an older build: read
|
|
67
|
+
* and dropped, so the room still loads.
|
|
68
|
+
*/
|
|
69
|
+
export function defineRoomSettings(fields, opts = {}) {
|
|
70
|
+
const keys = Object.keys(fields);
|
|
71
|
+
const retired = new Set(opts.retired ?? []);
|
|
72
|
+
const fieldOf = (k) => (Object.hasOwn(fields, k) ? fields[k] : undefined);
|
|
73
|
+
const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
74
|
+
const schema = z.strictObject(Object.fromEntries(keys.map((k) => [k, fields[k].schema])));
|
|
75
|
+
return {
|
|
76
|
+
fields,
|
|
77
|
+
keys,
|
|
78
|
+
schema,
|
|
79
|
+
defaults: () => structuredClone(Object.fromEntries(keys.map((k) => [k, fields[k].default]))),
|
|
80
|
+
parsePatch(raw) {
|
|
81
|
+
if (!isObject(raw))
|
|
82
|
+
return { ok: false, error: 'settings must be an object' };
|
|
83
|
+
// A bad value is named before an undeclared field, as a PATCH has always answered.
|
|
84
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
85
|
+
const field = fieldOf(k);
|
|
86
|
+
if (field !== undefined && !field.schema.safeParse(v).success)
|
|
87
|
+
return { ok: false, error: `settings.${k} ${field.must}` };
|
|
88
|
+
}
|
|
89
|
+
const unknown = Object.keys(raw).filter((k) => fieldOf(k) === undefined);
|
|
90
|
+
if (unknown.length > 0)
|
|
91
|
+
return { ok: false, error: `unknown setting(s): ${unknown.join(', ')}` };
|
|
92
|
+
if (Object.keys(raw).length === 0)
|
|
93
|
+
return { ok: false, error: 'settings names no setting' };
|
|
94
|
+
return { ok: true, patch: structuredClone(raw) };
|
|
95
|
+
},
|
|
96
|
+
readStored(raw) {
|
|
97
|
+
if (raw === undefined)
|
|
98
|
+
return this.defaults();
|
|
99
|
+
if (!isObject(raw))
|
|
100
|
+
return null;
|
|
101
|
+
const out = this.defaults();
|
|
102
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
103
|
+
if (retired.has(k))
|
|
104
|
+
continue;
|
|
105
|
+
const field = fieldOf(k);
|
|
106
|
+
if (field === undefined || !field.schema.safeParse(v).success)
|
|
107
|
+
return null;
|
|
108
|
+
out[k] = structuredClone(v);
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
},
|
|
112
|
+
apply(current, patch, lockedBy) {
|
|
113
|
+
const changed = keys.filter((k) => k in patch && patch[k] !== undefined && !same(patch[k], current[k]));
|
|
114
|
+
for (const k of changed) {
|
|
115
|
+
const lock = fields[k].lock;
|
|
116
|
+
const reason = lock === undefined ? null : lockedBy(lock);
|
|
117
|
+
if (reason !== null)
|
|
118
|
+
return { ok: false, reason };
|
|
119
|
+
}
|
|
120
|
+
const next = { ...current };
|
|
121
|
+
for (const k of changed)
|
|
122
|
+
next[k] = structuredClone(patch[k]);
|
|
123
|
+
return { ok: true, next, changed };
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/platform-wire",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.114",
|
|
4
|
+
"gitHead": "f0a3ba514aea20ac6407a4734cefe7bafd966ed8",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"description": "The wire any game speaks: room ids, telemetry, presence, events and tables, the Discord relay, looks, the restart notice, secure random and a generic server envelope. Knows no game; a game's own part rides as a type parameter. MIT; imports only zod.",
|
|
7
|
+
"description": "The wire any game speaks: room ids, telemetry, presence, events and tables, the Discord relay, looks, room settings, the restart notice, secure random and a generic server envelope. Knows no game; a game's own part rides as a type parameter. MIT; imports only zod.",
|
|
8
8
|
"exports": {
|
|
9
9
|
"./room-feed": {
|
|
10
10
|
"types": "./dist/room-feed.d.ts",
|
|
@@ -53,6 +53,10 @@
|
|
|
53
53
|
"./tiers": {
|
|
54
54
|
"types": "./dist/tiers.d.ts",
|
|
55
55
|
"import": "./dist/tiers.js"
|
|
56
|
+
},
|
|
57
|
+
"./room-settings": {
|
|
58
|
+
"types": "./dist/room-settings.d.ts",
|
|
59
|
+
"import": "./dist/room-settings.js"
|
|
56
60
|
}
|
|
57
61
|
},
|
|
58
62
|
"dependencies": {
|