How sourcelocation/geoduels works
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: runtime.go Gameplay node · app.go API service · store.go PostgreSQL · game-controller.ts Client controllers
Components 19
Web client
- Next.js routes · Lobby and match pages compose feature UI.
apps/web/pages/index.tsx - Home model · Assembles lobby and game-facing client state.
apps/web/features/home/model/useHomeModel.ts - Client controllers · Isolate session, matchmaking route, and game transport behavior.
apps/web/features/game/controllers/game-controller.ts - Runtime config
apps/web/public/runtime-config.js
Realtime and coordination
- API service · Owns auth, profiles, maps, sessions, history, and administration.
services/api/app.go - Match coordinator · Owns queues, parties, presence, node assignment, and tickets.
services/match-coordinator/main.go - Realtime gateway · Routes assigned gameplay sockets to nodes.
services/realtime-gateway/main.go - Session policy
pkg/sessionpolicy/policy.go
Gameplay
- Gameplay node · Executes live matches and broadcasts snapshots.
services/gameplay-node/runtime.go - Duel engine · Applies duel rules and scoring.
pkg/duel/engine.go - Persisted match plans · Locks maps and creates bounded immutable round plans.
pkg/persistence/map_match_plans.go
Data and async
- PostgreSQL · Source of truth for application, map, match, replay, and moderation data.
pkg/persistence/store.go - Redis · Stores coordination and gameplay-node ownership state.
- Moderation worker · Projects and enforces moderation concerns.
services/moderation-worker/main.go - Discord worker · Synchronizes Discord roles and badges.
services/discord-worker/main.go - Storage maintenance · Compresses replays and performs retention cleanup.
workers/storage-maintenance/main.go
Platform and integrations
- Kubernetes / k3s · Independently deploys web, services, gameplay nodes, and workers.
infra/k3s/base/kustomization.yaml - Google OIDC
pkg/auth/google_oidc.go - Discord
services/api/discord_oauth_handlers.go
Connections 24
- 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