A reusable TypeScript game engine extracted from the Territory Conquest game, designed for building sophisticated multiplayer games with real-time synchronization, resource management, and grid-based mechanics.
- Universal Game Engine: Abstract base classes for any game type
- Resource Management: Sophisticated move regeneration and spending system
- State Management: Centralized game state with validation and transitions
- Type-Safe APIs: Full TypeScript support with comprehensive type definitions
- Spatial Algorithms: Advanced flood-fill and pathfinding
- Territory Mechanics: Enclosed area detection and capture
- Chunk Management: Efficient rendering for large worlds
- Mobile Optimization: Touch controls and performance scaling
- Modular Architecture: 6 specialized packages for different concerns
- Proven Infrastructure: Extracted from production Territory Conquest game
- Performance Optimized: Sub-100ms real-time multiplayer synchronization
- Developer Experience: Comprehensive examples and documentation
# Clone the repository
git clone <repository-url>
cd multiplayer-engine
# Install dependencies
pnpm install
# Build all packages
pnpm buildCreate a simple Tic-Tac-Toe game:
import { GridGameEngine, GridMoveResult } from '@multiplayer-engine/grid';
import { InMemoryResourceManager } from '@multiplayer-engine/core';
class TicTacToeEngine extends GridGameEngine {
constructor(gameId: string) {
super(gameId, {
gridOptions: { width: 3, height: 3, finite: true },
resources: { max: 1, regenSeconds: 0, starting: 1 }
});
}
protected createResourceManager() {
return new InMemoryResourceManager(this.rules.resources);
}
async canPlaceAt(x: number, y: number): Promise<boolean> {
const cells = await this.getCellsInRegion(0, 2, 0, 2);
return !cells.has(`${x},${y}`); // Cell must be empty
}
async applyGridMove(x: number, y: number, playerId: string): Promise<GridMoveResult> {
const changedCells = [{ x, y, owner: playerId }];
await this.saveCells(changedCells);
return {
success: true,
changedCells,
affectedChunks: this.getAffectedChunks(changedCells)
};
}
// ... implement remaining abstract methods
}@multiplayer-engine/
โโโ core/ # Universal game engine interfaces & resource management โ
โโโ grid/ # Grid-based game specialization with spatial algorithms โ
โโโ realtime/ # WebSocket real-time multiplayer with Redis pub/sub โ
โโโ rendering/ # Canvas renderer with animations & touch controls โ
โโโ auth/ # JWT authentication with dual-token security โ
โโโ storage/ # Universal database abstractions with Prisma โ
- GameEngine: Abstract base class for all games
- ResourceManager: Move regeneration and spending system
- GameStateManager: Centralized state with transitions
- Type Definitions: Comprehensive TypeScript interfaces
- GridGameEngine: Specialized engine for grid-based games
- Spatial Algorithms: Flood-fill, pathfinding, neighbor detection
- Territory Mechanics: Enclosed area detection and capture
- ChunkManager: Efficient operations on large grids
- SocketManager: WebSocket connection management with rooms
- PubSubManager: Redis integration for scalable real-time updates
- StateSync: Optimistic updates with server-side conflict resolution
- ChunkUpdater: Efficient regional updates for large game worlds
- CanvasRenderer: Hardware-accelerated 2D rendering with device pixel ratio support
- ViewportManager: Touch/mouse controls with pinch-to-zoom and panning
- AnimationEngine: Smooth animations with accessibility support (prefers-reduced-motion)
- ThemeEngine: Universal theming with dark/light/high-contrast modes
- InputManager: Unified input handling for touch/mouse/keyboard
- AuthManager: JWT-based authentication with password validation
- AuthMiddleware: Express.js middleware with built-in rate limiting
- Dual-token Security: Short-lived access tokens + HTTP-only refresh cookies
- User Management: Signup, login, password changes, session management
- GameStorage: Universal game data management interfaces
- AuthStorage: User and authentication data abstractions
- PrismaImplementations: Production-ready Prisma database integration
- Optimistic Locking: State versioning for concurrent updates
See complete implementation in examples/chess/:
import { ChessEngine } from './examples/chess/src/ChessEngine';
const game = new ChessEngine('game-1');
// Make moves
await game.applyChessMove(4, 1, 4, 3, 'player1'); // e2-e4
await game.applyChessMove(4, 6, 4, 4, 'player2'); // e7-e5
// Check game state
const state = await game.getGameState();
console.log(`Current player: ${state.currentPlayer}`);Run the chess demo:
cd examples/chess
pnpm devReference implementation showing sophisticated mechanics:
import { TerritoryEngine } from '@multiplayer-engine/grid';
const game = new TerritoryEngine('territory-1');
// Place territory cell
const result = await game.applyGridMove(10, 10, 'red');
console.log(`Changed ${result.changedCells?.length} cells`);Every game extends the base GameEngine class:
abstract class GameEngine<TGameState, TMoveData, TGameRules> {
// Universal move pipeline
async makeMove(playerId: string, moveData: TMoveData): Promise<MoveResult> {
const hasResources = await this.resourceManager.canSpend(playerId);
const isValid = await this.validateMove(playerId, moveData);
const result = await this.applyMove(playerId, moveData);
await this.resourceManager.spendResource(playerId);
return result;
}
// Game-specific implementations
abstract validateMove(playerId: string, moveData: TMoveData): Promise<boolean>;
abstract applyMove(playerId: string, moveData: TMoveData): Promise<MoveResult>;
}Sophisticated resource system supporting:
- Move Regeneration: Configurable regeneration rates
- Resource Limits: Maximum capacity per player
- Spending Validation: Ensure players have sufficient resources
- Persistence: Abstract storage layer for different backends
const resourceManager = new InMemoryResourceManager({
max: 25, // Maximum resources
regenSeconds: 60, // Regeneration interval
starting: 5 // Initial resources
});
const canMove = await resourceManager.canSpend('player1');
if (canMove) {
await resourceManager.spendResource('player1');
}Grid-based games get additional spatial capabilities:
class GridGameEngine extends GameEngine {
// Spatial utilities
get4Neighbors(x: number, y: number): GridCoordinate[]
get8Neighbors(x: number, y: number): GridCoordinate[]
// Advanced algorithms
floodFill(startX: number, startY: number, predicate: Function): FloodFillRegion
findEnclosedAreas(cellMap: Map<string, string>, playerColour: string): GridCell[]
// Performance optimization
getAffectedChunks(changedCells: GridCell[]): string[]
}-
Chess (
examples/chess/)- Turn-based strategy using 8x8 finite grid
- Complete piece movement validation
- Demonstrates resource management for turn control
- Lines of game-specific code: ~200
-
Territory Conquest (
packages/grid/src/TerritoryEngine.ts)- Real-time multiplayer territory expansion
- Advanced flood-fill algorithms for area capture
- Diagonal tunneling mechanics
- Lines of game-specific code: ~150
The engine can power many game types:
- Conway's Game of Life: Infinite grid cellular automaton
- Go: Territory control with capture mechanics
- Checkers: Diagonal movement and jumping
- Risk: Territory control with armies
- Settlers of Catan: Resource management and building
# Build all packages
pnpm build
# Build specific package
pnpm --filter @multiplayer-engine/core build
# Watch mode for development
pnpm --filter @multiplayer-engine/grid dev# Run all tests
pnpm test
# Test specific package
pnpm --filter @multiplayer-engine/core test# Lint all code
pnpm lint
# Type check
pnpm typecheck- Core engine abstractions
- Resource management system
- Grid game specialization
- Chess example implementation
- TypeScript compilation and packaging
- Real-time Package: WebSocket multiplayer with chunk subscriptions
- PubSub Manager: Redis integration for scalable real-time updates
- State Synchronization: Optimistic updates with conflict resolution
- Conway's Game of Life: Real-time multiplayer cellular automaton example
- Rendering Package: Canvas renderer with hardware acceleration
- Viewport Manager: Touch/mouse controls with pinch-to-zoom
- Animation Engine: Sophisticated animations with accessibility support
- Theme System: Universal theming with dark/light/high-contrast modes
- Input Manager: Universal input handling for touch/mouse/keyboard
- Authentication Package: JWT + dual-token security system
- Storage Package: Universal database abstractions
- Prisma Implementations: Production-ready database integration
- Rate Limiting: Built-in protection against abuse
interface MoveResult {
success: boolean;
error?: string;
changedCells?: CellChange[];
affectedChunks?: string[];
}
interface ResourceConfig {
max: number; // Maximum resources
regenSeconds: number; // Regeneration interval
starting: number; // Initial amount
}
interface GridCoordinate {
x: number;
y: number;
}// GameEngine
abstract validateMove(playerId: string, moveData: TMoveData): Promise<boolean>
abstract applyMove(playerId: string, moveData: TMoveData): Promise<MoveResult>
async makeMove(playerId: string, moveData: TMoveData): Promise<MoveResult>
// GridGameEngine
async canPlaceAt(x: number, y: number, playerId: string): Promise<boolean>
get4Neighbors(x: number, y: number): GridCoordinate[]
floodFill(startX: number, startY: number, predicate: Function): FloodFillRegion
// ResourceManager
async canSpend(playerId: string): Promise<boolean>
async spendResource(playerId: string): Promise<ResourceSpendResult>We welcome contributions! Here's how to get started:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes and add tests
- Run quality checks:
pnpm lint && pnpm typecheck && pnpm test - Commit:
git commit -m 'Add amazing feature' - Push:
git push origin feature/amazing-feature - Open a Pull Request
- New Game Examples: Implement games using the engine
- Performance Optimizations: Improve spatial algorithms
- Real-time Multiplayer: WebSocket synchronization patterns
- Mobile Optimization: Touch controls and responsive design
- Documentation: Tutorials and API improvements
MIT License - see LICENSE for details.
Extracted from the production Territory Conquest game, which demonstrates:
- Real-time multiplayer: 100+ concurrent players
- Mobile optimization: Touch controls and responsive design
- Advanced algorithms: Flood-fill territory capture
- Production infrastructure: Docker, SSL, monitoring, backups
The engine distills these battle-tested patterns into reusable abstractions for building sophisticated multiplayer games.
**๐ฏ Build your next multiplayer game in hours, not months.**SSH authentication configured successfully