Skip to main content

Overview

The @hyperscape/server package runs the authoritative game server:
  • Fastify 5 HTTP API with rate limiting
  • WebSocket real-time game state
  • PostgreSQL (production) / SQLite (local) database
  • LiveKit voice chat integration

Package Location

Entry Point

src/index.ts initializes:
  1. Database connection (PostgreSQL or SQLite)
  2. Asset CDN (Docker nginx, optional)
  3. Fastify HTTP server with plugins
  4. WebSocket handlers
  5. Game world and systems

API Routes

Database

The server supports both PostgreSQL (production via Neon) and SQLite (local development). Schema is defined with Drizzle ORM.

Key Tables

Database Commands

Environment Variables

Build Scripts

Model Bounds Extraction

The server includes build-time scripts for automatic collision footprint detection:
How it works:
  1. Scans world/assets/models/**/*.glb files
  2. Parses glTF position accessor min/max values
  3. Calculates bounding boxes and dimensions
  4. Computes tile footprints at scale 1.0
  5. Writes manifest for runtime use
Turbo Integration:
  • Runs automatically before build and dev commands
  • Cached based on GLB file changes
  • Only rebuilds when models are added/modified
Example Output:
Footprints are calculated from model dimensions × modelScale from stations.json. A furnace with raw dimensions 1.51×1.45 and scale 1.5 becomes 2.27×2.18 meters → 2×2 tiles.

Running

Development

Production

Server runs at http://localhost:5555 by default.

Dependencies

Docker Services

The server can use Docker for CDN and PostgreSQL:

Deployment

Cloudflare Workers

Railway

The railway.server.json in the project root configures Railway deployment.

ElizaCloud AI Integration (March 2026)

All duel arena AI agents now use @elizaos/plugin-elizacloud for unified model access (commit 4d1eb53). 13 Frontier Models: American Models:
  • openai/gpt-5 - GPT-5
  • anthropic/claude-sonnet-4.6 - Claude Sonnet 4.6
  • anthropic/claude-opus-4.6 - Claude Opus 4.6
  • google/gemini-3.1-pro-preview - Gemini 3.1 Pro
  • xai/grok-4 - Grok 4
  • meta/llama-4-maverick - Llama 4 Maverick
  • mistral/magistral-medium - Magistral Medium
Chinese Models:
  • deepseek/deepseek-v3.2 - DeepSeek V3.2
  • alibaba/qwen3-max - Qwen 3 Max
  • minimax/minimax-m2.5 - Minimax M2.5
  • zai/glm-5 - GLM-5
  • moonshotai/kimi-k2.5 - Kimi K2.5
  • bytedance/seed-1.8 - Seed 1.8
Configuration:
Benefits:
  • Simplified configuration (one API key instead of multiple provider keys)
  • Access to 13 frontier models from 13 different providers
  • Consistent model routing and error handling
  • Reduced dependency complexity
Files:
  • src/eliza/agentHelpers.ts - Added elizacloud provider
  • src/eliza/ModelAgentSpawner.ts - Updated model agent spawning
  • packages/plugin-hyperscape/src/index.ts - Added ElizaCloud plugin types

Duel Arena Oracle (March 2026)

The server publishes duel outcomes to EVM and Solana oracle contracts for verifiable results (commit aecab58). New Oracle Fields:
  • damageA - Total damage dealt by participant A
  • damageB - Total damage dealt by participant B
  • winReason - Detailed win reason (knockout, timeout, forfeit, draw)
  • seed - Cryptographic seed for replay verification
  • replayHashHex - Hash of replay data for integrity verification
  • resultHashHex - Combined hash of all duel outcome data
Configuration:
Database Schema: Oracle records are stored in the arena_rounds table with comprehensive outcome data for betting market settlement and replay verification. Files:
  • src/oracle/DuelArenaOraclePublisher.ts - Oracle publishing logic
  • src/oracle/config.ts - Oracle configuration and target management
  • src/oracle/types.ts - Oracle type definitions

Duel System

The server includes a comprehensive duel arena system with AI combat, streaming, and betting integration.

Combat Roles (PR #933, commit 82ff784)

Duel agents are assigned weighted random combat roles:
  • Melee (50%): Bronze weapons (longsword, scimitar, 2h sword)
  • Ranged (25%): Shortbow + bronze arrows (500 qty)
  • Mage (25%): Staff of air + wind strike autocast + runes
Implementation:
  • DuelOrchestrator.pickCombatRole(): Weighted random selection
  • DuelOrchestrator.ensureAgentCombatSetup(): Role-specific gear equipping
  • DuelOrchestrator.cleanupAgentCombatSetup(): Full gear removal after duel
  • DuelCombatAI: Adapts style switching based on combat role
Gear Lifecycle:
  1. Pre-duel: Role selected → gear equipped → food filled → health restored
  2. Post-duel: Gear removed → food removed → health restored → teleport back

Critical Bug Fixes (PR #933)

Combat State Key Mismatch:
  • Issue: CombatStateService syncs abbreviated keys (data.c/data.ct) but getGameState() only read full keys
  • Impact: DuelCombatAI always saw inCombat=false and flooded executeAttack every tick
  • Fix: EmbeddedHyperscapeService now reads both abbreviated and full keys
  • File: packages/server/src/eliza/EmbeddedHyperscapeService.ts
Magic Attack TOCTOU Race:
  • Issue: Cooldown checked early but claimed after async consumeRunesForSpell call
  • Impact: Duplicate magic projectiles, double rune consumption
  • Fix: Moved cooldown claim and enterCombat before async rune consumption
  • File: packages/shared/src/systems/shared/combat/CombatSystem.ts

Terrain Flat Zones (commit 7a60135)

DuelArenaVisualsSystem registers flat zones programmatically for all 8 floor areas (6 arenas + lobby + hospital):
  • Prevents players/agents from sinking ~0.4m into arena floors
  • Terrain height queries return correct floor-level values
  • Terrain mesh carved under floors to prevent grass/vegetation clipping
  • File: packages/shared/src/systems/client/DuelArenaVisualsSystem.ts

Key Files