# wathsara02/pusl3190 architecture

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

- Interactive diagram: https://gitdiagram.com/wathsara02/pusl3190
- Repository: https://github.com/wathsara02/pusl3190

## Overview

#### Purpose and Entry Points
This repository builds, trains, evaluates, and deploys a multi-agent reinforcement-learning player for the four-player Omi card game. It has two major subsystems: an offline MARL project in `finalwm/` and a live React/FastAPI game application in `last/`.

Primary execution entry points:
- Training/evaluation: `finalwm/scripts/train.py`, `finalwm/scripts/eval.py`, `finalwm/scripts/export.py`
- Backend service: `last/backend/app/main.py`
- Frontend client: `last/frontend/src/main.tsx`, `last/frontend/src/App.tsx`
- Standalone demonstrations: `demo/01_weights_check.py` through `demo/07_ai_card_decision.py`

#### Omi Rules and Environment Boundary
The authoritative gameplay model is a Gymnasium/PettingZoo-compatible environment. It manages dealing, trump declaration, action legality, trick resolution, team scoring, termination at five tricks, and action masks for the 36-action space.

Core training-game paths:
- `finalwm/omi_env/env.py`
- `finalwm/omi_env/rules.py`
- `finalwm/omi_env/encoding.py`

`encoding.py` turns game state into the 195-dimensional policy observation. The web backend contains a separately packaged copy of this game implementation, allowing deployed play to run independently of the training project:
- `last/backend/app/game/omi_env/env.py`
- `last/backend/app/game/omi_env/rules.py`
- `last/backend/app/game/omi_env/encoding.py`

#### MARL Training Pipeline
Training uses MAPPO: multiple agents interact with vectorized Omi environments, collect rollout trajectories, and update shared policy/value networks with masked legal actions. Training configuration controls parallelism, device, schedules, and episode limits.

Architecture-defining stages:
1. Vector environment orchestration: `finalwm/marl/vector_env.py`
2. Rollout/buffer storage: `finalwm/buffer.py`
3. MAPPO/PPO optimization and checkpoint lifecycle: `finalwm/marl/r_mappo.py`
4. Actor policy and action distribution: `finalwm/models/policy.py`
5. Centralized value estimation: `finalwm/models/critic.py`

`finalwm/scripts/train.py` composes these layers from YAML configs such as `finalwm/configs/default.yaml`. Baseline opponents provide comparative evaluation rather than core serving behavior:
- `finalwm/baselines/random_agent.py`
- `finalwm/baselines/rule_based_agent.py`

#### Model Export and Runtime Inference
A trained policy checkpoint is transformed into a deployment artifact by `finalwm/scripts/export.py`. The backend ships its own model wrapper and serialized weights:
- `last/backend/rl_model/omi_agent.py`
- `last/backend/rl_model/weights.pt`

At runtime, bot management obtains a legal game-state representation, invokes the PyTorch policy wrapper, and applies the selected card or trump action through the room/game manager. This is the key boundary between ML inference and server-authoritative game state:
- `last/backend/app/ai/bot_manager.py`
- `last/backend/app/game/room_manager.py`

#### Backend Service, State, and Real-Time Flow
FastAPI exposes REST endpoints for authentication, rooms, and game state, while WebSockets push interactive updates. The room manager owns live game-session lifecycle; it should remain the coordination point for human moves, bot turns, state transitions, and broadcasts.

Key paths:
- HTTP app/bootstrap and CORS: `last/backend/app/main.py`
- REST and JWT auth: `last/backend/app/api/routes.py`, `last/backend/app/api/auth.py`
- WebSocket handlers and connection registry: `last/backend/app/ws/sockets.py`, `last/backend/app/ws/connection.py`
- SQLAlchemy persistence: `last/backend/app/db/database.py`, `last/backend/app/db/models.py`

Infrastructure includes Uvicorn, SQLAlchemy, and SQLite or PostgreSQL selected through `DB_URL`. JWT secrets and allowed browser origins are environment-driven.

#### Frontend Client and External Boundaries
The frontend is a Vite React/TypeScript application. Pages handle auth, lobby/room navigation, gameplay, profiles, and history; a shared game-state hook/store bridges REST initialization and WebSocket-driven updates.

Core client paths:
- `last/frontend/src/pages/Game.tsx`
- `last/frontend/src/hooks/useGameState.ts`
- `last/frontend/src/store/gameStore.ts`
- `last/frontend/src/lib/api.ts`

The client connects to backend HTTP and WebSocket URLs via environment variables. It also includes a Supabase integration boundary at `last/frontend/src/lib/supabase.ts`; authentication behavior should be traced alongside the backend JWT endpoints before modifying identity flows.

Main entry points: [`last/backend/app/game/room_manager.py`](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/game/room_manager.py) (Room manager), [`last/backend/app/api/routes.py`](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/api/routes.py) (REST and JWT API), [`last/backend/app/ws/sockets.py`](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/ws/sockets.py) (WebSocket gateway), [`finalwm/marl/r_mappo.py`](https://github.com/wathsara02/pusl3190/blob/main/finalwm/marl/r_mappo.py) (MAPPO optimizer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_training["Offline MARL"]
  node_train["Train / eval entrypoints<br/>Python scripts<br/>[train.py]"]
  node_env["Omi training environment<br/>PettingZoo environment<br/>[env.py]"]
  node_vector_env["Vector environments<br/>environment orchestration<br/>[vector_env.py]"]
  node_buffer[("Rollout buffer<br/>trajectory storage<br/>[buffer.py]")]
  node_mappo["MAPPO optimizer<br/>PPO training<br/>[r_mappo.py]"]
  node_policy["Actor policy<br/>PyTorch policy<br/>[policy.py]"]
  node_critic["Centralized critic<br/>value network<br/>[critic.py]"]
  node_export["Model export<br/>deployment artifact builder<br/>[export.py]"]
end

subgraph group_backend["Live Backend"]
  node_main["FastAPI application<br/>HTTP service bootstrap<br/>[main.py]"]
  node_api["REST and JWT API<br/>HTTP routes<br/>[routes.py]"]
  node_websocket["WebSocket gateway<br/>real-time handlers<br/>[sockets.py]"]
  node_rooms["Room manager<br/>game-session coordinator<br/>[room_manager.py]"]
  node_live_env["Deployed Omi environment<br/>game engine<br/>[env.py]"]
  node_bots["Bot manager<br/>inference coordinator<br/>[bot_manager.py]"]
  node_runtime_model["Runtime policy and weights<br/>PyTorch inference<br/>[omi_agent.py]"]
  node_database[("Game persistence<br/>SQLAlchemy database<br/>[database.py]")]
end

subgraph group_frontend["React Client"]
  node_client_app["React application<br/>Vite client entry<br/>[App.tsx]"]
  node_game_ui["Game page and state<br/>gameplay UI<br/>[useGameState.ts]"]
  node_client_api["Backend API client<br/>HTTP/WebSocket client<br/>[api.ts]"]
  node_supabase{{"Supabase integration<br/>external identity boundary<br/>[supabase.ts]"}}
end

node_train -->|"creates"| node_vector_env
node_vector_env -->|"runs parallel games"| node_env
node_env -->|"observations and rollouts"| node_buffer
node_buffer -->|"training batches"| node_mappo
node_mappo -->|"optimizes"| node_policy
node_mappo -->|"optimizes"| node_critic
node_policy -->|"checkpoint"| node_export
node_export -->|"deploys weights"| node_runtime_model
node_main -->|"serves"| node_api
node_main -->|"serves"| node_websocket
node_api -->|"room and game commands"| node_rooms
node_api -->|"persists users and games"| node_database
node_websocket -->|"live move events"| node_rooms
node_rooms -->|"broadcasts state"| node_websocket
node_rooms -->|"applies game actions"| node_live_env
node_rooms -->|"requests bot turn"| node_bots
node_bots -->|"legal-state inference"| node_runtime_model
node_bots -->|"selected action"| node_rooms
node_client_app -->|"renders"| node_game_ui
node_game_ui -->|"fetches and subscribes"| node_client_api
node_client_api -->|"HTTP"| node_api
node_client_api -->|"WebSocket"| node_websocket
node_client_app -.->|"identity integration"| node_supabase

click node_train "https://github.com/wathsara02/pusl3190/blob/main/finalwm/scripts/train.py"
click node_env "https://github.com/wathsara02/pusl3190/blob/main/finalwm/omi_env/env.py"
click node_vector_env "https://github.com/wathsara02/pusl3190/blob/main/finalwm/marl/vector_env.py"
click node_buffer "https://github.com/wathsara02/pusl3190/blob/main/finalwm/buffer.py"
click node_mappo "https://github.com/wathsara02/pusl3190/blob/main/finalwm/marl/r_mappo.py"
click node_policy "https://github.com/wathsara02/pusl3190/blob/main/finalwm/models/policy.py"
click node_critic "https://github.com/wathsara02/pusl3190/blob/main/finalwm/models/critic.py"
click node_export "https://github.com/wathsara02/pusl3190/blob/main/finalwm/scripts/export.py"
click node_main "https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/main.py"
click node_api "https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/api/routes.py"
click node_websocket "https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/ws/sockets.py"
click node_rooms "https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/game/room_manager.py"
click node_live_env "https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/game/omi_env/env.py"
click node_bots "https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/ai/bot_manager.py"
click node_runtime_model "https://github.com/wathsara02/pusl3190/blob/main/last/backend/rl_model/omi_agent.py"
click node_database "https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/db/database.py"
click node_client_app "https://github.com/wathsara02/pusl3190/blob/main/last/frontend/src/App.tsx"
click node_game_ui "https://github.com/wathsara02/pusl3190/blob/main/last/frontend/src/hooks/useGameState.ts"
click node_client_api "https://github.com/wathsara02/pusl3190/blob/main/last/frontend/src/lib/api.ts"
click node_supabase "https://github.com/wathsara02/pusl3190/blob/main/last/frontend/src/lib/supabase.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_train,node_env,node_vector_env,node_buffer,node_mappo,node_policy,node_critic,node_export toneBlue
class node_main,node_api,node_websocket,node_rooms,node_live_env,node_bots,node_runtime_model,node_database toneAmber
class node_client_app,node_game_ui,node_client_api,node_supabase toneMint
```

## Components

### Offline MARL

Training, evaluation, and policy export.

- [Train / eval entrypoints](https://github.com/wathsara02/pusl3190/blob/main/finalwm/scripts/train.py): Python scripts · `finalwm/scripts/train.py`
- [Omi training environment](https://github.com/wathsara02/pusl3190/blob/main/finalwm/omi_env/env.py): PettingZoo environment · Authoritative rules, state encoding, and legal-action masks. · `finalwm/omi_env/env.py`
- [Vector environments](https://github.com/wathsara02/pusl3190/blob/main/finalwm/marl/vector_env.py): environment orchestration · `finalwm/marl/vector_env.py`
- [Rollout buffer](https://github.com/wathsara02/pusl3190/blob/main/finalwm/buffer.py): trajectory storage · `finalwm/buffer.py`
- [MAPPO optimizer](https://github.com/wathsara02/pusl3190/blob/main/finalwm/marl/r_mappo.py): PPO training · Updates shared policy/value networks and manages checkpoints. · `finalwm/marl/r_mappo.py`
- [Actor policy](https://github.com/wathsara02/pusl3190/blob/main/finalwm/models/policy.py): PyTorch policy · Produces masked actions over the 36-action space. · `finalwm/models/policy.py`
- [Centralized critic](https://github.com/wathsara02/pusl3190/blob/main/finalwm/models/critic.py): value network · `finalwm/models/critic.py`
- [Model export](https://github.com/wathsara02/pusl3190/blob/main/finalwm/scripts/export.py): deployment artifact builder · `finalwm/scripts/export.py`

### Live Backend

Authoritative game sessions and bot inference.

- [FastAPI application](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/main.py): HTTP service bootstrap · Configures REST, WebSocket, CORS, and runtime service. · `last/backend/app/main.py`
- [REST and JWT API](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/api/routes.py): HTTP routes · `last/backend/app/api/routes.py`
- [WebSocket gateway](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/ws/sockets.py): real-time handlers · `last/backend/app/ws/sockets.py`
- [Room manager](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/game/room_manager.py): game-session coordinator · Owns turns, state transitions, bot turns, and broadcasts. · `last/backend/app/game/room_manager.py`
- [Deployed Omi environment](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/game/omi_env/env.py): game engine · Separate packaged game implementation for live play. · `last/backend/app/game/omi_env/env.py`
- [Bot manager](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/ai/bot_manager.py): inference coordinator · `last/backend/app/ai/bot_manager.py`
- [Runtime policy and weights](https://github.com/wathsara02/pusl3190/blob/main/last/backend/rl_model/omi_agent.py): PyTorch inference · `last/backend/rl_model/omi_agent.py`
- [Game persistence](https://github.com/wathsara02/pusl3190/blob/main/last/backend/app/db/database.py): SQLAlchemy database · SQLite or PostgreSQL via DB_URL. · `last/backend/app/db/database.py`

### React Client

Browser gameplay and identity integration.

- [React application](https://github.com/wathsara02/pusl3190/blob/main/last/frontend/src/App.tsx): Vite client entry · `last/frontend/src/App.tsx`
- [Game page and state](https://github.com/wathsara02/pusl3190/blob/main/last/frontend/src/hooks/useGameState.ts): gameplay UI · Bridges REST initialization and WebSocket updates. · `last/frontend/src/hooks/useGameState.ts`
- [Backend API client](https://github.com/wathsara02/pusl3190/blob/main/last/frontend/src/lib/api.ts): HTTP/WebSocket client · `last/frontend/src/lib/api.ts`
- [Supabase integration](https://github.com/wathsara02/pusl3190/blob/main/last/frontend/src/lib/supabase.ts): external identity boundary · Trace alongside backend JWT flows before changing authentication. · `last/frontend/src/lib/supabase.ts`

## Connections

- Train / eval entrypoints → Vector environments: creates
- Vector environments → Omi training environment: runs parallel games
- Omi training environment → Rollout buffer: observations and rollouts
- Rollout buffer → MAPPO optimizer: training batches
- MAPPO optimizer → Actor policy: optimizes
- MAPPO optimizer → Centralized critic: optimizes
- Actor policy → Model export: checkpoint
- Model export → Runtime policy and weights: deploys weights
- FastAPI application → REST and JWT API: serves
- FastAPI application → WebSocket gateway: serves
- REST and JWT API → Room manager: room and game commands
- REST and JWT API → Game persistence: persists users and games
- WebSocket gateway → Room manager: live move events
- Room manager → WebSocket gateway: broadcasts state
- Room manager → Deployed Omi environment: applies game actions
- Room manager → Bot manager: requests bot turn
- Bot manager → Runtime policy and weights: legal-state inference
- Bot manager → Room manager: selected action
- React application → Game page and state: renders
- Game page and state → Backend API client: fetches and subscribes
- Backend API client → REST and JWT API: HTTP
- Backend API client → WebSocket gateway: WebSocket
- React application → Supabase integration: identity integration
