# sabarishr-dev/arena architecture

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

- Interactive diagram: https://gitdiagram.com/sabarishr-dev/arena
- Repository: https://github.com/sabarishr-dev/arena

## Overview

#### Purpose and Runtime
Arena is a browser-based office gaming platform: users authenticate, discover games, launch Unity WebGL or HTML builds, and participate in progress/leaderboard-oriented play. It is a Next.js application using both App Router UI/API routes and legacy Pages Router API routes. Root composition begins at `app/layout.tsx` and the landing route is `app/page.tsx`.

#### Identity and Session Boundary
Authentication is handled by NextAuth with Azure Entra ID/OpenID, exposed through the Pages Router catch-all auth endpoint at `pages/api/auth/[...nextauth].ts`. Client session access is provided by `components/auth/SessionProvider.tsx`; sign-in UI is in `components/auth/SignIn.tsx`. This boundary supplies user identity to profile, gameplay, and platform-service actions.

#### Game Catalog and Local Persistence
The catalog is backed by a local SQLite database (`data/games.db` at runtime), initialized by `scripts/init-db.ts` and accessed through `lib/db.ts`. It stores game metadata, categories, and build type (`unity` or `html`). Catalog CRUD/read flows are exposed through App Router endpoints:
- `app/api/arena/games/route.ts`
- `app/api/arena/games/[id]/route.ts`
- `app/api/profile/my-games/route.ts`

#### Player-Facing Navigation
The principal user journeys are catalog discovery, game detail, game launch, dashboard/profile, and leaderboard views. The Arena routes are `app/arena/page.tsx`, `app/arena/[id]/page.tsx`, and `app/arena/play-game/page.tsx`; account-oriented views are `app/dashboard/page.tsx`, `app/profile/page.tsx`, and `app/leaderboard/page.tsx`. `components/sidebar/SideBar.tsx` provides shared application navigation.

#### Game Launch and Rendering
`components/game/GameLoader.tsx` is the game-type dispatch boundary. Unity builds are embedded through `components/unity/game-view.tsx`, while non-Unity/browser content is rendered through `components/html/html-game-view.tsx`. Unity WebGL is the gameplay runtime; Photon Fusion 2 supplies multiplayer behavior within Unity builds rather than as a visible Next.js server subsystem.

#### Gameplay Session and Time Validation
Play duration and session state are held client-side through `context/PlayTimeContext.tsx` and `components/timer/PlayTimer.tsx`; Unity-specific session coordination is in `context/UnitySessionContext.tsx`. The client validates playtime through `app/api/playtime/validate/route.ts`, creating a server boundary for accepting or rejecting duration-related actions.

#### External Platform Services and Rewards
PlayFab integration is centralized in `lib/playfab/playfab.ts` and supports player progress, achievements, leaderboards, and virtual currency. Coupon/reward delivery crosses into email via `app/api/send-coupon-mail/route.ts`; redemption UI is at `app/coupon/redeem/page.tsx`. Ticket-related logic resides in `lib/ticket/ticket.ts`, likely supporting reward or access workflows.

#### Publishing Flow
Contributors use `app/contribute/page.tsx` and `app/contribute/publish/page.tsx` to supply game content. File/folder selection components (`components/publish/File.tsx`, `components/publish/Folder.tsx`) feed the legacy upload/publish endpoint `pages/api/publish.ts`. Published metadata then enters the SQLite-backed catalog, from which Arena discovery and launch routes consume it.

Main entry points: [`app/arena/page.tsx`](https://github.com/sabarishr-dev/arena/blob/main/app/arena/page.tsx) (Arena catalog & detail), [`app/arena/play-game/page.tsx`](https://github.com/sabarishr-dev/arena/blob/main/app/arena/play-game/page.tsx) (Game launch route), [`components/game/GameLoader.tsx`](https://github.com/sabarishr-dev/arena/blob/main/components/game/GameLoader.tsx) (Game loader), [`context/PlayTimeContext.tsx`](https://github.com/sabarishr-dev/arena/blob/main/context/PlayTimeContext.tsx) (Playtime & Unity session)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Next.js web app"]
  node_root["Root layout<br/>Next.js composition<br/>[layout.tsx]"]
  node_landing["Landing page<br/>route<br/>[page.tsx]"]
  node_navigation["Shared sidebar<br/>navigation<br/>[SideBar.tsx]"]
  node_arena_routes["Arena catalog &amp; detail<br/>routes<br/>[page.tsx]"]
  node_account_routes["Dashboard, profile &amp; leaderboard<br/>routes<br/>[page.tsx]"]
end

subgraph group_gameplay["Gameplay runtime"]
  node_play_route["Game launch route<br/>[page.tsx]"]
  node_game_loader["Game loader<br/>build dispatcher<br/>[GameLoader.tsx]"]
  node_unity_view["Unity WebGL view<br/>game renderer<br/>[game-view.tsx]"]
  node_html_view["HTML game view<br/>game renderer<br/>[html-game-view.tsx]"]
  node_play_session["Playtime &amp; Unity session<br/>client session state"]
  node_playtime_validation{{"Playtime validation<br/>App Router API<br/>[route.ts]"}}
end

subgraph group_platform["Platform services"]
  node_auth_endpoint{{"NextAuth Entra endpoint<br/>auth API<br/>[&#91;...nextauth&#93;.ts]"}}
  node_session_provider["Session provider<br/>client auth"]
  node_catalog_api{{"Catalog APIs<br/>App Router APIs<br/>[route.ts]"}}
  node_sqlite[("Games SQLite database<br/>runtime catalog store")]
  node_db_access["Database access<br/>SQLite adapter<br/>[db.ts]"]
  node_playfab{{"PlayFab<br/>platform integration<br/>[playfab.ts]"}}
  node_rewards["Coupon rewards<br/>reward delivery<br/>[page.tsx]"]
end

subgraph group_publishing["Publishing"]
  node_contributor_ui["Contributor publishing UI<br/>routes<br/>[page.tsx]"]
  node_publish_api{{"Legacy publish endpoint<br/>Pages Router API<br/>[publish.ts]"}}
end

node_root -->|"composes"| node_landing
node_root -->|"provides session"| node_session_provider
node_navigation -->|"navigates"| node_arena_routes
node_navigation -->|"navigates"| node_account_routes
node_session_provider -->|"authenticates via"| node_auth_endpoint
node_session_provider -->|"supplies identity"| node_account_routes
node_arena_routes -->|"reads catalog"| node_catalog_api
node_catalog_api -->|"uses"| node_db_access
node_db_access -->|"reads and writes"| node_sqlite
node_arena_routes -->|"launches game"| node_play_route
node_play_route -->|"renders through"| node_game_loader
node_game_loader -->|"Unity build"| node_unity_view
node_game_loader -->|"HTML build"| node_html_view
node_play_route -->|"tracks session"| node_play_session
node_play_session -->|"validates duration"| node_playtime_validation
node_account_routes -->|"loads progress and rankings"| node_playfab
node_play_session -->|"updates gameplay progress"| node_playfab
node_playfab -->|"drives rewards"| node_rewards
node_contributor_ui -->|"uploads content"| node_publish_api
node_publish_api -->|"publishes metadata"| node_db_access

click node_root "https://github.com/sabarishr-dev/arena/blob/main/app/layout.tsx"
click node_landing "https://github.com/sabarishr-dev/arena/blob/main/app/page.tsx"
click node_navigation "https://github.com/sabarishr-dev/arena/blob/main/components/sidebar/SideBar.tsx"
click node_arena_routes "https://github.com/sabarishr-dev/arena/blob/main/app/arena/page.tsx"
click node_account_routes "https://github.com/sabarishr-dev/arena/blob/main/app/dashboard/page.tsx"
click node_auth_endpoint "https://github.com/sabarishr-dev/arena/blob/main/pages/api/auth/%5B...nextauth%5D.ts"
click node_session_provider "https://github.com/sabarishr-dev/arena/blob/main/components/auth/SessionProvider.tsx"
click node_catalog_api "https://github.com/sabarishr-dev/arena/blob/main/app/api/arena/games/route.ts"
click node_db_access "https://github.com/sabarishr-dev/arena/blob/main/lib/db.ts"
click node_play_route "https://github.com/sabarishr-dev/arena/blob/main/app/arena/play-game/page.tsx"
click node_game_loader "https://github.com/sabarishr-dev/arena/blob/main/components/game/GameLoader.tsx"
click node_unity_view "https://github.com/sabarishr-dev/arena/blob/main/components/unity/game-view.tsx"
click node_html_view "https://github.com/sabarishr-dev/arena/blob/main/components/html/html-game-view.tsx"
click node_play_session "https://github.com/sabarishr-dev/arena/blob/main/context/PlayTimeContext.tsx"
click node_playtime_validation "https://github.com/sabarishr-dev/arena/blob/main/app/api/playtime/validate/route.ts"
click node_playfab "https://github.com/sabarishr-dev/arena/blob/main/lib/playfab/playfab.ts"
click node_rewards "https://github.com/sabarishr-dev/arena/blob/main/app/coupon/redeem/page.tsx"
click node_contributor_ui "https://github.com/sabarishr-dev/arena/blob/main/app/contribute/publish/page.tsx"
click node_publish_api "https://github.com/sabarishr-dev/arena/blob/main/pages/api/publish.ts"

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_root,node_landing,node_navigation,node_arena_routes,node_account_routes toneBlue
class node_play_route,node_game_loader,node_unity_view,node_html_view,node_play_session,node_playtime_validation toneAmber
class node_auth_endpoint,node_session_provider,node_catalog_api,node_sqlite,node_db_access,node_playfab,node_rewards toneMint
class node_contributor_ui,node_publish_api toneRose
```

## Components

### Next.js web app

App Router views and API routes.

- [Root layout](https://github.com/sabarishr-dev/arena/blob/main/app/layout.tsx): Next.js composition · `app/layout.tsx`
- [Landing page](https://github.com/sabarishr-dev/arena/blob/main/app/page.tsx): route · `app/page.tsx`
- [Shared sidebar](https://github.com/sabarishr-dev/arena/blob/main/components/sidebar/SideBar.tsx): navigation · `components/sidebar/SideBar.tsx`
- [Arena catalog & detail](https://github.com/sabarishr-dev/arena/blob/main/app/arena/page.tsx): routes · Discovery and game-detail views. · `app/arena/page.tsx`
- [Dashboard, profile & leaderboard](https://github.com/sabarishr-dev/arena/blob/main/app/dashboard/page.tsx): routes · Account-oriented player views. · `app/dashboard/page.tsx`

### Gameplay runtime

Client-side launch and session handling.

- [Game launch route](https://github.com/sabarishr-dev/arena/blob/main/app/arena/play-game/page.tsx): route · `app/arena/play-game/page.tsx`
- [Game loader](https://github.com/sabarishr-dev/arena/blob/main/components/game/GameLoader.tsx): build dispatcher · Selects Unity or HTML rendering by game type. · `components/game/GameLoader.tsx`
- [Unity WebGL view](https://github.com/sabarishr-dev/arena/blob/main/components/unity/game-view.tsx): game renderer · Unity builds use Photon Fusion 2 for multiplayer. · `components/unity/game-view.tsx`
- [HTML game view](https://github.com/sabarishr-dev/arena/blob/main/components/html/html-game-view.tsx): game renderer · `components/html/html-game-view.tsx`
- [Playtime & Unity session](https://github.com/sabarishr-dev/arena/blob/main/context/PlayTimeContext.tsx): client session state · Tracks duration and Unity-specific session state. · `context/PlayTimeContext.tsx`
- [Playtime validation](https://github.com/sabarishr-dev/arena/blob/main/app/api/playtime/validate/route.ts): App Router API · Accepts or rejects duration-related actions. · `app/api/playtime/validate/route.ts`

### Platform services

Identity, catalog, progress, and rewards.

- [NextAuth Entra endpoint](https://github.com/sabarishr-dev/arena/blob/main/pages/api/auth/%5B...nextauth%5D.ts): auth API · `pages/api/auth/[...nextauth].ts`
- [Session provider](https://github.com/sabarishr-dev/arena/blob/main/components/auth/SessionProvider.tsx): client auth · `components/auth/SessionProvider.tsx`
- [Catalog APIs](https://github.com/sabarishr-dev/arena/blob/main/app/api/arena/games/route.ts): App Router APIs · Games and player-game catalog access. · `app/api/arena/games/route.ts`
- **Games SQLite database**: runtime catalog store · Runtime database at data/games.db.
- [Database access](https://github.com/sabarishr-dev/arena/blob/main/lib/db.ts): SQLite adapter · `lib/db.ts`
- [PlayFab](https://github.com/sabarishr-dev/arena/blob/main/lib/playfab/playfab.ts): platform integration · Progress, achievements, leaderboards, and currency. · `lib/playfab/playfab.ts`
- [Coupon rewards](https://github.com/sabarishr-dev/arena/blob/main/app/coupon/redeem/page.tsx): reward delivery · Redemption UI and coupon email delivery. · `app/coupon/redeem/page.tsx`

### Publishing

Contributor upload flow.

- [Contributor publishing UI](https://github.com/sabarishr-dev/arena/blob/main/app/contribute/publish/page.tsx): routes · Game contribution and publish workflow. · `app/contribute/publish/page.tsx`
- [Legacy publish endpoint](https://github.com/sabarishr-dev/arena/blob/main/pages/api/publish.ts): Pages Router API · Receives selected game files and metadata. · `pages/api/publish.ts`

## Connections

- Root layout → Landing page: composes
- Root layout → Session provider: provides session
- Shared sidebar → Arena catalog & detail: navigates
- Shared sidebar → Dashboard, profile & leaderboard: navigates
- Session provider → NextAuth Entra endpoint: authenticates via
- Session provider → Dashboard, profile & leaderboard: supplies identity
- Arena catalog & detail → Catalog APIs: reads catalog
- Catalog APIs → Database access: uses
- Database access → Games SQLite database: reads and writes
- Arena catalog & detail → Game launch route: launches game
- Game launch route → Game loader: renders through
- Game loader → Unity WebGL view: Unity build
- Game loader → HTML game view: HTML build
- Game launch route → Playtime & Unity session: tracks session
- Playtime & Unity session → Playtime validation: validates duration
- Dashboard, profile & leaderboard → PlayFab: loads progress and rankings
- Playtime & Unity session → PlayFab: updates gameplay progress
- PlayFab → Coupon rewards: drives rewards
- Contributor publishing UI → Legacy publish endpoint: uploads content
- Legacy publish endpoint → Database access: publishes metadata
