How lokesh-1511/chain-reaction works
Purpose and entry points
Chain Reaction is a React/Vite progressive web app for local, bot, and real-time multiplayer chain-reaction gameplay. The browser starts at src/main.jsx, renders the application shell in src/App.jsx, and is built through vite.config.js. The separate Node/Express multiplayer server starts from backend/server.js (documented on port 5000).
Client application and UI
src/App.jsx owns top-level mode/navigation state and composes the menu, board, profile, and difficulty views. src/components/Menu.jsx drives game-mode and room-selection interactions; src/components/GameBoard.jsx renders turns and board state; individual positions are represented by src/components/GridCell.jsx. This is the primary UI boundary: components receive game state/actions rather than implementing network authority.
Gameplay engines
The core rule is placing an orb on an allowed cell, then resolving critical-mass explosions, ownership capture, chained propagation, elimination, and winner detection. There are distinct implementations for execution contexts:
• Browser/local play: src/services/localGameLogic.js
• Bot decision-making: src/ai/chainReactionBot.js
• Server-authoritative multiplayer: backend/gameLogic.js
• Optional Firebase Functions path: functions/gameLogic.js
Keeping these rule implementations aligned is a material consistency risk and an important planner boundary.
Multiplayer transport and authority
The frontend socket adapter in src/services/socket.js connects UI actions and server events. backend/server.js combines Express and Socket.IO, managing room creation/joining, player membership, turn submissions, and broadcasts of authoritative game updates. The intended flow is: UI input → socket event → server game logic/room state → broadcast → client board render. HTTP-oriented calls are separated in src/services/api.js.
Identity, profiles, and persistent statistics
Firebase is initialized client-side in src/config/firebase.js. Firebase Authentication provides identity; Firestore stores profiles, aggregate stats, achievements, leaderboards, and history as described by FIRESTORE_SCHEMA.md. Client persistence services are divided between profile behavior in src/services/userProfile.js and statistics updates/queries in src/services/userStats.js. src/components/UserProfile.jsx is the principal profile presentation boundary. Access control and query support are deployed through firestore.rules and firestore.indexes.json.
Offline/PWA behavior
The application has a PWA manifest and service worker at manifest.json and public/sw.js. Connectivity is detected through src/hooks/useOfflineDetection.js and src/services/offlineDetector.js, allowing the UI to distinguish unavailable multiplayer/backend paths from local gameplay. Firebase Hosting configuration resides in firebase.json.
Deployment runtimes and alternatives
Primary runtimes are browser React/Vite, a Node.js Express/Socket.IO server, and Firebase Authentication/Firestore. Firebase Cloud Functions are present as an optional backend surface via functions/index.js. The backend directory also contains legacy or recovery server variants—backend/server_fixed.js and backend/server_old.js—but backend/server.js is the documented runtime entry point. GitHub Actions deploy Firebase Hosting on merge and pull-request workflows under .github/workflows/.
Main entry points: App.jsx Application shell · socket.js Socket adapter · gameLogic.js Authoritative rules · FIRESTORE_SCHEMA.md Authentication and Firestore
Components 21
Browser PWA
- Vite build
vite.config.js - React entry
src/main.jsx - Application shell · Owns top-level mode and navigation state.
src/App.jsx - Game menu
src/components/Menu.jsx - Game board
src/components/GameBoard.jsx - Grid cell
src/components/GridCell.jsx - Local game rules
src/services/localGameLogic.js - Bot decisions
src/ai/chainReactionBot.js - Socket adapter
src/services/socket.js - HTTP API client
src/services/api.js - Profile and stats services
src/services/userProfile.js - User profile view
src/components/UserProfile.jsx - Offline detection
src/services/offlineDetector.js - Service worker
public/sw.js
Multiplayer Server
- Express / Socket.IO server · Manages rooms, membership, turns, and broadcasts.
backend/server.js - Authoritative rules
backend/gameLogic.js
Firebase Platform
- Firebase client
src/config/firebase.js - Authentication and Firestore · Stores identity, profiles, stats, achievements, leaderboards, and history.
FIRESTORE_SCHEMA.md - Firestore access rules
firestore.rules - Optional Cloud Functions · Alternative server-side gameplay surface.
functions/index.js - Function game rules · Must remain consistent with local and server rules.
functions/gameLogic.js
Connections 25
- Vite build → React entry: builds
- React entry → Application shell: renders
- Application shell → Game menu: composes
- Application shell → Game board: composes
- Game board → Grid cell: renders
- Application shell → Local game rules: local moves
- Application shell → Bot decisions: bot turns
- Application shell → Socket adapter: multiplayer actions
- Socket adapter → Express / Socket.IO server: Socket.IO events
- Express / Socket.IO server → Authoritative rules: validates and resolves turns
- Authoritative rules → Socket adapter: authoritative updates
- Socket adapter → Game board: board state
- Application shell → HTTP API client: HTTP calls
- Application shell → Offline detection: connectivity state
- Service worker → Application shell: offline support
- Application shell → Firebase client: initializes services
- Firebase client → Authentication and Firestore: auth and data access
- Profile and stats services → Authentication and Firestore: profiles and statistics
- Application shell → Profile and stats services: persistence actions
- Profile and stats services → User profile view: profile data
- Firestore access rules → Authentication and Firestore: secures
- Optional Cloud Functions → Function game rules: uses
- Function game rules → Authentication and Firestore: optional persistence
- Local game rules → Authoritative rules: rule parity risk
- Authoritative rules → Function game rules: rule parity risk