# anshumansharma69-bit/fightclub architecture

> Architecture diagram of the GitHub repository anshumansharma69-bit/fightclub, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/anshumansharma69-bit/fightclub
- Repository: https://github.com/anshumansharma69-bit/fightclub

## Overview

1. **High-level split**
   - The repo is a two-tier application: a **Next.js frontend** in `client/` and an **Express/MongoDB backend** in `server/`.
   - The frontend is mostly a presentation and interaction layer; the backend owns all domain logic for fighters, challenges, zones, auth, and rankings.

2. **Frontend surface and routing**
   - The client uses the **Next.js App Router** with route-based pages for the main experiences: map, login/register, onboarding, leaderboard, fighter directory, and public fighter profile pages.
   - Public browsing is intentionally supported: map, fighter profiles, leaderboard, and nearby fighter discovery can be viewed without login, while mutation flows require authentication.

3. **Frontend state and API boundary**
   - `client/context/AuthContext.jsx` centralizes authentication state on the frontend.
   - `client/lib/api.js` is the main boundary to the backend API; UI components call into it rather than talking to the server directly.
   - Reusable UI pieces like `MapView`, `FighterCard`, `ChallengeModal`, `NotificationsPanel`, and `ShareInviteModal` compose the main user flows.

4. **Backend structure**
   - The server is organized by domain: `auth`, `fighter`, `challenge`, `zone`, plus `admin`.
   - Each domain has a route layer, controller layer, and in most cases a Mongoose model, which makes the backend the source of truth for all state transitions.

5. **Authentication and user onboarding**
   - Authentication supports **email/password plus Google OAuth** via Passport.js.
   - JWTs protect authenticated routes, while middleware enforces access control.
   - Google sign-in can create a placeholder fighter profile that is redirected into an onboarding step before full participation in the app.

6. **Core fighter domain**
   - Fighters are the public identity in the system, separate from raw auth users.
   - The fighter API supports public profile reads, nearby fighter discovery, and authenticated updates for location, availability, and stats.
   - Public profile pages expose history and ranking data without requiring login.

7. **Location and map subsystem**
   - The app’s map experience is built around **Leaflet / React-Leaflet** on the client and **MongoDB geospatial queries** on the server.
   - Fighters are stored as geographic points, while city territories are stored as GeoJSON polygons.
   - This enables two important data flows: “fighters near me” queries and “which zone does this fight/location belong to?” checks.

8. **Challenge and fight workflow**
   - Challenges are the main transactional flow: send, accept/decline, upload proof, then confirm or dispute the result.
   - The backend persists challenge state in MongoDB and drives all authoritative updates to fighters, ELO, and badge ownership.
   - The client uses modal-driven UI and live notifications to manage this multi-step flow.

9. **Real-time communication**
   - The repo uses **Socket.io** for instant challenge and status notifications.
   - The architecture is targeted rather than broadcast-oriented: events are delivered to specific fighters, which keeps the app responsive without polling.

10. **Rankings and territory system**
   - Fighter strength is modeled with an **ELO rating** implemented in `server/utils/elo.js`.
   - Territory ownership is driven by city zones and badge logic in `server/utils/badge.js`; zone claims can change based on fight outcomes and location.
   - The leaderboard and zone pages are therefore derived views over combat outcomes and badge ownership, not independent data stores.

11. **Media and proof handling**
   - Fight proof images are uploaded through the backend and stored in **Cloudinary**.
   - The design keeps proof URLs in the challenge record so both fighters can review evidence during disputes or confirmation.

12. **Data storage and schema boundaries**
   - MongoDB models are split by domain: `User`, `Fighter`, `Challenge`, and `Zone`.
   - `User` handles auth identity, while `Fighter` carries public gameplay state; this separation helps the app support social login, onboarding, and game-state updates cleanly.

13. **Admin and operational paths**
   - There is a separate admin route and middleware path, indicating privileged operations are isolated from normal fighter actions.
   - Zone seeding is also treated as an operational/backend concern rather than a frontend feature.

14. **External services and deployment shape**
   - The architecture relies on **MongoDB Atlas** for persistence, **Cloudinary** for media, **Google OAuth** for identity federation, and **OpenStreetMap/Leaflet** for maps.
   - Deployment is split by layer: frontend on **Vercel**, backend on **Render**, which matches the clean API boundary between `client/` and `server/`.

Main entry points: [`client/app`](https://github.com/anshumansharma69-bit/fightclub/tree/main/client/app) (App Router), [`server/index.js`](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/index.js) (API Server), [`server/models`](https://github.com/anshumansharma69-bit/fightclub/tree/main/server/models) (Mongo Models), [`server/controllers/fighter.controller.js`](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/controllers/fighter.controller.js) (Fighter Logic)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_client_app["App Router<br/>next routes"]
  node_client_auth(("Auth State<br/>react context<br/>[AuthContext.jsx]"))
  node_client_api["API Client<br/>api boundary<br/>[api.js]"]
  node_client_map["Map View<br/>UI component<br/>[MapView.jsx]"]
  node_client_fightercard["Fighter Card<br/>UI component<br/>[FighterCard.jsx]"]
  node_client_challengemodal["Challenge Modal<br/>UI component<br/>[ChallengeModal.jsx]"]
  node_client_notifications["Notifications<br/>UI component"]
  node_client_share["Invite Modal<br/>UI component"]
end

subgraph group_server["Server"]
  node_server_index["API Server<br/>express app<br/>[index.js]"]
  node_server_auth_routes["Auth Routes<br/>route layer<br/>[auth.routes.js]"]
  node_server_fighter_routes["Fighter Routes<br/>route layer<br/>[fighter.routes.js]"]
  node_server_challenge_routes["Challenge Routes<br/>route layer"]
  node_server_zone_routes["Zone Routes<br/>route layer<br/>[zone.routes.js]"]
  node_server_admin_routes["Admin Routes<br/>route layer<br/>[admin.routes.js]"]
  node_server_auth_ctrl["Auth Logic<br/>controller<br/>[auth.controller.js]"]
  node_server_fighter_ctrl["Fighter Logic<br/>controller"]
  node_server_challenge_ctrl["Challenge Logic<br/>controller"]
  node_server_zone_ctrl["Zone Logic<br/>controller<br/>[zone.controller.js]"]
  node_server_models[("Mongo Models<br/>schema layer")]
  node_server_security["Auth Middleware<br/>security middleware<br/>[auth.middleware.js]"]
  node_server_runtime["Domain Utils<br/>service utils"]
end

node_client_app -->|"uses session"| node_client_auth
node_client_app -->|"calls API"| node_client_api
node_client_app -->|"renders map"| node_client_map
node_client_app -->|"drives flow"| node_client_challengemodal
node_client_app -->|"shows alerts"| node_client_notifications
node_client_app -->|"shows fighters"| node_client_fightercard
node_client_app -->|"invites"| node_client_share
node_client_api -->|"HTTP requests"| node_server_index
node_server_index -->|"mounts"| node_server_auth_routes
node_server_index -->|"mounts"| node_server_fighter_routes
node_server_index -->|"mounts"| node_server_challenge_routes
node_server_index -->|"mounts"| node_server_zone_routes
node_server_index -->|"mounts"| node_server_admin_routes
node_server_auth_routes -->|"delegates"| node_server_auth_ctrl
node_server_fighter_routes -->|"delegates"| node_server_fighter_ctrl
node_server_challenge_routes -->|"delegates"| node_server_challenge_ctrl
node_server_zone_routes -->|"delegates"| node_server_zone_ctrl
node_server_admin_routes -->|"protects"| node_server_security
node_server_auth_ctrl -->|"reads/writes"| node_server_models
node_server_fighter_ctrl -->|"reads/writes"| node_server_models
node_server_challenge_ctrl -->|"reads/writes"| node_server_models
node_server_zone_ctrl -->|"reads/writes"| node_server_models
node_server_challenge_ctrl -->|"uses"| node_server_runtime
node_server_fighter_ctrl -->|"uses"| node_server_runtime
node_server_zone_ctrl -->|"uses"| node_server_runtime
node_server_index -->|"applies"| node_server_security

click node_client_app "https://github.com/anshumansharma69-bit/fightclub/tree/main/client/app"
click node_client_auth "https://github.com/anshumansharma69-bit/fightclub/blob/main/client/context/AuthContext.jsx"
click node_client_api "https://github.com/anshumansharma69-bit/fightclub/blob/main/client/lib/api.js"
click node_client_map "https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/MapView.jsx"
click node_client_fightercard "https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/FighterCard.jsx"
click node_client_challengemodal "https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/ChallengeModal.jsx"
click node_client_notifications "https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/NotificationsPanel.jsx"
click node_client_share "https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/ShareInviteModal.jsx"
click node_server_index "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/index.js"
click node_server_auth_routes "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/auth.routes.js"
click node_server_fighter_routes "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/fighter.routes.js"
click node_server_challenge_routes "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/challenge.routes.js"
click node_server_zone_routes "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/zone.routes.js"
click node_server_admin_routes "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/admin.routes.js"
click node_server_auth_ctrl "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/controllers/auth.controller.js"
click node_server_fighter_ctrl "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/controllers/fighter.controller.js"
click node_server_challenge_ctrl "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/controllers/challenge.controller.js"
click node_server_zone_ctrl "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/controllers/zone.controller.js"
click node_server_models "https://github.com/anshumansharma69-bit/fightclub/tree/main/server/models"
click node_server_security "https://github.com/anshumansharma69-bit/fightclub/blob/main/server/middleware/auth.middleware.js"
click node_server_runtime "https://github.com/anshumansharma69-bit/fightclub/tree/main/server/utils"

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_client_app,node_client_auth,node_client_api,node_client_map,node_client_fightercard,node_client_challengemodal,node_client_notifications,node_client_share toneBlue
class node_server_index,node_server_auth_routes,node_server_fighter_routes,node_server_challenge_routes,node_server_zone_routes,node_server_admin_routes,node_server_auth_ctrl,node_server_fighter_ctrl,node_server_challenge_ctrl,node_server_zone_ctrl,node_server_models,node_server_security,node_server_runtime toneAmber
```

## Components

### Client

Next.js frontend for public browsing and authenticated gameplay flows.

- [App Router](https://github.com/anshumansharma69-bit/fightclub/tree/main/client/app): next routes · Route-based entry points for map, auth, onboarding, leaderboard, fighters, and admin pages. · `client/app`
- [Auth State](https://github.com/anshumansharma69-bit/fightclub/blob/main/client/context/AuthContext.jsx): react context · Central frontend authentication/session state. · `client/context/AuthContext.jsx`
- [API Client](https://github.com/anshumansharma69-bit/fightclub/blob/main/client/lib/api.js): api boundary · Frontend gateway for calling backend endpoints. · `client/lib/api.js`
- [Map View](https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/MapView.jsx): UI component · Leaflet-based map experience and nearby fighter discovery. · `client/components/MapView.jsx`
- [Fighter Card](https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/FighterCard.jsx): UI component · Reusable fighter summary and profile presentation. · `client/components/FighterCard.jsx`
- [Challenge Modal](https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/ChallengeModal.jsx): UI component · Modal-driven challenge send, proof, and resolution flow. · `client/components/ChallengeModal.jsx`
- [Notifications](https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/NotificationsPanel.jsx): UI component · Live challenge and status updates for targeted fighters. · `client/components/NotificationsPanel.jsx`
- [Invite Modal](https://github.com/anshumansharma69-bit/fightclub/blob/main/client/components/ShareInviteModal.jsx): UI component · Share and invitation entry point for social gameplay. · `client/components/ShareInviteModal.jsx`

### Server

Express API and MongoDB-backed domain logic.

- [API Server](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/index.js): express app · Backend entrypoint wiring routes, middleware, and startup. · `server/index.js`
- [Auth Routes](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/auth.routes.js): route layer · Login, register, JWT, and OAuth endpoints. · `server/routes/auth.routes.js`
- [Fighter Routes](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/fighter.routes.js): route layer · Public fighter reads and authenticated fighter updates. · `server/routes/fighter.routes.js`
- [Challenge Routes](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/challenge.routes.js): route layer · Challenge lifecycle endpoints for battle workflow. · `server/routes/challenge.routes.js`
- [Zone Routes](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/zone.routes.js): route layer · City territory and zone lookup endpoints. · `server/routes/zone.routes.js`
- [Admin Routes](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/routes/admin.routes.js): route layer · Privileged operational endpoints. · `server/routes/admin.routes.js`
- [Auth Logic](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/controllers/auth.controller.js): controller · Credential auth, Google sign-in, onboarding handoff. · `server/controllers/auth.controller.js`
- [Fighter Logic](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/controllers/fighter.controller.js): controller · Public profile, nearby discovery, and fighter updates. · `server/controllers/fighter.controller.js`
- [Challenge Logic](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/controllers/challenge.controller.js): controller · Challenge state transitions, proof handling, and result confirmation. · `server/controllers/challenge.controller.js`
- [Zone Logic](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/controllers/zone.controller.js): controller · Zone ownership, lookup, and territory operations. · `server/controllers/zone.controller.js`
- [Mongo Models](https://github.com/anshumansharma69-bit/fightclub/tree/main/server/models): schema layer · User, Fighter, Challenge, and Zone persistence models. · `server/models`
- [Auth Middleware](https://github.com/anshumansharma69-bit/fightclub/blob/main/server/middleware/auth.middleware.js): security middleware · JWT protection and admin access control. · `server/middleware/auth.middleware.js`
- [Domain Utils](https://github.com/anshumansharma69-bit/fightclub/tree/main/server/utils): service utils · ELO, badges, geo queries, geocoding, and Cloudinary helpers. · `server/utils`

## Connections

- App Router → Auth State: uses session · Pages and route flows read auth state.
- App Router → API Client: calls API · Route pages fetch and mutate through the shared API client.
- App Router → Map View: renders map · Map page composes the map subsystem.
- App Router → Challenge Modal: drives flow · Challenge flows are initiated from route pages.
- App Router → Notifications: shows alerts · Authenticated UX surfaces live updates.
- App Router → Fighter Card: shows fighters · Directory and profile pages reuse fighter cards.
- App Router → Invite Modal: invites · Invite and sharing interactions are exposed in the app.
- API Client → API Server: HTTP requests · Frontend communicates with the backend API boundary.
- API Server → Auth Routes: mounts · API server registers auth endpoints.
- API Server → Fighter Routes: mounts · API server registers fighter endpoints.
- API Server → Challenge Routes: mounts · API server registers challenge endpoints.
- API Server → Zone Routes: mounts · API server registers zone endpoints.
- API Server → Admin Routes: mounts · API server registers admin endpoints.
- Auth Routes → Auth Logic: delegates · Route handlers forward auth work to controller logic.
- Fighter Routes → Fighter Logic: delegates · Route handlers forward fighter work to controller logic.
- Challenge Routes → Challenge Logic: delegates · Route handlers forward challenge work to controller logic.
- Zone Routes → Zone Logic: delegates · Route handlers forward zone work to controller logic.
- Admin Routes → Auth Middleware: protects · Admin operations are guarded by middleware.
- Auth Logic → Mongo Models: reads/writes · Auth logic persists user identities and onboarding state.
- Fighter Logic → Mongo Models: reads/writes · Fighter logic manages public gameplay state.
- Challenge Logic → Mongo Models: reads/writes · Challenge logic persists battle state.
- Zone Logic → Mongo Models: reads/writes · Zone logic persists territory data.
- Challenge Logic → Domain Utils: uses · Challenge outcomes depend on ELO, badges, geolocation, and media services.
- Fighter Logic → Domain Utils: uses · Fighter discovery and location updates use geo utilities.
- Zone Logic → Domain Utils: uses · Zone logic uses geospatial and badge helpers.
- API Server → Auth Middleware: applies · API server applies authentication and access control middleware.
