feat(bot): POST /api/games instantiates CasualBrain + BotDriver
This commit is contained in:
@@ -8,9 +8,11 @@ import {
|
|||||||
chooseSide,
|
chooseSide,
|
||||||
createGame,
|
createGame,
|
||||||
pruneFinished,
|
pruneFinished,
|
||||||
|
attachBotDriver,
|
||||||
} from './games.js';
|
} from './games.js';
|
||||||
import { attachSocket } from './ws.js';
|
import { attachSocket } from './ws.js';
|
||||||
import { createGameSchema } from './validation.js';
|
import { createGameSchema } from './validation.js';
|
||||||
|
import { CasualBrain, BotDriver } from './bot/index.js';
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
@@ -45,13 +47,28 @@ fastify.post('/api/games', async (req, reply) => {
|
|||||||
reply.code(400);
|
reply.code(400);
|
||||||
return { error: 'malformed', detail: parsed.error.issues };
|
return { error: 'malformed', detail: parsed.error.issues };
|
||||||
}
|
}
|
||||||
const { mode, side, highlightingEnabled } = parsed.data;
|
const { mode, side, highlightingEnabled, vsAi } = parsed.data;
|
||||||
|
|
||||||
|
// Phase 1: only 'casual' is implemented. 'recon' returns 503.
|
||||||
|
if (vsAi && vsAi.brain === 'recon') {
|
||||||
|
reply.code(503);
|
||||||
|
return { error: 'ai_offline', detail: 'recon bot not yet implemented' };
|
||||||
|
}
|
||||||
|
|
||||||
const creatorSide = chooseSide(side);
|
const creatorSide = chooseSide(side);
|
||||||
const { game, creatorToken } = createGame({ mode, creatorSide, highlightingEnabled });
|
const { game, creatorToken } = createGame({ mode, creatorSide, highlightingEnabled, vsAi });
|
||||||
|
|
||||||
|
// For AI games, wire the bot.
|
||||||
|
if (vsAi && game.aiOpponent) {
|
||||||
|
const brain = new CasualBrain({});
|
||||||
|
const driver = new BotDriver({ game, brain, color: game.aiOpponent.color });
|
||||||
|
await driver.init();
|
||||||
|
attachBotDriver(game.id, driver);
|
||||||
|
}
|
||||||
|
|
||||||
const publicBase = PUBLIC_BASE
|
const publicBase = PUBLIC_BASE
|
||||||
|| (req.headers.host ? `${req.protocol}://${req.headers.host}` : '');
|
|| (req.headers.host ? `${req.protocol}://${req.headers.host}` : '');
|
||||||
const joinUrl = `${publicBase}/g/${game.id}`;
|
const joinUrl = vsAi ? null : `${publicBase}/g/${game.id}`;
|
||||||
return { gameId: game.id, creatorToken, creatorColor: creatorSide, joinUrl };
|
return { gameId: game.id, creatorToken, creatorColor: creatorSide, joinUrl };
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user