Skip to content

Commit c70cccc

Browse files
committed
multiplayer session flow
1 parent 3a53621 commit c70cccc

7 files changed

Lines changed: 35 additions & 23 deletions

File tree

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Block Mining Game</title>
8-
<script type="module" crossorigin src="/assets/index-D2EA-oE_.js"></script>
8+
<script type="module" crossorigin src="/assets/index-BnocEE6w.js"></script>
99
<link rel="stylesheet" crossorigin href="/assets/index-BwGfHuG6.css">
1010
</head>
1111
<body>

‎src/App.jsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import {
4444
saveRoomSession,
4545
loadRoomSession,
4646
clearRoomSession,
47+
setRoomResumeIntent,
48+
shouldAutoResumeRoom,
4749
} from './lib/roomSession';
4850
import { useLocale } from './i18n/LocaleContext';
4951
import { clearSoloSessionMeta, loadSoloSessionMeta, saveSoloSessionMeta } from './lib/gameDraft';
@@ -136,6 +138,7 @@ function App() {
136138
};
137139

138140
const returnToMenu = () => {
141+
setRoomResumeIntent(false);
139142
resetToMenu();
140143
};
141144

@@ -393,7 +396,7 @@ function App() {
393396

394397
useEffect(() => {
395398
const saved = loadRoomSession();
396-
if (!saved?.seed || !saved?.sessionToken) return;
399+
if (!saved?.seed || !saved?.sessionToken || !shouldAutoResumeRoom()) return;
397400
setRestoringSession(true);
398401

399402
let cancelled = false;

‎src/components/HostDashboard.jsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ export default function HostDashboard({
119119
</div>
120120

121121
{sorted.length > 0 && (
122-
<PanelCard className="bp-host-dash__waiting-miners" title={tr('hostLobbyMiners')} iconSrc={ICON.miner}>
122+
<PanelCard className="bp-host-dash__waiting-miners" title={tr('hostLobbyMiners')} iconSrc={ICON.pickaxe}>
123123
<ol className="bp-host-race-list" aria-label={tr('hostLobbyMiners')}>
124124
{sorted.map((player, index) => (
125125
<li key={player.name} className="bp-host-race-list__row">
126126
<span className="bp-host-race-list__rank">{index + 1}</span>
127127
<span className="bp-host-race-list__miner">
128-
<BpIcon src={ICON.miner} className="bp-icon--sm" tone="primary" />
128+
<BpIcon src={ICON.pickaxe} className="bp-icon--sm" tone="primary" />
129129
</span>
130130
<span className="bp-host-race-list__name" title={player.name}>{player.name}</span>
131131
</li>

‎src/components/game/GameHud.jsx‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@ export default function GameHud({
2828
{tr(difficulty === 'easy' ? 'difficultyEasy' : 'difficultyHard')}
2929
</span>
3030
{roomSeed && (
31-
<>
32-
<span className="bp-game-hud__pill bp-game-hud__pill--room" title={tr('roomCode')}>
33-
{roomSeed}
34-
</span>
35-
<span
36-
className={'bp-game-hud__pill bp-game-hud__pill--sync bp-game-hud__pill--sync-' + syncStatus}
37-
role="status"
38-
aria-live="polite"
39-
>
40-
{tr(syncStatus === 'online' ? 'syncOnline' : syncStatus === 'offline' ? 'syncOffline' : 'syncReconnecting')}
41-
</span>
42-
</>
31+
<span
32+
className={'bp-game-hud__pill bp-game-hud__pill--sync bp-game-hud__pill--sync-' + syncStatus}
33+
role="status"
34+
aria-live="polite"
35+
>
36+
{tr(syncStatus === 'online' ? 'syncOnline' : syncStatus === 'offline' ? 'syncOffline' : 'syncReconnecting')}
37+
</span>
4338
)}
4439
</div>
4540
<div className="bp-game-hud__nav-end">

‎src/lib/roomSession.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const STORAGE_KEY = 'bp-room-session-v5';
2+
const RESUME_INTENT_KEY = 'bp-room-resume-intent-v1';
23

34
export function saveRoomSession(session) {
45
if (typeof localStorage === 'undefined' || !session?.seed || !session?.sessionToken) return;
56
localStorage.setItem(STORAGE_KEY, JSON.stringify(session));
7+
setRoomResumeIntent(true);
68
}
79

810
export function loadRoomSession() {
@@ -15,11 +17,23 @@ export function loadRoomSession() {
1517
}
1618
}
1719

20+
export function setRoomResumeIntent(active) {
21+
if (typeof sessionStorage === 'undefined') return;
22+
if (active) sessionStorage.setItem(RESUME_INTENT_KEY, 'active');
23+
else sessionStorage.removeItem(RESUME_INTENT_KEY);
24+
}
25+
26+
export function shouldAutoResumeRoom() {
27+
return typeof sessionStorage !== 'undefined'
28+
&& sessionStorage.getItem(RESUME_INTENT_KEY) === 'active';
29+
}
30+
1831
export function clearRoomSession() {
1932
if (typeof localStorage === 'undefined') return;
2033
localStorage.removeItem(STORAGE_KEY);
2134
localStorage.removeItem('bp-room-session-v4');
2235
localStorage.removeItem('bp-room-session-v3');
2336
localStorage.removeItem('bp-room-session-v2');
2437
localStorage.removeItem('bp-room-session');
38+
setRoomResumeIntent(false);
2539
}

‎tests/e2e/mining-game.spec.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ test('a host can return to the menu and securely resume the room', async ({ page
333333
expect(saved).toMatchObject({ seed: code, role: 'host' });
334334
expect(saved.sessionToken).toBeTruthy();
335335

336+
await page.reload();
337+
await expect(page.getByRole('button', { name: 'Resume room' })).toBeVisible();
338+
await expect(page.getByRole('heading', { name: 'Host dashboard' })).toHaveCount(0);
339+
336340
await page.getByRole('button', { name: 'Resume room' }).click();
337341
await expect(page.getByRole('heading', { name: 'Host dashboard' })).toBeVisible();
338342
await expect(page.locator('.bp-room-invite__code').first()).toHaveText(code);
@@ -375,11 +379,7 @@ test('a spectator host does not occupy a player slot', async ({ page, browser })
375379
await expect(page.locator('.bp-room-invite--featured')).toBeVisible();
376380
const codeBox = await page.locator('.bp-room-invite--featured .bp-room-invite__code-block').boundingBox();
377381
const qrBox = await qrButton.boundingBox();
378-
if ((page.viewportSize()?.width ?? 0) >= 1100) {
379-
expect(qrBox.x).toBeGreaterThan(codeBox.x + codeBox.width - 2);
380-
} else {
381-
expect(qrBox.y).toBeGreaterThan(codeBox.y + codeBox.height - 2);
382-
}
382+
expect(qrBox.y).toBeGreaterThan(codeBox.y + codeBox.height - 2);
383383
await qrButton.click();
384384
await expect(page.getByRole('dialog').locator('.bp-qr-dialog__image')).toBeVisible();
385385
await page.keyboard.press('Escape');

0 commit comments

Comments
 (0)