# sourcelocation/geoduels architecture

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

- Interactive diagram: https://gitdiagram.com/sourcelocation/geoduels
- Repository: https://github.com/sourcelocation/geoduels

## Overview

#### Purpose and Entry Points
GeoDuels is a multiplayer GeoGuessr-style game centered on user-created maps and real-time duels. Browser entry points are the Next.js pages: lobby at `apps/web/pages/index.tsx` and match route at `apps/web/pages/match/[id].tsx`. Backend executables begin at `services/api/main.go`, `services/match-coordinator/main.go`, `services/realtime-gateway/main.go`, and `services/gameplay-node/main.go`.

#### Web Application and Client Boundaries
`apps/web` is a Node/Next.js browser shell. Its route pages compose feature-owned UI; the home/lobby/game state is primarily assembled in `apps/web/features/home/page/HomePageView.tsx` and `apps/web/features/home/model/useHomeModel.ts`. Client controllers isolate network/session behavior from presentation, notably `apps/web/features/auth/controllers/session-controller.ts`, `apps/web/features/matchmaking/controllers/match-route-controller.ts`, and `apps/web/features/game/controllers/game-controller.ts`.

The web client calls HTTP `/v1` APIs, opens matchmaking WebSockets, then connects to an assigned gameplay route. Runtime browser configuration is externally supplied via `apps/web/public/runtime-config.js`.

#### API and Persistent Application Domain
`services/api` is the HTTP authority for authentication, profiles, maps, parties, match history/session bootstrap, administration, moderation, content, and support. Routing/application setup is in `services/api/app.go`; domain endpoints are separated in `services/api/auth_handlers.go`, `services/api/game_handlers.go`, and `services/api/map_handlers.go`.

PostgreSQL is the durable source of truth, accessed through `pkg/persistence/store.go` and domain repositories such as `pkg/persistence/maps.go`, `pkg/persistence/matches_write.go`, and `pkg/persistence/match_sessions.go`. Schema evolution is migration-driven under `db/migrations/`, including map, session, moderation, chat, rating, and replay storage models.

#### Map Ingestion and Match Planning
Custom-map uploads enter through API map handlers, are validated/normalized, and persist current locations rather than retaining source JSON. Map storage and revision/current-location mechanics are implemented in `pkg/persistence/map_ingest.go`, `pkg/persistence/map_revisions.go`, and `pkg/persistence/map_match_plans.go`.

Before a game starts, the selected map is locked, a bounded complete round plan is persisted, and that plan is sent to gameplay. This deliberately prevents gameplay nodes from loading mutable map catalogs. Official datasets live under `datasets/` and `datasets-config/`; generated thumbnail catalog artifacts are shared in `apps/web/shared/mapthumbnails/`.

#### Matchmaking, Routing, and Authoritative Gameplay
The match coordinator owns queueing, party coordination/chat, presence, node assignment, and issuance of gameplay tickets. It uses shared launch and coordinator primitives in `pkg/matchlaunch/launcher.go` and `pkg/coordinator/state.go`; service handlers are in `services/match-coordinator/party_handlers.go` and `services/match-coordinator/chat_handlers.go`.

The realtime gateway accepts `/ws/{node}` and proxies socket traffic to the assigned node (`services/realtime-gateway/main.go`). `services/gameplay-node` owns authoritative live match execution; its runtime is in `services/gameplay-node/runtime.go` and duel rules/scoring reside in `pkg/duel/engine.go` and `pkg/gameplay/scoring.go`. It broadcasts state snapshots while persistence captures completed matches/replays.

#### State, Reliability, and Background Processing
Redis holds distributed matchmaking/coordination state and gameplay-node ownership; PostgreSQL retains durable state. Session and reconnect policy are represented by `pkg/realtime/session_store.go` and `pkg/sessionpolicy/policy.go`. Services implement drain/readiness behavior so Kubernetes stops assigning new work while active matches or sockets finish.

Asynchronous concerns are isolated: moderation projection/enforcement in `services/moderation-worker/main.go`, Discord role/badge synchronization in `services/discord-worker/main.go`, and replay compression/retention cleanup in `workers/storage-maintenance/main.go`.

#### Infrastructure and External Integrations
Local runtime uses Docker Compose with Go 1.26, PostgreSQL 16, and Redis 7 (`docker-compose.yml`). Production is Kubernetes/k3s with independently deployed API, coordinator, gateway, gameplay nodes, workers, web, and PgBouncer; base manifests are in `infra/k3s/base/kustomization.yaml` and service deployments such as `infra/k3s/base/gameplay-node-deployment.yaml`.

External integrations include Google OIDC (`pkg/auth/google_oidc.go`), Google Street View validation tooling, Cloudflare Turnstile (`services/api/turnstile.go`), and Discord OAuth/worker synchronization (`services/api/discord_oauth_handlers.go`).

Main entry points: [`services/gameplay-node/runtime.go`](https://github.com/sourcelocation/geoduels/blob/main/services/gameplay-node/runtime.go) (Gameplay node), [`services/api/app.go`](https://github.com/sourcelocation/geoduels/blob/main/services/api/app.go) (API service), [`pkg/persistence/store.go`](https://github.com/sourcelocation/geoduels/blob/main/pkg/persistence/store.go) (PostgreSQL), [`apps/web/features/game/controllers/game-controller.ts`](https://github.com/sourcelocation/geoduels/blob/main/apps/web/features/game/controllers/game-controller.ts) (Client controllers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web client"]
  node_web_pages["Next.js routes<br/>browser entry<br/>[index.tsx]"]
  node_home_model["Home model<br/>client state<br/>[useHomeModel.ts]"]
  node_client_controllers["Client controllers<br/>network boundary<br/>[game-controller.ts]"]
  node_runtime_config["Runtime config<br/>browser config<br/>[runtime-config.js]"]
end

subgraph group_edge["Realtime and coordination"]
  node_api["API service<br/>Go HTTP authority<br/>[app.go]"]
  node_match_coordinator["Match coordinator<br/>Go websocket service<br/>[main.go]"]
  node_realtime_gateway["Realtime gateway<br/>WebSocket proxy<br/>[main.go]"]
  node_session_policy["Session policy<br/>reconnect policy<br/>[policy.go]"]
end

subgraph group_gameplay["Gameplay"]
  node_gameplay_node["Gameplay node<br/>authoritative runtime<br/>[runtime.go]"]
  node_duel_engine["Duel engine<br/>rules engine<br/>[engine.go]"]
  node_match_plans["Persisted match plans<br/>round planning<br/>[map_match_plans.go]"]
end

subgraph group_data["Data and async"]
  node_postgres[("PostgreSQL<br/>durable datastore<br/>[store.go]")]
  node_redis[("Redis<br/>distributed state")]
  node_moderation_worker["Moderation worker<br/>background worker<br/>[main.go]"]
  node_discord_worker["Discord worker<br/>background worker<br/>[main.go]"]
  node_storage_maintenance["Storage maintenance<br/>background worker<br/>[main.go]"]
end

subgraph group_platform["Platform and integrations"]
  node_kubernetes{{"Kubernetes / k3s<br/>deployment platform<br/>[kustomization.yaml]"}}
  node_google_oidc["Google OIDC<br/>external identity<br/>[google_oidc.go]"]
  node_discord["Discord<br/>external integration"]
end

node_runtime_config -.->|"configures"| node_web_pages
node_web_pages -->|"renders"| node_home_model
node_home_model -->|"uses"| node_client_controllers
node_client_controllers -->|"HTTP /v1"| node_api
node_client_controllers -->|"matchmaking WebSocket"| node_match_coordinator
node_client_controllers -->|"assigned gameplay socket"| node_realtime_gateway
node_api -->|"reads and writes"| node_postgres
node_api -->|"creates before launch"| node_match_plans
node_api -->|"authenticates"| node_google_oidc
node_api -->|"OAuth"| node_discord
node_match_coordinator -->|"coordination state"| node_redis
node_match_coordinator -->|"assigns node and ticket"| node_gameplay_node
node_realtime_gateway -->|"proxies sockets"| node_gameplay_node
node_session_policy -.->|"governs reconnects"| node_match_coordinator
node_gameplay_node -->|"runs rules"| node_duel_engine
node_match_plans -->|"immutable rounds"| node_gameplay_node
node_gameplay_node -->|"persists results and replays"| node_postgres
node_gameplay_node -->|"node ownership"| node_redis
node_moderation_worker -->|"projects and enforces"| node_postgres
node_discord_worker -->|"reads badge state"| node_postgres
node_discord_worker -->|"synchronizes roles"| node_discord
node_storage_maintenance -->|"maintains replays"| node_postgres
node_kubernetes -.->|"deploys"| node_api
node_kubernetes -.->|"scales independently"| node_gameplay_node

click node_web_pages "https://github.com/sourcelocation/geoduels/blob/main/apps/web/pages/index.tsx"
click node_home_model "https://github.com/sourcelocation/geoduels/blob/main/apps/web/features/home/model/useHomeModel.ts"
click node_client_controllers "https://github.com/sourcelocation/geoduels/blob/main/apps/web/features/game/controllers/game-controller.ts"
click node_runtime_config "https://github.com/sourcelocation/geoduels/blob/main/apps/web/public/runtime-config.js"
click node_api "https://github.com/sourcelocation/geoduels/blob/main/services/api/app.go"
click node_match_coordinator "https://github.com/sourcelocation/geoduels/blob/main/services/match-coordinator/main.go"
click node_realtime_gateway "https://github.com/sourcelocation/geoduels/blob/main/services/realtime-gateway/main.go"
click node_session_policy "https://github.com/sourcelocation/geoduels/blob/main/pkg/sessionpolicy/policy.go"
click node_gameplay_node "https://github.com/sourcelocation/geoduels/blob/main/services/gameplay-node/runtime.go"
click node_duel_engine "https://github.com/sourcelocation/geoduels/blob/main/pkg/duel/engine.go"
click node_match_plans "https://github.com/sourcelocation/geoduels/blob/main/pkg/persistence/map_match_plans.go"
click node_postgres "https://github.com/sourcelocation/geoduels/blob/main/pkg/persistence/store.go"
click node_moderation_worker "https://github.com/sourcelocation/geoduels/blob/main/services/moderation-worker/main.go"
click node_discord_worker "https://github.com/sourcelocation/geoduels/blob/main/services/discord-worker/main.go"
click node_storage_maintenance "https://github.com/sourcelocation/geoduels/blob/main/workers/storage-maintenance/main.go"
click node_kubernetes "https://github.com/sourcelocation/geoduels/blob/main/infra/k3s/base/kustomization.yaml"
click node_google_oidc "https://github.com/sourcelocation/geoduels/blob/main/pkg/auth/google_oidc.go"
click node_discord "https://github.com/sourcelocation/geoduels/blob/main/services/api/discord_oauth_handlers.go"

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_web_pages,node_home_model,node_client_controllers,node_runtime_config toneBlue
class node_api,node_match_coordinator,node_realtime_gateway,node_session_policy toneAmber
class node_gameplay_node,node_duel_engine,node_match_plans toneMint
class node_postgres,node_redis,node_moderation_worker,node_discord_worker,node_storage_maintenance toneRose
class node_kubernetes,node_google_oidc,node_discord toneIndigo
```

## Components

### Web client

- [Next.js routes](https://github.com/sourcelocation/geoduels/blob/main/apps/web/pages/index.tsx): browser entry · Lobby and match pages compose feature UI. · `apps/web/pages/index.tsx`
- [Home model](https://github.com/sourcelocation/geoduels/blob/main/apps/web/features/home/model/useHomeModel.ts): client state · Assembles lobby and game-facing client state. · `apps/web/features/home/model/useHomeModel.ts`
- [Client controllers](https://github.com/sourcelocation/geoduels/blob/main/apps/web/features/game/controllers/game-controller.ts): network boundary · Isolate session, matchmaking route, and game transport behavior. · `apps/web/features/game/controllers/game-controller.ts`
- [Runtime config](https://github.com/sourcelocation/geoduels/blob/main/apps/web/public/runtime-config.js): browser config · `apps/web/public/runtime-config.js`

### Realtime and coordination

- [API service](https://github.com/sourcelocation/geoduels/blob/main/services/api/app.go): Go HTTP authority · Owns auth, profiles, maps, sessions, history, and administration. · `services/api/app.go`
- [Match coordinator](https://github.com/sourcelocation/geoduels/blob/main/services/match-coordinator/main.go): Go websocket service · Owns queues, parties, presence, node assignment, and tickets. · `services/match-coordinator/main.go`
- [Realtime gateway](https://github.com/sourcelocation/geoduels/blob/main/services/realtime-gateway/main.go): WebSocket proxy · Routes assigned gameplay sockets to nodes. · `services/realtime-gateway/main.go`
- [Session policy](https://github.com/sourcelocation/geoduels/blob/main/pkg/sessionpolicy/policy.go): reconnect policy · `pkg/sessionpolicy/policy.go`

### Gameplay

- [Gameplay node](https://github.com/sourcelocation/geoduels/blob/main/services/gameplay-node/runtime.go): authoritative runtime · Executes live matches and broadcasts snapshots. · `services/gameplay-node/runtime.go`
- [Duel engine](https://github.com/sourcelocation/geoduels/blob/main/pkg/duel/engine.go): rules engine · Applies duel rules and scoring. · `pkg/duel/engine.go`
- [Persisted match plans](https://github.com/sourcelocation/geoduels/blob/main/pkg/persistence/map_match_plans.go): round planning · Locks maps and creates bounded immutable round plans. · `pkg/persistence/map_match_plans.go`

### Data and async

- [PostgreSQL](https://github.com/sourcelocation/geoduels/blob/main/pkg/persistence/store.go): durable datastore · Source of truth for application, map, match, replay, and moderation data. · `pkg/persistence/store.go`
- **Redis**: distributed state · Stores coordination and gameplay-node ownership state.
- [Moderation worker](https://github.com/sourcelocation/geoduels/blob/main/services/moderation-worker/main.go): background worker · Projects and enforces moderation concerns. · `services/moderation-worker/main.go`
- [Discord worker](https://github.com/sourcelocation/geoduels/blob/main/services/discord-worker/main.go): background worker · Synchronizes Discord roles and badges. · `services/discord-worker/main.go`
- [Storage maintenance](https://github.com/sourcelocation/geoduels/blob/main/workers/storage-maintenance/main.go): background worker · Compresses replays and performs retention cleanup. · `workers/storage-maintenance/main.go`

### Platform and integrations

- [Kubernetes / k3s](https://github.com/sourcelocation/geoduels/blob/main/infra/k3s/base/kustomization.yaml): deployment platform · Independently deploys web, services, gameplay nodes, and workers. · `infra/k3s/base/kustomization.yaml`
- [Google OIDC](https://github.com/sourcelocation/geoduels/blob/main/pkg/auth/google_oidc.go): external identity · `pkg/auth/google_oidc.go`
- [Discord](https://github.com/sourcelocation/geoduels/blob/main/services/api/discord_oauth_handlers.go): external integration · `services/api/discord_oauth_handlers.go`

## Connections

- Runtime config → Next.js routes: configures
- Next.js routes → Home model: renders
- Home model → Client controllers: uses
- Client controllers → API service: HTTP /v1
- Client controllers → Match coordinator: matchmaking WebSocket
- Client controllers → Realtime gateway: assigned gameplay socket
- API service → PostgreSQL: reads and writes
- API service → Persisted match plans: creates before launch
- API service → Google OIDC: authenticates
- API service → Discord: OAuth
- Match coordinator → Redis: coordination state
- Match coordinator → Gameplay node: assigns node and ticket
- Realtime gateway → Gameplay node: proxies sockets
- Session policy → Match coordinator: governs reconnects
- Gameplay node → Duel engine: runs rules
- Persisted match plans → Gameplay node: immutable rounds
- Gameplay node → PostgreSQL: persists results and replays
- Gameplay node → Redis: node ownership
- Moderation worker → PostgreSQL: projects and enforces
- Discord worker → PostgreSQL: reads badge state
- Discord worker → Discord: synchronizes roles
- Storage maintenance → PostgreSQL: maintains replays
- Kubernetes / k3s → API service: deploys
- Kubernetes / k3s → Gameplay node: scales independently
