- packages/shared/src/bots.ts: 컴퓨터 id(bot:<n>:<ulid>)·프로필(컴퓨터 n)·BOT_GAMES
(도블·할리갈리·그림 맞히기·마피아 제외)
- packages/games/src/bots: 전략 계약(자기 view + 합법 수만), decideBotAction(validate 재검사),
게임별 <게임>/bot.ts 22개 + 행동 테스트, 전 게임 판 끝까지 돌리는 공통 테스트
- 서버: Room.addBot/removeBot(방장·대기실만, 방은 친구만), RoomManager.createPractice
(POST /api/rooms {practice:true}), BotDriver(사람 같은 지연, 사람과 같은 창이면 더 천천히,
재시작 후 다시 예약), 연습 게임은 전적 제외(result_json.practice), 사람이 모두 나가면 무효
- 프로토콜 addBot/removeBot, 문서 03·06 §12·08·09 §8
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
113 lines
4.5 KiB
TypeScript
113 lines
4.5 KiB
TypeScript
/** Computer players over the real HTTP/WS stack (docs/06-lobby-rooms.md §12). */
|
|
import { afterEach, describe, expect, test } from 'bun:test';
|
|
import { Client, api, boot, guest, sleep } from './test-utils';
|
|
|
|
type Booted = ReturnType<typeof boot>;
|
|
const running: Booted[] = [];
|
|
const clients: Client[] = [];
|
|
|
|
afterEach(async () => {
|
|
for (const c of clients.splice(0)) {
|
|
try {
|
|
c.close();
|
|
} catch {}
|
|
}
|
|
for (const s of running.splice(0)) {
|
|
try {
|
|
await s.stop();
|
|
} catch {}
|
|
}
|
|
});
|
|
|
|
const fastBots = { botDelay: () => 30 };
|
|
|
|
function start(dbPath?: string): Booted {
|
|
const s = boot(dbPath, fastBots);
|
|
running.push(s);
|
|
return s;
|
|
}
|
|
|
|
async function client(s: Booted, nick: string, cookie?: { cookie: string; id: string }): Promise<Client> {
|
|
const g = cookie ?? (await guest(s.base, nick));
|
|
const c = await new Client(s.base, g.cookie, g.id).connect();
|
|
clients.push(c);
|
|
return c;
|
|
}
|
|
|
|
async function practiceOmok(s: Booted, a: Client): Promise<string> {
|
|
const r = await api(s.base, '/api/rooms', { method: 'POST', cookie: a.cookie, body: JSON.stringify({ gameId: 'omok', practice: true }) });
|
|
expect(r.res.status).toBe(200);
|
|
const code = r.body.code as string;
|
|
a.send({ t: 'join', code, as: 'player' });
|
|
await a.next('room');
|
|
a.send({ t: 'config', options: { colorAssignment: 'hostBlack', ruleSet: 'free' } });
|
|
await a.next('room', (m) => (m.room.options as any).colorAssignment === 'hostBlack');
|
|
const st = a.next('state');
|
|
a.send({ t: 'start' });
|
|
await st;
|
|
return code;
|
|
}
|
|
|
|
describe('computer players', () => {
|
|
test('practice room: host + computer seated, private, not listed; the computer answers moves', async () => {
|
|
const s = start();
|
|
const a = await client(s, '호랑이');
|
|
const code = await practiceOmok(s, a);
|
|
const room = a.lastOf('room')!.room;
|
|
expect(room.visibility).toBe('private');
|
|
expect(room.seats.map((x) => x?.user.kind)).toEqual(['guest', 'bot']);
|
|
expect(room.seats[1]!.user.nickname).toBe('컴퓨터 1');
|
|
const pub = await api(s.base, '/api/games/omok/rooms');
|
|
expect(pub.body.rooms.find((r: { code: string }) => r.code === code)).toBeUndefined();
|
|
for (let i = 0; i < 3; i++) {
|
|
const answered = a.next('state', (m) => (m.view as any).moveCount === 2 * i + 2);
|
|
const board = (a.lastOf('state')!.view as any).board as number[];
|
|
const free = board.indexOf(0);
|
|
a.act({ type: 'place', x: free % 15, y: Math.floor(free / 15) });
|
|
await answered;
|
|
}
|
|
});
|
|
|
|
test('addBot / removeBot over the socket: host only, errors for others', async () => {
|
|
const s = start();
|
|
const a = await client(s, '호랑이');
|
|
const b = await client(s, '고양이');
|
|
const r = await api(s.base, '/api/rooms', { method: 'POST', cookie: a.cookie, body: JSON.stringify({ gameId: 'one-card', visibility: 'public' }) });
|
|
const code = r.body.code as string;
|
|
a.send({ t: 'join', code, as: 'player' });
|
|
await a.next('room');
|
|
b.send({ t: 'join', code, as: 'player' });
|
|
await b.next('room');
|
|
b.send({ t: 'addBot' });
|
|
expect((await b.next('error')).message).toBe('방장만 컴퓨터를 넣을 수 있어요.');
|
|
a.send({ t: 'addBot', seat: 3 });
|
|
const withBot = await a.next('room', (m) => m.room.seats.some((x) => x?.user.kind === 'bot'));
|
|
expect(withBot.room.visibility).toBe('private');
|
|
const botId = withBot.room.seats[3]!.user.id;
|
|
a.send({ t: 'removeBot', userId: botId });
|
|
await a.next('room', (m) => !m.room.seats.some((x) => x?.user.kind === 'bot'));
|
|
});
|
|
|
|
test('restart with the computer to move: after restore it plays on its own', async () => {
|
|
const s1 = boot(undefined, fastBots);
|
|
const a = await client(s1, '호랑이');
|
|
const code = await practiceOmok(s1, a);
|
|
// Stop the bot before it answers, then crash.
|
|
for (const r of s1.rooms.all()) r.clearTimer();
|
|
a.act({ type: 'place', x: 7, y: 7 });
|
|
await a.next('state', (m) => (m.view as any).moveCount === 1);
|
|
for (const r of s1.rooms.all()) r.clearTimer();
|
|
await Promise.race([s1.server.stop(true), sleep(300)]);
|
|
for (const r of s1.rooms.all()) r.clearTimer();
|
|
s1.db.close();
|
|
const s2 = start(s1.dbPath);
|
|
await sleep(200); // the restored driver plays without anyone connected
|
|
const room = s2.rooms.get(code)!;
|
|
expect((room.game!.state as any).moves.length).toBe(2);
|
|
const a2 = await client(s2, '', { cookie: a.cookie, id: a.id });
|
|
a2.send({ t: 'join', code, as: 'player' });
|
|
const st = await a2.next('state');
|
|
expect((st.view as any).moveCount).toBe(2);
|
|
});
|
|
});
|