# clxxiii/osu-bingo architecture

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

- Interactive diagram: https://gitdiagram.com/clxxiii/osu-bingo
- Repository: https://github.com/clxxiii/osu-bingo

## Overview

1. **Overall shape**
   - This is a **SvelteKit** application written in **TypeScript**, built and run with **Bun**.
   - It is not just a frontend: the repo contains the **web UI**, **API endpoints**, **server-side game logic**, **database access**, and **background/game-stream integration** for osu! Bingo.

2. **Primary user-facing surfaces**
   - The app is split into route groups:
     - **`(main)`**: the core product UI for games, templates, login, and the home page.
     - **`(overlays)`**: stream-overlay pages, especially the board overlay.
     - **`(api)`**: backend endpoints for auth, game stream control, and utility calls.
   - The UI is mostly composed from reusable Svelte components under `src/lib/components`, which act as the presentation layer for cards, lists, chat, timers, boards, settings, and overlays.

3. **Core domain model**
   - The central domain is **osu! Bingo game management**: games, templates, maps, squares, teams, scores, events, chat, users, sessions, and tokens.
   - The README’s gameplay loop is implemented around **claiming squares** on a bingo board, reclaiming them with better scores, and winning by completing lines.
   - Board presets are explicit in `src/lib/bingo-helpers/boards` for **3x3, 5x5, and 7x7** boards, with helper logic for win detection and “claimworthy” checks.

4. **Game rules and claim logic**
   - `src/lib/bingo-helpers` and `src/lib/gamerules` contain the rule engine for:
     - evaluating whether a score can claim/reclaim a square,
     - computing the best score,
     - checking wins,
     - deriving rule meaning/options.
   - This is the core gameplay boundary: the UI displays state, but these helpers determine the actual game outcomes.

5. **Persistence layer**
   - The data layer is built on **Drizzle ORM** with schema and migrations under `src/lib/drizzle`.
   - Query modules are split by domain:
     - `user`, `session`, `token`
     - `game`, `gameuser`, `square`, `score`
     - `map`, `mappool`, `template`
     - `event`, `chat`
   - This separation implies a service/repository boundary where route handlers and server services call domain-specific query modules rather than touching SQL directly.

6. **Auth and session flow**
   - Authentication is centered on **osu! account login** via API routes under `src/routes/(api)/auth/...`.
   - The app stores and reads login/session state server-side through the `session` and `token` data modules.
   - `src/routes/(api)/login_request` suggests a login-request handoff flow, likely used to coordinate browser login state with server-side auth/session creation.

7. **Live game ingestion and updates**
   - The live gameplay pipeline is handled by `src/lib/server/game` and related endpoints:
     - start/final-call/rule-change/update-score/watch logic,
     - automatic deletion and state management.
   - `src/routes/(api)/game_stream` and its subroutes for channel/game changes indicate a live stream of game state into the UI.
   - `src/lib/emitter` suggests an internal publish/update mechanism, while `src/lib/server/rabbit.ts` indicates a queue/messaging integration for asynchronous coordination.

8. **External osu! integration**
   - The server has a dedicated osu integration layer in `src/lib/server/osu.ts`, plus Bancho-specific support in `src/lib/server/bancho`.
   - The README confirms scores can come from **osu!stable** and **osu!lazer**, so the backend likely normalizes multiple score sources before applying bingo rules.
   - `bancho_board.ts` points to an integration boundary with osu! chat/lobby or match events, which is probably how live game activity is observed or controlled.

9. **Polling and scheduling**
   - `src/lib/server/polling` contains an event scheduler and token handling, which suggests periodic background work for:
     - refreshing game state,
     - syncing external data,
     - managing expiring tokens or timed game events.
   - This is a separate operational path from the request/response routes: the app has both **interactive HTTP flows** and **scheduled/background updates**.

10. **Streaming overlays**
   - The `(overlays)` route group is a distinct read-only presentation layer for live broadcast use.
   - The board overlay page likely consumes the same game state as the main UI but presents it in a minimal, stream-friendly format that updates as the game progresses.
   - This is an important boundary: overlay pages depend on the same game data model, but are optimized for display rather than control.

11. **Route-level responsibilities**
   - `(main)` pages handle the normal product experience: home, game detail, game list, templates, and creation flows.
   - API routes handle stateful operations like login/logout, game stream control, retrieving a user’s game, and closing login requests.
   - The application uses SvelteKit server-side load/actions heavily, so many state transitions happen on the server rather than in a separate backend service.

12. **Shared UI and app shell**
   - `src/lib/components` acts as a shared design system for the entire app: navigation, cards, lists, chat, timers, controls, board rendering, and game summaries.
   - `src/routes/(main)/+layout.*` and `src/routes/+layout.server.ts` define the application shell and server-provided global state.
   - `src/hooks.server.ts` is the top-level request hook boundary for cross-cutting server concerns such as auth, redirects, or request context.

13. **Operational/deployment context**
   - The presence of `Dockerfile`, `docker-compose.yml`, and GitHub Actions workflows shows the app is containerized and CI-tested.
   - Drizzle migrations are committed, so database schema evolution is part of the deployment workflow.
   - Overall, this repo is a **stateful, server-driven game platform** with a live-update architecture: database as source of truth, server game logic as the arbiter, and SvelteKit pages/components as the presentation layer.

Main entry points: [`src/lib/server/game`](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/server/game) (Game service), [`src/lib/drizzle/queries`](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/drizzle/queries) (Queries), [`src/routes/(api)`](https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(api)) (API routes), [`src/routes/(api)/auth`](https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(api)/auth) (Auth flow)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_presentation["Presentation"]
  node_src_routes_main["Main app<br/>route group"]
  node_src_routes_overlays_board["Board overlay<br/>overlay page"]
  node_src_lib_components["UI kit<br/>svelte components"]
end

subgraph group_api["API"]
  node_src_routes_api["API routes<br/>route group"]
  node_src_routes_api_auth["Auth flow<br/>endpoint group"]
  node_src_routes_api_game_stream["Game stream<br/>endpoint group"]
  node_src_routes_api_login_request["Login request<br/>endpoint<br/>[+server.ts]"]
  node_src_routes_api_get_user_game["User game lookup<br/>endpoint<br/>[+server.ts]"]
end

subgraph group_domain["Domain"]
  node_src_lib_bingo_helpers{{"Bingo helpers<br/>rules helpers"}}
  node_src_lib_gamerules{{"Rule engine<br/>game rules"}}
end

subgraph group_server["Server"]
  node_src_lib_server_game["Game service<br/>server service"]
  node_src_lib_server_bancho["Bancho bridge<br/>osu integration"]
  node_src_lib_server_osu["osu client<br/>external API<br/>[osu.ts]"]
  node_src_lib_server_polling["Polling jobs<br/>background jobs"]
  node_src_lib_emitter(("Emitter<br/>pub-sub"))
end

subgraph group_data["Data"]
  node_src_lib_drizzle_schema[("Schema<br/>[schema.ts]")]
  node_src_lib_drizzle_queries["Queries<br/>repository layer"]
  node_src_lib_drizzle_migrations["Migrations"]
end

subgraph group_platform["Platform"]
  node_src_hooks_server{{"Request hooks<br/>server hook<br/>[hooks.server.ts]"}}
  node_src_routes_layout_server["App state<br/>layout server<br/>[+layout.server.ts]"]
end

node_src_routes_main -->|"renders with"| node_src_lib_components
node_src_routes_overlays_board -->|"uses"| node_src_lib_components
node_src_routes_main -->|"loads state"| node_src_routes_layout_server
node_src_routes_api -->|"contains"| node_src_routes_api_auth
node_src_routes_api -->|"contains"| node_src_routes_api_game_stream
node_src_routes_api -->|"contains"| node_src_routes_api_login_request
node_src_routes_api -->|"contains"| node_src_routes_api_get_user_game
node_src_routes_api_auth -->|"authenticates via"| node_src_lib_server_osu
node_src_routes_api_auth -->|"persists sessions"| node_src_lib_drizzle_queries
node_src_routes_api_game_stream -->|"controls"| node_src_lib_server_game
node_src_routes_api_get_user_game -->|"reads"| node_src_lib_drizzle_queries
node_src_lib_server_game -->|"applies rules"| node_src_lib_bingo_helpers
node_src_lib_server_game -->|"evaluates rules"| node_src_lib_gamerules
node_src_lib_server_game -->|"reads/writes"| node_src_lib_drizzle_queries
node_src_lib_server_game -->|"publishes"| node_src_lib_emitter
node_src_lib_server_game -->|"ingests"| node_src_lib_server_bancho
node_src_lib_server_bancho -->|"depends on"| node_src_lib_server_osu
node_src_lib_server_polling -->|"syncs with"| node_src_lib_server_osu
node_src_lib_server_polling -->|"updates"| node_src_lib_drizzle_queries
node_src_lib_emitter -->|"feeds updates"| node_src_routes_api_game_stream
node_src_lib_drizzle_queries -->|"built on"| node_src_lib_drizzle_schema
node_src_lib_drizzle_queries -.->|"evolves with"| node_src_lib_drizzle_migrations
node_src_hooks_server -->|"initializes"| node_src_routes_layout_server
node_src_hooks_server -->|"guards"| node_src_routes_api

click node_src_routes_main "https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(main)"
click node_src_routes_overlays_board "https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(overlays)/overlays/board"
click node_src_lib_components "https://github.com/clxxiii/osu-bingo/tree/main/src/lib/components"
click node_src_routes_api "https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(api)"
click node_src_routes_api_auth "https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(api)/auth"
click node_src_routes_api_game_stream "https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(api)/game_stream"
click node_src_routes_api_login_request "https://github.com/clxxiii/osu-bingo/blob/main/src/routes/(api)/login_request/+server.ts"
click node_src_routes_api_get_user_game "https://github.com/clxxiii/osu-bingo/blob/main/src/routes/(api)/get_user_game/+server.ts"
click node_src_lib_bingo_helpers "https://github.com/clxxiii/osu-bingo/tree/main/src/lib/bingo-helpers"
click node_src_lib_gamerules "https://github.com/clxxiii/osu-bingo/tree/main/src/lib/gamerules"
click node_src_lib_server_game "https://github.com/clxxiii/osu-bingo/tree/main/src/lib/server/game"
click node_src_lib_server_bancho "https://github.com/clxxiii/osu-bingo/tree/main/src/lib/server/bancho"
click node_src_lib_server_osu "https://github.com/clxxiii/osu-bingo/blob/main/src/lib/server/osu.ts"
click node_src_lib_server_polling "https://github.com/clxxiii/osu-bingo/tree/main/src/lib/server/polling"
click node_src_lib_emitter "https://github.com/clxxiii/osu-bingo/tree/main/src/lib/emitter"
click node_src_lib_drizzle_schema "https://github.com/clxxiii/osu-bingo/blob/main/src/lib/drizzle/schema.ts"
click node_src_lib_drizzle_queries "https://github.com/clxxiii/osu-bingo/tree/main/src/lib/drizzle/queries"
click node_src_lib_drizzle_migrations "https://github.com/clxxiii/osu-bingo/tree/main/src/lib/drizzle/migrations"
click node_src_hooks_server "https://github.com/clxxiii/osu-bingo/blob/main/src/hooks.server.ts"
click node_src_routes_layout_server "https://github.com/clxxiii/osu-bingo/blob/main/src/routes/+layout.server.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_src_routes_main,node_src_routes_overlays_board,node_src_lib_components toneBlue
class node_src_routes_api,node_src_routes_api_auth,node_src_routes_api_game_stream,node_src_routes_api_login_request,node_src_routes_api_get_user_game toneAmber
class node_src_lib_bingo_helpers,node_src_lib_gamerules toneMint
class node_src_lib_server_game,node_src_lib_server_bancho,node_src_lib_server_osu,node_src_lib_server_polling,node_src_lib_emitter toneRose
class node_src_lib_drizzle_schema,node_src_lib_drizzle_queries,node_src_lib_drizzle_migrations toneIndigo
class node_src_hooks_server,node_src_routes_layout_server toneTeal
```

## Components

### Presentation

SvelteKit pages and shared UI components.

- [Main app](https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(main)): route group · Core product pages for games, templates, login, and home. · `src/routes/(main)`
- [Board overlay](https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(overlays)/overlays/board): overlay page · Read-only stream overlay for the bingo board. · `src/routes/(overlays)/overlays/board`
- [UI kit](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/components): svelte components · Shared presentation layer for cards, lists, timers, chat, boards, and controls. · `src/lib/components`

### API

HTTP endpoints for auth, game stream control, and lookups.

- [API routes](https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(api)): route group · Backend endpoints for auth, game streaming, and utility calls. · `src/routes/(api)`
- [Auth flow](https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(api)/auth): endpoint group · osu login, callback, logout, and auth state endpoints. · `src/routes/(api)/auth`
- [Game stream](https://github.com/clxxiii/osu-bingo/tree/main/src/routes/(api)/game_stream): endpoint group · Live game channel/game selection endpoints. · `src/routes/(api)/game_stream`
- [Login request](https://github.com/clxxiii/osu-bingo/blob/main/src/routes/(api)/login_request/+server.ts): endpoint · Hand-off endpoint for login request coordination. · `src/routes/(api)/login_request/+server.ts`
- [User game lookup](https://github.com/clxxiii/osu-bingo/blob/main/src/routes/(api)/get_user_game/+server.ts): endpoint · Fetches the active game for a user. · `src/routes/(api)/get_user_game/+server.ts`

### Domain

Game rules and state-transition logic.

- [Bingo helpers](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/bingo-helpers): rules helpers · Claim/reclaim evaluation, win checks, and board utilities. · `src/lib/bingo-helpers`
- [Rule engine](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/gamerules): game rules · Rule meaning and option derivation for gameplay behavior. · `src/lib/gamerules`

### Server

Runtime services that coordinate live games and external integrations.

- [Game service](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/server/game): server service · Starts games, updates scores, applies rule changes, watches state, and handles deletion. · `src/lib/server/game`
- [Bancho bridge](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/server/bancho): osu integration · Bancho-specific match/chat integration for live game events. · `src/lib/server/bancho`
- [osu client](https://github.com/clxxiii/osu-bingo/blob/main/src/lib/server/osu.ts): external API · Server-side integration with osu account and score data. · `src/lib/server/osu.ts`
- [Polling jobs](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/server/polling): background jobs · Scheduled event syncing and token management. · `src/lib/server/polling`
- [Emitter](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/emitter): pub-sub · Internal update propagation and server-side event emission. · `src/lib/emitter`

### Data

Drizzle schema, migrations, and query modules.

- [Schema](https://github.com/clxxiii/osu-bingo/blob/main/src/lib/drizzle/schema.ts): database schema · Drizzle schema definitions and relations. · `src/lib/drizzle/schema.ts`
- [Queries](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/drizzle/queries): repository layer · Domain-specific query modules for users, games, scores, maps, events, and chat. · `src/lib/drizzle/queries`
- [Migrations](https://github.com/clxxiii/osu-bingo/tree/main/src/lib/drizzle/migrations): db migrations · Committed schema evolution for deployment. · `src/lib/drizzle/migrations`

### Platform

App shell and request-level cross-cutting hooks.

- [Request hooks](https://github.com/clxxiii/osu-bingo/blob/main/src/hooks.server.ts): server hook · Top-level request context, auth, and redirect boundary. · `src/hooks.server.ts`
- [App state](https://github.com/clxxiii/osu-bingo/blob/main/src/routes/+layout.server.ts): layout server · Global server-provided state for the application shell. · `src/routes/+layout.server.ts`

## Connections

- Main app → UI kit: renders with · Main pages compose the shared UI kit.
- Board overlay → UI kit: uses · Overlay page reuses board and display components.
- Main app → App state: loads state · Main route group receives global layout data.
- API routes → Auth flow: contains · Auth-related API routes live under the API group.
- API routes → Game stream: contains · Game stream control endpoints live under the API group.
- API routes → Login request: contains · Login request handoff endpoint is part of API surface.
- API routes → User game lookup: contains · User-game lookup endpoint is part of API surface.
- Auth flow → osu client: authenticates via · Login and callback flow depends on osu integration.
- Auth flow → Queries: persists sessions · Auth flow reads and writes session/token state.
- Game stream → Game service: controls · Stream endpoints manipulate live game state.
- User game lookup → Queries: reads · Lookup endpoint queries game ownership/state.
- Game service → Bingo helpers: applies rules · Game service uses claim and win helpers.
- Game service → Rule engine: evaluates rules · Live game service consults rule meaning/options.
- Game service → Queries: reads/writes · Game service persists games, squares, scores, events, and chat.
- Game service → Emitter: publishes · Game changes are emitted for downstream consumers.
- Game service → Bancho bridge: ingests · Game service receives Bancho-driven events.
- Bancho bridge → osu client: depends on · Bancho bridge normalizes osu-side data and identities.
- Polling jobs → osu client: syncs with · Background jobs refresh external osu data and tokens.
- Polling jobs → Queries: updates · Polling jobs persist scheduled state and token changes.
- Emitter → Game stream: feeds updates · Emitter supports live game-stream propagation to clients.
- Queries → Schema: built on · Query modules operate over the shared schema.
- Queries → Migrations: evolves with · Query layer tracks the committed migration history.
- Request hooks → App state: initializes · Request hook sets up context used by the app shell.
- Request hooks → API routes: guards · Cross-cutting server logic applies to API requests.
