# lokesh-1511/chain-reaction architecture

> Architecture diagram of the GitHub repository lokesh-1511/chain-reaction, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lokesh-1511/chain-reaction
- Repository: https://github.com/lokesh-1511/chain-reaction

## Overview

#### 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: [`src/App.jsx`](https://github.com/lokesh-1511/chain-reaction/blob/main/src/App.jsx) (Application shell), [`src/services/socket.js`](https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/socket.js) (Socket adapter), [`backend/gameLogic.js`](https://github.com/lokesh-1511/chain-reaction/blob/main/backend/gameLogic.js) (Authoritative rules), [`FIRESTORE_SCHEMA.md`](https://github.com/lokesh-1511/chain-reaction/blob/main/FIRESTORE_SCHEMA.md) (Authentication and Firestore)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser PWA"]
  node_vite{{"Vite build<br/>build tool<br/>[vite.config.js]"}}
  node_main["React entry<br/>browser entry<br/>[main.jsx]"]
  node_app["Application shell<br/>React state boundary<br/>[App.jsx]"]
  node_menu["Game menu<br/>React component<br/>[Menu.jsx]"]
  node_board["Game board<br/>React component<br/>[GameBoard.jsx]"]
  node_cell["Grid cell<br/>React component<br/>[GridCell.jsx]"]
  node_local_rules["Local game rules<br/>game engine<br/>[localGameLogic.js]"]
  node_bot["Bot decisions<br/>AI engine"]
  node_socket["Socket adapter<br/>realtime client<br/>[socket.js]"]
  node_api["HTTP API client<br/>client service<br/>[api.js]"]
  node_profiles["Profile and stats services<br/>persistence client<br/>[userProfile.js]"]
  node_user_profile["User profile view<br/>React component<br/>[UserProfile.jsx]"]
  node_offline["Offline detection<br/>connectivity service<br/>[offlineDetector.js]"]
  node_service_worker["Service worker<br/>PWA runtime<br/>[sw.js]"]
end

subgraph group_multiplayer["Multiplayer Server"]
  node_server{{"Express / Socket.IO server<br/>Node entry point<br/>[server.js]"}}
  node_server_rules["Authoritative rules<br/>game engine<br/>[gameLogic.js]"]
end

subgraph group_firebase["Firebase Platform"]
  node_firebase_client["Firebase client<br/>SDK initialization<br/>[firebase.js]"]
  node_firebase_platform[("Authentication and Firestore<br/>managed identity and database")]
  node_firestore_rules["Firestore access rules<br/>database policy<br/>[firestore.rules]"]
  node_functions["Optional Cloud Functions<br/>Firebase backend entry<br/>[index.js]"]
  node_function_rules["Function game rules<br/>game engine<br/>[gameLogic.js]"]
end

node_vite -->|"builds"| node_main
node_main -->|"renders"| node_app
node_app -->|"composes"| node_menu
node_app -->|"composes"| node_board
node_board -->|"renders"| node_cell
node_app -->|"local moves"| node_local_rules
node_app -->|"bot turns"| node_bot
node_app -->|"multiplayer actions"| node_socket
node_socket -->|"Socket.IO events"| node_server
node_server -->|"validates and resolves turns"| node_server_rules
node_server_rules -->|"authoritative updates"| node_socket
node_socket -->|"board state"| node_board
node_app -->|"HTTP calls"| node_api
node_app -->|"connectivity state"| node_offline
node_service_worker -.->|"offline support"| node_app
node_app -->|"initializes services"| node_firebase_client
node_firebase_client -->|"auth and data access"| node_firebase_platform
node_profiles -->|"profiles and statistics"| node_firebase_platform
node_app -->|"persistence actions"| node_profiles
node_profiles -->|"profile data"| node_user_profile
node_firestore_rules -->|"secures"| node_firebase_platform
node_functions -->|"uses"| node_function_rules
node_function_rules -.->|"optional persistence"| node_firebase_platform
node_local_rules -.->|"rule parity risk"| node_server_rules
node_server_rules -.->|"rule parity risk"| node_function_rules

click node_vite "https://github.com/lokesh-1511/chain-reaction/blob/main/vite.config.js"
click node_main "https://github.com/lokesh-1511/chain-reaction/blob/main/src/main.jsx"
click node_app "https://github.com/lokesh-1511/chain-reaction/blob/main/src/App.jsx"
click node_menu "https://github.com/lokesh-1511/chain-reaction/blob/main/src/components/Menu.jsx"
click node_board "https://github.com/lokesh-1511/chain-reaction/blob/main/src/components/GameBoard.jsx"
click node_cell "https://github.com/lokesh-1511/chain-reaction/blob/main/src/components/GridCell.jsx"
click node_local_rules "https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/localGameLogic.js"
click node_bot "https://github.com/lokesh-1511/chain-reaction/blob/main/src/ai/chainReactionBot.js"
click node_socket "https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/socket.js"
click node_api "https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/api.js"
click node_profiles "https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/userProfile.js"
click node_user_profile "https://github.com/lokesh-1511/chain-reaction/blob/main/src/components/UserProfile.jsx"
click node_offline "https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/offlineDetector.js"
click node_service_worker "https://github.com/lokesh-1511/chain-reaction/blob/main/public/sw.js"
click node_server "https://github.com/lokesh-1511/chain-reaction/blob/main/backend/server.js"
click node_server_rules "https://github.com/lokesh-1511/chain-reaction/blob/main/backend/gameLogic.js"
click node_firebase_client "https://github.com/lokesh-1511/chain-reaction/blob/main/src/config/firebase.js"
click node_firebase_platform "https://github.com/lokesh-1511/chain-reaction/blob/main/FIRESTORE_SCHEMA.md"
click node_firestore_rules "https://github.com/lokesh-1511/chain-reaction/blob/main/firestore.rules"
click node_functions "https://github.com/lokesh-1511/chain-reaction/blob/main/functions/index.js"
click node_function_rules "https://github.com/lokesh-1511/chain-reaction/blob/main/functions/gameLogic.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_vite,node_main,node_app,node_menu,node_board,node_cell,node_local_rules,node_bot,node_socket,node_api,node_profiles,node_user_profile,node_offline,node_service_worker toneBlue
class node_server,node_server_rules toneAmber
class node_firebase_client,node_firebase_platform,node_firestore_rules,node_functions,node_function_rules toneMint
```

## Components

### Browser PWA

- [Vite build](https://github.com/lokesh-1511/chain-reaction/blob/main/vite.config.js): build tool · `vite.config.js`
- [React entry](https://github.com/lokesh-1511/chain-reaction/blob/main/src/main.jsx): browser entry · `src/main.jsx`
- [Application shell](https://github.com/lokesh-1511/chain-reaction/blob/main/src/App.jsx): React state boundary · Owns top-level mode and navigation state. · `src/App.jsx`
- [Game menu](https://github.com/lokesh-1511/chain-reaction/blob/main/src/components/Menu.jsx): React component · `src/components/Menu.jsx`
- [Game board](https://github.com/lokesh-1511/chain-reaction/blob/main/src/components/GameBoard.jsx): React component · `src/components/GameBoard.jsx`
- [Grid cell](https://github.com/lokesh-1511/chain-reaction/blob/main/src/components/GridCell.jsx): React component · `src/components/GridCell.jsx`
- [Local game rules](https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/localGameLogic.js): game engine · `src/services/localGameLogic.js`
- [Bot decisions](https://github.com/lokesh-1511/chain-reaction/blob/main/src/ai/chainReactionBot.js): AI engine · `src/ai/chainReactionBot.js`
- [Socket adapter](https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/socket.js): realtime client · `src/services/socket.js`
- [HTTP API client](https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/api.js): client service · `src/services/api.js`
- [Profile and stats services](https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/userProfile.js): persistence client · `src/services/userProfile.js`
- [User profile view](https://github.com/lokesh-1511/chain-reaction/blob/main/src/components/UserProfile.jsx): React component · `src/components/UserProfile.jsx`
- [Offline detection](https://github.com/lokesh-1511/chain-reaction/blob/main/src/services/offlineDetector.js): connectivity service · `src/services/offlineDetector.js`
- [Service worker](https://github.com/lokesh-1511/chain-reaction/blob/main/public/sw.js): PWA runtime · `public/sw.js`

### Multiplayer Server

- [Express / Socket.IO server](https://github.com/lokesh-1511/chain-reaction/blob/main/backend/server.js): Node entry point · Manages rooms, membership, turns, and broadcasts. · `backend/server.js`
- [Authoritative rules](https://github.com/lokesh-1511/chain-reaction/blob/main/backend/gameLogic.js): game engine · `backend/gameLogic.js`

### Firebase Platform

- [Firebase client](https://github.com/lokesh-1511/chain-reaction/blob/main/src/config/firebase.js): SDK initialization · `src/config/firebase.js`
- [Authentication and Firestore](https://github.com/lokesh-1511/chain-reaction/blob/main/FIRESTORE_SCHEMA.md): managed identity and database · Stores identity, profiles, stats, achievements, leaderboards, and history. · `FIRESTORE_SCHEMA.md`
- [Firestore access rules](https://github.com/lokesh-1511/chain-reaction/blob/main/firestore.rules): database policy · `firestore.rules`
- [Optional Cloud Functions](https://github.com/lokesh-1511/chain-reaction/blob/main/functions/index.js): Firebase backend entry · Alternative server-side gameplay surface. · `functions/index.js`
- [Function game rules](https://github.com/lokesh-1511/chain-reaction/blob/main/functions/gameLogic.js): game engine · Must remain consistent with local and server rules. · `functions/gameLogic.js`

## Connections

- 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
