fix: znemožnění spuštění Tetrisu v jednom hráči
CI / Generate TypeScript types (push) Successful in 10s
CI / Server unit tests (push) Successful in 26s
CI / Build server (push) Successful in 29s
CI / Build client (push) Successful in 43s
CI / Playwright E2E tests (push) Successful in 2m2s
CI / Build and push Docker image (push) Successful in 52s
CI / Notify (push) Successful in 4s

This commit is contained in:
mates committed 2026-10-20 09:47:00 +02:00
1 parent bc2cc75d7d
commit 304c7656e9
3 files changed
+19 -6

No files matched your search

+12 -2
View File
@@ -4,12 +4,12 @@ import { fakeClock } from './helpers/fakeClock';
const BOARD = '0'.repeat(BOARD_WIDTH * (BOARD_HEIGHT - 1)) + '1'.repeat(BOARD_WIDTH);
function setup() {
function setup(minPlayers = 1) {
const clock = fakeClock();
const all: TetrisState[] = [];
const watched: TetrisState[] = [];
const garbage: GarbageEvent[] = [];
const game = new TetrisGame(s => all.push(s), s => watched.push(s), e => garbage.push(e), 1, clock);
const game = new TetrisGame(s => all.push(s), s => watched.push(s), e => garbage.push(e), minPlayers, clock);
const last = () => all[all.length - 1];
const startWith = (settings: Partial<TetrisSettings>, ...logins: string[]) => {
logins.forEach(l => game.join(l));
@@ -22,6 +22,16 @@ function setup() {
}
describe('tetris', () => {
test('společnou hru nejde spustit jen s jedním hráčem', () => {
const ctx = setup(2);
ctx.game.join('alice');
expect(() => ctx.game.start('alice')).toThrow(GameError);
expect(ctx.last()).toMatchObject({ phase: 'lobby' });
ctx.game.join('bob');
ctx.game.start('alice');
expect(ctx.last()).toMatchObject({ phase: 'countdown' });
});
test('po odpočtu začne hra se společným seedem', () => {
const ctx = setup();
ctx.startGame('alice', 'bob');
+4 -3
View File
@@ -1,5 +1,5 @@
import { Server } from "socket.io";
import { Clock, defaultClock, GameError, gameHandler } from "./minigames";
import { Clock, defaultClock, GameError, gameHandler, minPlayersForEnv } from "./minigames";
/**
* Multiplayerový Tetris (battle) přes websocket.
@@ -124,7 +124,7 @@ export class TetrisGame {
private readonly broadcastAll: (state: TetrisState) => void,
private readonly broadcastWatchers: (state: TetrisState) => void,
private readonly sendGarbage: (event: GarbageEvent) => void,
private readonly minPlayers = 1,
private readonly minPlayers = 2,
private readonly clock: Clock = defaultClock,
) { }
@@ -366,7 +366,8 @@ export function registerTetrisGame(io: Server) {
state => io.emit('tetris:state', state),
state => io.to(WATCH_ROOM).emit('tetris:state', state),
event => io.to(WATCH_ROOM).emit('tetris:garbage', event),
1, // Tetris se dá hrát i sólo
// Sólo hra je jen Trénink v prohlížeči, společná hra by blokovala ostatní
minPlayersForEnv(2),
);
io.on('connection', socket => {