Compare commits
35 Commits
pr-181
...
feat/issue
| Author | SHA1 | Date | |
|---|---|---|---|
| 5957660802 | |||
| 177574ae19 | |||
| d26d2b1a09 | |||
| c4850f2e0e | |||
| 60ce650653 | |||
| fb782432ea | |||
| 71c90109e4 | |||
| 7f42fa12c9 | |||
| 9a69110c7d | |||
| 82711dd537 | |||
| 8ed88c9762 | |||
| 8ba737be7f | |||
| f3ea19fcd7 | |||
| 386ac5b7c1 | |||
| 0e7bb1b041 | |||
| de5007943e | |||
| 634bd617e7 | |||
| 7cc1e4c17f | |||
| a20dcebe0a | |||
| 55e646651e | |||
| 3fc92c9ba0 | |||
| 0fb10f08c9 | |||
| fbfb948e99 | |||
| 25688cde79 | |||
| 3253f4d343 | |||
| c6aaef9d94 | |||
| a5c9e4f255 | |||
| 84c88e5627 | |||
| de4302622b | |||
| 70c9b71f99 | |||
| 89870f44ac | |||
| 176218c360 | |||
| b0aca04420 | |||
| 24a319fd8f | |||
| 093a928e6a |
28
docs/ISSUE-180-SPA-NEXT-ROUND-FINAL-LEADERBOARD-EVIDENCE.md
Normal file
28
docs/ISSUE-180-SPA-NEXT-ROUND-FINAL-LEADERBOARD-EVIDENCE.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# Issue #180 Evidence — SPA next-round + final leaderboard
|
||||
|
||||
## Flow log (Angular SPA)
|
||||
1. Host reaches `reveal` phase and runs `loadScoreboard()` (`GET /lobby/sessions/:code/scoreboard`).
|
||||
2. Host can start next round directly in SPA via `startNextRound()` (`POST /lobby/sessions/:code/rounds/next`) and then session hydrate (`GET /lobby/sessions/:code`) without page reload.
|
||||
3. Host can finish game directly in SPA via `finishGame()` (`POST /lobby/sessions/:code/finish`), rendering winner + sorted final leaderboard (plus raw payload for debug) in the same shell.
|
||||
4. Player SPA renders final leaderboard from refreshed finished-session payload (sorted by score desc, nickname tiebreak) without leaving SPA route.
|
||||
5. Error/retry paths are implemented and covered:
|
||||
- scoreboard failure -> `scoreboardError` + retry button
|
||||
- next-round failure -> `nextRoundError` + retry button
|
||||
- finish-game/final-leaderboard failure -> `finishError` + retry button
|
||||
|
||||
## Test evidence
|
||||
### `frontend/angular` (Vitest)
|
||||
- `src/app/features/host/host-shell.component.spec.ts`
|
||||
- `runs next-round transition without reload and clears scoreboard payload`
|
||||
- `captures finish-game failure for retry and stores final leaderboard on success`
|
||||
- `src/app/features/player/player-shell.component.spec.ts`
|
||||
- `builds final leaderboard in finished status without legacy page hop`
|
||||
|
||||
Result:
|
||||
- Test Files: 2 passed
|
||||
- Tests: 9 passed
|
||||
|
||||
### `frontend` shared SPA tests (regression)
|
||||
Result:
|
||||
- Test Files: 5 passed
|
||||
- Tests: 24 passed
|
||||
@@ -22,13 +22,20 @@ Formål: levere et lille, ensartet evidensformat for release-nær gameplay-smoke
|
||||
- Host authenticated in Django admin: <yes/no>
|
||||
- Active category/questions present: <yes/no>
|
||||
- Participants: host + <N> players
|
||||
- `USE_SPA_UI`: <on/off>
|
||||
- UI route used:
|
||||
- OFF (legacy): `/lobby/ui/host` + `/lobby/ui/player`
|
||||
- ON (SPA shell): `/lobby/ui/host/<spa-path>` + `/lobby/ui/player`
|
||||
|
||||
#### Checks (PASS/FAIL)
|
||||
1. Lobby -> join -> start
|
||||
1. Cutover route sanity
|
||||
- Flag OFF serves legacy UI templates: <pass/fail>
|
||||
- Flag ON serves SPA shell on expected path(s): <pass/fail>
|
||||
2. Lobby -> join -> start
|
||||
- Mixed-case + whitespace session code accepted: <pass/fail>
|
||||
2. One full round to scoreboard
|
||||
3. One full round to scoreboard
|
||||
- submit lie -> mix -> submit guess -> calculate score -> show scoreboard: <pass/fail>
|
||||
3. Next-round + game-end sanity
|
||||
4. Next-round + game-end sanity
|
||||
- next round transitions: <pass/fail>
|
||||
- final leaderboard visible: <pass/fail>
|
||||
|
||||
|
||||
@@ -4,15 +4,20 @@
|
||||
- Host er logget ind i Django.
|
||||
- Mindst én aktiv kategori med spørgsmål findes.
|
||||
|
||||
## Cutover-forudsætning (`USE_SPA_UI`)
|
||||
- `USE_SPA_UI=false` (default): brug legacy routes `/lobby/ui/host` + `/lobby/ui/player`.
|
||||
- `USE_SPA_UI=true`: host må gerne testes på SPA deep-link route `/lobby/ui/host/<spa-path>` (fx `/lobby/ui/host/guess`), player på `/lobby/ui/player`.
|
||||
|
||||
## Flow
|
||||
1. Åbn host-siden på /lobby/ui/host og tryk Opret session.
|
||||
2. Åbn player-siden i 3 faner/enheder på /lobby/ui/player.
|
||||
3. Join alle spillere med sessionkode og nickname.
|
||||
4. Host: vælg kategori, Start runde, Vis spørgsmål.
|
||||
5. Spillere: brug round_question_id og submit løgn.
|
||||
6. Host: Mix svar.
|
||||
7. Spillere: submit gæt.
|
||||
8. Host: Beregn score og Vis scoreboard.
|
||||
9. Host: Næste runde eller Afslut spil.
|
||||
1. Verificér cutover-route matcher valgt flag (legacy vs SPA shell).
|
||||
2. Åbn host-siden og tryk Opret session.
|
||||
3. Åbn player-siden i 3 faner/enheder.
|
||||
4. Join alle spillere med sessionkode og nickname.
|
||||
5. Host: vælg kategori, Start runde, Vis spørgsmål.
|
||||
6. Spillere: brug round_question_id og submit løgn.
|
||||
7. Host: Mix svar.
|
||||
8. Spillere: submit gæt.
|
||||
9. Host: Beregn score og Vis scoreboard.
|
||||
10. Host: Næste runde eller Afslut spil.
|
||||
|
||||
Resultat: En fuld runde kan køres uden rå API-kald fra terminal.
|
||||
|
||||
@@ -15,4 +15,6 @@ Backward compatibility under cutover:
|
||||
## Verifikation
|
||||
- Flag OFF: `UiScreenTests.test_legacy_templates_are_used_when_spa_flag_is_off`
|
||||
- Flag ON (host): `UiScreenTests.test_host_screen_can_render_angular_shell_when_feature_flag_enabled`
|
||||
- Flag ON (host deep-link): `UiScreenTests.test_host_screen_deeplink_preserves_spa_path_when_feature_flag_enabled`
|
||||
- Flag ON (player): `UiScreenTests.test_player_screen_can_render_angular_shell_when_feature_flag_enabled`
|
||||
- Smoke-checkliste for cutover paths: `docs/STAGING_GAMEPLAY_SMOKE_ARTIFACT.md` + `docs/UI_SMOKE.md`
|
||||
|
||||
1375
frontend/angular/package-lock.json
generated
1375
frontend/angular/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,8 @@
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "ng serve",
|
||||
"build": "ng build"
|
||||
"build": "ng build",
|
||||
"test": "vitest run"
|
||||
},
|
||||
"dependencies": {
|
||||
"@angular/common": "^19.2.0",
|
||||
@@ -21,6 +22,7 @@
|
||||
"@angular/build": "^19.2.0",
|
||||
"@angular/cli": "^19.2.0",
|
||||
"@angular/compiler-cli": "^19.2.0",
|
||||
"typescript": "~5.7.2"
|
||||
"typescript": "~5.7.2",
|
||||
"vitest": "^2.1.9"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,47 @@
|
||||
import { Routes } from '@angular/router';
|
||||
|
||||
import {
|
||||
hostRouteContextResolver,
|
||||
hostRouteGuard,
|
||||
playerRouteContextResolver,
|
||||
playerRouteGuard,
|
||||
} from './session-route-context';
|
||||
|
||||
export const routes: Routes = [
|
||||
{
|
||||
path: 'host',
|
||||
resolve: { routeContext: hostRouteContextResolver },
|
||||
canActivate: [hostRouteGuard],
|
||||
loadComponent: () => import('./features/host/host-shell.component').then((m) => m.HostShellComponent),
|
||||
},
|
||||
{
|
||||
path: 'host/:phase',
|
||||
resolve: { routeContext: hostRouteContextResolver },
|
||||
canActivate: [hostRouteGuard],
|
||||
loadComponent: () => import('./features/host/host-shell.component').then((m) => m.HostShellComponent),
|
||||
},
|
||||
{
|
||||
path: 'host/:phase/:context',
|
||||
resolve: { routeContext: hostRouteContextResolver },
|
||||
canActivate: [hostRouteGuard],
|
||||
loadComponent: () => import('./features/host/host-shell.component').then((m) => m.HostShellComponent),
|
||||
},
|
||||
{
|
||||
path: 'player',
|
||||
resolve: { routeContext: playerRouteContextResolver },
|
||||
canActivate: [playerRouteGuard],
|
||||
loadComponent: () => import('./features/player/player-shell.component').then((m) => m.PlayerShellComponent),
|
||||
},
|
||||
{
|
||||
path: 'player/:phase',
|
||||
resolve: { routeContext: playerRouteContextResolver },
|
||||
canActivate: [playerRouteGuard],
|
||||
loadComponent: () => import('./features/player/player-shell.component').then((m) => m.PlayerShellComponent),
|
||||
},
|
||||
{
|
||||
path: 'player/:phase/:context',
|
||||
resolve: { routeContext: playerRouteContextResolver },
|
||||
canActivate: [playerRouteGuard],
|
||||
loadComponent: () => import('./features/player/player-shell.component').then((m) => m.PlayerShellComponent),
|
||||
},
|
||||
{ path: '', pathMatch: 'full', redirectTo: 'player' },
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { HostShellComponent } from './host-shell.component';
|
||||
|
||||
type FetchMock = ReturnType<typeof vi.fn>;
|
||||
|
||||
function jsonResponse(status: number, body: unknown) {
|
||||
return {
|
||||
ok: status >= 200 && status < 300,
|
||||
status,
|
||||
json: vi.fn().mockResolvedValue(body),
|
||||
} as unknown as Response;
|
||||
}
|
||||
|
||||
function sessionDetailPayload(status: string, options?: { roundQuestionId?: number | null }) {
|
||||
const roundQuestionId = options?.roundQuestionId ?? 41;
|
||||
|
||||
return {
|
||||
session: {
|
||||
code: 'ABCD12',
|
||||
status,
|
||||
host_id: 1,
|
||||
current_round: status === 'lobby' ? 2 : 1,
|
||||
players_count: 2,
|
||||
},
|
||||
round_question:
|
||||
roundQuestionId === null
|
||||
? null
|
||||
: {
|
||||
id: roundQuestionId,
|
||||
round_number: 1,
|
||||
prompt: 'Q?',
|
||||
shown_at: '2026-01-01T00:00:00Z',
|
||||
answers: [],
|
||||
},
|
||||
players: [
|
||||
{ id: 1, nickname: 'Host', score: 0, is_connected: true },
|
||||
{ id: 2, nickname: 'Mads', score: 120, is_connected: true },
|
||||
],
|
||||
phase_view_model: {
|
||||
status,
|
||||
round_number: 1,
|
||||
players_count: 2,
|
||||
constraints: {
|
||||
min_players_to_start: 2,
|
||||
max_players_mvp: 8,
|
||||
min_players_reached: true,
|
||||
max_players_allowed: true,
|
||||
},
|
||||
host: {
|
||||
can_start_round: status === 'lobby',
|
||||
can_show_question: status === 'lie',
|
||||
can_mix_answers: status === 'lie',
|
||||
can_calculate_scores: status === 'guess',
|
||||
can_reveal_scoreboard: status === 'reveal',
|
||||
can_start_next_round: status === 'scoreboard',
|
||||
can_finish_game: status === 'scoreboard',
|
||||
},
|
||||
player: {
|
||||
can_join: status === 'lobby',
|
||||
can_submit_lie: status === 'lie',
|
||||
can_submit_guess: status === 'guess',
|
||||
can_view_final_result: status === 'finished',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('HostShellComponent gameplay wiring', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('runs startRound transition and refreshes session details', async () => {
|
||||
const fetchMock: FetchMock = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(
|
||||
jsonResponse(201, {
|
||||
session: { code: 'ABCD12', status: 'lie', current_round: 1 },
|
||||
round: { number: 1, category: { slug: 'history', name: 'History' } },
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('lie')));
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new HostShellComponent();
|
||||
component.sessionCode = ' abcd12 ';
|
||||
component.categorySlug = ' history ';
|
||||
|
||||
await component.startRound();
|
||||
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'/lobby/sessions/ABCD12/rounds/start',
|
||||
expect.objectContaining({ method: 'POST', body: JSON.stringify({ category_slug: 'history' }) })
|
||||
);
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(2, '/lobby/sessions/ABCD12', expect.objectContaining({ method: 'GET' }));
|
||||
expect(component.session?.session.status).toBe('lie');
|
||||
expect(component.roundQuestionId).toBe('41');
|
||||
expect(component.loading).toBe(false);
|
||||
});
|
||||
|
||||
it('captures scoreboard error for retry path', async () => {
|
||||
const fetchMock: FetchMock = vi.fn().mockResolvedValue(jsonResponse(500, { error: 'Scoreboard unavailable' }));
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new HostShellComponent();
|
||||
component.sessionCode = 'ABCD12';
|
||||
|
||||
await component.loadScoreboard();
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith('/lobby/sessions/ABCD12/scoreboard', expect.objectContaining({ method: 'GET' }));
|
||||
expect(component.scoreboardError).toContain('Scoreboard failed: Scoreboard unavailable');
|
||||
expect(component.loading).toBe(false);
|
||||
});
|
||||
|
||||
it('wires showQuestion, mixAnswers and calculateScores with expected request payloads', async () => {
|
||||
const fetchMock: FetchMock = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(
|
||||
jsonResponse(200, {
|
||||
round_question: {
|
||||
id: 77,
|
||||
round_number: 1,
|
||||
prompt: 'Q?',
|
||||
shown_at: '2026-01-01T00:00:00Z',
|
||||
lie_deadline_at: '2026-01-01T00:00:45Z',
|
||||
},
|
||||
config: { lie_seconds: 45 },
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('lie', { roundQuestionId: 77 })))
|
||||
.mockResolvedValueOnce(
|
||||
jsonResponse(200, {
|
||||
session: { code: 'ABCD12', status: 'guess', current_round: 1 },
|
||||
round_question: { id: 77, round_number: 1 },
|
||||
answers: [{ text: 'A' }],
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('guess', { roundQuestionId: 77 })))
|
||||
.mockResolvedValueOnce(
|
||||
jsonResponse(200, {
|
||||
session: { code: 'ABCD12', status: 'reveal', current_round: 1 },
|
||||
round_question: { id: 77, round_number: 1 },
|
||||
events_created: 2,
|
||||
leaderboard: [{ id: 1, nickname: 'Luna', score: 320 }],
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('reveal', { roundQuestionId: 77 })));
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new HostShellComponent();
|
||||
component.sessionCode = ' abcd12 ';
|
||||
component.roundQuestionId = ' 77 ';
|
||||
|
||||
await component.showQuestion();
|
||||
await component.mixAnswers();
|
||||
await component.calculateScores();
|
||||
|
||||
expect(component.error).toBe('');
|
||||
expect(component.loading).toBe(false);
|
||||
});
|
||||
|
||||
it('runs next-round transition without reload and clears scoreboard payload', async () => {
|
||||
const fetchMock: FetchMock = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(jsonResponse(200, { session: { code: 'ABCD12', status: 'lobby', current_round: 2 } }))
|
||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('lobby', { roundQuestionId: null })));
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new HostShellComponent();
|
||||
component.sessionCode = ' abcd12 ';
|
||||
component.scoreboardPayload = '{"leaderboard":[]}';
|
||||
component.finalLeaderboardPayload = '{"leaderboard":[{"nickname":"Old","score":1}]}' ;
|
||||
component.finalLeaderboard = [{ id: 9, nickname: 'Old', score: 1 }];
|
||||
|
||||
await component.startNextRound();
|
||||
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'/lobby/sessions/ABCD12/rounds/next',
|
||||
expect.objectContaining({ method: 'POST', body: JSON.stringify({}) })
|
||||
);
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(2, '/lobby/sessions/ABCD12', expect.objectContaining({ method: 'GET' }));
|
||||
expect(component.session?.session.status).toBe('lobby');
|
||||
expect(component.scoreboardPayload).toBe('');
|
||||
expect(component.finalLeaderboardPayload).toBe('');
|
||||
expect(component.finalLeaderboard).toEqual([]);
|
||||
expect(component.nextRoundError).toBe('');
|
||||
});
|
||||
|
||||
it('captures finish-game failure for retry and stores final leaderboard on success', async () => {
|
||||
const fetchMock: FetchMock = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(jsonResponse(503, { error: 'Final leaderboard timeout' }))
|
||||
.mockResolvedValueOnce(
|
||||
jsonResponse(200, {
|
||||
session: { code: 'ABCD12', status: 'finished', current_round: 2 },
|
||||
winner: { id: 1, nickname: 'Luna', score: 320 },
|
||||
leaderboard: [
|
||||
{ id: 2, nickname: 'Mads', score: 120 },
|
||||
{ id: 1, nickname: 'Luna', score: 320 },
|
||||
],
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('finished', { roundQuestionId: null })));
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new HostShellComponent();
|
||||
component.sessionCode = 'ABCD12';
|
||||
|
||||
await component.finishGame();
|
||||
expect(component.finishError).toContain('Finish game failed: Final leaderboard timeout');
|
||||
|
||||
await component.finishGame();
|
||||
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'/lobby/sessions/ABCD12/finish',
|
||||
expect.objectContaining({ method: 'POST', body: JSON.stringify({}) })
|
||||
);
|
||||
expect(component.finishError).toBe('');
|
||||
expect(component.finalLeaderboardPayload).toContain('"status": "finished"');
|
||||
expect(component.finalWinner?.nickname).toBe('Luna');
|
||||
expect(component.finalLeaderboard.map((entry) => entry.nickname)).toEqual(['Luna', 'Mads']);
|
||||
});
|
||||
|
||||
it('guards next-round and finish actions when session code is missing', async () => {
|
||||
const fetchMock: FetchMock = vi.fn();
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new HostShellComponent();
|
||||
component.sessionCode = ' ';
|
||||
|
||||
await component.startNextRound();
|
||||
await component.finishGame();
|
||||
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
expect(component.nextRoundError).toContain('Session code is required');
|
||||
expect(component.finishError).toContain('Session code is required');
|
||||
});
|
||||
|
||||
it('bootstraps refresh from resolver route context on init', () => {
|
||||
vi.stubGlobal('window', {
|
||||
sessionStorage: {
|
||||
getItem: vi.fn().mockReturnValue(null),
|
||||
setItem: vi.fn(),
|
||||
},
|
||||
});
|
||||
|
||||
const component = new HostShellComponent({ snapshot: { data: { routeContext: { sessionCode: 'ab12' } } } } as never);
|
||||
const refreshSpy = vi.spyOn(component, 'refreshSession').mockResolvedValue();
|
||||
|
||||
component.ngOnInit();
|
||||
|
||||
expect(component.sessionCode).toBe('AB12');
|
||||
expect(refreshSpy).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,22 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component } from '@angular/core';
|
||||
import { Component, OnInit, Optional } from '@angular/core';
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
|
||||
import { createApiClient } from '../../../../../src/api/client';
|
||||
import type { FinishGameResponse, ScoreboardResponse } from '../../../../../src/api/types';
|
||||
import { createVerticalSliceController } from '../../../../../src/spa/vertical-slice';
|
||||
import type { RouteSessionContext } from '../../session-route-context';
|
||||
|
||||
interface SessionDetail {
|
||||
session: { code: string; status: string; current_round: number };
|
||||
round_question: { id: number; prompt: string; answers: Array<{ text: string }> } | null;
|
||||
players: Array<{ id: number; nickname: string; score: number }>;
|
||||
}
|
||||
|
||||
type LeaderboardEntry = ScoreboardResponse['leaderboard'][number];
|
||||
type LeaderboardResponse = FinishGameResponse;
|
||||
|
||||
@Component({
|
||||
selector: 'app-host-shell',
|
||||
standalone: true,
|
||||
@@ -24,11 +33,17 @@ interface SessionDetail {
|
||||
<button (click)="mixAnswers()" [disabled]="loading || !roundQuestionId">Mix answers → guess</button>
|
||||
<button (click)="calculateScores()" [disabled]="loading || !roundQuestionId">Calculate scores → reveal</button>
|
||||
<button (click)="loadScoreboard()" [disabled]="loading">Load scoreboard</button>
|
||||
<button (click)="startNextRound()" [disabled]="loading">Start next round</button>
|
||||
<button (click)="finishGame()" [disabled]="loading">Finish game</button>
|
||||
<button *ngIf="scoreboardError" (click)="loadScoreboard()" [disabled]="loading">Retry scoreboard</button>
|
||||
<button *ngIf="nextRoundError" (click)="startNextRound()" [disabled]="loading">Retry next round</button>
|
||||
<button *ngIf="finishError" (click)="finishGame()" [disabled]="loading">Retry finish game</button>
|
||||
</div>
|
||||
|
||||
<p *ngIf="error" class="error">{{ error }}</p>
|
||||
<p *ngIf="scoreboardError" class="error">{{ scoreboardError }}</p>
|
||||
<p *ngIf="nextRoundError" class="error">{{ nextRoundError }}</p>
|
||||
<p *ngIf="finishError" class="error">{{ finishError }}</p>
|
||||
|
||||
<div *ngIf="session" class="panel">
|
||||
<p><strong>Status:</strong> {{ session.session.status }} · round {{ session.session.current_round }}</p>
|
||||
@@ -38,23 +53,66 @@ interface SessionDetail {
|
||||
<li *ngFor="let p of session.players">{{ p.nickname }}: {{ p.score }}</li>
|
||||
</ul>
|
||||
<pre *ngIf="scoreboardPayload">{{ scoreboardPayload }}</pre>
|
||||
<div *ngIf="finalLeaderboard.length">
|
||||
<h3>Final leaderboard</h3>
|
||||
<p *ngIf="finalWinner"><strong>Winner:</strong> {{ finalWinner.nickname }} ({{ finalWinner.score }} pts)</p>
|
||||
<ol>
|
||||
<li *ngFor="let entry of finalLeaderboard">{{ entry.nickname }}: {{ entry.score }}</li>
|
||||
</ol>
|
||||
</div>
|
||||
<pre *ngIf="finalLeaderboardPayload">{{ finalLeaderboardPayload }}</pre>
|
||||
</div>
|
||||
`,
|
||||
})
|
||||
export class HostShellComponent {
|
||||
export class HostShellComponent implements OnInit {
|
||||
sessionCode = '';
|
||||
categorySlug = 'general';
|
||||
roundQuestionId = '';
|
||||
loading = false;
|
||||
error = '';
|
||||
scoreboardError = '';
|
||||
nextRoundError = '';
|
||||
finishError = '';
|
||||
scoreboardPayload = '';
|
||||
finalLeaderboardPayload = '';
|
||||
finalLeaderboard: LeaderboardEntry[] = [];
|
||||
finalWinner: LeaderboardEntry | null = null;
|
||||
session: SessionDetail | null = null;
|
||||
|
||||
private readonly api = createApiClient();
|
||||
private readonly controller = createVerticalSliceController(this.api);
|
||||
|
||||
constructor(@Optional() private readonly route: ActivatedRoute | null = null) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
if (typeof window === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
const routeContext = this.route?.snapshot.data['routeContext'] as RouteSessionContext | undefined;
|
||||
const codeFromRoute = routeContext?.sessionCode ?? '';
|
||||
const storedCode = window.sessionStorage.getItem('wpp.host-session-code') ?? '';
|
||||
const candidate = codeFromRoute || storedCode;
|
||||
|
||||
if (!candidate) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sessionCode = this.normalizeCode(candidate);
|
||||
this.persistSessionCode(this.sessionCode);
|
||||
void this.refreshSession();
|
||||
}
|
||||
|
||||
private normalizeCode(value: string): string {
|
||||
return value.trim().toUpperCase();
|
||||
}
|
||||
|
||||
private persistSessionCode(code: string): void {
|
||||
if (typeof window !== 'undefined') {
|
||||
window.sessionStorage.setItem('wpp.host-session-code', code);
|
||||
}
|
||||
}
|
||||
|
||||
private async request<T>(path: string, method: 'GET' | 'POST', payload?: unknown): Promise<T> {
|
||||
const response = await fetch(path, {
|
||||
method,
|
||||
@@ -78,11 +136,20 @@ export class HostShellComponent {
|
||||
this.loading = true;
|
||||
this.error = '';
|
||||
this.scoreboardError = '';
|
||||
this.nextRoundError = '';
|
||||
this.finishError = '';
|
||||
try {
|
||||
const code = this.normalizeCode(this.sessionCode);
|
||||
this.session = await this.request<SessionDetail>(`/lobby/sessions/${encodeURIComponent(code)}`, 'GET');
|
||||
const state = await this.controller.hydrateLobby(this.sessionCode);
|
||||
if (!state.session || state.errorMessage) {
|
||||
throw new Error(state.errorMessage ?? 'Unknown error');
|
||||
}
|
||||
this.session = state.session as SessionDetail;
|
||||
this.sessionCode = this.session.session.code;
|
||||
this.persistSessionCode(this.sessionCode);
|
||||
this.roundQuestionId = this.session.round_question?.id ? String(this.session.round_question.id) : '';
|
||||
if (this.session.session.status !== 'finished') {
|
||||
this.resetFinalLeaderboard();
|
||||
}
|
||||
} catch (error) {
|
||||
this.error = `Session refresh failed: ${(error as Error).message}`;
|
||||
} finally {
|
||||
@@ -92,11 +159,16 @@ export class HostShellComponent {
|
||||
|
||||
async startRound(): Promise<void> {
|
||||
await this.runAction(async () => {
|
||||
const code = this.normalizeCode(this.sessionCode);
|
||||
await this.request(`/lobby/sessions/${encodeURIComponent(code)}/rounds/start`, 'POST', {
|
||||
category_slug: this.categorySlug.trim(),
|
||||
});
|
||||
await this.refreshSession();
|
||||
const state = await this.controller.startRound(this.sessionCode, this.categorySlug.trim());
|
||||
if (!state.session || state.errorMessage) {
|
||||
throw new Error(state.errorMessage ?? 'Unknown error');
|
||||
}
|
||||
this.session = state.session as SessionDetail;
|
||||
this.sessionCode = this.session.session.code;
|
||||
this.persistSessionCode(this.sessionCode);
|
||||
this.roundQuestionId = this.session.round_question?.id ? String(this.session.round_question.id) : '';
|
||||
this.scoreboardPayload = '';
|
||||
this.resetFinalLeaderboard();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -142,6 +214,58 @@ export class HostShellComponent {
|
||||
}
|
||||
}
|
||||
|
||||
async startNextRound(): Promise<void> {
|
||||
this.loading = true;
|
||||
this.nextRoundError = '';
|
||||
this.error = '';
|
||||
try {
|
||||
const code = this.normalizeCode(this.sessionCode);
|
||||
if (!code) {
|
||||
throw new Error('Session code is required');
|
||||
}
|
||||
await this.request(`/lobby/sessions/${encodeURIComponent(code)}/rounds/next`, 'POST', {});
|
||||
this.scoreboardPayload = '';
|
||||
this.resetFinalLeaderboard();
|
||||
await this.refreshSession();
|
||||
} catch (error) {
|
||||
this.nextRoundError = `Next round failed: ${(error as Error).message}`;
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async finishGame(): Promise<void> {
|
||||
this.loading = true;
|
||||
this.finishError = '';
|
||||
this.error = '';
|
||||
try {
|
||||
const code = this.normalizeCode(this.sessionCode);
|
||||
if (!code) {
|
||||
throw new Error('Session code is required');
|
||||
}
|
||||
const payload = await this.request<LeaderboardResponse>(`/lobby/sessions/${encodeURIComponent(code)}/finish`, 'POST', {});
|
||||
this.finalLeaderboardPayload = JSON.stringify(payload, null, 2);
|
||||
this.finalLeaderboard = [...payload.leaderboard].sort((a, b) => {
|
||||
if (b.score !== a.score) {
|
||||
return b.score - a.score;
|
||||
}
|
||||
return a.nickname.localeCompare(b.nickname);
|
||||
});
|
||||
this.finalWinner = payload.winner ?? this.finalLeaderboard[0] ?? null;
|
||||
await this.refreshSession();
|
||||
} catch (error) {
|
||||
this.finishError = `Finish game failed: ${(error as Error).message}`;
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
private resetFinalLeaderboard(): void {
|
||||
this.finalLeaderboardPayload = '';
|
||||
this.finalLeaderboard = [];
|
||||
this.finalWinner = null;
|
||||
}
|
||||
|
||||
private async runAction(action: () => Promise<void>): Promise<void> {
|
||||
this.loading = true;
|
||||
this.error = '';
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { PlayerShellComponent } from './player-shell.component';
|
||||
|
||||
type FetchMock = ReturnType<typeof vi.fn>;
|
||||
|
||||
function jsonResponse(status: number, body: unknown) {
|
||||
return {
|
||||
ok: status >= 200 && status < 300,
|
||||
status,
|
||||
json: vi.fn().mockResolvedValue(body),
|
||||
} as unknown as Response;
|
||||
}
|
||||
|
||||
function sessionDetailPayload(status: string, options?: { answers?: string[]; players?: Array<{ id: number; nickname: string; score: number }>; roundQuestionId?: number | null }) {
|
||||
const answers = options?.answers ?? [];
|
||||
const roundQuestionId = options?.roundQuestionId ?? 11;
|
||||
|
||||
return {
|
||||
session: {
|
||||
code: 'ABCD12',
|
||||
status,
|
||||
host_id: null,
|
||||
current_round: 1,
|
||||
players_count: (options?.players ?? []).length,
|
||||
},
|
||||
round_question:
|
||||
roundQuestionId === null
|
||||
? null
|
||||
: {
|
||||
id: roundQuestionId,
|
||||
round_number: 1,
|
||||
prompt: 'Q?',
|
||||
shown_at: '2026-01-01T00:00:00Z',
|
||||
answers: answers.map((text) => ({ text })),
|
||||
},
|
||||
players: (options?.players ?? []).map((player) => ({
|
||||
...player,
|
||||
is_connected: true,
|
||||
})),
|
||||
phase_view_model: {
|
||||
status,
|
||||
round_number: 1,
|
||||
players_count: (options?.players ?? []).length,
|
||||
constraints: {
|
||||
min_players_to_start: 2,
|
||||
max_players_mvp: 8,
|
||||
min_players_reached: true,
|
||||
max_players_allowed: true,
|
||||
},
|
||||
host: {
|
||||
can_start_round: false,
|
||||
can_show_question: false,
|
||||
can_mix_answers: false,
|
||||
can_calculate_scores: false,
|
||||
can_reveal_scoreboard: false,
|
||||
can_start_next_round: false,
|
||||
can_finish_game: false,
|
||||
},
|
||||
player: {
|
||||
can_join: status === 'lobby',
|
||||
can_submit_lie: status === 'lie',
|
||||
can_submit_guess: status === 'guess',
|
||||
can_view_final_result: status === 'finished',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('PlayerShellComponent gameplay wiring', () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('clears selected guess when refreshed status is no longer guess', async () => {
|
||||
const fetchMock: FetchMock = vi.fn().mockResolvedValue(
|
||||
jsonResponse(200, sessionDetailPayload('reveal', { answers: ['A'] }))
|
||||
);
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new PlayerShellComponent();
|
||||
component.sessionCode = 'abcd12';
|
||||
component.selectedGuess = 'A';
|
||||
|
||||
await component.refreshSession();
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
'/lobby/sessions/ABCD12',
|
||||
expect.objectContaining({ method: 'GET' })
|
||||
);
|
||||
expect(component.selectedGuess).toBe('');
|
||||
});
|
||||
|
||||
it('surfaces lie submit error and allows retry success flow', async () => {
|
||||
const fetchMock: FetchMock = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(jsonResponse(500, { error: 'Temporary submit outage' }))
|
||||
.mockResolvedValueOnce(jsonResponse(201, { lie: { id: 1, player_id: 9, round_question_id: 11, text: 'my lie', created_at: '2026-01-01T00:00:01Z' }, window: { lie_deadline_at: '2026-01-01T00:00:45Z' } }))
|
||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('guess', { answers: ['A', 'B'] })));
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new PlayerShellComponent();
|
||||
component.sessionCode = 'ABCD12';
|
||||
component.playerId = 9;
|
||||
component.sessionToken = 'token-1';
|
||||
component.lieText = 'my lie';
|
||||
component.session = {
|
||||
session: { code: 'ABCD12', status: 'lie', current_round: 1 },
|
||||
round_question: { id: 11, prompt: 'Q?', answers: [] },
|
||||
players: [],
|
||||
};
|
||||
|
||||
await component.submitLie();
|
||||
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'/lobby/sessions/ABCD12/questions/11/lies/submit',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ player_id: 9, session_token: 'token-1', text: 'my lie' }),
|
||||
})
|
||||
);
|
||||
expect(component.submitError?.kind).toBe('lie');
|
||||
expect(component.submitError?.message).toContain('Lie submit failed: Temporary submit outage');
|
||||
|
||||
await component.submitLie();
|
||||
|
||||
expect(component.submitError).toBeNull();
|
||||
expect(component.session?.session.status).toBe('guess');
|
||||
expect(fetchMock).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it('builds final leaderboard in finished status without legacy page hop', async () => {
|
||||
const fetchMock: FetchMock = vi.fn().mockResolvedValue(
|
||||
jsonResponse(
|
||||
200,
|
||||
sessionDetailPayload('finished', {
|
||||
roundQuestionId: null,
|
||||
players: [
|
||||
{ id: 2, nickname: 'Mads', score: 150 },
|
||||
{ id: 1, nickname: 'Luna', score: 320 },
|
||||
],
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new PlayerShellComponent();
|
||||
component.sessionCode = 'ABCD12';
|
||||
|
||||
await component.refreshSession();
|
||||
|
||||
expect(component.finalLeaderboard.map((entry) => entry.nickname)).toEqual(['Luna', 'Mads']);
|
||||
});
|
||||
|
||||
it('surfaces guess submit error and retries with selected answer payload', async () => {
|
||||
const fetchMock: FetchMock = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(jsonResponse(503, { error: 'Guess queue busy' }))
|
||||
.mockResolvedValueOnce(jsonResponse(201, { guess: { id: 2, player_id: 9, round_question_id: 11, selected_text: 'B', is_correct: false, fooled_player_id: 3, created_at: '2026-01-01T00:00:10Z' }, window: { guess_deadline_at: '2026-01-01T00:01:30Z' } }))
|
||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('reveal', { answers: ['A', 'B'] })));
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new PlayerShellComponent();
|
||||
component.sessionCode = ' abcd12 ';
|
||||
component.playerId = 9;
|
||||
component.sessionToken = 'token-1';
|
||||
component.selectedGuess = 'B';
|
||||
component.session = {
|
||||
session: { code: 'ABCD12', status: 'guess', current_round: 1 },
|
||||
round_question: { id: 11, prompt: 'Q?', answers: [{ text: 'A' }, { text: 'B' }] },
|
||||
players: [],
|
||||
};
|
||||
|
||||
await component.submitGuess();
|
||||
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'/lobby/sessions/ABCD12/questions/11/guesses/submit',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ player_id: 9, session_token: 'token-1', selected_text: 'B' }),
|
||||
})
|
||||
);
|
||||
expect(component.submitError?.kind).toBe('guess');
|
||||
expect(component.submitError?.message).toContain('Guess submit failed: Guess queue busy');
|
||||
|
||||
await component.submitGuess();
|
||||
|
||||
expect(component.submitError).toBeNull();
|
||||
expect(component.session?.session.status).toBe('reveal');
|
||||
expect(component.selectedGuess).toBe('');
|
||||
expect(fetchMock).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it('enters reconnecting state when network request fails while online', async () => {
|
||||
vi.stubGlobal('navigator', { onLine: true });
|
||||
|
||||
const fetchMock: FetchMock = vi.fn().mockRejectedValueOnce(new TypeError('Failed to fetch'));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new PlayerShellComponent();
|
||||
component.sessionCode = 'ABCD12';
|
||||
|
||||
await component.refreshSession();
|
||||
|
||||
expect(component.connectionState).toBe('reconnecting');
|
||||
expect(component.error).toContain('Session refresh failed: Could not load lobby status.');
|
||||
});
|
||||
|
||||
it('uses offline state when browser reports disconnected network', async () => {
|
||||
vi.stubGlobal('navigator', { onLine: false });
|
||||
|
||||
const fetchMock: FetchMock = vi.fn().mockRejectedValue(new TypeError('Failed to fetch'));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new PlayerShellComponent();
|
||||
component.sessionCode = 'ABCD12';
|
||||
|
||||
await component.refreshSession();
|
||||
|
||||
expect(component.connectionState).toBe('offline');
|
||||
expect(component.error).toContain('Session refresh failed');
|
||||
});
|
||||
|
||||
it('tracks loading transition message for join action', async () => {
|
||||
let resolveJoin: ((value: Response) => void) | null = null;
|
||||
const fetchMock: FetchMock = vi.fn().mockImplementation(
|
||||
() =>
|
||||
new Promise<Response>((resolve) => {
|
||||
resolveJoin = resolve;
|
||||
})
|
||||
);
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const component = new PlayerShellComponent();
|
||||
component.sessionCode = 'ABCD12';
|
||||
component.nickname = 'Luna';
|
||||
|
||||
const joinPromise = component.joinSession();
|
||||
|
||||
expect(component.loading).toBe(true);
|
||||
expect(component.loadingMessage).toBe('Joining session… restoring your player state.');
|
||||
|
||||
resolveJoin?.(jsonResponse(201, sessionDetailPayload('lobby', { roundQuestionId: null })));
|
||||
await joinPromise;
|
||||
|
||||
expect(component.loading).toBe(false);
|
||||
expect(component.loadingTransition).toBeNull();
|
||||
});
|
||||
|
||||
it('hydrates session context from resolver payload on init', () => {
|
||||
const component = new PlayerShellComponent({
|
||||
snapshot: {
|
||||
data: {
|
||||
routeContext: {
|
||||
sessionCode: 'ab12',
|
||||
playerId: 7,
|
||||
token: 'tok-7',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as never);
|
||||
const refreshSpy = vi.spyOn(component, 'refreshSession').mockResolvedValue();
|
||||
|
||||
component.ngOnInit();
|
||||
|
||||
expect(component.sessionCode).toBe('AB12');
|
||||
expect(component.playerId).toBe(7);
|
||||
expect(component.sessionToken).toBe('tok-7');
|
||||
expect(refreshSpy).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('returnToJoin clears persisted session context and transient state', () => {
|
||||
const values = new Map<string, string>();
|
||||
const localStorage = {
|
||||
getItem: vi.fn((key: string) => values.get(key) ?? null),
|
||||
setItem: vi.fn((key: string, value: string) => {
|
||||
values.set(key, value);
|
||||
}),
|
||||
removeItem: vi.fn((key: string) => {
|
||||
values.delete(key);
|
||||
}),
|
||||
};
|
||||
|
||||
vi.stubGlobal('window', {
|
||||
addEventListener: vi.fn(),
|
||||
removeEventListener: vi.fn(),
|
||||
localStorage,
|
||||
});
|
||||
|
||||
values.set('wpp.session-context', JSON.stringify({ sessionCode: 'ABCD12', playerId: 9, token: 'tok-1' }));
|
||||
|
||||
const component = new PlayerShellComponent();
|
||||
component.sessionCode = 'ABCD12';
|
||||
component.playerId = 9;
|
||||
component.sessionToken = 'tok-1';
|
||||
component.error = 'Session refresh failed';
|
||||
component.submitError = { kind: 'guess', message: 'Guess submit failed' };
|
||||
component.session = {
|
||||
session: { code: 'ABCD12', status: 'guess', current_round: 1 },
|
||||
round_question: { id: 11, prompt: 'Q?', answers: [{ text: 'A' }] },
|
||||
players: [],
|
||||
};
|
||||
|
||||
component.returnToJoin();
|
||||
|
||||
expect(component.playerId).toBe(0);
|
||||
expect(component.sessionToken).toBe('');
|
||||
expect(component.session).toBeNull();
|
||||
expect(component.error).toBe('');
|
||||
expect(component.submitError).toBeNull();
|
||||
expect(values.get('wpp.session-context')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,10 +1,27 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component } from '@angular/core';
|
||||
import { Component, OnDestroy, OnInit, Optional } from '@angular/core';
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
|
||||
import { createApiClient } from '../../../../../src/api/client';
|
||||
import { createSessionContextStore } from '../../../../../src/spa/session-context-store';
|
||||
import { createVerticalSliceController } from '../../../../../src/spa/vertical-slice';
|
||||
import type { RouteSessionContext } from '../../session-route-context';
|
||||
|
||||
interface SessionDetail {
|
||||
session: { code: string; status: string; current_round: number };
|
||||
round_question: { id: number; prompt: string; answers: Array<{ text: string }> } | null;
|
||||
players: Array<{ id: number; nickname: string; score: number }>;
|
||||
}
|
||||
|
||||
type ConnectionState = 'online' | 'reconnecting' | 'offline';
|
||||
type LoadingTransition = 'refresh' | 'join' | 'submit-lie' | 'submit-guess' | null;
|
||||
|
||||
function resolveLocalStorage(): Storage | undefined {
|
||||
if (typeof window === 'undefined') {
|
||||
return undefined;
|
||||
}
|
||||
return window.localStorage;
|
||||
}
|
||||
|
||||
@Component({
|
||||
@@ -21,6 +38,19 @@ interface SessionDetail {
|
||||
<button (click)="joinSession()" [disabled]="loading">Join</button>
|
||||
</div>
|
||||
|
||||
<p *ngIf="connectionState === 'reconnecting'" class="error">
|
||||
Reconnecting… trying to refresh session state.
|
||||
<button type="button" (click)="retryReconnect()" [disabled]="loading">Retry now</button>
|
||||
<button type="button" (click)="returnToJoin()" [disabled]="loading">Back to join</button>
|
||||
</p>
|
||||
<p *ngIf="connectionState === 'offline'" class="error">
|
||||
You are offline. Reconnect to continue gameplay.
|
||||
<button type="button" (click)="retryReconnect()" [disabled]="loading">Retry now</button>
|
||||
<button type="button" (click)="returnToJoin()" [disabled]="loading">Back to join</button>
|
||||
</p>
|
||||
|
||||
<p *ngIf="loading" class="hint">{{ loadingMessage }}</p>
|
||||
|
||||
<div class="panel" *ngIf="session">
|
||||
<p><strong>Status:</strong> {{ session.session.status }}</p>
|
||||
<p *ngIf="session.round_question"><strong>Prompt:</strong> {{ session.round_question.prompt }}</p>
|
||||
@@ -43,13 +73,25 @@ interface SessionDetail {
|
||||
|
||||
<button (click)="submitGuess()" [disabled]="loading || session.session.status !== 'guess' || !selectedGuess">Submit guess</button>
|
||||
<button *ngIf="submitError?.kind === 'guess'" (click)="submitGuess()" [disabled]="loading">Retry guess submit</button>
|
||||
|
||||
<div *ngIf="session.session.status === 'finished' && finalLeaderboard.length">
|
||||
<h3>Final leaderboard</h3>
|
||||
<ol>
|
||||
<li *ngFor="let entry of finalLeaderboard">{{ entry.nickname }}: {{ entry.score }}</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p *ngIf="error" class="error">{{ error }}</p>
|
||||
<p *ngIf="submitError" class="error">{{ submitError.message }}</p>
|
||||
|
||||
<div class="panel" *ngIf="error || submitError">
|
||||
<button type="button" (click)="retryReconnect()" [disabled]="loading">Retry</button>
|
||||
<button type="button" (click)="returnToJoin()" [disabled]="loading">Back to join</button>
|
||||
</div>
|
||||
`,
|
||||
})
|
||||
export class PlayerShellComponent {
|
||||
export class PlayerShellComponent implements OnInit, OnDestroy {
|
||||
sessionCode = '';
|
||||
nickname = '';
|
||||
playerId = 0;
|
||||
@@ -60,11 +102,163 @@ export class PlayerShellComponent {
|
||||
error = '';
|
||||
submitError: { kind: 'lie' | 'guess'; message: string } | null = null;
|
||||
session: SessionDetail | null = null;
|
||||
finalLeaderboard: Array<{ id: number; nickname: string; score: number }> = [];
|
||||
connectionState: ConnectionState = 'online';
|
||||
loadingTransition: LoadingTransition = null;
|
||||
|
||||
private readonly sessionContextStore = createSessionContextStore(resolveLocalStorage());
|
||||
private readonly controller = createVerticalSliceController(createApiClient(), this.sessionContextStore);
|
||||
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
constructor(@Optional() private readonly route: ActivatedRoute | null = null) {
|
||||
if (typeof navigator !== 'undefined' && !navigator.onLine) {
|
||||
this.connectionState = 'offline';
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('online', this.handleOnline);
|
||||
window.addEventListener('offline', this.handleOffline);
|
||||
}
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
const routeContext = this.route?.snapshot.data['routeContext'] as RouteSessionContext | undefined;
|
||||
const persistedContext = this.sessionContextStore.get();
|
||||
|
||||
this.playerId = routeContext?.playerId ?? persistedContext?.playerId ?? 0;
|
||||
this.sessionToken = routeContext?.token ?? persistedContext?.token ?? '';
|
||||
|
||||
const candidate = routeContext?.sessionCode || persistedContext?.sessionCode || '';
|
||||
if (!candidate) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sessionCode = this.normalizeCode(candidate);
|
||||
void this.refreshSession();
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
if (typeof window !== 'undefined') {
|
||||
window.removeEventListener('online', this.handleOnline);
|
||||
window.removeEventListener('offline', this.handleOffline);
|
||||
}
|
||||
this.clearReconnectTimer();
|
||||
}
|
||||
|
||||
private readonly handleOnline = (): void => {
|
||||
this.connectionState = 'reconnecting';
|
||||
void this.retryReconnect();
|
||||
};
|
||||
|
||||
private readonly handleOffline = (): void => {
|
||||
this.connectionState = 'offline';
|
||||
this.clearReconnectTimer();
|
||||
};
|
||||
|
||||
private clearReconnectTimer(): void {
|
||||
if (this.reconnectTimer) {
|
||||
clearTimeout(this.reconnectTimer);
|
||||
this.reconnectTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
get loadingMessage(): string {
|
||||
switch (this.loadingTransition) {
|
||||
case 'join':
|
||||
return 'Joining session… restoring your player state.';
|
||||
case 'submit-lie':
|
||||
return 'Submitting lie… waiting for guess phase.';
|
||||
case 'submit-guess':
|
||||
return 'Submitting guess… waiting for reveal.';
|
||||
case 'refresh':
|
||||
default:
|
||||
return 'Loading latest session state…';
|
||||
}
|
||||
}
|
||||
|
||||
private normalizeCode(value: string): string {
|
||||
return value.trim().toUpperCase();
|
||||
}
|
||||
|
||||
private toMessage(error: unknown): string {
|
||||
if (error instanceof Error && error.message) {
|
||||
return error.message;
|
||||
}
|
||||
return 'Unknown error';
|
||||
}
|
||||
|
||||
private markOnline(): void {
|
||||
this.connectionState = 'online';
|
||||
this.clearReconnectTimer();
|
||||
}
|
||||
|
||||
private markConnectionIssue(error: unknown): void {
|
||||
if (typeof navigator !== 'undefined' && !navigator.onLine) {
|
||||
this.connectionState = 'offline';
|
||||
return;
|
||||
}
|
||||
|
||||
const message = this.toMessage(error).toLowerCase();
|
||||
if (
|
||||
message.includes('fetch') ||
|
||||
message.includes('network') ||
|
||||
message.includes('failed to') ||
|
||||
message.includes('could not load lobby status') ||
|
||||
message.includes('session refresh failed')
|
||||
) {
|
||||
this.connectionState = 'reconnecting';
|
||||
this.scheduleReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
private scheduleReconnect(): void {
|
||||
if (this.reconnectTimer || !this.sessionCode.trim()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.reconnectTimer = setTimeout(() => {
|
||||
this.reconnectTimer = null;
|
||||
void this.retryReconnect();
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
async retryReconnect(): Promise<void> {
|
||||
if (!this.sessionCode.trim() || this.loading) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.refreshSession();
|
||||
}
|
||||
|
||||
returnToJoin(): void {
|
||||
this.loadingTransition = null;
|
||||
this.clearReconnectTimer();
|
||||
this.connectionState = typeof navigator !== 'undefined' && !navigator.onLine ? 'offline' : 'online';
|
||||
this.session = null;
|
||||
this.finalLeaderboard = [];
|
||||
this.selectedGuess = '';
|
||||
this.lieText = '';
|
||||
this.submitError = null;
|
||||
this.error = '';
|
||||
this.playerId = 0;
|
||||
this.sessionToken = '';
|
||||
this.sessionContextStore.clear();
|
||||
}
|
||||
|
||||
private syncFinalLeaderboard(): void {
|
||||
if (!this.session || this.session.session.status !== 'finished') {
|
||||
this.finalLeaderboard = [];
|
||||
return;
|
||||
}
|
||||
|
||||
this.finalLeaderboard = [...this.session.players].sort((a, b) => {
|
||||
if (b.score !== a.score) {
|
||||
return b.score - a.score;
|
||||
}
|
||||
return a.nickname.localeCompare(b.nickname);
|
||||
});
|
||||
}
|
||||
|
||||
private async request<T>(path: string, method: 'GET' | 'POST', payload?: unknown): Promise<T> {
|
||||
const response = await fetch(path, {
|
||||
method,
|
||||
@@ -86,40 +280,55 @@ export class PlayerShellComponent {
|
||||
|
||||
async refreshSession(): Promise<void> {
|
||||
this.loading = true;
|
||||
this.loadingTransition = 'refresh';
|
||||
this.error = '';
|
||||
try {
|
||||
const code = this.normalizeCode(this.sessionCode);
|
||||
this.session = await this.request<SessionDetail>(`/lobby/sessions/${encodeURIComponent(code)}`, 'GET');
|
||||
const state = await this.controller.hydrateLobby(this.sessionCode);
|
||||
if (!state.session || state.errorMessage) {
|
||||
throw new Error(state.errorMessage ?? 'Unknown error');
|
||||
}
|
||||
this.session = state.session as SessionDetail;
|
||||
this.sessionCode = this.session.session.code;
|
||||
if (this.session.session.status !== 'guess') {
|
||||
this.selectedGuess = '';
|
||||
}
|
||||
this.syncFinalLeaderboard();
|
||||
this.markOnline();
|
||||
} catch (error) {
|
||||
this.error = `Session refresh failed: ${(error as Error).message}`;
|
||||
this.error = `Session refresh failed: ${this.toMessage(error)}`;
|
||||
this.markConnectionIssue(error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
this.loadingTransition = null;
|
||||
}
|
||||
}
|
||||
|
||||
async joinSession(): Promise<void> {
|
||||
this.loading = true;
|
||||
this.loadingTransition = 'join';
|
||||
this.error = '';
|
||||
try {
|
||||
const payload = await this.request<{
|
||||
player: { id: number; session_token: string };
|
||||
session: { code: string };
|
||||
}>('/lobby/sessions/join', 'POST', {
|
||||
code: this.normalizeCode(this.sessionCode),
|
||||
nickname: this.nickname.trim(),
|
||||
});
|
||||
this.playerId = payload.player.id;
|
||||
this.sessionToken = payload.player.session_token;
|
||||
this.sessionCode = payload.session.code;
|
||||
await this.refreshSession();
|
||||
const state = await this.controller.joinLobby(this.sessionCode, this.nickname);
|
||||
if (!state.session || state.errorMessage) {
|
||||
throw new Error(state.errorMessage ?? 'Unknown error');
|
||||
}
|
||||
this.session = state.session as SessionDetail;
|
||||
this.sessionCode = this.session.session.code;
|
||||
|
||||
const sessionContext = this.sessionContextStore.get();
|
||||
this.playerId = sessionContext?.playerId ?? 0;
|
||||
this.sessionToken = sessionContext?.token ?? '';
|
||||
if (this.session.session.status !== 'guess') {
|
||||
this.selectedGuess = '';
|
||||
}
|
||||
this.syncFinalLeaderboard();
|
||||
this.markOnline();
|
||||
} catch (error) {
|
||||
this.error = `Join failed: ${(error as Error).message}`;
|
||||
this.error = `Join failed: ${this.toMessage(error)}`;
|
||||
this.markConnectionIssue(error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
this.loadingTransition = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +337,7 @@ export class PlayerShellComponent {
|
||||
return;
|
||||
}
|
||||
this.loading = true;
|
||||
this.loadingTransition = 'submit-lie';
|
||||
this.submitError = null;
|
||||
try {
|
||||
await this.request(
|
||||
@@ -140,10 +350,13 @@ export class PlayerShellComponent {
|
||||
}
|
||||
);
|
||||
await this.refreshSession();
|
||||
this.markOnline();
|
||||
} catch (error) {
|
||||
this.submitError = { kind: 'lie', message: `Lie submit failed: ${(error as Error).message}` };
|
||||
this.submitError = { kind: 'lie', message: `Lie submit failed: ${this.toMessage(error)}` };
|
||||
this.markConnectionIssue(error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
this.loadingTransition = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,6 +365,7 @@ export class PlayerShellComponent {
|
||||
return;
|
||||
}
|
||||
this.loading = true;
|
||||
this.loadingTransition = 'submit-guess';
|
||||
this.submitError = null;
|
||||
try {
|
||||
await this.request(
|
||||
@@ -164,10 +378,13 @@ export class PlayerShellComponent {
|
||||
}
|
||||
);
|
||||
await this.refreshSession();
|
||||
this.markOnline();
|
||||
} catch (error) {
|
||||
this.submitError = { kind: 'guess', message: `Guess submit failed: ${(error as Error).message}` };
|
||||
this.submitError = { kind: 'guess', message: `Guess submit failed: ${this.toMessage(error)}` };
|
||||
this.markConnectionIssue(error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
this.loadingTransition = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
86
frontend/angular/src/app/session-route-context.spec.ts
Normal file
86
frontend/angular/src/app/session-route-context.spec.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { hostRouteContextResolver, playerRouteContextResolver, resolveSessionCode } from './session-route-context';
|
||||
|
||||
type RouteLike = {
|
||||
paramMap: { get: (key: string) => string | null };
|
||||
queryParamMap: { get: (key: string) => string | null };
|
||||
};
|
||||
|
||||
function route(params: Record<string, string | null>, query: Record<string, string | null> = {}): RouteLike {
|
||||
return {
|
||||
paramMap: { get: (key: string) => params[key] ?? null },
|
||||
queryParamMap: { get: (key: string) => query[key] ?? null },
|
||||
};
|
||||
}
|
||||
|
||||
function storageMock(initial: Record<string, string> = {}): Storage {
|
||||
const data = new Map<string, string>(Object.entries(initial));
|
||||
|
||||
return {
|
||||
getItem: vi.fn((key: string) => data.get(key) ?? null),
|
||||
setItem: vi.fn((key: string, value: string) => {
|
||||
data.set(key, value);
|
||||
}),
|
||||
removeItem: vi.fn((key: string) => {
|
||||
data.delete(key);
|
||||
}),
|
||||
clear: vi.fn(() => {
|
||||
data.clear();
|
||||
}),
|
||||
key: vi.fn((index: number) => Array.from(data.keys())[index] ?? null),
|
||||
get length() {
|
||||
return data.size;
|
||||
},
|
||||
} as unknown as Storage;
|
||||
}
|
||||
|
||||
function setWindow(localStorage: Storage, sessionStorage: Storage): void {
|
||||
vi.stubGlobal('window', { localStorage, sessionStorage });
|
||||
}
|
||||
|
||||
describe('session route context', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('resolves player code from persisted session context when route has no code', () => {
|
||||
setWindow(
|
||||
storageMock({ 'wpp.session-context': JSON.stringify({ sessionCode: 'ab12', playerId: 7, token: 'tok' }) }),
|
||||
storageMock()
|
||||
);
|
||||
|
||||
expect(resolveSessionCode(route({}, {}) as never, 'player')).toBe('AB12');
|
||||
});
|
||||
|
||||
it('resolves host code from session query string', () => {
|
||||
setWindow(storageMock(), storageMock());
|
||||
|
||||
expect(resolveSessionCode(route({}, { session: 'qwe9' }) as never, 'host')).toBe('QWE9');
|
||||
});
|
||||
|
||||
it('player resolver emits player id/token when context matches route session', () => {
|
||||
setWindow(
|
||||
storageMock({ 'wpp.session-context': JSON.stringify({ sessionCode: 'AB12', playerId: 5, token: 'tok-5' }) }),
|
||||
storageMock()
|
||||
);
|
||||
|
||||
expect(playerRouteContextResolver(route({ context: 'ab12' }) as never, {} as never)).toEqual({
|
||||
sessionCode: 'AB12',
|
||||
playerId: 5,
|
||||
token: 'tok-5',
|
||||
});
|
||||
});
|
||||
|
||||
it('host resolver stores normalized host session code for refresh bootstrap', () => {
|
||||
const sessionStorage = storageMock();
|
||||
setWindow(storageMock(), sessionStorage);
|
||||
|
||||
expect(hostRouteContextResolver(route({ context: 'ab12' }) as never, {} as never)).toEqual({
|
||||
sessionCode: 'AB12',
|
||||
playerId: null,
|
||||
token: null,
|
||||
});
|
||||
expect(sessionStorage.setItem).toHaveBeenCalledWith('wpp.host-session-code', 'AB12');
|
||||
});
|
||||
});
|
||||
140
frontend/angular/src/app/session-route-context.ts
Normal file
140
frontend/angular/src/app/session-route-context.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { inject } from '@angular/core';
|
||||
import { type ActivatedRouteSnapshot, type CanActivateFn, type ResolveFn, Router, type UrlTree } from '@angular/router';
|
||||
|
||||
import { createSessionContextStore } from '../../../src/spa/session-context-store';
|
||||
|
||||
export interface RouteSessionContext {
|
||||
sessionCode: string | null;
|
||||
playerId: number | null;
|
||||
token: string | null;
|
||||
}
|
||||
|
||||
const HOST_STORAGE_KEY = 'wpp.host-session-code';
|
||||
|
||||
function normalizeCode(value: string): string {
|
||||
return value.trim().toUpperCase();
|
||||
}
|
||||
|
||||
function isCodeLike(value: string | null | undefined): value is string {
|
||||
return !!value && /^[A-Za-z0-9]{4,12}$/.test(value.trim());
|
||||
}
|
||||
|
||||
function hasPlayerSessionContext(sessionCode: string): boolean {
|
||||
const context = createSessionContextStore(window.localStorage).get();
|
||||
if (!context) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
isCodeLike(context.sessionCode) &&
|
||||
normalizeCode(context.sessionCode) === normalizeCode(sessionCode) &&
|
||||
Number.isInteger(context.playerId) &&
|
||||
context.playerId > 0 &&
|
||||
!!context.token.trim()
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveSessionCode(route: ActivatedRouteSnapshot, mode: 'host' | 'player'): string | null {
|
||||
const contextParam = route.paramMap.get('context');
|
||||
const queryCode = route.queryParamMap.get('session');
|
||||
|
||||
if (isCodeLike(contextParam)) {
|
||||
return normalizeCode(contextParam);
|
||||
}
|
||||
if (isCodeLike(queryCode)) {
|
||||
return normalizeCode(queryCode);
|
||||
}
|
||||
|
||||
if (mode === 'player') {
|
||||
const persisted = createSessionContextStore(window.localStorage).get()?.sessionCode;
|
||||
if (isCodeLike(persisted)) {
|
||||
return normalizeCode(persisted);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const stored = window.sessionStorage.getItem(HOST_STORAGE_KEY);
|
||||
if (isCodeLike(stored)) {
|
||||
return normalizeCode(stored);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function sessionExists(code: string): Promise<boolean> {
|
||||
const response = await fetch(`/lobby/sessions/${encodeURIComponent(code)}`, {
|
||||
method: 'GET',
|
||||
headers: { Accept: 'application/json' },
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
|
||||
return response.ok;
|
||||
}
|
||||
|
||||
async function requireSessionContext(route: ActivatedRouteSnapshot, mode: 'host' | 'player'): Promise<boolean> {
|
||||
const phase = route.paramMap.get('phase');
|
||||
const code = resolveSessionCode(route, mode);
|
||||
|
||||
if (!phase) {
|
||||
if (mode === 'host' && code) {
|
||||
window.sessionStorage.setItem(HOST_STORAGE_KEY, code);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!code) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mode === 'player' && !hasPlayerSessionContext(code)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ok = await sessionExists(code);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mode === 'host') {
|
||||
window.sessionStorage.setItem(HOST_STORAGE_KEY, code);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async function guard(mode: 'host' | 'player', route: ActivatedRouteSnapshot): Promise<boolean | UrlTree> {
|
||||
const router = inject(Router);
|
||||
const allowed = await requireSessionContext(route, mode);
|
||||
if (allowed) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return router.createUrlTree([`/${mode}`]);
|
||||
}
|
||||
|
||||
export const hostRouteGuard: CanActivateFn = (route) => guard('host', route);
|
||||
|
||||
export const playerRouteGuard: CanActivateFn = (route) => guard('player', route);
|
||||
|
||||
export const hostRouteContextResolver: ResolveFn<RouteSessionContext> = (route) => {
|
||||
const code = resolveSessionCode(route, 'host');
|
||||
if (code) {
|
||||
window.sessionStorage.setItem(HOST_STORAGE_KEY, code);
|
||||
}
|
||||
return { sessionCode: code, playerId: null, token: null };
|
||||
};
|
||||
|
||||
export const playerRouteContextResolver: ResolveFn<RouteSessionContext> = (route) => {
|
||||
const code = resolveSessionCode(route, 'player');
|
||||
const context = createSessionContextStore(window.localStorage).get();
|
||||
|
||||
if (!code || !context || normalizeCode(context.sessionCode) !== code) {
|
||||
return { sessionCode: code, playerId: null, token: null };
|
||||
}
|
||||
|
||||
return {
|
||||
sessionCode: code,
|
||||
playerId: Number.isInteger(context.playerId) && context.playerId > 0 ? context.playerId : null,
|
||||
token: context.token.trim() || null,
|
||||
};
|
||||
};
|
||||
1
frontend/angular/src/test-setup.ts
Normal file
1
frontend/angular/src/test-setup.ts
Normal file
@@ -0,0 +1 @@
|
||||
import '@angular/compiler';
|
||||
8
frontend/angular/vitest.config.ts
Normal file
8
frontend/angular/vitest.config.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { defineConfig } from 'vitest/config';
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
include: ['src/**/*.spec.ts'],
|
||||
setupFiles: ['src/test-setup.ts'],
|
||||
},
|
||||
});
|
||||
@@ -1,12 +1,36 @@
|
||||
import {
|
||||
mapCalculateScoresResponse,
|
||||
mapFinishGameResponse,
|
||||
mapHealthResponse,
|
||||
mapJoinSessionResponse,
|
||||
mapMixAnswersResponse,
|
||||
mapScoreboardResponse,
|
||||
mapSessionDetailResponse,
|
||||
mapShowQuestionResponse,
|
||||
mapStartNextRoundResponse,
|
||||
mapStartRoundResponse,
|
||||
mapSubmitGuessResponse,
|
||||
mapSubmitLieResponse
|
||||
} from './mappers';
|
||||
import type {
|
||||
ApiFailure,
|
||||
ApiResult,
|
||||
CalculateScoresResponse,
|
||||
FinishGameResponse,
|
||||
HealthResponse,
|
||||
JoinSessionRequest,
|
||||
JoinSessionResponse,
|
||||
MixAnswersResponse,
|
||||
ScoreboardResponse,
|
||||
SessionDetailResponse,
|
||||
ShowQuestionResponse,
|
||||
StartNextRoundResponse,
|
||||
StartRoundRequest,
|
||||
StartRoundResponse
|
||||
StartRoundResponse,
|
||||
SubmitGuessRequest,
|
||||
SubmitGuessResponse,
|
||||
SubmitLieRequest,
|
||||
SubmitLieResponse
|
||||
} from './types';
|
||||
|
||||
export interface AngularHttpError {
|
||||
@@ -25,6 +49,18 @@ export interface AngularApiClient {
|
||||
getSession(code: string): Promise<ApiResult<SessionDetailResponse>>;
|
||||
joinSession(payload: JoinSessionRequest): Promise<ApiResult<JoinSessionResponse>>;
|
||||
startRound(code: string, payload: StartRoundRequest): Promise<ApiResult<StartRoundResponse>>;
|
||||
showQuestion(code: string): Promise<ApiResult<ShowQuestionResponse>>;
|
||||
mixAnswers(code: string, roundQuestionId: number): Promise<ApiResult<MixAnswersResponse>>;
|
||||
calculateScores(code: string, roundQuestionId: number): Promise<ApiResult<CalculateScoresResponse>>;
|
||||
getScoreboard(code: string): Promise<ApiResult<ScoreboardResponse>>;
|
||||
startNextRound(code: string): Promise<ApiResult<StartNextRoundResponse>>;
|
||||
finishGame(code: string): Promise<ApiResult<FinishGameResponse>>;
|
||||
submitLie(code: string, roundQuestionId: number, payload: SubmitLieRequest): Promise<ApiResult<SubmitLieResponse>>;
|
||||
submitGuess(
|
||||
code: string,
|
||||
roundQuestionId: number,
|
||||
payload: SubmitGuessRequest
|
||||
): Promise<ApiResult<SubmitGuessResponse>>;
|
||||
}
|
||||
|
||||
function toFailure(error: unknown): ApiFailure {
|
||||
@@ -60,10 +96,10 @@ function buildUrl(baseUrl: string, path: string): string {
|
||||
return `${normalizeBaseUrl(baseUrl)}${path}`;
|
||||
}
|
||||
|
||||
async function wrap<T>(call: () => Promise<T>): Promise<ApiResult<T>> {
|
||||
async function wrap<T>(call: () => Promise<unknown>, mapper: (payload: unknown) => T): Promise<ApiResult<T>> {
|
||||
let payload: unknown;
|
||||
try {
|
||||
const data = await call();
|
||||
return { ok: true, status: 200, data };
|
||||
payload = await call();
|
||||
} catch (error: unknown) {
|
||||
return {
|
||||
ok: false,
|
||||
@@ -71,35 +107,156 @@ async function wrap<T>(call: () => Promise<T>): Promise<ApiResult<T>> {
|
||||
error: toFailure(error)
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
return { ok: true, status: 200, data: mapper(payload) };
|
||||
} catch (error: unknown) {
|
||||
return {
|
||||
ok: false,
|
||||
status: 200,
|
||||
error: {
|
||||
kind: 'parse',
|
||||
status: 200,
|
||||
message: error instanceof Error ? error.message : 'Invalid API response contract',
|
||||
payload
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function createAngularApiClient(http: AngularHttpClientLike, baseUrl = ''): AngularApiClient {
|
||||
return {
|
||||
health: () => wrap(() => http.get<HealthResponse>(buildUrl(baseUrl, '/healthz'), { withCredentials: true })),
|
||||
health: () =>
|
||||
wrap(() => http.get<HealthResponse>(buildUrl(baseUrl, '/healthz'), { withCredentials: true }), mapHealthResponse),
|
||||
getSession: (code: string) =>
|
||||
wrap(() =>
|
||||
http.get<SessionDetailResponse>(buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}`), {
|
||||
withCredentials: true
|
||||
})
|
||||
wrap(
|
||||
() =>
|
||||
http.get<SessionDetailResponse>(buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}`), {
|
||||
withCredentials: true
|
||||
}),
|
||||
mapSessionDetailResponse
|
||||
),
|
||||
joinSession: (payload: JoinSessionRequest) =>
|
||||
wrap(() =>
|
||||
http.post<JoinSessionResponse>(
|
||||
buildUrl(baseUrl, '/lobby/sessions/join'),
|
||||
{
|
||||
code: normalizeCode(payload.code),
|
||||
nickname: payload.nickname.trim()
|
||||
},
|
||||
{ withCredentials: true }
|
||||
)
|
||||
wrap(
|
||||
() =>
|
||||
http.post<JoinSessionResponse>(
|
||||
buildUrl(baseUrl, '/lobby/sessions/join'),
|
||||
{
|
||||
code: normalizeCode(payload.code),
|
||||
nickname: payload.nickname.trim()
|
||||
},
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapJoinSessionResponse
|
||||
),
|
||||
startRound: (code: string, payload: StartRoundRequest) =>
|
||||
wrap(() =>
|
||||
http.post<StartRoundResponse>(
|
||||
buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/start`),
|
||||
payload,
|
||||
{ withCredentials: true }
|
||||
)
|
||||
wrap(
|
||||
() =>
|
||||
http.post<StartRoundResponse>(
|
||||
buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/start`),
|
||||
payload,
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapStartRoundResponse
|
||||
),
|
||||
showQuestion: (code: string) =>
|
||||
wrap(
|
||||
() =>
|
||||
http.post<ShowQuestionResponse>(
|
||||
buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/show`),
|
||||
{},
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapShowQuestionResponse
|
||||
),
|
||||
mixAnswers: (code: string, roundQuestionId: number) =>
|
||||
wrap(
|
||||
() =>
|
||||
http.post<MixAnswersResponse>(
|
||||
buildUrl(
|
||||
baseUrl,
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/${roundQuestionId}/answers/mix`
|
||||
),
|
||||
{},
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapMixAnswersResponse
|
||||
),
|
||||
calculateScores: (code: string, roundQuestionId: number) =>
|
||||
wrap(
|
||||
() =>
|
||||
http.post<CalculateScoresResponse>(
|
||||
buildUrl(
|
||||
baseUrl,
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/${roundQuestionId}/scores/calculate`
|
||||
),
|
||||
{},
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapCalculateScoresResponse
|
||||
),
|
||||
getScoreboard: (code: string) =>
|
||||
wrap(
|
||||
() =>
|
||||
http.get<ScoreboardResponse>(
|
||||
buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/scoreboard`),
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapScoreboardResponse
|
||||
),
|
||||
startNextRound: (code: string) =>
|
||||
wrap(
|
||||
() =>
|
||||
http.post<StartNextRoundResponse>(
|
||||
buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/next`),
|
||||
{},
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapStartNextRoundResponse
|
||||
),
|
||||
finishGame: (code: string) =>
|
||||
wrap(
|
||||
() =>
|
||||
http.post<FinishGameResponse>(
|
||||
buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/finish`),
|
||||
{},
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapFinishGameResponse
|
||||
),
|
||||
submitLie: (code: string, roundQuestionId: number, payload: SubmitLieRequest) =>
|
||||
wrap(
|
||||
() =>
|
||||
http.post<SubmitLieResponse>(
|
||||
buildUrl(
|
||||
baseUrl,
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/${roundQuestionId}/lies/submit`
|
||||
),
|
||||
{
|
||||
player_id: payload.player_id,
|
||||
session_token: payload.session_token,
|
||||
text: payload.text
|
||||
},
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapSubmitLieResponse
|
||||
),
|
||||
submitGuess: (code: string, roundQuestionId: number, payload: SubmitGuessRequest) =>
|
||||
wrap(
|
||||
() =>
|
||||
http.post<SubmitGuessResponse>(
|
||||
buildUrl(
|
||||
baseUrl,
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/${roundQuestionId}/guesses/submit`
|
||||
),
|
||||
{
|
||||
player_id: payload.player_id,
|
||||
session_token: payload.session_token,
|
||||
selected_text: payload.selected_text
|
||||
},
|
||||
{ withCredentials: true }
|
||||
),
|
||||
mapSubmitGuessResponse
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,11 +1,35 @@
|
||||
import {
|
||||
mapCalculateScoresResponse,
|
||||
mapFinishGameResponse,
|
||||
mapHealthResponse,
|
||||
mapJoinSessionResponse,
|
||||
mapMixAnswersResponse,
|
||||
mapNextRoundResponse,
|
||||
mapScoreboardResponse,
|
||||
mapSessionDetailResponse,
|
||||
mapShowQuestionResponse,
|
||||
mapStartRoundResponse,
|
||||
mapSubmitGuessResponse,
|
||||
mapSubmitLieResponse
|
||||
} from './mappers';
|
||||
import type {
|
||||
ApiResult,
|
||||
CalculateScoresResponse,
|
||||
FinishGameResponse,
|
||||
HealthResponse,
|
||||
JoinSessionRequest,
|
||||
JoinSessionResponse,
|
||||
MixAnswersResponse,
|
||||
NextRoundResponse,
|
||||
ScoreboardResponse,
|
||||
SessionDetailResponse,
|
||||
ShowQuestionResponse,
|
||||
StartRoundRequest,
|
||||
StartRoundResponse
|
||||
StartRoundResponse,
|
||||
SubmitGuessRequest,
|
||||
SubmitGuessResponse,
|
||||
SubmitLieRequest,
|
||||
SubmitLieResponse
|
||||
} from './types';
|
||||
|
||||
export interface ApiClient {
|
||||
@@ -13,10 +37,23 @@ export interface ApiClient {
|
||||
getSession(code: string): Promise<ApiResult<SessionDetailResponse>>;
|
||||
joinSession(payload: JoinSessionRequest): Promise<ApiResult<JoinSessionResponse>>;
|
||||
startRound(code: string, payload: StartRoundRequest): Promise<ApiResult<StartRoundResponse>>;
|
||||
showQuestion(code: string): Promise<ApiResult<ShowQuestionResponse>>;
|
||||
mixAnswers(code: string, roundQuestionId: number): Promise<ApiResult<MixAnswersResponse>>;
|
||||
calculateScores(code: string, roundQuestionId: number): Promise<ApiResult<CalculateScoresResponse>>;
|
||||
getScoreboard(code: string): Promise<ApiResult<ScoreboardResponse>>;
|
||||
startNextRound(code: string): Promise<ApiResult<NextRoundResponse>>;
|
||||
finishGame(code: string): Promise<ApiResult<FinishGameResponse>>;
|
||||
submitLie(code: string, roundQuestionId: number, payload: SubmitLieRequest): Promise<ApiResult<SubmitLieResponse>>;
|
||||
submitGuess(code: string, roundQuestionId: number, payload: SubmitGuessRequest): Promise<ApiResult<SubmitGuessResponse>>;
|
||||
}
|
||||
|
||||
export function createApiClient(baseUrl = '', fetchImpl: typeof fetch = fetch): ApiClient {
|
||||
async function request<T>(path: string, method: 'GET' | 'POST', payload?: unknown): Promise<ApiResult<T>> {
|
||||
async function request<T>(
|
||||
path: string,
|
||||
method: 'GET' | 'POST',
|
||||
mapper: (payload: unknown) => T,
|
||||
payload?: unknown
|
||||
): Promise<ApiResult<T>> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetchImpl(`${baseUrl}${path}`, {
|
||||
@@ -59,22 +96,102 @@ export function createApiClient(baseUrl = '', fetchImpl: typeof fetch = fetch):
|
||||
};
|
||||
}
|
||||
|
||||
return { ok: true, status: response.status, data: responsePayload as T };
|
||||
try {
|
||||
return { ok: true, status: response.status, data: mapper(responsePayload) };
|
||||
} catch (error) {
|
||||
return {
|
||||
ok: false,
|
||||
status: response.status,
|
||||
error: {
|
||||
kind: 'parse',
|
||||
status: response.status,
|
||||
message: error instanceof Error ? error.message : 'Invalid API response contract',
|
||||
payload: responsePayload
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeCode = (value: string): string => value.trim().toUpperCase();
|
||||
|
||||
return {
|
||||
health: () => request<HealthResponse>('/healthz', 'GET'),
|
||||
health: () => request<HealthResponse>('/healthz', 'GET', mapHealthResponse),
|
||||
getSession: (code: string) =>
|
||||
request<SessionDetailResponse>(`/lobby/sessions/${encodeURIComponent(code.trim().toUpperCase())}`, 'GET'),
|
||||
request<SessionDetailResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}`,
|
||||
'GET',
|
||||
mapSessionDetailResponse
|
||||
),
|
||||
joinSession: (payload: JoinSessionRequest) =>
|
||||
request<JoinSessionResponse>('/lobby/sessions/join', 'POST', {
|
||||
code: payload.code.trim().toUpperCase(),
|
||||
nickname: payload.nickname.trim()
|
||||
}),
|
||||
request<JoinSessionResponse>(
|
||||
'/lobby/sessions/join',
|
||||
'POST',
|
||||
mapJoinSessionResponse,
|
||||
{
|
||||
code: normalizeCode(payload.code),
|
||||
nickname: payload.nickname.trim()
|
||||
}
|
||||
),
|
||||
startRound: (code: string, payload: StartRoundRequest) =>
|
||||
request<StartRoundResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(code.trim().toUpperCase())}/rounds/start`,
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/start`,
|
||||
'POST',
|
||||
mapStartRoundResponse,
|
||||
payload
|
||||
),
|
||||
showQuestion: (code: string) =>
|
||||
request<ShowQuestionResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/show`,
|
||||
'POST',
|
||||
mapShowQuestionResponse,
|
||||
{}
|
||||
),
|
||||
mixAnswers: (code: string, roundQuestionId: number) =>
|
||||
request<MixAnswersResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/${roundQuestionId}/answers/mix`,
|
||||
'POST',
|
||||
mapMixAnswersResponse,
|
||||
{}
|
||||
),
|
||||
calculateScores: (code: string, roundQuestionId: number) =>
|
||||
request<CalculateScoresResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/${roundQuestionId}/scores/calculate`,
|
||||
'POST',
|
||||
mapCalculateScoresResponse,
|
||||
{}
|
||||
),
|
||||
getScoreboard: (code: string) =>
|
||||
request<ScoreboardResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/scoreboard`,
|
||||
'GET',
|
||||
mapScoreboardResponse
|
||||
),
|
||||
startNextRound: (code: string) =>
|
||||
request<NextRoundResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/next`,
|
||||
'POST',
|
||||
mapNextRoundResponse,
|
||||
{}
|
||||
),
|
||||
finishGame: (code: string) =>
|
||||
request<FinishGameResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/finish`,
|
||||
'POST',
|
||||
mapFinishGameResponse,
|
||||
{}
|
||||
),
|
||||
submitLie: (code: string, roundQuestionId: number, payload: SubmitLieRequest) =>
|
||||
request<SubmitLieResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/${roundQuestionId}/lies/submit`,
|
||||
'POST',
|
||||
mapSubmitLieResponse,
|
||||
payload
|
||||
),
|
||||
submitGuess: (code: string, roundQuestionId: number, payload: SubmitGuessRequest) =>
|
||||
request<SubmitGuessResponse>(
|
||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/questions/${roundQuestionId}/guesses/submit`,
|
||||
'POST',
|
||||
mapSubmitGuessResponse,
|
||||
payload
|
||||
)
|
||||
};
|
||||
|
||||
359
frontend/src/api/mappers.ts
Normal file
359
frontend/src/api/mappers.ts
Normal file
@@ -0,0 +1,359 @@
|
||||
import type {
|
||||
CalculateScoresResponse,
|
||||
FinishGameResponse,
|
||||
HealthResponse,
|
||||
JoinSessionResponse,
|
||||
MixAnswersResponse,
|
||||
ScoreboardResponse,
|
||||
SessionDetailResponse,
|
||||
ShowQuestionResponse,
|
||||
StartNextRoundResponse,
|
||||
StartRoundResponse,
|
||||
SubmitGuessResponse,
|
||||
SubmitLieResponse
|
||||
} from './types';
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null;
|
||||
}
|
||||
|
||||
function isBoolean(value: unknown): value is boolean {
|
||||
return typeof value === 'boolean';
|
||||
}
|
||||
|
||||
function isNumber(value: unknown): value is number {
|
||||
return typeof value === 'number' && Number.isFinite(value);
|
||||
}
|
||||
|
||||
function isString(value: unknown): value is string {
|
||||
return typeof value === 'string';
|
||||
}
|
||||
|
||||
function asRecord(value: unknown, path: string): Record<string, unknown> {
|
||||
if (!isRecord(value)) {
|
||||
throw new Error(`Invalid API contract: expected object at ${path}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function readString(record: Record<string, unknown>, key: string, path: string): string {
|
||||
const value = record[key];
|
||||
if (!isString(value)) {
|
||||
throw new Error(`Invalid API contract: expected string at ${path}.${key}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function readNumber(record: Record<string, unknown>, key: string, path: string): number {
|
||||
const value = record[key];
|
||||
if (!isNumber(value)) {
|
||||
throw new Error(`Invalid API contract: expected number at ${path}.${key}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function readBoolean(record: Record<string, unknown>, key: string, path: string): boolean {
|
||||
const value = record[key];
|
||||
if (!isBoolean(value)) {
|
||||
throw new Error(`Invalid API contract: expected boolean at ${path}.${key}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function mapHealthResponse(payload: unknown): HealthResponse {
|
||||
const root = asRecord(payload, 'health');
|
||||
return {
|
||||
ok: readBoolean(root, 'ok', 'health'),
|
||||
service: readString(root, 'service', 'health')
|
||||
};
|
||||
}
|
||||
|
||||
function mapSessionDetail(payload: unknown): SessionDetailResponse {
|
||||
const root = asRecord(payload, 'session_detail');
|
||||
const session = asRecord(root.session, 'session_detail.session');
|
||||
const players = root.players;
|
||||
if (!Array.isArray(players)) {
|
||||
throw new Error('Invalid API contract: expected array at session_detail.players');
|
||||
}
|
||||
|
||||
const roundQuestionRaw = root.round_question;
|
||||
let roundQuestion: SessionDetailResponse['round_question'] = null;
|
||||
if (roundQuestionRaw !== null) {
|
||||
const roundQuestionRecord = asRecord(roundQuestionRaw, 'session_detail.round_question');
|
||||
const answersRaw = roundQuestionRecord.answers;
|
||||
if (!Array.isArray(answersRaw)) {
|
||||
throw new Error('Invalid API contract: expected array at session_detail.round_question.answers');
|
||||
}
|
||||
|
||||
roundQuestion = {
|
||||
id: readNumber(roundQuestionRecord, 'id', 'session_detail.round_question'),
|
||||
round_number: readNumber(roundQuestionRecord, 'round_number', 'session_detail.round_question'),
|
||||
prompt: readString(roundQuestionRecord, 'prompt', 'session_detail.round_question'),
|
||||
shown_at: readString(roundQuestionRecord, 'shown_at', 'session_detail.round_question'),
|
||||
answers: answersRaw.map((answer, index) => {
|
||||
const answerRecord = asRecord(answer, `session_detail.round_question.answers[${index}]`);
|
||||
return { text: readString(answerRecord, 'text', `session_detail.round_question.answers[${index}]`) };
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
const phase = asRecord(root.phase_view_model, 'session_detail.phase_view_model');
|
||||
const constraints = asRecord(phase.constraints, 'session_detail.phase_view_model.constraints');
|
||||
const host = asRecord(phase.host, 'session_detail.phase_view_model.host');
|
||||
const player = asRecord(phase.player, 'session_detail.phase_view_model.player');
|
||||
|
||||
return {
|
||||
session: {
|
||||
code: readString(session, 'code', 'session_detail.session'),
|
||||
status: readString(session, 'status', 'session_detail.session'),
|
||||
host_id: (() => {
|
||||
const hostId = session.host_id;
|
||||
if (hostId === null) {
|
||||
return null;
|
||||
}
|
||||
if (!isNumber(hostId)) {
|
||||
throw new Error('Invalid API contract: expected number|null at session_detail.session.host_id');
|
||||
}
|
||||
return hostId;
|
||||
})(),
|
||||
current_round: readNumber(session, 'current_round', 'session_detail.session'),
|
||||
players_count: readNumber(session, 'players_count', 'session_detail.session')
|
||||
},
|
||||
players: players.map((item, index) => {
|
||||
const record = asRecord(item, `session_detail.players[${index}]`);
|
||||
return {
|
||||
id: readNumber(record, 'id', `session_detail.players[${index}]`),
|
||||
nickname: readString(record, 'nickname', `session_detail.players[${index}]`),
|
||||
score: readNumber(record, 'score', `session_detail.players[${index}]`),
|
||||
is_connected: readBoolean(record, 'is_connected', `session_detail.players[${index}]`)
|
||||
};
|
||||
}),
|
||||
round_question: roundQuestion,
|
||||
phase_view_model: {
|
||||
status: readString(phase, 'status', 'session_detail.phase_view_model'),
|
||||
round_number: readNumber(phase, 'round_number', 'session_detail.phase_view_model'),
|
||||
players_count: readNumber(phase, 'players_count', 'session_detail.phase_view_model'),
|
||||
constraints: {
|
||||
min_players_to_start: readNumber(constraints, 'min_players_to_start', 'session_detail.phase_view_model.constraints'),
|
||||
max_players_mvp: readNumber(constraints, 'max_players_mvp', 'session_detail.phase_view_model.constraints'),
|
||||
min_players_reached: readBoolean(constraints, 'min_players_reached', 'session_detail.phase_view_model.constraints'),
|
||||
max_players_allowed: readBoolean(constraints, 'max_players_allowed', 'session_detail.phase_view_model.constraints')
|
||||
},
|
||||
host: {
|
||||
can_start_round: readBoolean(host, 'can_start_round', 'session_detail.phase_view_model.host'),
|
||||
can_show_question: readBoolean(host, 'can_show_question', 'session_detail.phase_view_model.host'),
|
||||
can_mix_answers: readBoolean(host, 'can_mix_answers', 'session_detail.phase_view_model.host'),
|
||||
can_calculate_scores: readBoolean(host, 'can_calculate_scores', 'session_detail.phase_view_model.host'),
|
||||
can_reveal_scoreboard: readBoolean(host, 'can_reveal_scoreboard', 'session_detail.phase_view_model.host'),
|
||||
can_start_next_round: readBoolean(host, 'can_start_next_round', 'session_detail.phase_view_model.host'),
|
||||
can_finish_game: readBoolean(host, 'can_finish_game', 'session_detail.phase_view_model.host')
|
||||
},
|
||||
player: {
|
||||
can_join: readBoolean(player, 'can_join', 'session_detail.phase_view_model.player'),
|
||||
can_submit_lie: readBoolean(player, 'can_submit_lie', 'session_detail.phase_view_model.player'),
|
||||
can_submit_guess: readBoolean(player, 'can_submit_guess', 'session_detail.phase_view_model.player'),
|
||||
can_view_final_result: readBoolean(player, 'can_view_final_result', 'session_detail.phase_view_model.player')
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function mapSessionDetailResponse(payload: unknown): SessionDetailResponse {
|
||||
return mapSessionDetail(payload);
|
||||
}
|
||||
|
||||
export function mapJoinSessionResponse(payload: unknown): JoinSessionResponse {
|
||||
const root = asRecord(payload, 'join_session');
|
||||
const player = asRecord(root.player, 'join_session.player');
|
||||
const session = asRecord(root.session, 'join_session.session');
|
||||
|
||||
return {
|
||||
player: {
|
||||
id: readNumber(player, 'id', 'join_session.player'),
|
||||
nickname: readString(player, 'nickname', 'join_session.player'),
|
||||
session_token: readString(player, 'session_token', 'join_session.player'),
|
||||
score: readNumber(player, 'score', 'join_session.player')
|
||||
},
|
||||
session: {
|
||||
code: readString(session, 'code', 'join_session.session'),
|
||||
status: readString(session, 'status', 'join_session.session')
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function mapStartRoundResponse(payload: unknown): StartRoundResponse {
|
||||
const root = asRecord(payload, 'start_round');
|
||||
const session = asRecord(root.session, 'start_round.session');
|
||||
const round = asRecord(root.round, 'start_round.round');
|
||||
const category = asRecord(round.category, 'start_round.round.category');
|
||||
|
||||
return {
|
||||
session: {
|
||||
code: readString(session, 'code', 'start_round.session'),
|
||||
status: readString(session, 'status', 'start_round.session'),
|
||||
current_round: readNumber(session, 'current_round', 'start_round.session')
|
||||
},
|
||||
round: {
|
||||
number: readNumber(round, 'number', 'start_round.round'),
|
||||
category: {
|
||||
slug: readString(category, 'slug', 'start_round.round.category'),
|
||||
name: readString(category, 'name', 'start_round.round.category')
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function mapLeaderboardEntry(payload: unknown, path: string): { id: number; nickname: string; score: number } {
|
||||
const record = asRecord(payload, path);
|
||||
return {
|
||||
id: readNumber(record, 'id', path),
|
||||
nickname: readString(record, 'nickname', path),
|
||||
score: readNumber(record, 'score', path)
|
||||
};
|
||||
}
|
||||
|
||||
function mapSessionState(payload: unknown, path: string): { code: string; status: string; current_round: number } {
|
||||
const session = asRecord(payload, path);
|
||||
return {
|
||||
code: readString(session, 'code', path),
|
||||
status: readString(session, 'status', path),
|
||||
current_round: readNumber(session, 'current_round', path)
|
||||
};
|
||||
}
|
||||
|
||||
export function mapShowQuestionResponse(payload: unknown): ShowQuestionResponse {
|
||||
const root = asRecord(payload, 'show_question');
|
||||
const roundQuestion = asRecord(root.round_question, 'show_question.round_question');
|
||||
const config = asRecord(root.config, 'show_question.config');
|
||||
|
||||
return {
|
||||
round_question: {
|
||||
id: readNumber(roundQuestion, 'id', 'show_question.round_question'),
|
||||
prompt: readString(roundQuestion, 'prompt', 'show_question.round_question'),
|
||||
round_number: readNumber(roundQuestion, 'round_number', 'show_question.round_question'),
|
||||
shown_at: readString(roundQuestion, 'shown_at', 'show_question.round_question'),
|
||||
lie_deadline_at: readString(roundQuestion, 'lie_deadline_at', 'show_question.round_question')
|
||||
},
|
||||
config: {
|
||||
lie_seconds: readNumber(config, 'lie_seconds', 'show_question.config')
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function mapMixAnswersResponse(payload: unknown): MixAnswersResponse {
|
||||
const root = asRecord(payload, 'mix_answers');
|
||||
const roundQuestion = asRecord(root.round_question, 'mix_answers.round_question');
|
||||
const answersRaw = root.answers;
|
||||
if (!Array.isArray(answersRaw)) {
|
||||
throw new Error('Invalid API contract: expected array at mix_answers.answers');
|
||||
}
|
||||
|
||||
return {
|
||||
session: mapSessionState(root.session, 'mix_answers.session'),
|
||||
round_question: {
|
||||
id: readNumber(roundQuestion, 'id', 'mix_answers.round_question'),
|
||||
round_number: readNumber(roundQuestion, 'round_number', 'mix_answers.round_question')
|
||||
},
|
||||
answers: answersRaw.map((answer, index) => {
|
||||
const record = asRecord(answer, `mix_answers.answers[${index}]`);
|
||||
return { text: readString(record, 'text', `mix_answers.answers[${index}]`) };
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
export function mapCalculateScoresResponse(payload: unknown): CalculateScoresResponse {
|
||||
const root = asRecord(payload, 'calculate_scores');
|
||||
const roundQuestion = asRecord(root.round_question, 'calculate_scores.round_question');
|
||||
const leaderboardRaw = root.leaderboard;
|
||||
if (!Array.isArray(leaderboardRaw)) {
|
||||
throw new Error('Invalid API contract: expected array at calculate_scores.leaderboard');
|
||||
}
|
||||
|
||||
return {
|
||||
session: mapSessionState(root.session, 'calculate_scores.session'),
|
||||
round_question: {
|
||||
id: readNumber(roundQuestion, 'id', 'calculate_scores.round_question'),
|
||||
round_number: readNumber(roundQuestion, 'round_number', 'calculate_scores.round_question')
|
||||
},
|
||||
events_created: readNumber(root, 'events_created', 'calculate_scores'),
|
||||
leaderboard: leaderboardRaw.map((entry, index) => mapLeaderboardEntry(entry, `calculate_scores.leaderboard[${index}]`))
|
||||
};
|
||||
}
|
||||
|
||||
export function mapScoreboardResponse(payload: unknown): ScoreboardResponse {
|
||||
const root = asRecord(payload, 'scoreboard');
|
||||
const leaderboardRaw = root.leaderboard;
|
||||
if (!Array.isArray(leaderboardRaw)) {
|
||||
throw new Error('Invalid API contract: expected array at scoreboard.leaderboard');
|
||||
}
|
||||
|
||||
return {
|
||||
session: mapSessionState(root.session, 'scoreboard.session'),
|
||||
leaderboard: leaderboardRaw.map((entry, index) => mapLeaderboardEntry(entry, `scoreboard.leaderboard[${index}]`))
|
||||
};
|
||||
}
|
||||
|
||||
export function mapStartNextRoundResponse(payload: unknown): StartNextRoundResponse {
|
||||
const root = asRecord(payload, 'start_next_round');
|
||||
return { session: mapSessionState(root.session, 'start_next_round.session') };
|
||||
}
|
||||
|
||||
export function mapFinishGameResponse(payload: unknown): FinishGameResponse {
|
||||
const root = asRecord(payload, 'finish_game');
|
||||
const leaderboardRaw = root.leaderboard;
|
||||
if (!Array.isArray(leaderboardRaw)) {
|
||||
throw new Error('Invalid API contract: expected array at finish_game.leaderboard');
|
||||
}
|
||||
|
||||
const winnerRaw = root.winner;
|
||||
|
||||
return {
|
||||
session: mapSessionState(root.session, 'finish_game.session'),
|
||||
winner: winnerRaw === null ? null : mapLeaderboardEntry(winnerRaw, 'finish_game.winner'),
|
||||
leaderboard: leaderboardRaw.map((entry, index) => mapLeaderboardEntry(entry, `finish_game.leaderboard[${index}]`))
|
||||
};
|
||||
}
|
||||
|
||||
export function mapSubmitLieResponse(payload: unknown): SubmitLieResponse {
|
||||
const root = asRecord(payload, 'submit_lie');
|
||||
const lie = asRecord(root.lie, 'submit_lie.lie');
|
||||
const window = asRecord(root.window, 'submit_lie.window');
|
||||
|
||||
return {
|
||||
lie: {
|
||||
id: readNumber(lie, 'id', 'submit_lie.lie'),
|
||||
player_id: readNumber(lie, 'player_id', 'submit_lie.lie'),
|
||||
round_question_id: readNumber(lie, 'round_question_id', 'submit_lie.lie'),
|
||||
text: readString(lie, 'text', 'submit_lie.lie'),
|
||||
created_at: readString(lie, 'created_at', 'submit_lie.lie')
|
||||
},
|
||||
window: {
|
||||
lie_deadline_at: readString(window, 'lie_deadline_at', 'submit_lie.window')
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function mapSubmitGuessResponse(payload: unknown): SubmitGuessResponse {
|
||||
const root = asRecord(payload, 'submit_guess');
|
||||
const guess = asRecord(root.guess, 'submit_guess.guess');
|
||||
const window = asRecord(root.window, 'submit_guess.window');
|
||||
const fooledPlayerId = guess.fooled_player_id;
|
||||
if (fooledPlayerId !== null && !isNumber(fooledPlayerId)) {
|
||||
throw new Error('Invalid API contract: expected number|null at submit_guess.guess.fooled_player_id');
|
||||
}
|
||||
|
||||
return {
|
||||
guess: {
|
||||
id: readNumber(guess, 'id', 'submit_guess.guess'),
|
||||
player_id: readNumber(guess, 'player_id', 'submit_guess.guess'),
|
||||
round_question_id: readNumber(guess, 'round_question_id', 'submit_guess.guess'),
|
||||
selected_text: readString(guess, 'selected_text', 'submit_guess.guess'),
|
||||
is_correct: readBoolean(guess, 'is_correct', 'submit_guess.guess'),
|
||||
fooled_player_id: fooledPlayerId,
|
||||
created_at: readString(guess, 'created_at', 'submit_guess.guess')
|
||||
},
|
||||
window: {
|
||||
guess_deadline_at: readString(window, 'guess_deadline_at', 'submit_guess.window')
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -101,6 +101,113 @@ export interface StartRoundResponse {
|
||||
};
|
||||
}
|
||||
|
||||
export interface ShowQuestionResponse {
|
||||
round_question: {
|
||||
id: number;
|
||||
prompt: string;
|
||||
round_number: number;
|
||||
shown_at: string;
|
||||
lie_deadline_at: string;
|
||||
};
|
||||
config: {
|
||||
lie_seconds: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface MixAnswersResponse {
|
||||
session: {
|
||||
code: string;
|
||||
status: string;
|
||||
current_round: number;
|
||||
};
|
||||
round_question: {
|
||||
id: number;
|
||||
round_number: number;
|
||||
};
|
||||
answers: Array<{ text: string }>;
|
||||
}
|
||||
|
||||
export interface CalculateScoresResponse {
|
||||
session: {
|
||||
code: string;
|
||||
status: string;
|
||||
current_round: number;
|
||||
};
|
||||
round_question: {
|
||||
id: number;
|
||||
round_number: number;
|
||||
};
|
||||
events_created: number;
|
||||
leaderboard: Array<{ id: number; nickname: string; score: number }>;
|
||||
}
|
||||
|
||||
export interface ScoreboardResponse {
|
||||
session: {
|
||||
code: string;
|
||||
status: string;
|
||||
current_round: number;
|
||||
};
|
||||
leaderboard: Array<{ id: number; nickname: string; score: number }>;
|
||||
}
|
||||
|
||||
export interface StartNextRoundResponse {
|
||||
session: {
|
||||
code: string;
|
||||
status: string;
|
||||
current_round: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface FinishGameResponse {
|
||||
session: {
|
||||
code: string;
|
||||
status: string;
|
||||
current_round: number;
|
||||
};
|
||||
winner: { id: number; nickname: string; score: number } | null;
|
||||
leaderboard: Array<{ id: number; nickname: string; score: number }>;
|
||||
}
|
||||
|
||||
export interface SubmitLieRequest {
|
||||
player_id: number;
|
||||
session_token: string;
|
||||
text: string;
|
||||
}
|
||||
|
||||
export interface SubmitLieResponse {
|
||||
lie: {
|
||||
id: number;
|
||||
player_id: number;
|
||||
round_question_id: number;
|
||||
text: string;
|
||||
created_at: string;
|
||||
};
|
||||
window: {
|
||||
lie_deadline_at: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface SubmitGuessRequest {
|
||||
player_id: number;
|
||||
session_token: string;
|
||||
selected_text: string;
|
||||
}
|
||||
|
||||
export interface SubmitGuessResponse {
|
||||
guess: {
|
||||
id: number;
|
||||
player_id: number;
|
||||
round_question_id: number;
|
||||
selected_text: string;
|
||||
is_correct: boolean;
|
||||
fooled_player_id: number | null;
|
||||
created_at: string;
|
||||
};
|
||||
window: {
|
||||
guess_deadline_at: string;
|
||||
};
|
||||
}
|
||||
|
||||
export type ApiErrorKind = 'network' | 'http' | 'parse';
|
||||
|
||||
export interface ApiFailure {
|
||||
|
||||
58
frontend/src/spa/gameplay-phase-machine.ts
Normal file
58
frontend/src/spa/gameplay-phase-machine.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import type { SessionDetailResponse } from '../api/types';
|
||||
|
||||
export type GameplayPhase = 'lie' | 'guess' | 'reveal' | 'scoreboard';
|
||||
|
||||
export type GameplayPhaseEvent =
|
||||
| 'LIES_LOCKED'
|
||||
| 'GUESSES_LOCKED'
|
||||
| 'SCOREBOARD_READY'
|
||||
| 'NEXT_ROUND';
|
||||
|
||||
export interface GameplayTransitionResult {
|
||||
phase: GameplayPhase;
|
||||
changed: boolean;
|
||||
}
|
||||
|
||||
const TRANSITIONS: Record<GameplayPhase, Partial<Record<GameplayPhaseEvent, GameplayPhase>>> = {
|
||||
lie: {
|
||||
LIES_LOCKED: 'guess'
|
||||
},
|
||||
guess: {
|
||||
GUESSES_LOCKED: 'reveal'
|
||||
},
|
||||
reveal: {
|
||||
SCOREBOARD_READY: 'scoreboard'
|
||||
},
|
||||
scoreboard: {
|
||||
NEXT_ROUND: 'lie'
|
||||
}
|
||||
};
|
||||
|
||||
export function transitionGameplayPhase(phase: GameplayPhase, event: GameplayPhaseEvent): GameplayTransitionResult {
|
||||
const next = TRANSITIONS[phase][event] ?? phase;
|
||||
return {
|
||||
phase: next,
|
||||
changed: next !== phase
|
||||
};
|
||||
}
|
||||
|
||||
export function allowedGameplayEvents(phase: GameplayPhase): GameplayPhaseEvent[] {
|
||||
return Object.keys(TRANSITIONS[phase]) as GameplayPhaseEvent[];
|
||||
}
|
||||
|
||||
export function deriveGameplayPhase(session: SessionDetailResponse | null): GameplayPhase | null {
|
||||
const status = session?.session.status;
|
||||
if (!status) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (status === 'lie' || status === 'guess' || status === 'reveal') {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (status === 'finished') {
|
||||
return 'scoreboard';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
31
frontend/src/spa/lobby-i18n.ts
Normal file
31
frontend/src/spa/lobby-i18n.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import lobbyCatalog from '../../../shared/i18n/lobby.json';
|
||||
|
||||
type FrontendErrorKey = keyof typeof lobbyCatalog.frontend.errors;
|
||||
|
||||
const frontendErrors = lobbyCatalog.frontend.errors;
|
||||
|
||||
const apiErrorMap: Record<string, FrontendErrorKey> = {
|
||||
session_code_required: 'session_code_required',
|
||||
session_not_found: 'session_not_found',
|
||||
nickname_invalid: 'nickname_invalid',
|
||||
nickname_taken: 'nickname_taken'
|
||||
};
|
||||
|
||||
export function lobbyMessage(key: FrontendErrorKey): string {
|
||||
return frontendErrors[key] ?? frontendErrors.unknown;
|
||||
}
|
||||
|
||||
export function lobbyMessageFromApiPayload(payload: unknown, fallbackKey: FrontendErrorKey): string {
|
||||
if (!payload || typeof payload !== 'object') {
|
||||
return lobbyMessage(fallbackKey);
|
||||
}
|
||||
|
||||
const record = payload as Record<string, unknown>;
|
||||
const code = typeof record.error_code === 'string' ? record.error_code : '';
|
||||
const mappedKey = apiErrorMap[code];
|
||||
if (!mappedKey) {
|
||||
return lobbyMessage(fallbackKey);
|
||||
}
|
||||
|
||||
return lobbyMessage(mappedKey);
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import {
|
||||
type SessionContextInput,
|
||||
type SessionContextStore as PersistedSessionContextStore
|
||||
} from './session-context-store';
|
||||
import { deriveGameplayPhase, type GameplayPhase } from './gameplay-phase-machine';
|
||||
import { lobbyMessage, lobbyMessageFromApiPayload } from './lobby-i18n';
|
||||
|
||||
export type AsyncState = 'idle' | 'loading' | 'success' | 'error';
|
||||
|
||||
@@ -14,6 +16,7 @@ export type SessionContextStore = Pick<PersistedSessionContextStore, 'get' | 'se
|
||||
export interface VerticalSliceState {
|
||||
sessionCode: string;
|
||||
session: SessionDetailResponse | null;
|
||||
gameplayPhase: GameplayPhase | null;
|
||||
joinState: AsyncState;
|
||||
startRoundState: AsyncState;
|
||||
loadingSession: boolean;
|
||||
@@ -36,6 +39,7 @@ export function createVerticalSliceController(
|
||||
const state: VerticalSliceState = {
|
||||
sessionCode: persistedContext?.sessionCode ?? '',
|
||||
session: null,
|
||||
gameplayPhase: null,
|
||||
joinState: 'idle',
|
||||
startRoundState: 'idle',
|
||||
loadingSession: false,
|
||||
@@ -54,7 +58,7 @@ export function createVerticalSliceController(
|
||||
|
||||
if (!state.sessionCode) {
|
||||
state.loadingSession = false;
|
||||
state.errorMessage = 'Session-kode mangler.';
|
||||
state.errorMessage = lobbyMessage('session_code_required');
|
||||
return { ...state };
|
||||
}
|
||||
|
||||
@@ -62,11 +66,13 @@ export function createVerticalSliceController(
|
||||
state.loadingSession = false;
|
||||
|
||||
if (!result.ok) {
|
||||
state.errorMessage = 'Kunne ikke hente lobby-status.';
|
||||
state.errorMessage = lobbyMessageFromApiPayload(result.error.payload, 'session_fetch_failed');
|
||||
state.gameplayPhase = null;
|
||||
return { ...state };
|
||||
}
|
||||
|
||||
state.session = result.data;
|
||||
state.gameplayPhase = deriveGameplayPhase(result.data);
|
||||
state.sessionCode = normalizeCode(result.data.session.code);
|
||||
|
||||
if (persistedContext && state.sessionCode === normalizeCode(persistedContext.sessionCode)) {
|
||||
@@ -87,7 +93,7 @@ export function createVerticalSliceController(
|
||||
const join = await api.joinSession({ code: requestCode, nickname });
|
||||
if (!join.ok) {
|
||||
state.joinState = 'error';
|
||||
state.errorMessage = 'Join fejlede. Tjek kode eller nickname og prøv igen.';
|
||||
state.errorMessage = lobbyMessageFromApiPayload(join.error.payload, 'join_failed');
|
||||
return { ...state };
|
||||
}
|
||||
|
||||
@@ -114,14 +120,14 @@ export function createVerticalSliceController(
|
||||
|
||||
if (!codeToUse) {
|
||||
state.startRoundState = 'error';
|
||||
state.errorMessage = 'Session-kode mangler.';
|
||||
state.errorMessage = lobbyMessage('session_code_required');
|
||||
return { ...state };
|
||||
}
|
||||
|
||||
const start = await api.startRound(codeToUse, { category_slug: categorySlug });
|
||||
if (!start.ok) {
|
||||
state.startRoundState = 'error';
|
||||
state.errorMessage = 'Kunne ikke starte runden. Opdatér lobbyen og prøv igen.';
|
||||
state.errorMessage = lobbyMessageFromApiPayload(start.error.payload, 'start_round_failed');
|
||||
return { ...state };
|
||||
}
|
||||
|
||||
|
||||
@@ -189,6 +189,143 @@ describe('createAngularApiClient', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('returns parse error when successful payload breaks typed contract', async () => {
|
||||
const http = {
|
||||
get: vi.fn<AngularHttpClientLike['get']>(async <T>() => ({ ok: true } as T)),
|
||||
post: vi.fn<AngularHttpClientLike['post']>(async <T>() => ({ ok: true } as T))
|
||||
};
|
||||
|
||||
const client = createAngularApiClient(http as AngularHttpClientLike);
|
||||
const session = await client.getSession('ABCD12');
|
||||
|
||||
expect(session.ok).toBe(false);
|
||||
if (!session.ok) {
|
||||
expect(session.status).toBe(200);
|
||||
expect(session.error.kind).toBe('parse');
|
||||
expect(session.error.message).toContain('Invalid API contract');
|
||||
}
|
||||
});
|
||||
|
||||
it('maps host/player gameplay endpoints through typed response mappers', async () => {
|
||||
const get = vi.fn<AngularHttpClientLike['get']>(async <T>(url: string) => {
|
||||
if (url === '/lobby/sessions/ABCD12/scoreboard') {
|
||||
return {
|
||||
session: { code: 'ABCD12', status: 'reveal', current_round: 1 },
|
||||
leaderboard: [
|
||||
{ id: 2, nickname: 'Maja', score: 11 },
|
||||
{ id: 3, nickname: 'Bo', score: 7 }
|
||||
]
|
||||
} as T;
|
||||
}
|
||||
throw { status: 404, error: { error: 'Not found' } };
|
||||
});
|
||||
|
||||
const post = vi.fn<AngularHttpClientLike['post']>(async <T>(url: string, body: unknown) => {
|
||||
if (url === '/lobby/sessions/ABCD12/questions/show') {
|
||||
expect(body).toEqual({});
|
||||
return {
|
||||
round_question: {
|
||||
id: 77,
|
||||
prompt: 'Prompt?',
|
||||
round_number: 1,
|
||||
shown_at: '2026-03-01T16:00:00Z',
|
||||
lie_deadline_at: '2026-03-01T16:00:30Z'
|
||||
},
|
||||
config: { lie_seconds: 30 }
|
||||
} as T;
|
||||
}
|
||||
if (url === '/lobby/sessions/ABCD12/questions/77/answers/mix') {
|
||||
expect(body).toEqual({});
|
||||
return {
|
||||
session: { code: 'ABCD12', status: 'guess', current_round: 1 },
|
||||
round_question: { id: 77, round_number: 1 },
|
||||
answers: [{ text: 'A' }, { text: 'B' }]
|
||||
} as T;
|
||||
}
|
||||
if (url === '/lobby/sessions/ABCD12/questions/77/scores/calculate') {
|
||||
expect(body).toEqual({});
|
||||
return {
|
||||
session: { code: 'ABCD12', status: 'reveal', current_round: 1 },
|
||||
round_question: { id: 77, round_number: 1 },
|
||||
events_created: 3,
|
||||
leaderboard: [{ id: 2, nickname: 'Maja', score: 11 }]
|
||||
} as T;
|
||||
}
|
||||
if (url === '/lobby/sessions/ABCD12/rounds/next') {
|
||||
expect(body).toEqual({});
|
||||
return { session: { code: 'ABCD12', status: 'lobby', current_round: 2 } } as T;
|
||||
}
|
||||
if (url === '/lobby/sessions/ABCD12/finish') {
|
||||
expect(body).toEqual({});
|
||||
return {
|
||||
session: { code: 'ABCD12', status: 'finished', current_round: 2 },
|
||||
winner: { id: 2, nickname: 'Maja', score: 15 },
|
||||
leaderboard: [{ id: 2, nickname: 'Maja', score: 15 }]
|
||||
} as T;
|
||||
}
|
||||
if (url === '/lobby/sessions/ABCD12/questions/77/lies/submit') {
|
||||
expect(body).toEqual({ player_id: 9, session_token: 'tok', text: 'my lie' });
|
||||
return {
|
||||
lie: {
|
||||
id: 100,
|
||||
player_id: 9,
|
||||
round_question_id: 77,
|
||||
text: 'my lie',
|
||||
created_at: '2026-03-01T16:00:10Z'
|
||||
},
|
||||
window: { lie_deadline_at: '2026-03-01T16:00:30Z' }
|
||||
} as T;
|
||||
}
|
||||
if (url === '/lobby/sessions/ABCD12/questions/77/guesses/submit') {
|
||||
expect(body).toEqual({ player_id: 9, session_token: 'tok', selected_text: 'A' });
|
||||
return {
|
||||
guess: {
|
||||
id: 200,
|
||||
player_id: 9,
|
||||
round_question_id: 77,
|
||||
selected_text: 'A',
|
||||
is_correct: false,
|
||||
fooled_player_id: 3,
|
||||
created_at: '2026-03-01T16:01:00Z'
|
||||
},
|
||||
window: { guess_deadline_at: '2026-03-01T16:01:30Z' }
|
||||
} as T;
|
||||
}
|
||||
|
||||
throw { status: 404, error: { error: 'Not found' } };
|
||||
});
|
||||
|
||||
const client = createAngularApiClient({ get, post } as AngularHttpClientLike);
|
||||
|
||||
const showQuestion = await client.showQuestion('abcd12');
|
||||
expect(showQuestion.ok).toBe(true);
|
||||
|
||||
const mixAnswers = await client.mixAnswers('abcd12', 77);
|
||||
expect(mixAnswers.ok).toBe(true);
|
||||
|
||||
const calculateScores = await client.calculateScores('abcd12', 77);
|
||||
expect(calculateScores.ok).toBe(true);
|
||||
|
||||
const scoreboard = await client.getScoreboard('abcd12');
|
||||
expect(scoreboard.ok).toBe(true);
|
||||
|
||||
const nextRound = await client.startNextRound('abcd12');
|
||||
expect(nextRound.ok).toBe(true);
|
||||
|
||||
const finish = await client.finishGame('abcd12');
|
||||
expect(finish.ok).toBe(true);
|
||||
|
||||
const submitLie = await client.submitLie('abcd12', 77, { player_id: 9, session_token: 'tok', text: 'my lie' });
|
||||
expect(submitLie.ok).toBe(true);
|
||||
|
||||
const submitGuess = await client.submitGuess('abcd12', 77, {
|
||||
player_id: 9,
|
||||
session_token: 'tok',
|
||||
selected_text: 'A'
|
||||
});
|
||||
expect(submitGuess.ok).toBe(true);
|
||||
});
|
||||
|
||||
it('maps HttpErrorResponse-style failures to ApiResult errors', async () => {
|
||||
const http = {
|
||||
get: vi.fn<AngularHttpClientLike['get']>(async () => {
|
||||
|
||||
@@ -74,6 +74,12 @@ beforeAll(async () => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.url === '/lobby/sessions/BADMAP' && req.method === 'GET') {
|
||||
res.writeHead(200, { 'content-type': 'application/json' });
|
||||
res.end(JSON.stringify({ session: { code: 'BADMAP' } }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.url?.startsWith('/lobby/sessions/')) {
|
||||
res.writeHead(404, { 'content-type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Session not found' }));
|
||||
@@ -123,6 +129,18 @@ describe('createApiClient', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('returns parse error when response violates typed contract', async () => {
|
||||
const client = createApiClient(baseUrl);
|
||||
|
||||
const invalid = await client.getSession('badmap');
|
||||
expect(invalid.ok).toBe(false);
|
||||
if (!invalid.ok) {
|
||||
expect(invalid.status).toBe(200);
|
||||
expect(invalid.error.kind).toBe('parse');
|
||||
expect(invalid.error.message).toContain('Invalid API contract');
|
||||
}
|
||||
});
|
||||
|
||||
it('returns consistent HTTP error shape for 4xx/5xx', async () => {
|
||||
const client = createApiClient(baseUrl);
|
||||
|
||||
|
||||
106
frontend/tests/gameplay-phase-machine.test.ts
Normal file
106
frontend/tests/gameplay-phase-machine.test.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
allowedGameplayEvents,
|
||||
deriveGameplayPhase,
|
||||
transitionGameplayPhase,
|
||||
type GameplayPhase
|
||||
} from '../src/spa/gameplay-phase-machine';
|
||||
|
||||
describe('gameplay phase machine skeleton', () => {
|
||||
it('supports canonical phase progression lie -> guess -> reveal -> scoreboard -> lie', () => {
|
||||
let phase: GameplayPhase = 'lie';
|
||||
|
||||
phase = transitionGameplayPhase(phase, 'LIES_LOCKED').phase;
|
||||
expect(phase).toBe('guess');
|
||||
|
||||
phase = transitionGameplayPhase(phase, 'GUESSES_LOCKED').phase;
|
||||
expect(phase).toBe('reveal');
|
||||
|
||||
phase = transitionGameplayPhase(phase, 'SCOREBOARD_READY').phase;
|
||||
expect(phase).toBe('scoreboard');
|
||||
|
||||
phase = transitionGameplayPhase(phase, 'NEXT_ROUND').phase;
|
||||
expect(phase).toBe('lie');
|
||||
});
|
||||
|
||||
it('keeps state unchanged for invalid transition events', () => {
|
||||
const transition = transitionGameplayPhase('lie', 'NEXT_ROUND');
|
||||
expect(transition.phase).toBe('lie');
|
||||
expect(transition.changed).toBe(false);
|
||||
});
|
||||
|
||||
it('exposes allowed events per phase', () => {
|
||||
expect(allowedGameplayEvents('guess')).toEqual(['GUESSES_LOCKED']);
|
||||
expect(allowedGameplayEvents('scoreboard')).toEqual(['NEXT_ROUND']);
|
||||
});
|
||||
|
||||
it('derives gameplay phase from session detail status', () => {
|
||||
expect(
|
||||
deriveGameplayPhase({
|
||||
session: { code: 'ABCD12', status: 'lie', host_id: 1, current_round: 1, players_count: 3 },
|
||||
players: [],
|
||||
round_question: null,
|
||||
phase_view_model: {
|
||||
status: 'lie',
|
||||
round_number: 1,
|
||||
players_count: 3,
|
||||
constraints: {
|
||||
min_players_to_start: 3,
|
||||
max_players_mvp: 5,
|
||||
min_players_reached: true,
|
||||
max_players_allowed: true
|
||||
},
|
||||
host: {
|
||||
can_start_round: false,
|
||||
can_show_question: true,
|
||||
can_mix_answers: true,
|
||||
can_calculate_scores: false,
|
||||
can_reveal_scoreboard: false,
|
||||
can_start_next_round: false,
|
||||
can_finish_game: false
|
||||
},
|
||||
player: {
|
||||
can_join: false,
|
||||
can_submit_lie: true,
|
||||
can_submit_guess: false,
|
||||
can_view_final_result: false
|
||||
}
|
||||
}
|
||||
})
|
||||
).toBe('lie');
|
||||
|
||||
expect(
|
||||
deriveGameplayPhase({
|
||||
session: { code: 'ABCD12', status: 'finished', host_id: 1, current_round: 1, players_count: 3 },
|
||||
players: [],
|
||||
round_question: null,
|
||||
phase_view_model: {
|
||||
status: 'finished',
|
||||
round_number: 1,
|
||||
players_count: 3,
|
||||
constraints: {
|
||||
min_players_to_start: 3,
|
||||
max_players_mvp: 5,
|
||||
min_players_reached: true,
|
||||
max_players_allowed: true
|
||||
},
|
||||
host: {
|
||||
can_start_round: false,
|
||||
can_show_question: false,
|
||||
can_mix_answers: false,
|
||||
can_calculate_scores: false,
|
||||
can_reveal_scoreboard: false,
|
||||
can_start_next_round: false,
|
||||
can_finish_game: false
|
||||
},
|
||||
player: {
|
||||
can_join: false,
|
||||
can_submit_lie: false,
|
||||
can_submit_guess: false,
|
||||
can_view_final_result: true
|
||||
}
|
||||
}
|
||||
})
|
||||
).toBe('scoreboard');
|
||||
});
|
||||
});
|
||||
@@ -105,6 +105,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
||||
vi.doUnmock('../src/spa/session-context-store');
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
it('tracks loading and success state for join + start flow', async () => {
|
||||
const api = makeApiMock();
|
||||
const controller = createVerticalSliceController(api);
|
||||
@@ -160,7 +161,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
||||
joinSession: vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
status: 404,
|
||||
error: { kind: 'http', status: 404, message: 'HTTP 404', payload: { error: 'Session not found' } }
|
||||
error: { kind: 'http', status: 404, message: 'HTTP 404', payload: { error: 'Session not found', error_code: 'session_not_found' } }
|
||||
})
|
||||
});
|
||||
|
||||
@@ -169,7 +170,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
||||
|
||||
const state = controller.getState();
|
||||
expect(state.joinState).toBe('error');
|
||||
expect(state.errorMessage).toContain('Join fejlede');
|
||||
expect(state.errorMessage).toBe('Session code is invalid or the session no longer exists.');
|
||||
});
|
||||
|
||||
it('surfaces a friendly error when round start fails', async () => {
|
||||
@@ -177,7 +178,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
||||
startRound: vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
status: 400,
|
||||
error: { kind: 'http', status: 400, message: 'HTTP 400', payload: { error: 'Round can only be started from lobby' } }
|
||||
error: { kind: 'http', status: 400, message: 'HTTP 400', payload: { error: 'Round can only be started from lobby', error_code: 'round_start_invalid_phase' } }
|
||||
})
|
||||
});
|
||||
|
||||
@@ -186,7 +187,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
||||
|
||||
const state = controller.getState();
|
||||
expect(state.startRoundState).toBe('error');
|
||||
expect(state.errorMessage).toContain('Kunne ikke starte runden');
|
||||
expect(state.errorMessage).toBe('Could not start round. Refresh the lobby and try again.');
|
||||
});
|
||||
|
||||
it('shows local validation error and avoids API call when hydrating without any session code', async () => {
|
||||
@@ -196,7 +197,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
||||
await controller.hydrateLobby(' ');
|
||||
|
||||
const state = controller.getState();
|
||||
expect(state.errorMessage).toBe('Session-kode mangler.');
|
||||
expect(state.errorMessage).toBe('Session code is required.');
|
||||
expect(state.loadingSession).toBe(false);
|
||||
expect(api.getSession).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -209,7 +210,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
||||
|
||||
const state = controller.getState();
|
||||
expect(state.startRoundState).toBe('error');
|
||||
expect(state.errorMessage).toBe('Session-kode mangler.');
|
||||
expect(state.errorMessage).toBe('Session code is required.');
|
||||
expect(api.startRound).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"lib": ["ES2022", "DOM"],
|
||||
"types": ["vitest/globals", "node"]
|
||||
},
|
||||
"include": ["src", "tests"]
|
||||
"include": ["src", "tests", "../shared/i18n/*.json"]
|
||||
}
|
||||
|
||||
20
lobby/i18n.py
Normal file
20
lobby/i18n.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import json
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from django.http import JsonResponse
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def lobby_i18n_catalog() -> dict:
|
||||
catalog_path = Path(__file__).resolve().parents[1] / "shared" / "i18n" / "lobby.json"
|
||||
with catalog_path.open(encoding="utf-8") as handle:
|
||||
return json.load(handle)
|
||||
|
||||
|
||||
def lobby_i18n_errors() -> dict:
|
||||
return lobby_i18n_catalog().get("backend", {}).get("error_codes", {})
|
||||
|
||||
|
||||
def api_error(*, code: str, message: str, status: int) -> JsonResponse:
|
||||
return JsonResponse({"error": message, "error_code": code}, status=status)
|
||||
@@ -5,6 +5,7 @@ from django.shortcuts import render
|
||||
from fupogfakta.models import Category
|
||||
|
||||
from .feature_flags import use_spa_ui
|
||||
from .i18n import lobby_i18n_catalog
|
||||
|
||||
|
||||
def _render_spa_shell(request, shell_route: str, shell_kind: str):
|
||||
@@ -15,6 +16,7 @@ def _render_spa_shell(request, shell_route: str, shell_kind: str):
|
||||
"shell_route": shell_route,
|
||||
"shell_kind": shell_kind,
|
||||
"spa_asset_base": settings.WPP_SPA_ASSET_BASE,
|
||||
"lobby_i18n": lobby_i18n_catalog(),
|
||||
},
|
||||
)
|
||||
|
||||
@@ -30,11 +32,18 @@ def host_screen(request, spa_path=None):
|
||||
return _render_spa_shell(request, host_route, "host")
|
||||
|
||||
categories = Category.objects.filter(is_active=True).order_by("name")
|
||||
return render(request, "lobby/host_screen.html", {"categories": categories})
|
||||
return render(
|
||||
request,
|
||||
"lobby/host_screen.html",
|
||||
{
|
||||
"categories": categories,
|
||||
"lobby_i18n": lobby_i18n_catalog(),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def player_screen(request):
|
||||
if use_spa_ui():
|
||||
return _render_spa_shell(request, "/player", "player")
|
||||
|
||||
return render(request, "lobby/player_screen.html")
|
||||
return render(request, "lobby/player_screen.html", {"lobby_i18n": lobby_i18n_catalog()})
|
||||
|
||||
@@ -20,6 +20,8 @@ from fupogfakta.models import (
|
||||
ScoreEvent,
|
||||
)
|
||||
|
||||
from .i18n import api_error, lobby_i18n_errors
|
||||
|
||||
SESSION_CODE_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
||||
SESSION_CODE_LENGTH = 6
|
||||
MAX_CODE_GENERATION_ATTEMPTS = 20
|
||||
@@ -29,6 +31,7 @@ JOINABLE_STATUSES = {
|
||||
GameSession.Status.GUESS,
|
||||
GameSession.Status.REVEAL,
|
||||
}
|
||||
ERROR_CODES = lobby_i18n_errors()
|
||||
|
||||
|
||||
def _json_body(request: HttpRequest) -> dict:
|
||||
@@ -124,21 +127,41 @@ def join_session(request: HttpRequest) -> JsonResponse:
|
||||
nickname = str(payload.get("nickname", "")).strip()
|
||||
|
||||
if not code:
|
||||
return JsonResponse({"error": "Session code is required"}, status=400)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("session_code_required", "session_code_required"),
|
||||
message="Session code is required",
|
||||
status=400,
|
||||
)
|
||||
|
||||
if len(nickname) < 2 or len(nickname) > 40:
|
||||
return JsonResponse({"error": "Nickname must be between 2 and 40 characters"}, status=400)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("nickname_invalid", "nickname_invalid"),
|
||||
message="Nickname must be between 2 and 40 characters",
|
||||
status=400,
|
||||
)
|
||||
|
||||
try:
|
||||
session = GameSession.objects.get(code=code)
|
||||
except GameSession.DoesNotExist:
|
||||
return JsonResponse({"error": "Session not found"}, status=404)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("session_not_found", "session_not_found"),
|
||||
message="Session not found",
|
||||
status=404,
|
||||
)
|
||||
|
||||
if session.status not in JOINABLE_STATUSES:
|
||||
return JsonResponse({"error": "Session is not joinable"}, status=400)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("session_not_joinable", "session_not_joinable"),
|
||||
message="Session is not joinable",
|
||||
status=400,
|
||||
)
|
||||
|
||||
if Player.objects.filter(session=session, nickname__iexact=nickname).exists():
|
||||
return JsonResponse({"error": "Nickname already taken"}, status=409)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("nickname_taken", "nickname_taken"),
|
||||
message="Nickname already taken",
|
||||
status=409,
|
||||
)
|
||||
|
||||
player = Player.objects.create(session=session, nickname=nickname)
|
||||
|
||||
@@ -166,7 +189,11 @@ def session_detail(request: HttpRequest, code: str) -> JsonResponse:
|
||||
try:
|
||||
session = GameSession.objects.get(code=session_code)
|
||||
except GameSession.DoesNotExist:
|
||||
return JsonResponse({"error": "Session not found"}, status=404)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("session_not_found", "session_not_found"),
|
||||
message="Session not found",
|
||||
status=404,
|
||||
)
|
||||
|
||||
players = list(
|
||||
session.players.order_by("nickname").values(
|
||||
@@ -223,25 +250,41 @@ def start_round(request: HttpRequest, code: str) -> JsonResponse:
|
||||
category_slug = str(payload.get("category_slug", "")).strip()
|
||||
|
||||
if not category_slug:
|
||||
return JsonResponse({"error": "category_slug is required"}, status=400)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("category_slug_required", "category_slug_required"),
|
||||
message="category_slug is required",
|
||||
status=400,
|
||||
)
|
||||
|
||||
session_code = _normalize_session_code(code)
|
||||
|
||||
try:
|
||||
session = GameSession.objects.get(code=session_code)
|
||||
except GameSession.DoesNotExist:
|
||||
return JsonResponse({"error": "Session not found"}, status=404)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("session_not_found", "session_not_found"),
|
||||
message="Session not found",
|
||||
status=404,
|
||||
)
|
||||
|
||||
if session.host_id != request.user.id:
|
||||
return JsonResponse({"error": "Only host can start round"}, status=403)
|
||||
|
||||
if session.status != GameSession.Status.LOBBY:
|
||||
return JsonResponse({"error": "Round can only be started from lobby"}, status=400)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("round_start_invalid_phase", "round_start_invalid_phase"),
|
||||
message="Round can only be started from lobby",
|
||||
status=400,
|
||||
)
|
||||
|
||||
try:
|
||||
category = Category.objects.get(slug=category_slug, is_active=True)
|
||||
except Category.DoesNotExist:
|
||||
return JsonResponse({"error": "Category not found"}, status=404)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("category_not_found", "category_not_found"),
|
||||
message="Category not found",
|
||||
status=404,
|
||||
)
|
||||
|
||||
if not Question.objects.filter(category=category, is_active=True).exists():
|
||||
return JsonResponse({"error": "Category has no active questions"}, status=400)
|
||||
@@ -249,7 +292,11 @@ def start_round(request: HttpRequest, code: str) -> JsonResponse:
|
||||
with transaction.atomic():
|
||||
session = GameSession.objects.select_for_update().get(pk=session.pk)
|
||||
if session.status != GameSession.Status.LOBBY:
|
||||
return JsonResponse({"error": "Round can only be started from lobby"}, status=400)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("round_start_invalid_phase", "round_start_invalid_phase"),
|
||||
message="Round can only be started from lobby",
|
||||
status=400,
|
||||
)
|
||||
|
||||
round_config, created = RoundConfig.objects.get_or_create(
|
||||
session=session,
|
||||
@@ -257,7 +304,11 @@ def start_round(request: HttpRequest, code: str) -> JsonResponse:
|
||||
defaults={"category": category},
|
||||
)
|
||||
if not created:
|
||||
return JsonResponse({"error": "Round already configured"}, status=409)
|
||||
return api_error(
|
||||
code=ERROR_CODES.get("round_already_configured", "round_already_configured"),
|
||||
message="Round already configured",
|
||||
status=409,
|
||||
)
|
||||
|
||||
session.status = GameSession.Status.LIE
|
||||
session.save(update_fields=["status"])
|
||||
|
||||
27
shared/i18n/lobby.json
Normal file
27
shared/i18n/lobby.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"frontend": {
|
||||
"errors": {
|
||||
"session_code_required": "Session code is required.",
|
||||
"session_fetch_failed": "Could not load lobby status.",
|
||||
"join_failed": "Join failed. Check code or nickname and try again.",
|
||||
"start_round_failed": "Could not start round. Refresh the lobby and try again.",
|
||||
"session_not_found": "Session code is invalid or the session no longer exists.",
|
||||
"nickname_invalid": "Nickname must be between 2 and 40 characters.",
|
||||
"nickname_taken": "Nickname is already taken.",
|
||||
"unknown": "Action failed. Refresh status and try again."
|
||||
}
|
||||
},
|
||||
"backend": {
|
||||
"error_codes": {
|
||||
"session_code_required": "session_code_required",
|
||||
"nickname_invalid": "nickname_invalid",
|
||||
"session_not_found": "session_not_found",
|
||||
"session_not_joinable": "session_not_joinable",
|
||||
"nickname_taken": "nickname_taken",
|
||||
"category_slug_required": "category_slug_required",
|
||||
"category_not_found": "category_not_found",
|
||||
"round_start_invalid_phase": "round_start_invalid_phase",
|
||||
"round_already_configured": "round_already_configured"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user