@hadoku/game-ui 0.1.216 → 0.1.217
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.
|
@@ -3,7 +3,7 @@ import { type LeaveRoom } from './room-exit.js';
|
|
|
3
3
|
import '../../components/Button.css';
|
|
4
4
|
import './RoomShell.css';
|
|
5
5
|
export interface RoomShellProps {
|
|
6
|
-
shellRef
|
|
6
|
+
shellRef?: RefCallback<HTMLElement>;
|
|
7
7
|
/** `view.members.length` once the room record has one; the room screen's own `data-members`. */
|
|
8
8
|
dataMembers: number | undefined;
|
|
9
9
|
/** The room's own name, once either read has it. */
|
|
@@ -12,7 +12,7 @@ export interface RoomShellProps {
|
|
|
12
12
|
titleAside?: ReactNode;
|
|
13
13
|
/** The room answered with a refusal rather than useful data: nothing more to wait for, so the
|
|
14
14
|
* title falls back to "Room" instead of staying a skeleton. */
|
|
15
|
-
settled
|
|
15
|
+
settled?: boolean;
|
|
16
16
|
/**
|
|
17
17
|
* The game's `/leave` for this room. Given, and the host has a way home (`RoomHomeProvider`), the
|
|
18
18
|
* head row draws Leave, and the page's top bar leaves through it too (`room-exit.tsx`).
|
|
@@ -24,24 +24,24 @@ export interface RoomShellProps {
|
|
|
24
24
|
*/
|
|
25
25
|
leaveAsks?: string;
|
|
26
26
|
/** Close, Settings, and whatever the game type's own settings panel wants beside them. */
|
|
27
|
-
headerTools
|
|
28
|
-
streamed
|
|
27
|
+
headerTools?: ReactNode;
|
|
28
|
+
streamed?: boolean;
|
|
29
29
|
/** A streamed room says so under its name; absent for a builtin one. */
|
|
30
|
-
streamedSubLine
|
|
30
|
+
streamedSubLine?: ReactNode;
|
|
31
31
|
/** The room record has answered, success or failure. */
|
|
32
32
|
resolved: boolean;
|
|
33
33
|
/** Neither read has answered enough to draw anything real yet. */
|
|
34
|
-
pending
|
|
34
|
+
pending?: boolean;
|
|
35
35
|
/** What a loading room draws while `pending`. */
|
|
36
|
-
pendingBody
|
|
36
|
+
pendingBody?: ReactNode;
|
|
37
37
|
/** A streamed room's own body — one container per seat, each a stream. */
|
|
38
|
-
streamedBody
|
|
39
|
-
locked
|
|
40
|
-
password
|
|
41
|
-
onPasswordChange
|
|
42
|
-
onPasswordSubmit
|
|
38
|
+
streamedBody?: ReactNode;
|
|
39
|
+
locked?: 'need' | 'wrong' | 'rate-limited' | null;
|
|
40
|
+
password?: string;
|
|
41
|
+
onPasswordChange?: (value: string) => void;
|
|
42
|
+
onPasswordSubmit?: () => void;
|
|
43
43
|
/** The table could not be read at all, and no password prompt is standing between here and it. */
|
|
44
|
-
roomMissingMessage
|
|
44
|
+
roomMissingMessage?: string | null;
|
|
45
45
|
/** The game in the body is playing a match (`GameSetupProps.onLive`): the head row steps aside until it is not. */
|
|
46
46
|
live?: boolean;
|
|
47
47
|
/** The game type's own content: its setup, its game, in that order. */
|
|
@@ -11,6 +11,10 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
11
11
|
* streamed — a streamed room draws `streamedBody` instead, and neither draws until the room record
|
|
12
12
|
* has answered.
|
|
13
13
|
*
|
|
14
|
+
* Only the member count, the title, `resolved` and the body are always given. Every other prop is
|
|
15
|
+
* off when absent: no tools, no streamed mode, no loading body, no password gate, no missing-room
|
|
16
|
+
* message. A game with none of those passes nothing for them.
|
|
17
|
+
*
|
|
14
18
|
* While the game in the body says a match is live (`live`), the head row is not drawn at all —
|
|
15
19
|
* no title, no Leave, no tools — so the game has the room to itself, and its own menu offers the
|
|
16
20
|
* way out (`GameSetupProps.leave`). It comes back the moment `live` is false again. The room's
|
|
@@ -20,13 +24,13 @@ import { useState } from 'react';
|
|
|
20
24
|
import { leaveRoomThen, useRoomHome, useRoomLeave } from "./room-exit.js";
|
|
21
25
|
import '../../components/Button.css';
|
|
22
26
|
import './RoomShell.css';
|
|
23
|
-
export function RoomShell({ shellRef, dataMembers, roomTitle, titleAside, settled, leave, leaveAsks, headerTools, streamed, streamedSubLine, resolved, pending, pendingBody, streamedBody, locked, password, onPasswordChange, onPasswordSubmit, roomMissingMessage, live = false, children, }) {
|
|
27
|
+
export function RoomShell({ shellRef, dataMembers, roomTitle, titleAside, settled = false, leave, leaveAsks, headerTools = null, streamed = false, streamedSubLine = null, resolved, pending = false, pendingBody = null, streamedBody = null, locked = null, password = '', onPasswordChange, onPasswordSubmit, roomMissingMessage = null, live = false, children, }) {
|
|
24
28
|
useRoomLeave(leaveAsks === undefined ? (leave ?? null) : null);
|
|
25
29
|
const home = useRoomHome();
|
|
26
30
|
return (_jsxs("div", { ref: shellRef, className: "shell shell--fit", "data-testid": "constructed", "data-members": dataMembers, "data-live": live ? 'true' : undefined, children: [!live && (_jsxs("header", { className: "shell__head", children: [roomTitle !== undefined ? (_jsx("h1", { className: "shell__title", "data-testid": "room-name", children: roomTitle })) : settled ? (_jsx("h1", { className: "shell__title", children: "Room" })) : (_jsx("span", { className: "skeleton skeleton--title" })), roomTitle !== undefined && titleAside, _jsxs("div", { className: "shell__tools", children: [leave !== undefined && home !== null && _jsx(RoomLeave, { leave: leave, asks: leaveAsks, home: home }), headerTools] }), resolved && streamed && streamedSubLine] })), _jsxs("main", { className: "lobby lobby--room", "aria-busy": pending ? 'true' : undefined, children: [pending && pendingBody, streamed && streamedBody, !streamed && resolved && (_jsxs(_Fragment, { children: [locked !== null && (_jsxs("form", { className: "lobby__locked ct-panel", "data-testid": "room-locked", onSubmit: (e) => {
|
|
27
31
|
e.preventDefault();
|
|
28
|
-
onPasswordSubmit();
|
|
29
|
-
}, children: [_jsx("p", { role: "alert", children: locked === 'need' ? 'This room needs a password.' : locked === 'wrong' ? 'Wrong password.' : 'Too many attempts.' }), _jsx("label", { htmlFor: "room-password", children: "Password" }), _jsx("input", { id: "room-password", "data-testid": "room-password", type: "password", autoComplete: "off", value: password, onChange: (e) => onPasswordChange(e.target.value) }), _jsx("button", { type: "submit", "data-testid": "room-password-submit", disabled: locked === 'rate-limited', children: "Join" })] })), locked === null && roomMissingMessage !== null && (_jsx("p", { className: "ct-error", role: "alert", "data-testid": "room-missing", children: roomMissingMessage })), children] }))] })] }));
|
|
32
|
+
onPasswordSubmit?.();
|
|
33
|
+
}, children: [_jsx("p", { role: "alert", children: locked === 'need' ? 'This room needs a password.' : locked === 'wrong' ? 'Wrong password.' : 'Too many attempts.' }), _jsx("label", { htmlFor: "room-password", children: "Password" }), _jsx("input", { id: "room-password", "data-testid": "room-password", type: "password", autoComplete: "off", value: password, onChange: (e) => onPasswordChange?.(e.target.value) }), _jsx("button", { type: "submit", "data-testid": "room-password-submit", disabled: locked === 'rate-limited', children: "Join" })] })), locked === null && roomMissingMessage !== null && (_jsx("p", { className: "ct-error", role: "alert", "data-testid": "room-missing", children: roomMissingMessage })), children] }))] })] }));
|
|
30
34
|
}
|
|
31
35
|
/** The head row's Leave. A leave that costs an event (`asks`) is asked once more, by its name, before it leaves. */
|
|
32
36
|
function RoomLeave({ leave, asks, home }) {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { RoomFeed } from '../feed.js';
|
|
2
|
+
import type { RoomViewBase } from '../game-setup.js';
|
|
3
|
+
import type { RoomCoreClient, RoomJoinOutcome } from '../room-core-client.js';
|
|
4
|
+
/** Why the room's password gate is up: none was sent, the one sent was refused, or too many were. */
|
|
5
|
+
export type RoomLock = 'need' | 'wrong' | 'rate-limited';
|
|
6
|
+
/** A join that did not go through. */
|
|
7
|
+
export type RoomJoinRefused = Exclude<RoomJoinOutcome<unknown>, {
|
|
8
|
+
ok: true;
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* Where a refused join leaves the room's screen. room-core refuses a wrong password and a missing room
|
|
12
|
+
* with the same answer, on purpose, so both ask for the password: `need` when none was sent, `wrong`
|
|
13
|
+
* when one was. An invite link that meets a locked room must not send its follower back to the list.
|
|
14
|
+
* A rate limit waits; anything else is the server's own sentence.
|
|
15
|
+
*/
|
|
16
|
+
export declare function joinRefusal(join: RoomJoinRefused, password: string): {
|
|
17
|
+
locked: RoomLock;
|
|
18
|
+
} | {
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
export interface RoomJoinOptions<R extends RoomViewBase> {
|
|
22
|
+
core: Pick<RoomCoreClient<R>, 'get' | 'join'>;
|
|
23
|
+
feed: Pick<RoomFeed, 'follow'>;
|
|
24
|
+
/** The feed key whose pings re-read the record; the room's id unless given. */
|
|
25
|
+
feedKey?: string;
|
|
26
|
+
/** A record the host already holds (read before it navigated here): the first render draws it. */
|
|
27
|
+
initial?: R;
|
|
28
|
+
/** Whether this read joins a room that does not name the viewer yet; every room unless given. */
|
|
29
|
+
shouldJoin?: (room: R) => boolean;
|
|
30
|
+
/** Each record the read lands, before it is drawn. */
|
|
31
|
+
onRead?: (room: R) => void;
|
|
32
|
+
/** The room is not there any more: its leader closed it. */
|
|
33
|
+
onGone?: () => void;
|
|
34
|
+
}
|
|
35
|
+
export interface RoomJoin<R extends RoomViewBase> {
|
|
36
|
+
/** The room record, or null until one has landed. */
|
|
37
|
+
view: R | null;
|
|
38
|
+
/** The record has answered, success or failure. */
|
|
39
|
+
resolved: boolean;
|
|
40
|
+
/** The room answered with a refusal rather than a record: nothing more to wait for. */
|
|
41
|
+
settled: boolean;
|
|
42
|
+
locked: RoomLock | null;
|
|
43
|
+
/** A join refused in the server's own words. */
|
|
44
|
+
joinError: string | null;
|
|
45
|
+
/** The password field's text. */
|
|
46
|
+
password: string;
|
|
47
|
+
setPassword: (value: string) => void;
|
|
48
|
+
/** Join again with the field's password: the gate comes down and the next attempt starts. */
|
|
49
|
+
submitPassword: () => void;
|
|
50
|
+
/**
|
|
51
|
+
* A join the host sent on its own route was refused: a lock raises the gate and answers null,
|
|
52
|
+
* anything else answers the server's sentence for the host to show.
|
|
53
|
+
*/
|
|
54
|
+
refuse: (join: RoomJoinRefused) => string | null;
|
|
55
|
+
/** The current attempt; a host's own join re-runs when it changes. */
|
|
56
|
+
attempt: number;
|
|
57
|
+
/** The password the current attempt joins with, `''` for none. */
|
|
58
|
+
sentPassword: string;
|
|
59
|
+
/** One of the host's own writes answered with the room: it is drawn, and a read sent before it is sent again. */
|
|
60
|
+
wrote: (room: R) => void;
|
|
61
|
+
}
|
|
62
|
+
export declare function useRoomJoin<R extends RoomViewBase>(roomId: string, options: RoomJoinOptions<R>): RoomJoin<R>;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A room screen's own read of its room record, and the join that makes the viewer a member.
|
|
3
|
+
*
|
|
4
|
+
* The record is read on the room's feed (`../feed.ts`): once when the screen starts following, then
|
|
5
|
+
* on every ping. Two reads close together can answer out of order, so a read lands only if no newer
|
|
6
|
+
* one was sent after it, and a read sent before one of the screen's own writes answered is sent
|
|
7
|
+
* again rather than landed (`wrote`): an older answer never puts older state back.
|
|
8
|
+
*
|
|
9
|
+
* `GET /api/rooms/:id` answers any caller, member or not, so a refusal never says "join me". What
|
|
10
|
+
* says it is the member list not naming `you.id` yet: true for a fresh invite link, false forever
|
|
11
|
+
* after a join lands one. The hook then joins, at most once per attempt, with the password the
|
|
12
|
+
* attempt carries, and reads again. A refused join stops the reads: retrying a locked room on every
|
|
13
|
+
* ping is a rate-limit trip and a password guessing loop nobody asked for. Submitting a password is
|
|
14
|
+
* the next attempt.
|
|
15
|
+
*
|
|
16
|
+
* What a game knows that the platform does not arrives as options: the feed key a game's live
|
|
17
|
+
* changes ping on (`feedKey`), which rooms this read joins at all (`shouldJoin`; a host with its own
|
|
18
|
+
* joining route says no for its own types), what to do with each record (`onRead`) and when the room
|
|
19
|
+
* is gone (`onGone`). One room per mount: a host showing another room mounts the hook again.
|
|
20
|
+
*/
|
|
21
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
22
|
+
import { messageOf } from "../../load.js";
|
|
23
|
+
/**
|
|
24
|
+
* Where a refused join leaves the room's screen. room-core refuses a wrong password and a missing room
|
|
25
|
+
* with the same answer, on purpose, so both ask for the password: `need` when none was sent, `wrong`
|
|
26
|
+
* when one was. An invite link that meets a locked room must not send its follower back to the list.
|
|
27
|
+
* A rate limit waits; anything else is the server's own sentence.
|
|
28
|
+
*/
|
|
29
|
+
export function joinRefusal(join, password) {
|
|
30
|
+
if (join.reason === 'refused')
|
|
31
|
+
return { locked: password === '' ? 'need' : 'wrong' };
|
|
32
|
+
if (join.reason === 'rate-limited')
|
|
33
|
+
return { locked: 'rate-limited' };
|
|
34
|
+
return { message: join.message };
|
|
35
|
+
}
|
|
36
|
+
export function useRoomJoin(roomId, options) {
|
|
37
|
+
const { core, feed, feedKey = roomId, initial } = options;
|
|
38
|
+
const [view, setView] = useState(initial ?? null);
|
|
39
|
+
const [resolved, setResolved] = useState(initial !== undefined);
|
|
40
|
+
const [locked, setLocked] = useState(null);
|
|
41
|
+
const [joinError, setJoinError] = useState(null);
|
|
42
|
+
const [password, setPassword] = useState('');
|
|
43
|
+
const [attempt, setAttempt] = useState({ count: 0, password: '' });
|
|
44
|
+
// The latest callbacks, read when a read lands: the host's fresh closures every render never re-follow the feed.
|
|
45
|
+
const callbacks = useRef(options);
|
|
46
|
+
callbacks.current = options;
|
|
47
|
+
const sent = useRef({ reads: 0, writes: 0 });
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
let live = true;
|
|
50
|
+
let joined = false;
|
|
51
|
+
let stop = false;
|
|
52
|
+
const read = () => {
|
|
53
|
+
if (stop)
|
|
54
|
+
return;
|
|
55
|
+
const mine = (sent.current.reads += 1);
|
|
56
|
+
const writesBefore = sent.current.writes;
|
|
57
|
+
const verdict = () => (mine !== sent.current.reads ? 'drop' : writesBefore !== sent.current.writes ? 'reread' : 'keep');
|
|
58
|
+
void core.get(roomId).then((room) => {
|
|
59
|
+
if (!live)
|
|
60
|
+
return;
|
|
61
|
+
const landed = verdict();
|
|
62
|
+
if (landed === 'drop')
|
|
63
|
+
return;
|
|
64
|
+
if (room === null) {
|
|
65
|
+
setResolved(true);
|
|
66
|
+
callbacks.current.onGone?.();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (landed === 'reread') {
|
|
70
|
+
read();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
callbacks.current.onRead?.(room);
|
|
74
|
+
setView(room);
|
|
75
|
+
setResolved(true);
|
|
76
|
+
setJoinError(null);
|
|
77
|
+
if (joined || room.members.some((m) => m.id === room.you.id) || callbacks.current.shouldJoin?.(room) === false)
|
|
78
|
+
return;
|
|
79
|
+
joined = true;
|
|
80
|
+
void core.join(roomId, attempt.password === '' ? undefined : attempt.password).then((join) => {
|
|
81
|
+
if (!live)
|
|
82
|
+
return;
|
|
83
|
+
if (join.ok) {
|
|
84
|
+
read();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
stop = true;
|
|
88
|
+
const refused = joinRefusal(join, attempt.password);
|
|
89
|
+
if ('locked' in refused)
|
|
90
|
+
setLocked(refused.locked);
|
|
91
|
+
else
|
|
92
|
+
setJoinError(refused.message);
|
|
93
|
+
}, (err) => {
|
|
94
|
+
if (!live)
|
|
95
|
+
return;
|
|
96
|
+
stop = true;
|
|
97
|
+
setJoinError(messageOf(err));
|
|
98
|
+
});
|
|
99
|
+
}, () => {
|
|
100
|
+
// The record could not be read. The host's other reads say why in words; what this must
|
|
101
|
+
// not do is keep the body hidden as well, so the gate opens.
|
|
102
|
+
if (live && verdict() !== 'drop')
|
|
103
|
+
setResolved(true);
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
const stopFollowing = feed.follow({ room: feedKey }, read);
|
|
107
|
+
return () => {
|
|
108
|
+
live = false;
|
|
109
|
+
stopFollowing();
|
|
110
|
+
};
|
|
111
|
+
}, [core, feed, roomId, feedKey, attempt]);
|
|
112
|
+
const submitPassword = useCallback(() => {
|
|
113
|
+
setLocked(null);
|
|
114
|
+
setJoinError(null);
|
|
115
|
+
setAttempt((last) => ({ count: last.count + 1, password }));
|
|
116
|
+
}, [password]);
|
|
117
|
+
const sentPassword = attempt.password;
|
|
118
|
+
const refuse = useCallback((join) => {
|
|
119
|
+
const refused = joinRefusal(join, sentPassword);
|
|
120
|
+
if ('message' in refused)
|
|
121
|
+
return refused.message;
|
|
122
|
+
setLocked(refused.locked);
|
|
123
|
+
return null;
|
|
124
|
+
}, [sentPassword]);
|
|
125
|
+
const wrote = useCallback((room) => {
|
|
126
|
+
sent.current.writes += 1;
|
|
127
|
+
setView(room);
|
|
128
|
+
}, []);
|
|
129
|
+
return {
|
|
130
|
+
view,
|
|
131
|
+
resolved,
|
|
132
|
+
settled: locked !== null || joinError !== null,
|
|
133
|
+
locked,
|
|
134
|
+
joinError,
|
|
135
|
+
password,
|
|
136
|
+
setPassword,
|
|
137
|
+
submitPassword,
|
|
138
|
+
refuse,
|
|
139
|
+
attempt: attempt.count,
|
|
140
|
+
sentPassword,
|
|
141
|
+
wrote,
|
|
142
|
+
};
|
|
143
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/game-ui",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.217",
|
|
4
|
+
"gitHead": "dab0c78b4ce593a14647007bd9d8a374952a0f4e",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "The browser parts any hosted game shares, above ui-shell: the room shell and session, seats and looks, the room route, the front page's generic pieces (gate, polls, gallery, patch notes), the bundle slot and relay channel a game's client mounts in, settings and trim screens, the HTTP and socket helpers, and the generic components (modal, popover, avatar, action button, radio cards). Knows no game; Magic's UI (games/magic/ui) plugs in, composed by the site (apps/ui). MIT; imports only react, react-dom, socket.io-client, @wolffm/* and the platform packages.",
|
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
"react-dom": "^19.2.8"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.
|
|
57
|
-
"@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.
|
|
56
|
+
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.221",
|
|
57
|
+
"@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.221",
|
|
58
58
|
"@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.3",
|
|
59
59
|
"@wolffm/rooms-client": "npm:@hadoku/rooms-client@^0.45.152",
|
|
60
60
|
"@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.152",
|