feat: přepínání režimů a systému vítězství v Tetrisu
CI / Generate TypeScript types (push) Canceled after 0s
CI / Server unit tests (push) Canceled after 0s
CI / Build server (push) Canceled after 0s
CI / Build client (push) Canceled after 0s
CI / Playwright E2E tests (push) Canceled after 0s
CI / Build and push Docker image (push) Canceled after 0s
CI / Notify (push) Canceled after 0s

This commit is contained in:
mates committed 2026-10-19 16:49:56 +02:00
1 parent ed64fc60e4
commit 297cb80662
5 files changed
+277 -39

No files matched your search

+66 -3
View File
@@ -1,4 +1,4 @@
import { TetrisGame, TetrisState, GarbageEvent, COUNTDOWN_SECONDS, MIN_ATTACK_GAP_MS, WATCH_FLUSH_MS, BOARD_WIDTH, BOARD_HEIGHT, PLAYER_TIMEOUT_MS } from '../tetrisGame';
import { TetrisGame, TetrisState, TetrisSettings, GarbageEvent, COUNTDOWN_SECONDS, MIN_ATTACK_GAP_MS, WATCH_FLUSH_MS, BOARD_WIDTH, BOARD_HEIGHT, PLAYER_TIMEOUT_MS, DEFAULT_SETTINGS } from '../tetrisGame';
import { GameError } from '../minigames';
import { fakeClock } from './helpers/fakeClock';
@@ -11,12 +11,14 @@ function setup() {
const garbage: GarbageEvent[] = [];
const game = new TetrisGame(s => all.push(s), s => watched.push(s), e => garbage.push(e), 1, clock);
const last = () => all[all.length - 1];
const startGame = (...logins: string[]) => {
const startWith = (settings: Partial<TetrisSettings>, ...logins: string[]) => {
logins.forEach(l => game.join(l));
game.configure(logins[0], settings);
game.start(logins[0]);
clock.advance(COUNTDOWN_SECONDS * 1000);
};
return { game, clock, all, watched, garbage, last, startGame };
const startGame = (...logins: string[]) => startWith({}, ...logins);
return { game, clock, all, watched, garbage, last, startGame, startWith };
}
describe('tetris', () => {
@@ -127,6 +129,67 @@ describe('tetris', () => {
expect(ctx.last().players.find(p => p.login === 'alice')?.place).toBe(1);
});
test('nastavení mění jen zakladatel v lobby, neplatné hodnoty se ignorují', () => {
const ctx = setup();
ctx.game.join('alice');
ctx.game.join('bob');
expect(ctx.last().settings).toEqual(DEFAULT_SETTINGS);
expect(() => ctx.game.configure('bob', { targeting: 'random' })).toThrow(GameError);
ctx.game.configure('alice', { targeting: 'payback', winMode: 'nesmysl', timeLimit: 7 });
expect(ctx.last().settings).toEqual({ ...DEFAULT_SETTINGS, targeting: 'payback' });
ctx.game.start('alice');
expect(() => ctx.game.configure('alice', { winMode: 'score' })).toThrow(GameError);
});
test('random míří náhodně bez ohledu na obdržený odpad', () => {
const ctx = setup();
ctx.startWith({ targeting: 'random' }, 'alice', 'bob', 'carol');
ctx.game.cleared('alice', 4);
ctx.clock.advance(MIN_ATTACK_GAP_MS);
ctx.game.cleared('alice', 4); // random = 0 → pořád první soupeř
expect(ctx.garbage.map(g => g.target)).toEqual(['bob', 'bob']);
});
test('payback vrací útok poslednímu útočníkovi, jinak míří jako even', () => {
const ctx = setup();
ctx.startWith({ targeting: 'payback' }, 'alice', 'bob', 'carol');
ctx.game.cleared('carol', 2); // carol nikdo nenapadl → even → alice
ctx.game.cleared('alice', 2); // alice napadla carol → carol
ctx.game.cleared('bob', 2); // bob nikdo nenapadl → even (alice 1, carol 1) → alice
ctx.clock.advance(MIN_ATTACK_GAP_MS);
ctx.game.cleared('alice', 2); // naposledy ji napadl bob → bob
ctx.game.died('bob');
ctx.game.cleared('bob', 2); // mrtvý neútočí
ctx.game.cleared('carol', 2); // naposledy ji napadla alice → alice
expect(ctx.garbage.map(g => `${g.from}>${g.target}`)).toEqual(['carol>alice', 'alice>carol', 'bob>alice', 'alice>bob', 'carol>alice']);
});
test('hra na skóre končí časovým limitem a pořadí je podle skóre', () => {
const ctx = setup();
ctx.startWith({ winMode: 'score', timeLimit: 120 }, 'alice', 'bob', 'carol');
expect(ctx.last().remainingMs).toBe(120_000);
ctx.game.update('bob', { board: BOARD, score: 900, lines: 8 });
ctx.game.died('bob');
ctx.game.died('alice'); // zbývá jediný hráč, ale hraje se dál
expect(ctx.last().phase).toBe('playing');
ctx.game.update('alice', { board: BOARD, score: 100, lines: 1 }); // mrtvému se skóre už nemění
ctx.game.update('carol', { board: BOARD, score: 500, lines: 4 });
ctx.clock.advance(120_000);
const state = ctx.last();
expect(state.phase).toBe('finished');
const place = (l: string) => state.players.find(p => p.login === l)?.place;
expect([place('bob'), place('carol'), place('alice')]).toEqual([1, 2, 3]);
});
test('hra na skóre skončí dřív, když vypadnou všichni', () => {
const ctx = setup();
ctx.startWith({ winMode: 'score' }, 'alice', 'bob');
ctx.game.died('alice');
expect(ctx.last().phase).toBe('playing');
ctx.game.died('bob');
expect(ctx.last().phase).toBe('finished');
});
test('do běžící hry se nelze připojit', () => {
const ctx = setup();
ctx.startGame('alice');