Technology Stack
Elemental Battlecards is a full-stack multiplayer card game built with modern web technologies.Backend Stack
Node.js + Express
RESTful API server handling authentication and HTTP endpoints
Socket.IO
Real-time bidirectional communication for multiplayer gameplay
Sequelize ORM
Database abstraction layer supporting PostgreSQL and SQLite
JWT Authentication
Secure token-based authentication with bcrypt password hashing
Frontend Stack
Phaser 3
WebGL/Canvas game framework for rendering and game logic
Vite
Lightning-fast build tool with HMR for development
Socket.IO Client
Real-time client for multiplayer synchronization
ES Modules
Modern JavaScript with native module imports
System Architecture
Backend Architecture
The backend follows a modular MVC-style structure optimized for real-time multiplayer gaming.Directory Structure
Core Server Setup
The main server (server.js) initializes the application with database connection and Socket.IO:
Backend/server.js
Database Layer
User Model (models/user.js):
API Endpoints
Real-Time Socket Manager
ThesocketManager.js handles all real-time multiplayer logic:
- Room Management: Creates 6-digit room codes for LAN games
- Player Matching: Manages host/guest connections
- Game State: Synchronizes turn changes and game events
- Connection Handling: Cleanup on disconnect
Frontend Architecture
Directory Structure
Phaser Configuration
The game is initialized inmain.js with specific scene order:
Frontend/src/main.js
Game Objects
Card Class
Represents individual cards with type, level, and state management
Player Class
Manages player state including deck, hand, field, and essences
Card Definitions
Static configuration for all card types and levels
Combat Resolver
Calculates battle outcomes based on type advantages and levels
Game State Management
Turn System
The game uses a turn-based state machine:- Pre-start: Waiting for both players to be ready
- Player Turn: Local player can perform one action
- Opponent Turn: Waiting for opponent action
- Game Over: Victory or defeat condition met
Action Types
- Place Card: Put a card from hand onto field (face-down)
- Attack: Select attacker and target, resolve combat
- Fuse: Combine two identical cards to level up
Synchronization
All game actions are synchronized via Socket.IO events:Environment Configuration
Backend Environment Variables
.env
Frontend Environment
The frontend determines the backend URL dynamically:- Query parameter:
?backend=http://192.168.1.100:3001 - Global variable:
window.BACKEND_URL - Default:
http://{location.hostname}:3001
This flexible approach allows LAN gaming where players connect to a local server via IP address.
Deployment
Development Mode
- Backend
- Frontend
Production Build
Network Configuration
For LAN play, the backend displays network information on startup:Performance Considerations
Asset Loading
- Video backgrounds use
muted: truefor autoplay compatibility - Mipmaps generated during load to prevent WebGL warnings
- Card textures normalized to fixed sizes (110x158px)
Memory Management
- Scene cleanup removes event listeners on shutdown
- Socket connections preserved when transitioning between game scenes
- DOM elements properly destroyed to prevent leaks
Network Optimization
- Binary WebSocket protocol via Socket.IO
- Events only sent on state changes, not every frame
- Room-based message routing reduces bandwidth
Security
Password Hashing
bcryptjs with salt rounds for secure password storage
JWT Tokens
Stateless authentication with configurable expiration
CORS Configuration
Controlled cross-origin resource sharing
Input Validation
Server-side validation of all game events
Next Steps
Game Scenes
Learn about all Phaser scenes and their flow
Socket Events
Complete reference of real-time multiplayer events
Contributing
Guidelines for contributing to the project