anvilsign in

collin/mahjong

1import { useLayoutEffect, useRef, useState } from 'react';
2import { STACKS_PER_SIDE } from '../game/tiles';
3import {
4 rhombus,
5 ringLayout,
6 RING,
7 WALL_SIDES,
8 wallStacks,
9 type Rhombus,
10 type WallProgress,
11} from '../game/wall';
12
13import { useT } from './lang';
14
15/**
16 * What is left of the wall, built as a square in the middle of the table.
17 *
18 * Four staggered sides like a #, with the corners open — but a square the size
19 * of the wall *as it is dealt*, not of 144 tiles. Eighteen full-size stacks a
20 * side is longer than the middle of any ordinary window is tall, and by the
21 * time anyone is looking there is nothing like that much wall left, so it is
22 * rebuilt smaller and the tiles stay the size of the ones in your hand. What
23 * goes where is `RING` and `ringLayout` in `game/wall.ts`; this measures the
24 * middle for them and puts the answer on screen.
25 *
26 * Every stack carries its index, and that is load-bearing — `table/geometry.ts`
27 * measures these elements to build the colliders rather than restating the CSS
28 * that sizes them.
29 */
30export function WallRing({ state }: { state: WallProgress }) {
31 const t = useT();
32 const ring = useRef<HTMLDivElement>(null);
33 const { opening, cell, shape } = useSquare(ring);
34 const sides = ringLayout(wallStacks(state), RING);
35
36 return (
37 <div
38 className="wall-ring"
39 ref={ring}
40 aria-label={t('牌牆', 'The wall')}
41 style={
42 opening
43 ? // `--ws` is set from here rather than from the tile, because the
44 // square is the fixed thing now and the tile is what gives.
45 ({
46 width: opening.w,
47 height: opening.h,
48 '--ws': `${cell}px`,
49 // A side is its stacks and nothing else, so there is no sum for
50 // the corners to come out of by a pixel.
51 '--wall-len': `${RING.h * cell}px`,
52 } as React.CSSProperties)
53 : undefined
54 }
55 >
56 {/* One cell, never drawn: the ring is measured in stacks, so something has
57 to be the size of a stack before any of them have been placed. */}
58 <i className="wall-probe" />
59 {WALL_SIDES.map((cls, side) => (
60 <div
61 className={`wall-side ${cls}`}
62 key={cls}
63 style={
64 shape
65 ? ({
66 '--vx': `${shape.places[side].x}px`,
67 '--vy': `${shape.places[side].y}px`,
68 '--dir': `${shape.places[side].dir}deg`,
69 } as React.CSSProperties)
70 : undefined
71 }
72 >
73 {sides[side].map(({ index, stack }) => (
74 <div
75 key={index}
76 data-stack={index}
77 // The 16-tile 底牌 tail is not marked out. It is still the tail —
78 // `Stack.dead` says so and the engine ends the hand on it — but a
79 // wall on a table is one wall, not a wall with the end greyed off.
80 className={`wall-stack s${stack.count}${stack.next ? ' next' : ''}`}
81 // The layout already knows which way this stack is lying, so the
82 // physics is told rather than left to work it out off a box that
83 // has been turned. See table/geometry.ts.
84 data-angle={shape ? shape.places[side].dir : 0}
85 >
86 <i />
87 </div>
88 ))}
89 </div>
90 ))}
91 </div>
92 );
93}
94
95interface Square {
96 /** The opening the square leaves, once there is a middle to measure. */
97 opening: { w: number; h: number } | null;
98 /** How big a stack came out — the wall's own tile size, in px. */
99 cell: number;
100 /** Where the four walls stand, from `rhombus`. */
101 shape: Rhombus | null;
102}
103
104/**
105 * How big to build the square, measured rather than worked out twice: how big a
106 * tile is is CSS's business, so one cell is rendered and asked how it came out,
107 * and the middle is whatever the four strips have left. Nothing here depends on
108 * the state of the hand — the square is built once for the window it is in and
109 * then eaten, so it does not close in on the discards lying inside it.
110 */
111function useSquare(ring: React.RefObject<HTMLDivElement | null>): Square {
112 const [square, setSquare] = useState<Square>({ opening: null, cell: 0, shape: null });
113
114 useLayoutEffect(() => {
115 const el = ring.current;
116 const middle = el?.parentElement;
117 const probe = el?.querySelector<HTMLElement>('.wall-probe');
118 if (!el || !middle || !probe) return;
119
120 const read = () => {
121 const box = probe.getBoundingClientRect();
122 if (box.width <= 0) return;
123 // What shape a stack is, is CSS's business — read it, don't restate it.
124 const ratio = box.height / box.width;
125 // Stood on its corner, a square needs its *diagonal* where it used to
126 // need its side — so the room it has to be built in shrinks by however
127 // much the turn costs. Square on that is 1 and nothing changes; at 45° it
128 // is √2 and the square comes out about seven tenths the size, which is
129 // what keeps its arms on the table rather than off the edge of it.
130 const style = getComputedStyle(el);
131 const turn = parseFloat(style.getPropertyValue('--wall-turn')) || 0;
132 const angle = parseFloat(style.getPropertyValue('--wall-angle')) || 90;
133 // A multiple of the overrun the corner actually needs. One is the tightest
134 // join there can be; more is a longer overlap, and there is no less.
135 const lap = parseFloat(style.getPropertyValue('--wall-lap')) || 1;
136
137 // Everything about the shape is linear in the stack, so it is asked for
138 // once at a stack of one and the answer scaled to the middle there is.
139 const unit = rhombus(STACKS_PER_SIDE, ratio, angle, turn, lap);
140 const s = Math.max(
141 1,
142 Math.min(middle.clientWidth / unit.box.w, middle.clientHeight / unit.box.h),
143 );
144 const shape = rhombus(STACKS_PER_SIDE * s, ratio * s, angle, turn, lap);
145 const opening = { w: shape.open.w, h: shape.open.h };
146 setSquare((was) =>
147 was.cell === s && was.opening?.w === opening.w && was.opening?.h === opening.h
148 ? was
149 : { opening, cell: s, shape },
150 );
151 };
152
153 read();
154 // The middle, not the square: the square's own size is this effect's doing,
155 // and watching it would be watching itself.
156 const observer = new ResizeObserver(read);
157 observer.observe(middle);
158 observer.observe(probe);
159 return () => observer.disconnect();
160 }, [ring]);
161
162 return square;
163}