Compare commits
1 Commits
feat/issue
...
pr-181
| Author | SHA1 | Date | |
|---|---|---|---|
| acc3420a86 |
@@ -1,28 +0,0 @@
|
|||||||
# 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,20 +22,13 @@ Formål: levere et lille, ensartet evidensformat for release-nær gameplay-smoke
|
|||||||
- Host authenticated in Django admin: <yes/no>
|
- Host authenticated in Django admin: <yes/no>
|
||||||
- Active category/questions present: <yes/no>
|
- Active category/questions present: <yes/no>
|
||||||
- Participants: host + <N> players
|
- 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)
|
#### Checks (PASS/FAIL)
|
||||||
1. Cutover route sanity
|
1. Lobby -> join -> start
|
||||||
- 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>
|
- Mixed-case + whitespace session code accepted: <pass/fail>
|
||||||
3. One full round to scoreboard
|
2. One full round to scoreboard
|
||||||
- submit lie -> mix -> submit guess -> calculate score -> show scoreboard: <pass/fail>
|
- submit lie -> mix -> submit guess -> calculate score -> show scoreboard: <pass/fail>
|
||||||
4. Next-round + game-end sanity
|
3. Next-round + game-end sanity
|
||||||
- next round transitions: <pass/fail>
|
- next round transitions: <pass/fail>
|
||||||
- final leaderboard visible: <pass/fail>
|
- final leaderboard visible: <pass/fail>
|
||||||
|
|
||||||
|
|||||||
@@ -4,20 +4,15 @@
|
|||||||
- Host er logget ind i Django.
|
- Host er logget ind i Django.
|
||||||
- Mindst én aktiv kategori med spørgsmål findes.
|
- 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
|
## Flow
|
||||||
1. Verificér cutover-route matcher valgt flag (legacy vs SPA shell).
|
1. Åbn host-siden på /lobby/ui/host og tryk Opret session.
|
||||||
2. Åbn host-siden og tryk Opret session.
|
2. Åbn player-siden i 3 faner/enheder på /lobby/ui/player.
|
||||||
3. Åbn player-siden i 3 faner/enheder.
|
3. Join alle spillere med sessionkode og nickname.
|
||||||
4. Join alle spillere med sessionkode og nickname.
|
4. Host: vælg kategori, Start runde, Vis spørgsmål.
|
||||||
5. Host: vælg kategori, Start runde, Vis spørgsmål.
|
5. Spillere: brug round_question_id og submit løgn.
|
||||||
6. Spillere: brug round_question_id og submit løgn.
|
6. Host: Mix svar.
|
||||||
7. Host: Mix svar.
|
7. Spillere: submit gæt.
|
||||||
8. Spillere: submit gæt.
|
8. Host: Beregn score og Vis scoreboard.
|
||||||
9. Host: Beregn score og Vis scoreboard.
|
9. Host: Næste runde eller Afslut spil.
|
||||||
10. Host: Næste runde eller Afslut spil.
|
|
||||||
|
|
||||||
Resultat: En fuld runde kan køres uden rå API-kald fra terminal.
|
Resultat: En fuld runde kan køres uden rå API-kald fra terminal.
|
||||||
|
|||||||
@@ -15,6 +15,4 @@ Backward compatibility under cutover:
|
|||||||
## Verifikation
|
## Verifikation
|
||||||
- Flag OFF: `UiScreenTests.test_legacy_templates_are_used_when_spa_flag_is_off`
|
- 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): `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`
|
- 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`
|
|
||||||
|
|||||||
@@ -1,47 +1,28 @@
|
|||||||
import { Routes } from '@angular/router';
|
import { Routes } from '@angular/router';
|
||||||
|
|
||||||
import {
|
|
||||||
hostRouteContextResolver,
|
|
||||||
hostRouteGuard,
|
|
||||||
playerRouteContextResolver,
|
|
||||||
playerRouteGuard,
|
|
||||||
} from './session-route-context';
|
|
||||||
|
|
||||||
export const routes: Routes = [
|
export const routes: Routes = [
|
||||||
{
|
{
|
||||||
path: 'host',
|
path: 'host',
|
||||||
resolve: { routeContext: hostRouteContextResolver },
|
|
||||||
canActivate: [hostRouteGuard],
|
|
||||||
loadComponent: () => import('./features/host/host-shell.component').then((m) => m.HostShellComponent),
|
loadComponent: () => import('./features/host/host-shell.component').then((m) => m.HostShellComponent),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'host/:phase',
|
path: 'host/:phase',
|
||||||
resolve: { routeContext: hostRouteContextResolver },
|
|
||||||
canActivate: [hostRouteGuard],
|
|
||||||
loadComponent: () => import('./features/host/host-shell.component').then((m) => m.HostShellComponent),
|
loadComponent: () => import('./features/host/host-shell.component').then((m) => m.HostShellComponent),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'host/:phase/:context',
|
path: 'host/:phase/:context',
|
||||||
resolve: { routeContext: hostRouteContextResolver },
|
|
||||||
canActivate: [hostRouteGuard],
|
|
||||||
loadComponent: () => import('./features/host/host-shell.component').then((m) => m.HostShellComponent),
|
loadComponent: () => import('./features/host/host-shell.component').then((m) => m.HostShellComponent),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'player',
|
path: 'player',
|
||||||
resolve: { routeContext: playerRouteContextResolver },
|
|
||||||
canActivate: [playerRouteGuard],
|
|
||||||
loadComponent: () => import('./features/player/player-shell.component').then((m) => m.PlayerShellComponent),
|
loadComponent: () => import('./features/player/player-shell.component').then((m) => m.PlayerShellComponent),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'player/:phase',
|
path: 'player/:phase',
|
||||||
resolve: { routeContext: playerRouteContextResolver },
|
|
||||||
canActivate: [playerRouteGuard],
|
|
||||||
loadComponent: () => import('./features/player/player-shell.component').then((m) => m.PlayerShellComponent),
|
loadComponent: () => import('./features/player/player-shell.component').then((m) => m.PlayerShellComponent),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'player/:phase/:context',
|
path: 'player/:phase/:context',
|
||||||
resolve: { routeContext: playerRouteContextResolver },
|
|
||||||
canActivate: [playerRouteGuard],
|
|
||||||
loadComponent: () => import('./features/player/player-shell.component').then((m) => m.PlayerShellComponent),
|
loadComponent: () => import('./features/player/player-shell.component').then((m) => m.PlayerShellComponent),
|
||||||
},
|
},
|
||||||
{ path: '', pathMatch: 'full', redirectTo: 'player' },
|
{ path: '', pathMatch: 'full', redirectTo: 'player' },
|
||||||
|
|||||||
@@ -12,60 +12,6 @@ function jsonResponse(status: number, body: unknown) {
|
|||||||
} as unknown as Response;
|
} 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', () => {
|
describe('HostShellComponent gameplay wiring', () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.restoreAllMocks();
|
vi.restoreAllMocks();
|
||||||
@@ -74,13 +20,14 @@ describe('HostShellComponent gameplay wiring', () => {
|
|||||||
it('runs startRound transition and refreshes session details', async () => {
|
it('runs startRound transition and refreshes session details', async () => {
|
||||||
const fetchMock: FetchMock = vi
|
const fetchMock: FetchMock = vi
|
||||||
.fn()
|
.fn()
|
||||||
|
.mockResolvedValueOnce(jsonResponse(201, { ok: true }))
|
||||||
.mockResolvedValueOnce(
|
.mockResolvedValueOnce(
|
||||||
jsonResponse(201, {
|
jsonResponse(200, {
|
||||||
session: { code: 'ABCD12', status: 'lie', current_round: 1 },
|
session: { code: 'ABCD12', status: 'lie', current_round: 2 },
|
||||||
round: { number: 1, category: { slug: 'history', name: 'History' } },
|
round_question: { id: 41, prompt: 'Q?', answers: [] },
|
||||||
|
players: [],
|
||||||
})
|
})
|
||||||
)
|
);
|
||||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('lie')));
|
|
||||||
|
|
||||||
vi.stubGlobal('fetch', fetchMock);
|
vi.stubGlobal('fetch', fetchMock);
|
||||||
|
|
||||||
@@ -95,14 +42,20 @@ describe('HostShellComponent gameplay wiring', () => {
|
|||||||
'/lobby/sessions/ABCD12/rounds/start',
|
'/lobby/sessions/ABCD12/rounds/start',
|
||||||
expect.objectContaining({ method: 'POST', body: JSON.stringify({ category_slug: 'history' }) })
|
expect.objectContaining({ method: 'POST', body: JSON.stringify({ category_slug: 'history' }) })
|
||||||
);
|
);
|
||||||
expect(fetchMock).toHaveBeenNthCalledWith(2, '/lobby/sessions/ABCD12', expect.objectContaining({ method: 'GET' }));
|
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||||
|
2,
|
||||||
|
'/lobby/sessions/ABCD12',
|
||||||
|
expect.objectContaining({ method: 'GET' })
|
||||||
|
);
|
||||||
expect(component.session?.session.status).toBe('lie');
|
expect(component.session?.session.status).toBe('lie');
|
||||||
expect(component.roundQuestionId).toBe('41');
|
expect(component.roundQuestionId).toBe('41');
|
||||||
expect(component.loading).toBe(false);
|
expect(component.loading).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('captures scoreboard error for retry path', async () => {
|
it('captures scoreboard error for retry path', async () => {
|
||||||
const fetchMock: FetchMock = vi.fn().mockResolvedValue(jsonResponse(500, { error: 'Scoreboard unavailable' }));
|
const fetchMock: FetchMock = vi.fn().mockResolvedValue(
|
||||||
|
jsonResponse(500, { error: 'Scoreboard unavailable' })
|
||||||
|
);
|
||||||
|
|
||||||
vi.stubGlobal('fetch', fetchMock);
|
vi.stubGlobal('fetch', fetchMock);
|
||||||
|
|
||||||
@@ -111,154 +64,11 @@ describe('HostShellComponent gameplay wiring', () => {
|
|||||||
|
|
||||||
await component.loadScoreboard();
|
await component.loadScoreboard();
|
||||||
|
|
||||||
expect(fetchMock).toHaveBeenCalledWith('/lobby/sessions/ABCD12/scoreboard', expect.objectContaining({ method: 'GET' }));
|
expect(fetchMock).toHaveBeenCalledWith(
|
||||||
|
'/lobby/sessions/ABCD12/scoreboard',
|
||||||
|
expect.objectContaining({ method: 'GET' })
|
||||||
|
);
|
||||||
expect(component.scoreboardError).toContain('Scoreboard failed: Scoreboard unavailable');
|
expect(component.scoreboardError).toContain('Scoreboard failed: Scoreboard unavailable');
|
||||||
expect(component.loading).toBe(false);
|
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,22 +1,13 @@
|
|||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { Component, OnInit, Optional } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
import { ActivatedRoute } from '@angular/router';
|
|
||||||
import { FormsModule } from '@angular/forms';
|
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 {
|
interface SessionDetail {
|
||||||
session: { code: string; status: string; current_round: number };
|
session: { code: string; status: string; current_round: number };
|
||||||
round_question: { id: number; prompt: string; answers: Array<{ text: string }> } | null;
|
round_question: { id: number; prompt: string; answers: Array<{ text: string }> } | null;
|
||||||
players: Array<{ id: number; nickname: string; score: number }>;
|
players: Array<{ id: number; nickname: string; score: number }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
type LeaderboardEntry = ScoreboardResponse['leaderboard'][number];
|
|
||||||
type LeaderboardResponse = FinishGameResponse;
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-host-shell',
|
selector: 'app-host-shell',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
@@ -33,17 +24,11 @@ type LeaderboardResponse = FinishGameResponse;
|
|||||||
<button (click)="mixAnswers()" [disabled]="loading || !roundQuestionId">Mix answers → guess</button>
|
<button (click)="mixAnswers()" [disabled]="loading || !roundQuestionId">Mix answers → guess</button>
|
||||||
<button (click)="calculateScores()" [disabled]="loading || !roundQuestionId">Calculate scores → reveal</button>
|
<button (click)="calculateScores()" [disabled]="loading || !roundQuestionId">Calculate scores → reveal</button>
|
||||||
<button (click)="loadScoreboard()" [disabled]="loading">Load scoreboard</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="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>
|
</div>
|
||||||
|
|
||||||
<p *ngIf="error" class="error">{{ error }}</p>
|
<p *ngIf="error" class="error">{{ error }}</p>
|
||||||
<p *ngIf="scoreboardError" class="error">{{ scoreboardError }}</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">
|
<div *ngIf="session" class="panel">
|
||||||
<p><strong>Status:</strong> {{ session.session.status }} · round {{ session.session.current_round }}</p>
|
<p><strong>Status:</strong> {{ session.session.status }} · round {{ session.session.current_round }}</p>
|
||||||
@@ -53,66 +38,23 @@ type LeaderboardResponse = FinishGameResponse;
|
|||||||
<li *ngFor="let p of session.players">{{ p.nickname }}: {{ p.score }}</li>
|
<li *ngFor="let p of session.players">{{ p.nickname }}: {{ p.score }}</li>
|
||||||
</ul>
|
</ul>
|
||||||
<pre *ngIf="scoreboardPayload">{{ scoreboardPayload }}</pre>
|
<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>
|
</div>
|
||||||
`,
|
`,
|
||||||
})
|
})
|
||||||
export class HostShellComponent implements OnInit {
|
export class HostShellComponent {
|
||||||
sessionCode = '';
|
sessionCode = '';
|
||||||
categorySlug = 'general';
|
categorySlug = 'general';
|
||||||
roundQuestionId = '';
|
roundQuestionId = '';
|
||||||
loading = false;
|
loading = false;
|
||||||
error = '';
|
error = '';
|
||||||
scoreboardError = '';
|
scoreboardError = '';
|
||||||
nextRoundError = '';
|
|
||||||
finishError = '';
|
|
||||||
scoreboardPayload = '';
|
scoreboardPayload = '';
|
||||||
finalLeaderboardPayload = '';
|
|
||||||
finalLeaderboard: LeaderboardEntry[] = [];
|
|
||||||
finalWinner: LeaderboardEntry | null = null;
|
|
||||||
session: SessionDetail | 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 {
|
private normalizeCode(value: string): string {
|
||||||
return value.trim().toUpperCase();
|
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> {
|
private async request<T>(path: string, method: 'GET' | 'POST', payload?: unknown): Promise<T> {
|
||||||
const response = await fetch(path, {
|
const response = await fetch(path, {
|
||||||
method,
|
method,
|
||||||
@@ -136,20 +78,11 @@ export class HostShellComponent implements OnInit {
|
|||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.error = '';
|
this.error = '';
|
||||||
this.scoreboardError = '';
|
this.scoreboardError = '';
|
||||||
this.nextRoundError = '';
|
|
||||||
this.finishError = '';
|
|
||||||
try {
|
try {
|
||||||
const state = await this.controller.hydrateLobby(this.sessionCode);
|
const code = this.normalizeCode(this.sessionCode);
|
||||||
if (!state.session || state.errorMessage) {
|
this.session = await this.request<SessionDetail>(`/lobby/sessions/${encodeURIComponent(code)}`, 'GET');
|
||||||
throw new Error(state.errorMessage ?? 'Unknown error');
|
|
||||||
}
|
|
||||||
this.session = state.session as SessionDetail;
|
|
||||||
this.sessionCode = this.session.session.code;
|
this.sessionCode = this.session.session.code;
|
||||||
this.persistSessionCode(this.sessionCode);
|
|
||||||
this.roundQuestionId = this.session.round_question?.id ? String(this.session.round_question.id) : '';
|
this.roundQuestionId = this.session.round_question?.id ? String(this.session.round_question.id) : '';
|
||||||
if (this.session.session.status !== 'finished') {
|
|
||||||
this.resetFinalLeaderboard();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.error = `Session refresh failed: ${(error as Error).message}`;
|
this.error = `Session refresh failed: ${(error as Error).message}`;
|
||||||
} finally {
|
} finally {
|
||||||
@@ -159,16 +92,11 @@ export class HostShellComponent implements OnInit {
|
|||||||
|
|
||||||
async startRound(): Promise<void> {
|
async startRound(): Promise<void> {
|
||||||
await this.runAction(async () => {
|
await this.runAction(async () => {
|
||||||
const state = await this.controller.startRound(this.sessionCode, this.categorySlug.trim());
|
const code = this.normalizeCode(this.sessionCode);
|
||||||
if (!state.session || state.errorMessage) {
|
await this.request(`/lobby/sessions/${encodeURIComponent(code)}/rounds/start`, 'POST', {
|
||||||
throw new Error(state.errorMessage ?? 'Unknown error');
|
category_slug: this.categorySlug.trim(),
|
||||||
}
|
});
|
||||||
this.session = state.session as SessionDetail;
|
await this.refreshSession();
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,58 +142,6 @@ export class HostShellComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
private async runAction(action: () => Promise<void>): Promise<void> {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.error = '';
|
this.error = '';
|
||||||
|
|||||||
@@ -12,70 +12,17 @@ function jsonResponse(status: number, body: unknown) {
|
|||||||
} as unknown as Response;
|
} 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', () => {
|
describe('PlayerShellComponent gameplay wiring', () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.useRealTimers();
|
|
||||||
vi.restoreAllMocks();
|
vi.restoreAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('clears selected guess when refreshed status is no longer guess', async () => {
|
it('clears selected guess when refreshed status is no longer guess', async () => {
|
||||||
const fetchMock: FetchMock = vi.fn().mockResolvedValue(
|
const fetchMock: FetchMock = vi.fn().mockResolvedValue(
|
||||||
jsonResponse(200, sessionDetailPayload('reveal', { answers: ['A'] }))
|
jsonResponse(200, {
|
||||||
|
session: { code: 'ABCD12', status: 'reveal', current_round: 1 },
|
||||||
|
round_question: { id: 11, prompt: 'Q?', answers: [{ text: 'A' }] },
|
||||||
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
vi.stubGlobal('fetch', fetchMock);
|
vi.stubGlobal('fetch', fetchMock);
|
||||||
@@ -97,8 +44,13 @@ describe('PlayerShellComponent gameplay wiring', () => {
|
|||||||
const fetchMock: FetchMock = vi
|
const fetchMock: FetchMock = vi
|
||||||
.fn()
|
.fn()
|
||||||
.mockResolvedValueOnce(jsonResponse(500, { error: 'Temporary submit outage' }))
|
.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, { ok: true }))
|
||||||
.mockResolvedValueOnce(jsonResponse(200, sessionDetailPayload('guess', { answers: ['A', 'B'] })));
|
.mockResolvedValueOnce(
|
||||||
|
jsonResponse(200, {
|
||||||
|
session: { code: 'ABCD12', status: 'guess', current_round: 1 },
|
||||||
|
round_question: { id: 11, prompt: 'Q?', answers: [{ text: 'A' }, { text: 'B' }] },
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
vi.stubGlobal('fetch', fetchMock);
|
vi.stubGlobal('fetch', fetchMock);
|
||||||
|
|
||||||
@@ -110,19 +62,10 @@ describe('PlayerShellComponent gameplay wiring', () => {
|
|||||||
component.session = {
|
component.session = {
|
||||||
session: { code: 'ABCD12', status: 'lie', current_round: 1 },
|
session: { code: 'ABCD12', status: 'lie', current_round: 1 },
|
||||||
round_question: { id: 11, prompt: 'Q?', answers: [] },
|
round_question: { id: 11, prompt: 'Q?', answers: [] },
|
||||||
players: [],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
await component.submitLie();
|
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?.kind).toBe('lie');
|
||||||
expect(component.submitError?.message).toContain('Lie submit failed: Temporary submit outage');
|
expect(component.submitError?.message).toContain('Lie submit failed: Temporary submit outage');
|
||||||
|
|
||||||
@@ -132,189 +75,4 @@ describe('PlayerShellComponent gameplay wiring', () => {
|
|||||||
expect(component.session?.session.status).toBe('guess');
|
expect(component.session?.session.status).toBe('guess');
|
||||||
expect(fetchMock).toHaveBeenCalledTimes(3);
|
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,27 +1,10 @@
|
|||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { Component, OnDestroy, OnInit, Optional } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
import { ActivatedRoute } from '@angular/router';
|
|
||||||
import { FormsModule } from '@angular/forms';
|
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 {
|
interface SessionDetail {
|
||||||
session: { code: string; status: string; current_round: number };
|
session: { code: string; status: string; current_round: number };
|
||||||
round_question: { id: number; prompt: string; answers: Array<{ text: string }> } | null;
|
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({
|
@Component({
|
||||||
@@ -38,19 +21,6 @@ function resolveLocalStorage(): Storage | undefined {
|
|||||||
<button (click)="joinSession()" [disabled]="loading">Join</button>
|
<button (click)="joinSession()" [disabled]="loading">Join</button>
|
||||||
</div>
|
</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">
|
<div class="panel" *ngIf="session">
|
||||||
<p><strong>Status:</strong> {{ session.session.status }}</p>
|
<p><strong>Status:</strong> {{ session.session.status }}</p>
|
||||||
<p *ngIf="session.round_question"><strong>Prompt:</strong> {{ session.round_question.prompt }}</p>
|
<p *ngIf="session.round_question"><strong>Prompt:</strong> {{ session.round_question.prompt }}</p>
|
||||||
@@ -73,25 +43,13 @@ function resolveLocalStorage(): Storage | undefined {
|
|||||||
|
|
||||||
<button (click)="submitGuess()" [disabled]="loading || session.session.status !== 'guess' || !selectedGuess">Submit guess</button>
|
<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>
|
<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>
|
</div>
|
||||||
|
|
||||||
<p *ngIf="error" class="error">{{ error }}</p>
|
<p *ngIf="error" class="error">{{ error }}</p>
|
||||||
<p *ngIf="submitError" class="error">{{ submitError.message }}</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 implements OnInit, OnDestroy {
|
export class PlayerShellComponent {
|
||||||
sessionCode = '';
|
sessionCode = '';
|
||||||
nickname = '';
|
nickname = '';
|
||||||
playerId = 0;
|
playerId = 0;
|
||||||
@@ -102,163 +60,11 @@ export class PlayerShellComponent implements OnInit, OnDestroy {
|
|||||||
error = '';
|
error = '';
|
||||||
submitError: { kind: 'lie' | 'guess'; message: string } | null = null;
|
submitError: { kind: 'lie' | 'guess'; message: string } | null = null;
|
||||||
session: SessionDetail | 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 {
|
private normalizeCode(value: string): string {
|
||||||
return value.trim().toUpperCase();
|
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> {
|
private async request<T>(path: string, method: 'GET' | 'POST', payload?: unknown): Promise<T> {
|
||||||
const response = await fetch(path, {
|
const response = await fetch(path, {
|
||||||
method,
|
method,
|
||||||
@@ -280,55 +86,40 @@ export class PlayerShellComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
async refreshSession(): Promise<void> {
|
async refreshSession(): Promise<void> {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.loadingTransition = 'refresh';
|
|
||||||
this.error = '';
|
this.error = '';
|
||||||
try {
|
try {
|
||||||
const state = await this.controller.hydrateLobby(this.sessionCode);
|
const code = this.normalizeCode(this.sessionCode);
|
||||||
if (!state.session || state.errorMessage) {
|
this.session = await this.request<SessionDetail>(`/lobby/sessions/${encodeURIComponent(code)}`, 'GET');
|
||||||
throw new Error(state.errorMessage ?? 'Unknown error');
|
|
||||||
}
|
|
||||||
this.session = state.session as SessionDetail;
|
|
||||||
this.sessionCode = this.session.session.code;
|
this.sessionCode = this.session.session.code;
|
||||||
if (this.session.session.status !== 'guess') {
|
if (this.session.session.status !== 'guess') {
|
||||||
this.selectedGuess = '';
|
this.selectedGuess = '';
|
||||||
}
|
}
|
||||||
this.syncFinalLeaderboard();
|
|
||||||
this.markOnline();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.error = `Session refresh failed: ${this.toMessage(error)}`;
|
this.error = `Session refresh failed: ${(error as Error).message}`;
|
||||||
this.markConnectionIssue(error);
|
|
||||||
} finally {
|
} finally {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
this.loadingTransition = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async joinSession(): Promise<void> {
|
async joinSession(): Promise<void> {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.loadingTransition = 'join';
|
|
||||||
this.error = '';
|
this.error = '';
|
||||||
try {
|
try {
|
||||||
const state = await this.controller.joinLobby(this.sessionCode, this.nickname);
|
const payload = await this.request<{
|
||||||
if (!state.session || state.errorMessage) {
|
player: { id: number; session_token: string };
|
||||||
throw new Error(state.errorMessage ?? 'Unknown error');
|
session: { code: string };
|
||||||
}
|
}>('/lobby/sessions/join', 'POST', {
|
||||||
this.session = state.session as SessionDetail;
|
code: this.normalizeCode(this.sessionCode),
|
||||||
this.sessionCode = this.session.session.code;
|
nickname: this.nickname.trim(),
|
||||||
|
});
|
||||||
const sessionContext = this.sessionContextStore.get();
|
this.playerId = payload.player.id;
|
||||||
this.playerId = sessionContext?.playerId ?? 0;
|
this.sessionToken = payload.player.session_token;
|
||||||
this.sessionToken = sessionContext?.token ?? '';
|
this.sessionCode = payload.session.code;
|
||||||
if (this.session.session.status !== 'guess') {
|
await this.refreshSession();
|
||||||
this.selectedGuess = '';
|
|
||||||
}
|
|
||||||
this.syncFinalLeaderboard();
|
|
||||||
this.markOnline();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.error = `Join failed: ${this.toMessage(error)}`;
|
this.error = `Join failed: ${(error as Error).message}`;
|
||||||
this.markConnectionIssue(error);
|
|
||||||
} finally {
|
} finally {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
this.loadingTransition = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -337,7 +128,6 @@ export class PlayerShellComponent implements OnInit, OnDestroy {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.loadingTransition = 'submit-lie';
|
|
||||||
this.submitError = null;
|
this.submitError = null;
|
||||||
try {
|
try {
|
||||||
await this.request(
|
await this.request(
|
||||||
@@ -350,13 +140,10 @@ export class PlayerShellComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
await this.refreshSession();
|
await this.refreshSession();
|
||||||
this.markOnline();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.submitError = { kind: 'lie', message: `Lie submit failed: ${this.toMessage(error)}` };
|
this.submitError = { kind: 'lie', message: `Lie submit failed: ${(error as Error).message}` };
|
||||||
this.markConnectionIssue(error);
|
|
||||||
} finally {
|
} finally {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
this.loadingTransition = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,7 +152,6 @@ export class PlayerShellComponent implements OnInit, OnDestroy {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.loadingTransition = 'submit-guess';
|
|
||||||
this.submitError = null;
|
this.submitError = null;
|
||||||
try {
|
try {
|
||||||
await this.request(
|
await this.request(
|
||||||
@@ -378,13 +164,10 @@ export class PlayerShellComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
await this.refreshSession();
|
await this.refreshSession();
|
||||||
this.markOnline();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.submitError = { kind: 'guess', message: `Guess submit failed: ${this.toMessage(error)}` };
|
this.submitError = { kind: 'guess', message: `Guess submit failed: ${(error as Error).message}` };
|
||||||
this.markConnectionIssue(error);
|
|
||||||
} finally {
|
} finally {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
this.loadingTransition = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,86 +0,0 @@
|
|||||||
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');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
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,36 +1,12 @@
|
|||||||
import {
|
|
||||||
mapCalculateScoresResponse,
|
|
||||||
mapFinishGameResponse,
|
|
||||||
mapHealthResponse,
|
|
||||||
mapJoinSessionResponse,
|
|
||||||
mapMixAnswersResponse,
|
|
||||||
mapScoreboardResponse,
|
|
||||||
mapSessionDetailResponse,
|
|
||||||
mapShowQuestionResponse,
|
|
||||||
mapStartNextRoundResponse,
|
|
||||||
mapStartRoundResponse,
|
|
||||||
mapSubmitGuessResponse,
|
|
||||||
mapSubmitLieResponse
|
|
||||||
} from './mappers';
|
|
||||||
import type {
|
import type {
|
||||||
ApiFailure,
|
ApiFailure,
|
||||||
ApiResult,
|
ApiResult,
|
||||||
CalculateScoresResponse,
|
|
||||||
FinishGameResponse,
|
|
||||||
HealthResponse,
|
HealthResponse,
|
||||||
JoinSessionRequest,
|
JoinSessionRequest,
|
||||||
JoinSessionResponse,
|
JoinSessionResponse,
|
||||||
MixAnswersResponse,
|
|
||||||
ScoreboardResponse,
|
|
||||||
SessionDetailResponse,
|
SessionDetailResponse,
|
||||||
ShowQuestionResponse,
|
|
||||||
StartNextRoundResponse,
|
|
||||||
StartRoundRequest,
|
StartRoundRequest,
|
||||||
StartRoundResponse,
|
StartRoundResponse
|
||||||
SubmitGuessRequest,
|
|
||||||
SubmitGuessResponse,
|
|
||||||
SubmitLieRequest,
|
|
||||||
SubmitLieResponse
|
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
export interface AngularHttpError {
|
export interface AngularHttpError {
|
||||||
@@ -49,18 +25,6 @@ export interface AngularApiClient {
|
|||||||
getSession(code: string): Promise<ApiResult<SessionDetailResponse>>;
|
getSession(code: string): Promise<ApiResult<SessionDetailResponse>>;
|
||||||
joinSession(payload: JoinSessionRequest): Promise<ApiResult<JoinSessionResponse>>;
|
joinSession(payload: JoinSessionRequest): Promise<ApiResult<JoinSessionResponse>>;
|
||||||
startRound(code: string, payload: StartRoundRequest): Promise<ApiResult<StartRoundResponse>>;
|
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 {
|
function toFailure(error: unknown): ApiFailure {
|
||||||
@@ -96,10 +60,10 @@ function buildUrl(baseUrl: string, path: string): string {
|
|||||||
return `${normalizeBaseUrl(baseUrl)}${path}`;
|
return `${normalizeBaseUrl(baseUrl)}${path}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function wrap<T>(call: () => Promise<unknown>, mapper: (payload: unknown) => T): Promise<ApiResult<T>> {
|
async function wrap<T>(call: () => Promise<T>): Promise<ApiResult<T>> {
|
||||||
let payload: unknown;
|
|
||||||
try {
|
try {
|
||||||
payload = await call();
|
const data = await call();
|
||||||
|
return { ok: true, status: 200, data };
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return {
|
return {
|
||||||
ok: false,
|
ok: false,
|
||||||
@@ -107,156 +71,35 @@ async function wrap<T>(call: () => Promise<unknown>, mapper: (payload: unknown)
|
|||||||
error: toFailure(error)
|
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 {
|
export function createAngularApiClient(http: AngularHttpClientLike, baseUrl = ''): AngularApiClient {
|
||||||
return {
|
return {
|
||||||
health: () =>
|
health: () => wrap(() => http.get<HealthResponse>(buildUrl(baseUrl, '/healthz'), { withCredentials: true })),
|
||||||
wrap(() => http.get<HealthResponse>(buildUrl(baseUrl, '/healthz'), { withCredentials: true }), mapHealthResponse),
|
|
||||||
getSession: (code: string) =>
|
getSession: (code: string) =>
|
||||||
wrap(
|
wrap(() =>
|
||||||
() =>
|
http.get<SessionDetailResponse>(buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}`), {
|
||||||
http.get<SessionDetailResponse>(buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}`), {
|
withCredentials: true
|
||||||
withCredentials: true
|
})
|
||||||
}),
|
|
||||||
mapSessionDetailResponse
|
|
||||||
),
|
),
|
||||||
joinSession: (payload: JoinSessionRequest) =>
|
joinSession: (payload: JoinSessionRequest) =>
|
||||||
wrap(
|
wrap(() =>
|
||||||
() =>
|
http.post<JoinSessionResponse>(
|
||||||
http.post<JoinSessionResponse>(
|
buildUrl(baseUrl, '/lobby/sessions/join'),
|
||||||
buildUrl(baseUrl, '/lobby/sessions/join'),
|
{
|
||||||
{
|
code: normalizeCode(payload.code),
|
||||||
code: normalizeCode(payload.code),
|
nickname: payload.nickname.trim()
|
||||||
nickname: payload.nickname.trim()
|
},
|
||||||
},
|
{ withCredentials: true }
|
||||||
{ withCredentials: true }
|
)
|
||||||
),
|
|
||||||
mapJoinSessionResponse
|
|
||||||
),
|
),
|
||||||
startRound: (code: string, payload: StartRoundRequest) =>
|
startRound: (code: string, payload: StartRoundRequest) =>
|
||||||
wrap(
|
wrap(() =>
|
||||||
() =>
|
http.post<StartRoundResponse>(
|
||||||
http.post<StartRoundResponse>(
|
buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/start`),
|
||||||
buildUrl(baseUrl, `/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/start`),
|
payload,
|
||||||
payload,
|
{ withCredentials: true }
|
||||||
{ 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,35 +1,11 @@
|
|||||||
import {
|
|
||||||
mapCalculateScoresResponse,
|
|
||||||
mapFinishGameResponse,
|
|
||||||
mapHealthResponse,
|
|
||||||
mapJoinSessionResponse,
|
|
||||||
mapMixAnswersResponse,
|
|
||||||
mapNextRoundResponse,
|
|
||||||
mapScoreboardResponse,
|
|
||||||
mapSessionDetailResponse,
|
|
||||||
mapShowQuestionResponse,
|
|
||||||
mapStartRoundResponse,
|
|
||||||
mapSubmitGuessResponse,
|
|
||||||
mapSubmitLieResponse
|
|
||||||
} from './mappers';
|
|
||||||
import type {
|
import type {
|
||||||
ApiResult,
|
ApiResult,
|
||||||
CalculateScoresResponse,
|
|
||||||
FinishGameResponse,
|
|
||||||
HealthResponse,
|
HealthResponse,
|
||||||
JoinSessionRequest,
|
JoinSessionRequest,
|
||||||
JoinSessionResponse,
|
JoinSessionResponse,
|
||||||
MixAnswersResponse,
|
|
||||||
NextRoundResponse,
|
|
||||||
ScoreboardResponse,
|
|
||||||
SessionDetailResponse,
|
SessionDetailResponse,
|
||||||
ShowQuestionResponse,
|
|
||||||
StartRoundRequest,
|
StartRoundRequest,
|
||||||
StartRoundResponse,
|
StartRoundResponse
|
||||||
SubmitGuessRequest,
|
|
||||||
SubmitGuessResponse,
|
|
||||||
SubmitLieRequest,
|
|
||||||
SubmitLieResponse
|
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
export interface ApiClient {
|
export interface ApiClient {
|
||||||
@@ -37,23 +13,10 @@ export interface ApiClient {
|
|||||||
getSession(code: string): Promise<ApiResult<SessionDetailResponse>>;
|
getSession(code: string): Promise<ApiResult<SessionDetailResponse>>;
|
||||||
joinSession(payload: JoinSessionRequest): Promise<ApiResult<JoinSessionResponse>>;
|
joinSession(payload: JoinSessionRequest): Promise<ApiResult<JoinSessionResponse>>;
|
||||||
startRound(code: string, payload: StartRoundRequest): Promise<ApiResult<StartRoundResponse>>;
|
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 {
|
export function createApiClient(baseUrl = '', fetchImpl: typeof fetch = fetch): ApiClient {
|
||||||
async function request<T>(
|
async function request<T>(path: string, method: 'GET' | 'POST', payload?: unknown): Promise<ApiResult<T>> {
|
||||||
path: string,
|
|
||||||
method: 'GET' | 'POST',
|
|
||||||
mapper: (payload: unknown) => T,
|
|
||||||
payload?: unknown
|
|
||||||
): Promise<ApiResult<T>> {
|
|
||||||
let response: Response;
|
let response: Response;
|
||||||
try {
|
try {
|
||||||
response = await fetchImpl(`${baseUrl}${path}`, {
|
response = await fetchImpl(`${baseUrl}${path}`, {
|
||||||
@@ -96,102 +59,22 @@ export function createApiClient(baseUrl = '', fetchImpl: typeof fetch = fetch):
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return { ok: true, status: response.status, data: responsePayload as T };
|
||||||
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 {
|
return {
|
||||||
health: () => request<HealthResponse>('/healthz', 'GET', mapHealthResponse),
|
health: () => request<HealthResponse>('/healthz', 'GET'),
|
||||||
getSession: (code: string) =>
|
getSession: (code: string) =>
|
||||||
request<SessionDetailResponse>(
|
request<SessionDetailResponse>(`/lobby/sessions/${encodeURIComponent(code.trim().toUpperCase())}`, 'GET'),
|
||||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}`,
|
|
||||||
'GET',
|
|
||||||
mapSessionDetailResponse
|
|
||||||
),
|
|
||||||
joinSession: (payload: JoinSessionRequest) =>
|
joinSession: (payload: JoinSessionRequest) =>
|
||||||
request<JoinSessionResponse>(
|
request<JoinSessionResponse>('/lobby/sessions/join', 'POST', {
|
||||||
'/lobby/sessions/join',
|
code: payload.code.trim().toUpperCase(),
|
||||||
'POST',
|
nickname: payload.nickname.trim()
|
||||||
mapJoinSessionResponse,
|
}),
|
||||||
{
|
|
||||||
code: normalizeCode(payload.code),
|
|
||||||
nickname: payload.nickname.trim()
|
|
||||||
}
|
|
||||||
),
|
|
||||||
startRound: (code: string, payload: StartRoundRequest) =>
|
startRound: (code: string, payload: StartRoundRequest) =>
|
||||||
request<StartRoundResponse>(
|
request<StartRoundResponse>(
|
||||||
`/lobby/sessions/${encodeURIComponent(normalizeCode(code))}/rounds/start`,
|
`/lobby/sessions/${encodeURIComponent(code.trim().toUpperCase())}/rounds/start`,
|
||||||
'POST',
|
'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
|
payload
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,359 +0,0 @@
|
|||||||
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,113 +101,6 @@ 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 type ApiErrorKind = 'network' | 'http' | 'parse';
|
||||||
|
|
||||||
export interface ApiFailure {
|
export interface ApiFailure {
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
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,8 +6,6 @@ import {
|
|||||||
type SessionContextInput,
|
type SessionContextInput,
|
||||||
type SessionContextStore as PersistedSessionContextStore
|
type SessionContextStore as PersistedSessionContextStore
|
||||||
} from './session-context-store';
|
} 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';
|
export type AsyncState = 'idle' | 'loading' | 'success' | 'error';
|
||||||
|
|
||||||
@@ -16,7 +14,6 @@ export type SessionContextStore = Pick<PersistedSessionContextStore, 'get' | 'se
|
|||||||
export interface VerticalSliceState {
|
export interface VerticalSliceState {
|
||||||
sessionCode: string;
|
sessionCode: string;
|
||||||
session: SessionDetailResponse | null;
|
session: SessionDetailResponse | null;
|
||||||
gameplayPhase: GameplayPhase | null;
|
|
||||||
joinState: AsyncState;
|
joinState: AsyncState;
|
||||||
startRoundState: AsyncState;
|
startRoundState: AsyncState;
|
||||||
loadingSession: boolean;
|
loadingSession: boolean;
|
||||||
@@ -39,7 +36,6 @@ export function createVerticalSliceController(
|
|||||||
const state: VerticalSliceState = {
|
const state: VerticalSliceState = {
|
||||||
sessionCode: persistedContext?.sessionCode ?? '',
|
sessionCode: persistedContext?.sessionCode ?? '',
|
||||||
session: null,
|
session: null,
|
||||||
gameplayPhase: null,
|
|
||||||
joinState: 'idle',
|
joinState: 'idle',
|
||||||
startRoundState: 'idle',
|
startRoundState: 'idle',
|
||||||
loadingSession: false,
|
loadingSession: false,
|
||||||
@@ -58,7 +54,7 @@ export function createVerticalSliceController(
|
|||||||
|
|
||||||
if (!state.sessionCode) {
|
if (!state.sessionCode) {
|
||||||
state.loadingSession = false;
|
state.loadingSession = false;
|
||||||
state.errorMessage = lobbyMessage('session_code_required');
|
state.errorMessage = 'Session-kode mangler.';
|
||||||
return { ...state };
|
return { ...state };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,13 +62,11 @@ export function createVerticalSliceController(
|
|||||||
state.loadingSession = false;
|
state.loadingSession = false;
|
||||||
|
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
state.errorMessage = lobbyMessageFromApiPayload(result.error.payload, 'session_fetch_failed');
|
state.errorMessage = 'Kunne ikke hente lobby-status.';
|
||||||
state.gameplayPhase = null;
|
|
||||||
return { ...state };
|
return { ...state };
|
||||||
}
|
}
|
||||||
|
|
||||||
state.session = result.data;
|
state.session = result.data;
|
||||||
state.gameplayPhase = deriveGameplayPhase(result.data);
|
|
||||||
state.sessionCode = normalizeCode(result.data.session.code);
|
state.sessionCode = normalizeCode(result.data.session.code);
|
||||||
|
|
||||||
if (persistedContext && state.sessionCode === normalizeCode(persistedContext.sessionCode)) {
|
if (persistedContext && state.sessionCode === normalizeCode(persistedContext.sessionCode)) {
|
||||||
@@ -93,7 +87,7 @@ export function createVerticalSliceController(
|
|||||||
const join = await api.joinSession({ code: requestCode, nickname });
|
const join = await api.joinSession({ code: requestCode, nickname });
|
||||||
if (!join.ok) {
|
if (!join.ok) {
|
||||||
state.joinState = 'error';
|
state.joinState = 'error';
|
||||||
state.errorMessage = lobbyMessageFromApiPayload(join.error.payload, 'join_failed');
|
state.errorMessage = 'Join fejlede. Tjek kode eller nickname og prøv igen.';
|
||||||
return { ...state };
|
return { ...state };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,14 +114,14 @@ export function createVerticalSliceController(
|
|||||||
|
|
||||||
if (!codeToUse) {
|
if (!codeToUse) {
|
||||||
state.startRoundState = 'error';
|
state.startRoundState = 'error';
|
||||||
state.errorMessage = lobbyMessage('session_code_required');
|
state.errorMessage = 'Session-kode mangler.';
|
||||||
return { ...state };
|
return { ...state };
|
||||||
}
|
}
|
||||||
|
|
||||||
const start = await api.startRound(codeToUse, { category_slug: categorySlug });
|
const start = await api.startRound(codeToUse, { category_slug: categorySlug });
|
||||||
if (!start.ok) {
|
if (!start.ok) {
|
||||||
state.startRoundState = 'error';
|
state.startRoundState = 'error';
|
||||||
state.errorMessage = lobbyMessageFromApiPayload(start.error.payload, 'start_round_failed');
|
state.errorMessage = 'Kunne ikke starte runden. Opdatér lobbyen og prøv igen.';
|
||||||
return { ...state };
|
return { ...state };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -189,143 +189,6 @@ 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 () => {
|
it('maps HttpErrorResponse-style failures to ApiResult errors', async () => {
|
||||||
const http = {
|
const http = {
|
||||||
get: vi.fn<AngularHttpClientLike['get']>(async () => {
|
get: vi.fn<AngularHttpClientLike['get']>(async () => {
|
||||||
|
|||||||
@@ -74,12 +74,6 @@ beforeAll(async () => {
|
|||||||
return;
|
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/')) {
|
if (req.url?.startsWith('/lobby/sessions/')) {
|
||||||
res.writeHead(404, { 'content-type': 'application/json' });
|
res.writeHead(404, { 'content-type': 'application/json' });
|
||||||
res.end(JSON.stringify({ error: 'Session not found' }));
|
res.end(JSON.stringify({ error: 'Session not found' }));
|
||||||
@@ -129,18 +123,6 @@ 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 () => {
|
it('returns consistent HTTP error shape for 4xx/5xx', async () => {
|
||||||
const client = createApiClient(baseUrl);
|
const client = createApiClient(baseUrl);
|
||||||
|
|
||||||
|
|||||||
@@ -1,106 +0,0 @@
|
|||||||
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,7 +105,6 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
|||||||
vi.doUnmock('../src/spa/session-context-store');
|
vi.doUnmock('../src/spa/session-context-store');
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('tracks loading and success state for join + start flow', async () => {
|
it('tracks loading and success state for join + start flow', async () => {
|
||||||
const api = makeApiMock();
|
const api = makeApiMock();
|
||||||
const controller = createVerticalSliceController(api);
|
const controller = createVerticalSliceController(api);
|
||||||
@@ -161,7 +160,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
|||||||
joinSession: vi.fn().mockResolvedValue({
|
joinSession: vi.fn().mockResolvedValue({
|
||||||
ok: false,
|
ok: false,
|
||||||
status: 404,
|
status: 404,
|
||||||
error: { kind: 'http', status: 404, message: 'HTTP 404', payload: { error: 'Session not found', error_code: 'session_not_found' } }
|
error: { kind: 'http', status: 404, message: 'HTTP 404', payload: { error: 'Session not found' } }
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -170,7 +169,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
|||||||
|
|
||||||
const state = controller.getState();
|
const state = controller.getState();
|
||||||
expect(state.joinState).toBe('error');
|
expect(state.joinState).toBe('error');
|
||||||
expect(state.errorMessage).toBe('Session code is invalid or the session no longer exists.');
|
expect(state.errorMessage).toContain('Join fejlede');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('surfaces a friendly error when round start fails', async () => {
|
it('surfaces a friendly error when round start fails', async () => {
|
||||||
@@ -178,7 +177,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
|||||||
startRound: vi.fn().mockResolvedValue({
|
startRound: vi.fn().mockResolvedValue({
|
||||||
ok: false,
|
ok: false,
|
||||||
status: 400,
|
status: 400,
|
||||||
error: { kind: 'http', status: 400, message: 'HTTP 400', payload: { error: 'Round can only be started from lobby', error_code: 'round_start_invalid_phase' } }
|
error: { kind: 'http', status: 400, message: 'HTTP 400', payload: { error: 'Round can only be started from lobby' } }
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -187,7 +186,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
|||||||
|
|
||||||
const state = controller.getState();
|
const state = controller.getState();
|
||||||
expect(state.startRoundState).toBe('error');
|
expect(state.startRoundState).toBe('error');
|
||||||
expect(state.errorMessage).toBe('Could not start round. Refresh the lobby and try again.');
|
expect(state.errorMessage).toContain('Kunne ikke starte runden');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('shows local validation error and avoids API call when hydrating without any session code', async () => {
|
it('shows local validation error and avoids API call when hydrating without any session code', async () => {
|
||||||
@@ -197,7 +196,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
|||||||
await controller.hydrateLobby(' ');
|
await controller.hydrateLobby(' ');
|
||||||
|
|
||||||
const state = controller.getState();
|
const state = controller.getState();
|
||||||
expect(state.errorMessage).toBe('Session code is required.');
|
expect(state.errorMessage).toBe('Session-kode mangler.');
|
||||||
expect(state.loadingSession).toBe(false);
|
expect(state.loadingSession).toBe(false);
|
||||||
expect(api.getSession).not.toHaveBeenCalled();
|
expect(api.getSession).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
@@ -210,7 +209,7 @@ describe('vertical slice controller: lobby -> join -> start round', () => {
|
|||||||
|
|
||||||
const state = controller.getState();
|
const state = controller.getState();
|
||||||
expect(state.startRoundState).toBe('error');
|
expect(state.startRoundState).toBe('error');
|
||||||
expect(state.errorMessage).toBe('Session code is required.');
|
expect(state.errorMessage).toBe('Session-kode mangler.');
|
||||||
expect(api.startRound).not.toHaveBeenCalled();
|
expect(api.startRound).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,10 @@
|
|||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"module": "ES2022",
|
"module": "ES2022",
|
||||||
"moduleResolution": "bundler",
|
"moduleResolution": "bundler",
|
||||||
"resolveJsonModule": true,
|
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"lib": ["ES2022", "DOM"],
|
"lib": ["ES2022", "DOM"],
|
||||||
"types": ["vitest/globals", "node"]
|
"types": ["vitest/globals", "node"]
|
||||||
},
|
},
|
||||||
"include": ["src", "tests", "../shared/i18n/*.json"]
|
"include": ["src", "tests"]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
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,7 +5,6 @@ from django.shortcuts import render
|
|||||||
from fupogfakta.models import Category
|
from fupogfakta.models import Category
|
||||||
|
|
||||||
from .feature_flags import use_spa_ui
|
from .feature_flags import use_spa_ui
|
||||||
from .i18n import lobby_i18n_catalog
|
|
||||||
|
|
||||||
|
|
||||||
def _render_spa_shell(request, shell_route: str, shell_kind: str):
|
def _render_spa_shell(request, shell_route: str, shell_kind: str):
|
||||||
@@ -16,7 +15,6 @@ def _render_spa_shell(request, shell_route: str, shell_kind: str):
|
|||||||
"shell_route": shell_route,
|
"shell_route": shell_route,
|
||||||
"shell_kind": shell_kind,
|
"shell_kind": shell_kind,
|
||||||
"spa_asset_base": settings.WPP_SPA_ASSET_BASE,
|
"spa_asset_base": settings.WPP_SPA_ASSET_BASE,
|
||||||
"lobby_i18n": lobby_i18n_catalog(),
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,18 +30,11 @@ def host_screen(request, spa_path=None):
|
|||||||
return _render_spa_shell(request, host_route, "host")
|
return _render_spa_shell(request, host_route, "host")
|
||||||
|
|
||||||
categories = Category.objects.filter(is_active=True).order_by("name")
|
categories = Category.objects.filter(is_active=True).order_by("name")
|
||||||
return render(
|
return render(request, "lobby/host_screen.html", {"categories": categories})
|
||||||
request,
|
|
||||||
"lobby/host_screen.html",
|
|
||||||
{
|
|
||||||
"categories": categories,
|
|
||||||
"lobby_i18n": lobby_i18n_catalog(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def player_screen(request):
|
def player_screen(request):
|
||||||
if use_spa_ui():
|
if use_spa_ui():
|
||||||
return _render_spa_shell(request, "/player", "player")
|
return _render_spa_shell(request, "/player", "player")
|
||||||
|
|
||||||
return render(request, "lobby/player_screen.html", {"lobby_i18n": lobby_i18n_catalog()})
|
return render(request, "lobby/player_screen.html")
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ from fupogfakta.models import (
|
|||||||
ScoreEvent,
|
ScoreEvent,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .i18n import api_error, lobby_i18n_errors
|
|
||||||
|
|
||||||
SESSION_CODE_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
SESSION_CODE_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
||||||
SESSION_CODE_LENGTH = 6
|
SESSION_CODE_LENGTH = 6
|
||||||
MAX_CODE_GENERATION_ATTEMPTS = 20
|
MAX_CODE_GENERATION_ATTEMPTS = 20
|
||||||
@@ -31,7 +29,6 @@ JOINABLE_STATUSES = {
|
|||||||
GameSession.Status.GUESS,
|
GameSession.Status.GUESS,
|
||||||
GameSession.Status.REVEAL,
|
GameSession.Status.REVEAL,
|
||||||
}
|
}
|
||||||
ERROR_CODES = lobby_i18n_errors()
|
|
||||||
|
|
||||||
|
|
||||||
def _json_body(request: HttpRequest) -> dict:
|
def _json_body(request: HttpRequest) -> dict:
|
||||||
@@ -127,41 +124,21 @@ def join_session(request: HttpRequest) -> JsonResponse:
|
|||||||
nickname = str(payload.get("nickname", "")).strip()
|
nickname = str(payload.get("nickname", "")).strip()
|
||||||
|
|
||||||
if not code:
|
if not code:
|
||||||
return api_error(
|
return JsonResponse({"error": "Session code is required"}, status=400)
|
||||||
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:
|
if len(nickname) < 2 or len(nickname) > 40:
|
||||||
return api_error(
|
return JsonResponse({"error": "Nickname must be between 2 and 40 characters"}, status=400)
|
||||||
code=ERROR_CODES.get("nickname_invalid", "nickname_invalid"),
|
|
||||||
message="Nickname must be between 2 and 40 characters",
|
|
||||||
status=400,
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session = GameSession.objects.get(code=code)
|
session = GameSession.objects.get(code=code)
|
||||||
except GameSession.DoesNotExist:
|
except GameSession.DoesNotExist:
|
||||||
return api_error(
|
return JsonResponse({"error": "Session not found"}, status=404)
|
||||||
code=ERROR_CODES.get("session_not_found", "session_not_found"),
|
|
||||||
message="Session not found",
|
|
||||||
status=404,
|
|
||||||
)
|
|
||||||
|
|
||||||
if session.status not in JOINABLE_STATUSES:
|
if session.status not in JOINABLE_STATUSES:
|
||||||
return api_error(
|
return JsonResponse({"error": "Session is not joinable"}, status=400)
|
||||||
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():
|
if Player.objects.filter(session=session, nickname__iexact=nickname).exists():
|
||||||
return api_error(
|
return JsonResponse({"error": "Nickname already taken"}, status=409)
|
||||||
code=ERROR_CODES.get("nickname_taken", "nickname_taken"),
|
|
||||||
message="Nickname already taken",
|
|
||||||
status=409,
|
|
||||||
)
|
|
||||||
|
|
||||||
player = Player.objects.create(session=session, nickname=nickname)
|
player = Player.objects.create(session=session, nickname=nickname)
|
||||||
|
|
||||||
@@ -189,11 +166,7 @@ def session_detail(request: HttpRequest, code: str) -> JsonResponse:
|
|||||||
try:
|
try:
|
||||||
session = GameSession.objects.get(code=session_code)
|
session = GameSession.objects.get(code=session_code)
|
||||||
except GameSession.DoesNotExist:
|
except GameSession.DoesNotExist:
|
||||||
return api_error(
|
return JsonResponse({"error": "Session not found"}, status=404)
|
||||||
code=ERROR_CODES.get("session_not_found", "session_not_found"),
|
|
||||||
message="Session not found",
|
|
||||||
status=404,
|
|
||||||
)
|
|
||||||
|
|
||||||
players = list(
|
players = list(
|
||||||
session.players.order_by("nickname").values(
|
session.players.order_by("nickname").values(
|
||||||
@@ -250,41 +223,25 @@ def start_round(request: HttpRequest, code: str) -> JsonResponse:
|
|||||||
category_slug = str(payload.get("category_slug", "")).strip()
|
category_slug = str(payload.get("category_slug", "")).strip()
|
||||||
|
|
||||||
if not category_slug:
|
if not category_slug:
|
||||||
return api_error(
|
return JsonResponse({"error": "category_slug is required"}, status=400)
|
||||||
code=ERROR_CODES.get("category_slug_required", "category_slug_required"),
|
|
||||||
message="category_slug is required",
|
|
||||||
status=400,
|
|
||||||
)
|
|
||||||
|
|
||||||
session_code = _normalize_session_code(code)
|
session_code = _normalize_session_code(code)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session = GameSession.objects.get(code=session_code)
|
session = GameSession.objects.get(code=session_code)
|
||||||
except GameSession.DoesNotExist:
|
except GameSession.DoesNotExist:
|
||||||
return api_error(
|
return JsonResponse({"error": "Session not found"}, status=404)
|
||||||
code=ERROR_CODES.get("session_not_found", "session_not_found"),
|
|
||||||
message="Session not found",
|
|
||||||
status=404,
|
|
||||||
)
|
|
||||||
|
|
||||||
if session.host_id != request.user.id:
|
if session.host_id != request.user.id:
|
||||||
return JsonResponse({"error": "Only host can start round"}, status=403)
|
return JsonResponse({"error": "Only host can start round"}, status=403)
|
||||||
|
|
||||||
if session.status != GameSession.Status.LOBBY:
|
if session.status != GameSession.Status.LOBBY:
|
||||||
return api_error(
|
return JsonResponse({"error": "Round can only be started from lobby"}, status=400)
|
||||||
code=ERROR_CODES.get("round_start_invalid_phase", "round_start_invalid_phase"),
|
|
||||||
message="Round can only be started from lobby",
|
|
||||||
status=400,
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
category = Category.objects.get(slug=category_slug, is_active=True)
|
category = Category.objects.get(slug=category_slug, is_active=True)
|
||||||
except Category.DoesNotExist:
|
except Category.DoesNotExist:
|
||||||
return api_error(
|
return JsonResponse({"error": "Category not found"}, status=404)
|
||||||
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():
|
if not Question.objects.filter(category=category, is_active=True).exists():
|
||||||
return JsonResponse({"error": "Category has no active questions"}, status=400)
|
return JsonResponse({"error": "Category has no active questions"}, status=400)
|
||||||
@@ -292,11 +249,7 @@ def start_round(request: HttpRequest, code: str) -> JsonResponse:
|
|||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
session = GameSession.objects.select_for_update().get(pk=session.pk)
|
session = GameSession.objects.select_for_update().get(pk=session.pk)
|
||||||
if session.status != GameSession.Status.LOBBY:
|
if session.status != GameSession.Status.LOBBY:
|
||||||
return api_error(
|
return JsonResponse({"error": "Round can only be started from lobby"}, status=400)
|
||||||
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(
|
round_config, created = RoundConfig.objects.get_or_create(
|
||||||
session=session,
|
session=session,
|
||||||
@@ -304,11 +257,7 @@ def start_round(request: HttpRequest, code: str) -> JsonResponse:
|
|||||||
defaults={"category": category},
|
defaults={"category": category},
|
||||||
)
|
)
|
||||||
if not created:
|
if not created:
|
||||||
return api_error(
|
return JsonResponse({"error": "Round already configured"}, status=409)
|
||||||
code=ERROR_CODES.get("round_already_configured", "round_already_configured"),
|
|
||||||
message="Round already configured",
|
|
||||||
status=409,
|
|
||||||
)
|
|
||||||
|
|
||||||
session.status = GameSession.Status.LIE
|
session.status = GameSession.Status.LIE
|
||||||
session.save(update_fields=["status"])
|
session.save(update_fields=["status"])
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
{
|
|
||||||
"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