@hadoku/game-ui 0.1.216 → 0.1.218
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/rooms/game-setup.d.ts +9 -12
- package/dist/rooms/game-setup.js +12 -16
- package/dist/rooms/screen/PlatformRoomScreen.d.ts +15 -0
- package/dist/rooms/screen/PlatformRoomScreen.js +39 -0
- package/dist/rooms/screen/RoomShell.d.ts +13 -13
- package/dist/rooms/screen/RoomShell.js +7 -3
- package/dist/rooms/screen/useRoomJoin.d.ts +62 -0
- package/dist/rooms/screen/useRoomJoin.js +143 -0
- package/package.json +4 -4
|
@@ -26,9 +26,8 @@ export interface GameSetupProps {
|
|
|
26
26
|
/** The caller's own member id (`view.you.id`), or `''` while `view` is null. */
|
|
27
27
|
userId: string;
|
|
28
28
|
/**
|
|
29
|
-
* The caller's seat
|
|
30
|
-
*
|
|
31
|
-
* its own seat list off `view`'s generic member data instead of this field.
|
|
29
|
+
* The caller's seat number where the host keeps a table of its own, or null. The platform
|
|
30
|
+
* screen keeps none and passes null: a game reads its seats off `view.seats`.
|
|
32
31
|
*/
|
|
33
32
|
seat: number | null;
|
|
34
33
|
isLeader: boolean;
|
|
@@ -52,14 +51,13 @@ export interface GameSetupProps {
|
|
|
52
51
|
* What a game type renders inside the platform room screen: its setup before the start, its game
|
|
53
52
|
* after.
|
|
54
53
|
*
|
|
55
|
-
* `setup` is what a player sees
|
|
56
|
-
*
|
|
57
|
-
*
|
|
54
|
+
* `setup` is what a player sees before the room is underway: the game's own options, seats and
|
|
55
|
+
* ready. A game type with nothing to set up leaves it absent, and the room screen draws nothing in
|
|
56
|
+
* its place.
|
|
58
57
|
*
|
|
59
|
-
* `game` is the game type
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* once underway, even if it is nothing more than a status line.
|
|
58
|
+
* `game` is what the game type draws for "the game": an external game's `<BundleSlot>`, which
|
|
59
|
+
* plays the match in the room itself. Required: every registered game type draws SOMETHING, even
|
|
60
|
+
* if it is nothing more than a status line.
|
|
63
61
|
*/
|
|
64
62
|
export interface GameRoomUi {
|
|
65
63
|
setup?: (p: GameSetupProps) => ReactNode;
|
|
@@ -67,8 +65,7 @@ export interface GameRoomUi {
|
|
|
67
65
|
}
|
|
68
66
|
/**
|
|
69
67
|
* Registers (or replaces) the UI a `gameType` renders inside the room screen. Called once, at
|
|
70
|
-
* module scope, from the game type's own module
|
|
71
|
-
* `constructed-1v1` included.
|
|
68
|
+
* module scope, from the game type's own module.
|
|
72
69
|
*/
|
|
73
70
|
export declare function registerGameRoomUi(gameType: string, ui: GameRoomUi): void;
|
|
74
71
|
/** The UI registered for a `gameType`, or null for one nothing has registered. */
|
package/dist/rooms/game-setup.js
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The seam between the platform room screen and a game type's own UI.
|
|
3
3
|
*
|
|
4
|
-
* The room screen owns
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* render.
|
|
4
|
+
* The platform room screen (`screen/PlatformRoomScreen.tsx`) owns what every room has: the read of
|
|
5
|
+
* the room record and the join, the password gate, the title, Leave and the host's tools. What a
|
|
6
|
+
* game type draws INSIDE that room is registered here, by its `gameType` string, so the screen
|
|
7
|
+
* never imports a game's own file to find out what to render. The screen is the registry's reader;
|
|
8
|
+
* its registrants are the external games (`external-games.tsx`, one per game the site hosts) and any
|
|
9
|
+
* second game, each calling `registerGameRoomUi` from ITS OWN module, once, at import, and reading
|
|
10
|
+
* the room's injected clients from `game-clients.tsx` rather than closing over the screen's props.
|
|
8
11
|
*
|
|
9
|
-
* `view` is nullable
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* room screen already does elsewhere (optional chaining, never a crash) rather than assume the
|
|
13
|
-
* two reads land together.
|
|
12
|
+
* `view` is nullable: a room whose record could not be read still draws its game's parts once the
|
|
13
|
+
* screen has stopped waiting, so a game's `setup`/`game` handles `view === null` (optional
|
|
14
|
+
* chaining, never a crash) rather than assume the record is there.
|
|
14
15
|
*
|
|
15
|
-
* This file is `platform`: it imports no game's own code
|
|
16
|
-
* itself by calling `registerGameRoomUi` from ITS OWN module, once, at import — Magic's `draft`
|
|
17
|
-
* and `constructed-1v1` do this from `screens/magic-room-ui.tsx`, the same way bomberman's own
|
|
18
|
-
* module does, reading the room's injected clients from `rooms/game-clients.tsx` rather than
|
|
19
|
-
* closing over `RoomTableScreen`'s props.
|
|
16
|
+
* This file is `platform`: it imports no game's own code.
|
|
20
17
|
*/
|
|
21
18
|
const registry = new Map();
|
|
22
19
|
/**
|
|
23
20
|
* Registers (or replaces) the UI a `gameType` renders inside the room screen. Called once, at
|
|
24
|
-
* module scope, from the game type's own module
|
|
25
|
-
* `constructed-1v1` included.
|
|
21
|
+
* module scope, from the game type's own module.
|
|
26
22
|
*/
|
|
27
23
|
export function registerGameRoomUi(gameType, ui) {
|
|
28
24
|
registry.set(gameType, ui);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type RoomViewBase } from '../game-setup.js';
|
|
3
|
+
import { type LeaveRoom } from './room-exit.js';
|
|
4
|
+
import type { RoomJoin } from './useRoomJoin.js';
|
|
5
|
+
export interface PlatformRoomScreenProps<R extends RoomViewBase> {
|
|
6
|
+
/** The host's read of the room. */
|
|
7
|
+
join: RoomJoin<R>;
|
|
8
|
+
/** What the host draws in the head row beside Leave: Close, Settings. */
|
|
9
|
+
headerTools?: ReactNode;
|
|
10
|
+
/** The game's `/leave` for this room; absent, the head row draws no Leave. */
|
|
11
|
+
leave?: LeaveRoom;
|
|
12
|
+
/** Told each time the game says a match is, or is no longer, live. */
|
|
13
|
+
onLive?: (live: boolean) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function PlatformRoomScreen<R extends RoomViewBase>({ join, headerTools, leave, onLive }: PlatformRoomScreenProps<R>): import("react").JSX.Element;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/*
|
|
3
|
+
* The room screen for a game type the platform hosts: the shell every room has (`RoomShell`), drawn
|
|
4
|
+
* from the host's one read of the room (`useRoomJoin`), with the game type's own parts from the
|
|
5
|
+
* registry (`../game-setup.ts`) in its body.
|
|
6
|
+
*
|
|
7
|
+
* What it draws: the room's name, Leave (where the host has a way home), the host's tools, the
|
|
8
|
+
* password gate while the join is refused, the join's refusal in words, and the registered `setup`
|
|
9
|
+
* and `game` for the room's `gameType`. Seats, spectators and options are the game's own, drawn in
|
|
10
|
+
* its `setup`. The host calls `useRoomJoin` itself and passes the result, so it can decide which
|
|
11
|
+
* screen to draw from the same read.
|
|
12
|
+
*
|
|
13
|
+
* While the game says a match is live (`GameSetupProps.onLive`), the head row steps aside; the game
|
|
14
|
+
* offers the way out itself (`GameSetupProps.leave`).
|
|
15
|
+
*/
|
|
16
|
+
import { useState } from 'react';
|
|
17
|
+
import { useChromeShell } from '@wolffm/ui-shell/chrome/shell';
|
|
18
|
+
import { gameRoomUiFor } from "../game-setup.js";
|
|
19
|
+
import { leaveRoomThen, useRoomHome } from "./room-exit.js";
|
|
20
|
+
import { RoomShell } from "./RoomShell.js";
|
|
21
|
+
export function PlatformRoomScreen({ join, headerTools, leave, onLive }) {
|
|
22
|
+
const shellRef = useChromeShell('fit');
|
|
23
|
+
const home = useRoomHome();
|
|
24
|
+
const [live, setLive] = useState(false);
|
|
25
|
+
const { view } = join;
|
|
26
|
+
const ui = view === null ? null : gameRoomUiFor(view.gameType);
|
|
27
|
+
const gameProps = {
|
|
28
|
+
view,
|
|
29
|
+
me: { userId: view?.you.id ?? '', seat: null, isLeader: view?.you.leader === true },
|
|
30
|
+
// The room record says nothing of a match: a game that plays one knows it itself, and says so through `onLive`.
|
|
31
|
+
underway: false,
|
|
32
|
+
onLive: (next) => {
|
|
33
|
+
setLive(next);
|
|
34
|
+
onLive?.(next);
|
|
35
|
+
},
|
|
36
|
+
...(home === null ? {} : { leave: () => void leaveRoomThen(home) }),
|
|
37
|
+
};
|
|
38
|
+
return (_jsxs(RoomShell, { shellRef: shellRef, dataMembers: view?.members.length, roomTitle: view?.name, settled: join.settled, leave: leave, headerTools: headerTools, resolved: join.resolved, pending: !join.resolved && !join.settled, locked: join.locked, password: join.password, onPasswordChange: join.setPassword, onPasswordSubmit: join.submitPassword, roomMissingMessage: join.joinError, live: live, children: [ui?.setup?.(gameProps), ui?.game(gameProps)] }));
|
|
39
|
+
}
|
|
@@ -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.218",
|
|
4
|
+
"gitHead": "a83da65a029cb16733501dbbb699989f109f2413",
|
|
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.222",
|
|
57
|
+
"@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.222",
|
|
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",
|