# code100x/chess architecture

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

- Interactive diagram: https://gitdiagram.com/code100x/chess
- Repository: https://github.com/code100x/chess

## Overview

##### Main Components

a) Client Applications:
- Web Frontend (React + TypeScript)
- Native Mobile App (React Native)
- As separate client interfaces

b) Backend Services:
- Main Backend Server (Node.js)
- WebSocket Server (dedicated real-time game service)
- Authentication Service (with GitHub/Google OAuth)

c) Data Stores:
- PostgreSQL Database (for user data, game records)
- Redis (for real-time game moves queue)

**[Client Layer]**
- ↕️
**[Service Layer]**
- ↕️
**[Data Layer]**

##### Client Layer
- Both web and mobile clients
- Frontend components:
- Chess Board Component
- Game Management
- Authentication UI
- User Settings

##### Service Layer
- Main Backend API Service:
- Auth Router
- Game Management
- User Management
- WebSocket Server:
- Game Manager
- Socket Manager
- Real-time Communication

##### Data Layer
- PostgreSQL with tables for:
- Users
- Games
- Moves
- Ratings
- Redis for real-time game state

##### Communication Flows

- Blue: Client-side components
- Green: Backend services
- Yellow: Data stores
- Red: Authentication flows
- Purple: WebSocket connections

Important Architectural Patterns to Highlight:

##### Monorepo Structure
- Separation of apps/ and packages/
- Shared packages (ui, store, db)

##### Real-time Game Architecture
- WebSocket server communication flow
- Game state management
- Move validation and synchronization

##### Authentication Flow
- OAuth providers
- Session management
- User authentication states

##### Data Flow Patterns
- Game move propagation
- State updates
- Rating calculations

Special Considerations:

##### Real-time nature of the chess game
- How moves are synchronized between players
- Time control management
- Rating update mechanism

##### Shared package architecture
- How common UI components are shared
- Shared types and configurations
- Database access patterns

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    subgraph Clients
        WebClient["Web Frontend"]:::client
        MobileClient["Mobile App"]:::client
        
        subgraph WebComponents
            ChessBoard["Chess Board"]:::component
            GameUI["Game Management"]:::component
            AuthUI["Authentication UI"]:::component
            Settings["User Settings"]:::component
        end
        
        subgraph MobileComponents
            MobileChessBoard["Chess Board"]:::component
            MobileAuthUI["Authentication UI"]:::component
        end
    end
    
    %% Service Layer
    subgraph Services
        Backend["Backend Server"]:::service
        WSServer["WebSocket Server"]:::service
        
        subgraph BackendServices
            AuthService["Authentication Service"]:::component
            GameService["Game Management"]:::component
            UserService["User Management"]:::component
        end
        
        subgraph WebSocketServices
            GameManager["Game Manager"]:::component
            SocketManager["Socket Manager"]:::component
        end
    end
    
    %% Data Layer
    subgraph Data
        PostgreSQL[(PostgreSQL)]:::database
        Redis[(Redis)]:::database
        
        subgraph Schema
            Users[("Users")]:::table
            Games[("Games")]:::table
            Moves[("Moves")]:::table
            Ratings[("Ratings")]:::table
        end
    end
    
    %% Connections
    WebClient --> Backend
    MobileClient --> Backend
    WebClient -.-> WSServer
    MobileClient -.-> WSServer
    Backend --> PostgreSQL
    WSServer --> Redis
    Backend --> Redis
    
    %% Component Relationships
    WebComponents --> WebClient
    MobileComponents --> MobileClient
    BackendServices --> Backend
    WebSocketServices --> WSServer
    Schema --> PostgreSQL
    
    %% Click Events
    click WebClient "https://github.com/code100x/chess/tree/main/apps/frontend"
    click MobileClient "https://github.com/code100x/chess/tree/main/apps/native"
    click Backend "https://github.com/code100x/chess/tree/main/apps/backend"
    click WSServer "https://github.com/code100x/chess/tree/main/apps/ws"
    click ChessBoard "https://github.com/code100x/chess/blob/main/apps/frontend/src/components/ChessBoard.tsx"
    click MobileChessBoard "https://github.com/code100x/chess/tree/main/apps/native/components/chessboard"
    click AuthService "https://github.com/code100x/chess/blob/main/apps/backend/src/router/auth.ts"
    click GameManager "https://github.com/code100x/chess/blob/main/apps/ws/src/GameManager.ts"
    click GameUI "https://github.com/code100x/chess/blob/main/apps/frontend/src/screens/Game.tsx"
    click AuthUI "https://github.com/code100x/chess/blob/main/apps/frontend/src/screens/Login.tsx"
    click MobileAuthUI "https://github.com/code100x/chess/blob/main/apps/native/app/login/index.tsx"
    click Settings "https://github.com/code100x/chess/blob/main/apps/frontend/src/screens/Settings.tsx"
    click SocketManager "https://github.com/code100x/chess/blob/main/apps/ws/src/SocketManager.ts"
    click PostgreSQL "https://github.com/code100x/chess/blob/main/packages/db/prisma/schema.prisma"
    click Schema "https://github.com/code100x/chess/tree/main/packages/db"
    click WebComponents "https://github.com/code100x/chess/tree/main/packages/ui/src"
    
    %% Styles
    classDef client fill:#2374ab
    classDef service fill:#2a9d8f
    classDef component fill:#457b9d
    classDef database fill:#e9c46a
    classDef table fill:#f4a261
    
    %% Legend
    subgraph Legend
        Client[Client Applications]:::client
        Service[Backend Services]:::service
        Component[Components]:::component
        Database[Databases]:::database
        Table[Database Tables]:::table
        
        %% Legend Connections
        REST[REST API]
        WS[WebSocket]
        DB[Database Connection]
        
        REST === WS
        WS -.- DB
    end
```
