# luisregardiz/caida-game architecture

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

- Interactive diagram: https://gitdiagram.com/luisregardiz/caida-game
- Repository: https://github.com/luisregardiz/caida-game

## Overview

1. **Project shape**
   - This is a **Next.js App Router** application in TypeScript.
   - The repo is organized around a **client/server split**: route files and server helpers on one side, interactive game screens and hooks on the other.

2. **Primary user-facing routes**
   - `src/app/page.tsx` is the landing entry point.
   - `src/app/login/page.tsx` handles authentication.
   - `src/app/lobby/page.tsx` is the pre-game hub.
   - `src/app/mesa/[id]/page.tsx` is the table/game room view, with `TableClient.tsx` doing the interactive work.
   - The route names and assets strongly suggest a **multiplayer card game** flow centered on joining a table.

3. **Shell and cross-cutting UI**
   - `src/app/layout.tsx`, `src/components/layout/Navbar.tsx`, and `AuthProvider.tsx` define the shared application shell.
   - `globals.css` is the global styling entry.
   - These pieces are the common frame around all routes, likely handling navigation, session awareness, and app-wide layout.

4. **Authentication and session boundary**
   - `src/config/supabase/client.ts`, `server.ts`, and `middleware.ts` show that **Supabase** is the main backend/auth boundary.
   - `src/proxy.ts` and the middleware layer imply route/session enforcement before users can access protected areas like the lobby or table.
   - `useSupabaseAuth.ts` is the client-side auth hook that likely hydrates user state into the app.

5. **Realtime/game-room collaboration boundary**
   - `useTablePresence.ts` suggests the app tracks who is currently in a table, which is typically powered by **Supabase realtime/presence**.
   - The lobby-to-table transition likely depends on backend state: discover or join a room in the lobby, then subscribe to presence/state once inside `/mesa/[id]`.

6. **Game state management**
   - `src/store/gameStore.ts`, `gameLogicStore.ts`, and `userStore.ts` separate application state into focused domains.
   - `userStore` likely holds the authenticated user and profile-related state.
   - `gameStore` likely holds table/game session state.
   - `gameLogicStore` likely coordinates rule evaluation, turn progression, or action validation.

7. **Rules engine and domain logic**
   - `src/lib/caidaRules.ts` and `src/hooks/useCaidaEngine.ts` indicate a dedicated **game rules engine** for “caida.”
   - This is the core non-UI domain boundary: rules are separated from rendering so the same logic can drive table interactions, validation, and state transitions.
   - The `caida.types.ts` file reinforces that this is a first-class domain model, not just incidental UI logic.

8. **Page-level composition**
   - The route pages are thin composition layers that mount specialized client components such as `LobbyClient.tsx` and `TableClient.tsx`.
   - This suggests server components are used mainly for routing/bootstrapping, while actual interactivity is pushed into client components and hooks.
   - That separation is important for an architecture diagram: page → client screen → hooks/stores → Supabase/rules engine.

9. **Data model and type safety**
   - `src/types/database.types.ts` implies generated or hand-maintained Supabase database typings.
   - `game.types.ts` and `caida.types.ts` model the game domain.
   - Type definitions are a key contract layer between UI, stores, and backend data access.

10. **Static assets and presentation**
   - `public/cards/**` contains card artwork organized by suit and additions.
   - This is a clear presentation asset boundary: the game UI renders from fixed static imagery rather than generated card graphics.
   - In diagram terms, assets are consumed by the table/game UI layer, not by the backend.

11. **Technology/runtime signals that matter**
   - The repo uses **Next.js**, **React**, **TypeScript**, and **Supabase**.
   - `eslint.config.mjs`, `postcss.config.mjs`, and `next.config.ts` indicate standard frontend tooling and build-time configuration.
   - Nothing in the tree suggests a separate custom backend service; Supabase appears to be the main external platform.

12. **End-to-end flow**
   - A user enters the app, session is checked through middleware/proxy and Supabase auth.
   - After login, the lobby provides table discovery/joining.
   - Entering a table loads the dynamic `/mesa/[id]` screen, which combines presence, game state, and rule evaluation.
   - The UI renders card assets while stores and hooks manage live interaction and state synchronization.

Main entry points: [`src/app/mesa/[id]/TableClient.tsx`](https://github.com/luisregardiz/caida-game/blob/main/src/app/mesa/[id]/TableClient.tsx) (Table client), [`src/hooks/useCaidaEngine.ts`](https://github.com/luisregardiz/caida-game/blob/main/src/hooks/useCaidaEngine.ts) (Engine hook), [`src/app/layout.tsx`](https://github.com/luisregardiz/caida-game/blob/main/src/app/layout.tsx) (App shell), [`src/hooks/useSupabaseAuth.ts`](https://github.com/luisregardiz/caida-game/blob/main/src/hooks/useSupabaseAuth.ts) (Auth hook)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App shell"]
  node_app_page["Home<br/>route<br/>[page.tsx]"]
  node_app_layout["App shell<br/>layout<br/>[layout.tsx]"]
  node_navbar["Navbar<br/>ui<br/>[Navbar.tsx]"]
  node_auth_provider["Auth provider<br/>client shell<br/>[AuthProvider.tsx]"]
end

subgraph group_game["Game room"]
  node_login_page["Login<br/>route<br/>[page.tsx]"]
  node_lobby_page["Lobby<br/>route<br/>[page.tsx]"]
  node_lobby_client["Lobby client<br/>interactive screen<br/>[LobbyClient.tsx]"]
  node_mesa_page["Table route<br/>[page.tsx]"]
  node_table_client["Table client<br/>interactive screen<br/>[TableClient.tsx]"]
end

subgraph group_state["State layer"]
  node_auth_hook["Auth hook<br/>[useSupabaseAuth.ts]"]
  node_presence_hook["Presence hook"]
  node_engine_hook["Engine hook<br/>[useCaidaEngine.ts]"]
  node_game_store["Game store<br/>state store<br/>[gameStore.ts]"]
  node_logic_store["Logic store<br/>state store<br/>[gameLogicStore.ts]"]
  node_user_store["User store<br/>state store<br/>[userStore.ts]"]
end

subgraph group_backend["Auth boundary"]
  node_auth_client["Supabase auth<br/>client api<br/>[client.ts]"]
  node_auth_server["Server auth<br/>server api<br/>[server.ts]"]
  node_middleware["Middleware<br/>route guard<br/>[middleware.ts]"]
  node_proxy["Proxy<br/>edge guard<br/>[proxy.ts]"]
end

subgraph group_domain["Domain logic"]
  node_rules{{"Caida rules<br/>rules engine<br/>[caidaRules.ts]"}}
  node_caida_types["Caida types<br/>domain types<br/>[caida.types.ts]"]
  node_game_types["Game types<br/>domain types<br/>[game.types.ts]"]
  node_db_types["DB types<br/>schema types<br/>[database.types.ts]"]
end

subgraph group_assets["Assets"]
  node_cards[("Card assets<br/>static assets")]
end

node_app_page -->|"renders in"| node_app_layout
node_login_page -->|"renders in"| node_app_layout
node_lobby_page -->|"mounts"| node_lobby_client
node_lobby_client -->|"reads session"| node_auth_hook
node_lobby_client -->|"updates"| node_game_store
node_mesa_page -->|"mounts"| node_table_client
node_table_client -->|"subscribes"| node_presence_hook
node_table_client -->|"drives"| node_engine_hook
node_engine_hook -->|"evaluates"| node_rules
node_engine_hook -->|"coordinates"| node_logic_store
node_engine_hook -->|"syncs"| node_game_store
node_table_client -->|"renders"| node_cards
node_app_layout -->|"includes"| node_navbar
node_app_layout -->|"wraps"| node_auth_provider
node_auth_provider -->|"uses"| node_auth_hook
node_auth_hook -->|"connects to"| node_auth_client
node_auth_hook -->|"hydrates"| node_user_store
node_middleware -->|"protects via"| node_auth_server
node_proxy -->|"routes through"| node_middleware
node_auth_server -->|"typed by"| node_db_types
node_game_store -->|"typed by"| node_game_types
node_logic_store -->|"typed by"| node_caida_types
node_rules -->|"defines"| node_caida_types

click node_app_page "https://github.com/luisregardiz/caida-game/blob/main/src/app/page.tsx"
click node_app_layout "https://github.com/luisregardiz/caida-game/blob/main/src/app/layout.tsx"
click node_navbar "https://github.com/luisregardiz/caida-game/blob/main/src/components/layout/Navbar.tsx"
click node_auth_provider "https://github.com/luisregardiz/caida-game/blob/main/src/components/layout/AuthProvider.tsx"
click node_login_page "https://github.com/luisregardiz/caida-game/blob/main/src/app/login/page.tsx"
click node_lobby_page "https://github.com/luisregardiz/caida-game/blob/main/src/app/lobby/page.tsx"
click node_lobby_client "https://github.com/luisregardiz/caida-game/blob/main/src/app/lobby/LobbyClient.tsx"
click node_mesa_page "https://github.com/luisregardiz/caida-game/blob/main/src/app/mesa/[id]/page.tsx"
click node_table_client "https://github.com/luisregardiz/caida-game/blob/main/src/app/mesa/[id]/TableClient.tsx"
click node_auth_client "https://github.com/luisregardiz/caida-game/blob/main/src/config/supabase/client.ts"
click node_auth_server "https://github.com/luisregardiz/caida-game/blob/main/src/config/supabase/server.ts"
click node_middleware "https://github.com/luisregardiz/caida-game/blob/main/src/config/supabase/middleware.ts"
click node_proxy "https://github.com/luisregardiz/caida-game/blob/main/src/proxy.ts"
click node_auth_hook "https://github.com/luisregardiz/caida-game/blob/main/src/hooks/useSupabaseAuth.ts"
click node_presence_hook "https://github.com/luisregardiz/caida-game/blob/main/src/hooks/useTablePresence.ts"
click node_engine_hook "https://github.com/luisregardiz/caida-game/blob/main/src/hooks/useCaidaEngine.ts"
click node_game_store "https://github.com/luisregardiz/caida-game/blob/main/src/store/gameStore.ts"
click node_logic_store "https://github.com/luisregardiz/caida-game/blob/main/src/store/gameLogicStore.ts"
click node_user_store "https://github.com/luisregardiz/caida-game/blob/main/src/store/userStore.ts"
click node_rules "https://github.com/luisregardiz/caida-game/blob/main/src/lib/caidaRules.ts"
click node_caida_types "https://github.com/luisregardiz/caida-game/blob/main/src/types/caida.types.ts"
click node_game_types "https://github.com/luisregardiz/caida-game/blob/main/src/types/game.types.ts"
click node_db_types "https://github.com/luisregardiz/caida-game/blob/main/src/types/database.types.ts"
click node_cards "https://github.com/luisregardiz/caida-game/tree/main/public/cards"

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_app_page,node_app_layout,node_navbar,node_auth_provider toneBlue
class node_login_page,node_lobby_page,node_lobby_client,node_mesa_page,node_table_client toneAmber
class node_auth_hook,node_presence_hook,node_engine_hook,node_game_store,node_logic_store,node_user_store toneMint
class node_auth_client,node_auth_server,node_middleware,node_proxy toneRose
class node_rules,node_caida_types,node_game_types,node_db_types toneIndigo
class node_cards toneTeal
```

## Components

### App shell

Next.js routes and shared UI frame

- [Home](https://github.com/luisregardiz/caida-game/blob/main/src/app/page.tsx): route · Landing entry route · `src/app/page.tsx`
- [App shell](https://github.com/luisregardiz/caida-game/blob/main/src/app/layout.tsx): layout · Global app layout wrapper · `src/app/layout.tsx`
- [Navbar](https://github.com/luisregardiz/caida-game/blob/main/src/components/layout/Navbar.tsx): ui · Shared navigation bar · `src/components/layout/Navbar.tsx`
- [Auth provider](https://github.com/luisregardiz/caida-game/blob/main/src/components/layout/AuthProvider.tsx): client shell · Provides session-aware UI context · `src/components/layout/AuthProvider.tsx`

### Game room

Lobby and table interaction surfaces

- [Login](https://github.com/luisregardiz/caida-game/blob/main/src/app/login/page.tsx): route · Authentication entry page · `src/app/login/page.tsx`
- [Lobby](https://github.com/luisregardiz/caida-game/blob/main/src/app/lobby/page.tsx): route · Pre-game hub page · `src/app/lobby/page.tsx`
- [Lobby client](https://github.com/luisregardiz/caida-game/blob/main/src/app/lobby/LobbyClient.tsx): interactive screen · Lobby interactions and table join flow · `src/app/lobby/LobbyClient.tsx`
- [Table route](https://github.com/luisregardiz/caida-game/blob/main/src/app/mesa/[id]/page.tsx): route · Dynamic table room page · `src/app/mesa/[id]/page.tsx`
- [Table client](https://github.com/luisregardiz/caida-game/blob/main/src/app/mesa/[id]/TableClient.tsx): interactive screen · Main in-table gameplay UI · `src/app/mesa/[id]/TableClient.tsx`

### State layer

Client state and hooks

- [Auth hook](https://github.com/luisregardiz/caida-game/blob/main/src/hooks/useSupabaseAuth.ts): hook · Hydrates client auth state · `src/hooks/useSupabaseAuth.ts`
- [Presence hook](https://github.com/luisregardiz/caida-game/blob/main/src/hooks/useTablePresence.ts): hook · Tracks table occupancy and presence · `src/hooks/useTablePresence.ts`
- [Engine hook](https://github.com/luisregardiz/caida-game/blob/main/src/hooks/useCaidaEngine.ts): hook · Connects UI to caida rules · `src/hooks/useCaidaEngine.ts`
- [Game store](https://github.com/luisregardiz/caida-game/blob/main/src/store/gameStore.ts): state store · Table and session state · `src/store/gameStore.ts`
- [Logic store](https://github.com/luisregardiz/caida-game/blob/main/src/store/gameLogicStore.ts): state store · Turn and action coordination · `src/store/gameLogicStore.ts`
- [User store](https://github.com/luisregardiz/caida-game/blob/main/src/store/userStore.ts): state store · Authenticated user state · `src/store/userStore.ts`

### Auth boundary

Supabase client/server and route protection

- [Supabase auth](https://github.com/luisregardiz/caida-game/blob/main/src/config/supabase/client.ts): client api · Client-side Supabase auth setup · `src/config/supabase/client.ts`
- [Server auth](https://github.com/luisregardiz/caida-game/blob/main/src/config/supabase/server.ts): server api · Server-side Supabase session access · `src/config/supabase/server.ts`
- [Middleware](https://github.com/luisregardiz/caida-game/blob/main/src/config/supabase/middleware.ts): route guard · Protects routes and sessions · `src/config/supabase/middleware.ts`
- [Proxy](https://github.com/luisregardiz/caida-game/blob/main/src/proxy.ts): edge guard · Additional request/session enforcement · `src/proxy.ts`

### Domain logic

Rules engine and types

- [Caida rules](https://github.com/luisregardiz/caida-game/blob/main/src/lib/caidaRules.ts): rules engine · Core game rules and validation · `src/lib/caidaRules.ts`
- [Caida types](https://github.com/luisregardiz/caida-game/blob/main/src/types/caida.types.ts): domain types · Game domain model · `src/types/caida.types.ts`
- [Game types](https://github.com/luisregardiz/caida-game/blob/main/src/types/game.types.ts): domain types · Shared gameplay types · `src/types/game.types.ts`
- [DB types](https://github.com/luisregardiz/caida-game/blob/main/src/types/database.types.ts): schema types · Supabase database typings · `src/types/database.types.ts`

### Assets

Static card imagery

- [Card assets](https://github.com/luisregardiz/caida-game/tree/main/public/cards): static assets · Suit and addition artwork · `public/cards`

## Connections

- Home → App shell: renders in · Home route is wrapped by the shared app shell
- Login → App shell: renders in · Login page uses the shared shell
- Lobby → Lobby client: mounts · Server route composes the interactive lobby client
- Lobby client → Auth hook: reads session · Lobby needs authenticated user state
- Lobby client → Game store: updates · Lobby selection and join flow feed game state
- Table route → Table client: mounts · Dynamic table route composes the gameplay client
- Table client → Presence hook: subscribes · Table view tracks who is present in the room
- Table client → Engine hook: drives · Gameplay UI delegates rule-driven actions to the engine hook
- Engine hook → Caida rules: evaluates · Hook applies caida validation and turn logic
- Engine hook → Logic store: coordinates · Engine updates gameplay coordination state
- Engine hook → Game store: syncs · Engine keeps table state in sync with UI
- Table client → Card assets: renders · Table UI uses static card artwork
- App shell → Navbar: includes · Shared layout includes navigation
- App shell → Auth provider: wraps · App shell provides auth context
- Auth provider → Auth hook: uses · Provider likely hydrates auth from the client hook
- Auth hook → Supabase auth: connects to · Client hook talks to Supabase auth
- Auth hook → User store: hydrates · Authenticated user state is stored locally
- Middleware → Server auth: protects via · Middleware relies on server-side session access
- Proxy → Middleware: routes through · Proxy adds request-level enforcement ahead of app access
- Server auth → DB types: typed by · Server access uses Supabase database typings
- Game store → Game types: typed by · Game state follows shared gameplay types
- Logic store → Caida types: typed by · Logic store uses domain-specific caida model
- Caida rules → Caida types: defines · Rule engine operates on the caida domain types
