Skip to content

Hollando78/multiplayer-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ Multiplayer Game Engine

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.

โœจ Features

๐Ÿ—๏ธ Core Engine

  • 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

๐Ÿ”ฒ Grid Games Specialization

  • 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

๐Ÿš€ Production Ready

  • 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

๐Ÿš€ Quick Start

Installation

# Clone the repository
git clone <repository-url>
cd multiplayer-engine

# Install dependencies
pnpm install

# Build all packages
pnpm build

Your First Game in 5 Minutes

Create 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
}

๐Ÿ“ฆ Package Architecture

@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 โœ…

Complete Package Ecosystem

@multiplayer-engine/core โœ…

  • GameEngine: Abstract base class for all games
  • ResourceManager: Move regeneration and spending system
  • GameStateManager: Centralized state with transitions
  • Type Definitions: Comprehensive TypeScript interfaces

@multiplayer-engine/grid โœ…

  • 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

@multiplayer-engine/realtime โœ…

  • 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

@multiplayer-engine/rendering โœ…

  • 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

@multiplayer-engine/auth โœ…

  • 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

@multiplayer-engine/storage โœ…

  • 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

๐ŸŽฏ Examples

Chess Game (~100 lines)

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 dev

Territory Conquest Engine

Reference 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`);

๐Ÿ—๏ธ Architecture Principles

Game Engine Abstraction

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>;
}

Resource Management

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 Specialization

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[]
}

๐ŸŽฎ Game Examples

Implemented Games

  1. 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
  2. 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

Potential Games

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

๐Ÿ”ง Development

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

Testing

# Run all tests
pnpm test

# Test specific package
pnpm --filter @multiplayer-engine/core test

Code Quality

# Lint all code
pnpm lint

# Type check
pnpm typecheck

๐Ÿ—บ๏ธ Roadmap

Phase 1 (Current) โœ…

  • Core engine abstractions
  • Resource management system
  • Grid game specialization
  • Chess example implementation
  • TypeScript compilation and packaging

Phase 2 โœ… Real-time Infrastructure

  • 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

Phase 3 โœ… Client-side Rendering

  • 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

Phase 4 โœ… Authentication & Storage Infrastructure

  • 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

๐Ÿ“š API Reference

Core Types

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;
}

Key Methods

// 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>

๐Ÿค Contributing

We welcome contributions! Here's how to get started:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes and add tests
  4. Run quality checks: pnpm lint && pnpm typecheck && pnpm test
  5. Commit: git commit -m 'Add amazing feature'
  6. Push: git push origin feature/amazing-feature
  7. Open a Pull Request

Areas for Contribution

  • 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

๐Ÿ“„ License

MIT License - see LICENSE for details.

๐Ÿ† Credits

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors