|
1 | | -# Turn-Based Games Platform - AI Developer Guide |
| 1 | +# Turn-Based Games Platform |
2 | 2 |
|
3 | | -## Architecture Overview |
| 3 | +## Purpose |
4 | 4 |
|
5 | | -This is a **monorepo workspace** with three packages that work together to create a turn-based games platform: |
| 5 | +These instructions guide Copilot across all files in this monorepo. Language-specific and framework-specific rules are in separate instruction files under `.github/instructions/`. |
6 | 6 |
|
7 | | -- **`shared/`** - Core game logic, types, and SQLite storage (TypeScript library) |
8 | | -- **`web/`** - Next.js 15 frontend with API routes (React + TailwindCSS) |
9 | | -- **`mcp-server/`** - Model Context Protocol server providing AI opponents (Node.js service) |
| 7 | +## Repository Structure |
10 | 8 |
|
11 | | -**Key Integration Pattern**: The MCP server communicates with the web app via HTTP calls to `/api/games/*` endpoints, not direct database access. This maintains clear service boundaries. |
| 9 | +- `shared/` - Core game logic, types, and SQLite storage (TypeScript library) |
| 10 | +- `web/` - Next.js 15 frontend with API routes (React + TailwindCSS) |
| 11 | +- `mcp-server/` - Model Context Protocol server providing AI opponents (Node.js service) |
12 | 12 |
|
13 | | -## Development Workflow |
| 13 | +## Code Standards |
14 | 14 |
|
15 | | -**Essential build order** (shared must be built first): |
16 | | -```bash |
17 | | -npm run build --workspace=shared # Always run first |
18 | | -npm run dev --workspace=web # Starts Next.js dev server (port 3000) |
19 | | -npm run dev --workspace=mcp-server # Starts MCP server (stdio) |
20 | | -``` |
| 15 | +### Required Before Each Commit |
| 16 | + |
| 17 | +- Run `npm run lint` to check for linting issues |
| 18 | +- Run `npm run test` to ensure all tests pass |
| 19 | +- Build shared package first when making cross-package changes |
| 20 | + |
| 21 | +### Development Flow |
| 22 | + |
| 23 | +- Build shared: `npm run build --workspace=shared` (always run first) |
| 24 | +- Dev web: `npm run dev --workspace=web` (starts Next.js on port 3000) |
| 25 | +- Dev MCP: `npm run dev --workspace=mcp-server` (starts MCP server via stdio) |
| 26 | +- Full test: `npm run test` (runs all workspace tests) |
| 27 | +- Full lint: `npm run lint` (runs all workspace linting) |
21 | 28 |
|
22 | | -**Database location**: SQLite file is at `web/games.db` by default, controlled by `GAMES_DB_PATH` env var. |
| 29 | +### Database |
23 | 30 |
|
24 | | -## Core Architecture Patterns |
| 31 | +- SQLite file location: `web/games.db` (default) |
| 32 | +- Controlled by `GAMES_DB_PATH` environment variable |
25 | 33 |
|
26 | | -### Game Implementation Pattern |
27 | | -All games follow the `Game<TGameState, TMove>` interface in `shared/src/types/game.ts`: |
28 | | -- `validateMove()` - Check if move is legal |
29 | | -- `applyMove()` - Apply move and return new state |
30 | | -- `checkGameEnd()` - Determine win/draw/continue |
31 | | -- `getValidMoves()` - Get available moves |
32 | | -- `getInitialState()` - Create starting game state |
| 34 | +## Architecture Guidelines |
33 | 35 |
|
34 | | -### Storage Pattern |
35 | | -Games use a dual-storage approach: |
36 | | -- **Web app**: Direct SQLite access via `shared/src/storage/sqlite-storage.ts` |
37 | | -- **MCP server**: HTTP calls to web API endpoints (no direct DB access) |
| 36 | +### Service Boundaries |
38 | 37 |
|
39 | | -### AI Integration Pattern |
40 | | -The MCP server exposes tools like `play_tic_tac_toe` and `create_tic_tac_toe_game`. It calls the web API to: |
41 | | -1. Fetch current game state |
42 | | -2. Calculate AI move using algorithms in `mcp-server/src/ai/` |
43 | | -3. Submit move back via API POST |
| 38 | +- MCP server communicates with web app via HTTP calls to `/api/games/*` endpoints |
| 39 | +- MCP server never accesses the database directly |
| 40 | +- Web app uses direct SQLite access via `shared/src/storage/sqlite-storage.ts` |
44 | 41 |
|
45 | | -## File Organization Conventions |
| 42 | +### Game Interface |
46 | 43 |
|
47 | | -### API Routes Structure |
48 | | -- `web/src/app/api/games/[game-type]/route.ts` - Create/list games |
49 | | -- `web/src/app/api/games/[game-type]/[id]/move/route.ts` - Make moves |
| 44 | +All games implement `Game<TGameState, TMove>` interface in `shared/src/types/game.ts`: |
50 | 45 |
|
51 | | -### Game-Specific Types |
52 | | -Game states extend `BaseGameState` and are defined in `shared/src/types/games.ts`: |
53 | | -- `TicTacToeGameState` includes `board: Board` and `playerSymbols` |
54 | | -- `RPSGameState` includes `rounds` and `scores` |
| 46 | +```typescript |
| 47 | +// Required methods for all game implementations |
| 48 | +validateMove(state, move) // Check if move is legal |
| 49 | +applyMove(state, move) // Apply move and return new state |
| 50 | +checkGameEnd(state) // Determine win/draw/continue |
| 51 | +getValidMoves(state) // Get available moves |
| 52 | +getInitialState(config) // Create starting game state |
| 53 | +``` |
| 54 | + |
| 55 | +### API Routes Pattern |
55 | 56 |
|
56 | | -### Component Organization |
57 | | -- `web/src/components/games/` - Game-specific UI components |
58 | | -- `web/src/app/games/[game-type]/` - Game-specific pages |
| 57 | +- Create/list games: `web/src/app/api/games/[game-type]/route.ts` |
| 58 | +- Make moves: `web/src/app/api/games/[game-type]/[id]/move/route.ts` |
59 | 59 |
|
60 | | -## Development Guidelines |
| 60 | +## Adding New Games |
61 | 61 |
|
62 | | -### Adding New Games |
63 | 62 | 1. Define types in `shared/src/types/games.ts` |
64 | 63 | 2. Implement game class in `shared/src/games/` |
65 | 64 | 3. Add API routes in `web/src/app/api/games/[new-game]/` |
66 | 65 | 4. Create AI implementation in `mcp-server/src/ai/` |
67 | 66 | 5. Add MCP tools in `mcp-server/src/server.ts` |
68 | 67 | 6. Build UI components in `web/src/components/games/` |
69 | 68 |
|
70 | | -### Environment Variables |
| 69 | +## Environment Variables |
| 70 | + |
71 | 71 | - `WEB_API_BASE` - MCP server's web app URL (default: `http://localhost:3000`) |
72 | 72 | - `GAMES_DB_PATH` - SQLite database location (default: `./games.db`) |
73 | 73 |
|
74 | | -### Key Dependencies |
75 | | -- `@modelcontextprotocol/sdk` - MCP server framework |
76 | | -- `sqlite3` - Database (shared between web/mcp-server) |
77 | | -- `@turn-based-mcp/shared` - Internal workspace dependency |
| 74 | +## Security Considerations |
| 75 | + |
| 76 | +- Never hardcode credentials or API keys |
| 77 | +- Validate all user inputs in API routes |
| 78 | +- Sanitize game IDs and player names before database operations |
0 commit comments