# bbc/genie architecture

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

- Interactive diagram: https://gitdiagram.com/bbc/genie
- Repository: https://github.com/bbc/genie

## Overview

##### Project type and purpose
- This is a pure-frontend framework (no custom backend) for building modular children’s games.
- The README calls it “BBC Games – Genie,” a library built with JavaScript/Phaser that provides core services (loader, layout, event bus, accessibility, debug) and pluggable UI components (home, how-to-play, game, narrative, select, shop, results).
- It includes build/CI scripts (webpack, Babel, license checker, JSDoc), unit tests (Jest), end-to-end tests (Cypress), theming assets, and developer documentation.

##### Top-level directories and their roles
- build-scripts: CI/build toolchain (webpack config, docs generator, license/schema validators, theme generator).
- dev: local development server assets and custom CSS, sample HTML files, helper scripts.
- docs: design/development documentation (Markdown, Sketch).
- lib: third-party libraries bundled in (basisu, lodash, polyfills).
- src: the application code itself
- src/core: core framework services (loader, event bus, accessibility, layout, debug, GMI, background, scaler, music).
- src/components: game screens and UI modules (home, how-to-play, narrative, select, shop, results overlays).
- src/main.js: application entry point.
- test: Jest unit tests mirroring src/core and src/components, plus Cypress fixtures/plugins/e2e suites for end-to-end.
- output (not shown but created by build): compiled bundles + stats.html for visualizer.

##### Key architectural patterns
- Modular component architecture: core services are decoupled from UI screens via an event bus.
- Build pipeline: Babel + webpack bundle source + lib + polyfills + themes into a single distributable.
- Theme-driven design: static JSON asset packs drive graphics and configuration, loaded at runtime by core/loader.
- Test automation: Jest for unit tests, Cypress for integration/e2e against a local or deployed build.
- Documentation pipeline: JSDoc + markdown → docs/api and docs/**/*.md.

##### Main components
A. Build & CI Layer
- build-scripts (webpack, Babel, schema-validator, license checker, theme generator)
- npm CLI (scripts: start, build, docs, create-theme2, test, cy:local, etc.)
- output folder (bundled JS/CSS, stats.html)
B. Development Server
- dev static files (index.dev.html / index.main.html)
- webpack-dev-server (npm start)
C. Runtime Frontend
1. Entry Point: src/main.js
2. Core Framework Modules (src/core)
- Loader (JSON5, particles, fonts, basisu, media)
- Event bus
- Layout engine (grid, DOM text, sprite/text/tweens wrappers)
- Accessibility layer
- Debug tools (console output, UI overlay)
- GMI (“game measurement interface”)
- State/config parser (parse-url-params, settings, custom-styles)
3. UI Components (src/components)
- Scenes: home, how-to-play, narrative, game, select, shop, results
- Overlays & sub-components (pause, results-rows, scrollable lists, confirm dialogs)
4. Themes & Assets (themes/default, theme2)
- Config JSON → loader → core services
- Media files (images, audio, videos, spine animations)
5. Third-party libs (lib/basisu, lib/lodash, polyfills)
D. Testing & Documentation
- Jest Runner (unit tests under test/)
- Cypress Runner (e2e tests under cypress/)
- JSDoc generator (outputs docs/api)
- Markdown docs (docs/ and dev/docs/)

##### Relationships and data flow
- Developer invokes npm script → build-scripts spawn webpack → consumes src/, lib/, themes/ → emits output bundle + stats.
- Browser loads index.html → main.js bootstraps core/startup → loader pulls theme JSON + assets → initializes Phaser game instance.
- Event bus glues core services to UI components: screens register event handlers, dispatch user actions, and forward to core/store.
- UI components import core services (layout, accessibility, debug) and present game screens.
- Tests import modules directly (unit) or drive the built output in a headless browser (Cypress).
- JSDoc parses src/core and src/components comments to build API site.

##### Architectural patterns/design principles
- Separation of concerns: build pipeline, core services, UI components, themes, tests, docs all live in dedicated directories.
- Inversion of Control: UI screens plug into the core event bus and loader services.
- Configuration-driven: runtime behaviour and assets controlled entirely via JSON theme files.
- Extensibility: new components or themes can be added without altering core logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Build & CI Layer
    subgraph "Build & CI Layer"
        direction TB
        npmCLI["npm CLI<br/>(start, build, docs, test)"]:::build
        webpack["webpack + Babel"]:::build
        license["License Checker"]:::build
        schema["Schema Validator"]:::build
        theme2["Theme2 Creator (shell)"]:::build
        npmCLI --> webpack
        npmCLI --> license
        npmCLI --> schema
        npmCLI --> theme2
        webpack --> output(("Output Bundle + stats.html")):::asset
    end

    %% Development Server
    subgraph "Development Server"
        direction TB
        indexDev["index.dev.html"]:::build
        indexMain["index.main.html"]:::build
        webConfig["web-config.js"]:::build
        devCSS["dev.css"]:::build
        indexDev --> webpack
        indexMain --> webpack
    end

    %% Runtime Frontend
    subgraph "Runtime Frontend"
        direction TB
        entry["src/main.js"]:::runtime
        subgraph "Core Framework Modules"
            direction TB
            loader["Loader"]:::runtime
            eventBus["Event Bus"]:::runtime
            layout["Layout Engine"]:::runtime
            accessibility["Accessibility Layer"]:::runtime
            debug["Debug Tools"]:::runtime
            gmi["GMI"]:::runtime
            parser["URL Parser & Settings"]:::runtime
            scaler["Scaler"]:::runtime
            entry --> loader
            loader --> eventBus
            eventBus --> layout
            layout --> accessibility
            accessibility --> debug
            debug --> gmi
            gmi --> parser
            parser --> scaler
        end
        subgraph "UI Components"
            direction TB
            home["Home Screen"]:::runtime
            howTo["How-to-Play"]:::runtime
            narrative["Narrative"]:::runtime
            game["Game Screen"]:::runtime
            select["Select Screen"]:::runtime
            shop["Shop Module"]:::runtime
            results["Results Screen"]:::runtime
            overlays["Pause Overlay"]:::runtime
            eventBus <--> home
            eventBus <--> howTo
            eventBus <--> narrative
            eventBus <--> game
            eventBus <--> select
            eventBus <--> shop
            eventBus <--> results
            eventBus <--> overlays
        end
        subgraph "Themes & Assets"
            direction TB
            themes(("Theme JSON & Media")):::asset
            loader --> themes
        end
        subgraph "Third-party Libraries"
            direction TB
            basisu["BasisU Lib"]:::lib
            lodash["Lodash"]:::lib
            polyfills["Polyfills"]:::lib
            entry --> basisu
            entry --> lodash
            entry --> polyfills
        end
    end

    %% Testing & Documentation
    subgraph "Testing & Documentation"
        direction TB
        jest[("Jest Runner")]:::test
        cypress[("Cypress Runner")]:::test
        jsdoc[("JSDoc Generator")]:::test
        markdown["Markdown Docs"]:::test
        jest --> testCore
        jest --> testComp
        cypress --> e2e
        jsdoc --> docsApi
    end

    %% Test nodes
    testCore["test/core/event-bus.test.js"]:::test
    testComp["test/components/home.test.js<br/>test/components/results-screen.test.js"]:::test
    e2e["cypress/e2e/*.cy.js"]:::test
    docsApi["docs/api"]:::test

    %% Click Events
    click webpack "https://github.com/bbc/genie/blob/master/build-scripts/webpack.config.js"
    click license "https://github.com/bbc/genie/blob/master/build-scripts/licensechecker/validator.js"
    click schema "https://github.com/bbc/genie/blob/master/build-scripts/schemavalidator/validator.js"
    click theme2 "https://github.com/bbc/genie/blob/master/build-scripts/theme2creator/create.sh"
    click indexDev "https://github.com/bbc/genie/blob/master/dev/index.dev.html"
    click indexMain "https://github.com/bbc/genie/blob/master/dev/index.main.html"
    click webConfig "https://github.com/bbc/genie/blob/master/dev/scripts/web-config.js"
    click devCSS "https://github.com/bbc/genie/blob/master/dev/dev.css"
    click entry "https://github.com/bbc/genie/blob/master/src/main.js"
    click loader "https://github.com/bbc/genie/blob/master/src/core/loader/loader.js"
    click eventBus "https://github.com/bbc/genie/blob/master/src/core/event-bus.js"
    click layout "https://github.com/bbc/genie/blob/master/src/core/layout/layout.js"
    click accessibility "https://github.com/bbc/genie/blob/master/src/core/accessibility/accessibility-layer.js"
    click debug "https://github.com/bbc/genie/blob/master/src/core/debug/debug.js"
    click gmi "https://github.com/bbc/genie/blob/master/src/core/gmi/gmi.js"
    click parser "https://github.com/bbc/genie/blob/master/src/core/parse-url-params.js"
    click scaler "https://github.com/bbc/genie/blob/master/src/core/scaler.js"
    click home "https://github.com/bbc/genie/blob/master/src/components/home.js"
    click howTo "https://github.com/bbc/genie/blob/master/src/components/how-to-play.js"
    click narrative "https://github.com/bbc/genie/blob/master/src/components/narrative.js"
    click game "https://github.com/bbc/genie/blob/master/src/components/game.js"
    click select "https://github.com/bbc/genie/blob/master/src/components/select/select-screen.js"
    click shop "https://github.com/bbc/genie/blob/master/src/components/shop/shop.js"
    click results "https://github.com/bbc/genie/blob/master/src/components/results/results-screen.js"
    click themes "https://github.com/bbc/genie/blob/master/themes/default/config.json"
    click basisu "https://github.com/bbc/genie/blob/master/lib/basisu/worker.js"
    click lodash "https://github.com/bbc/genie/blob/master/lib/lodash/lodash.js"
    click polyfills "https://github.com/bbc/genie/blob/master/lib/polyfills/element-remove-polyfil.js"
    click testCore "https://github.com/bbc/genie/blob/master/test/core/event-bus.test.js"
    click testComp "https://github.com/bbc/genie/blob/master/test/components/home.test.js"
    click e2e "https://github.com/bbc/genie/blob/master/cypress/e2e/genie.cy.js"
    click jsdoc "https://github.com/bbc/genie/blob/master/build-scripts/docs/.jsdoc.js"
    click markdown "https://github.com/bbc/genie/blob/master/docs/development/event-bus.md"

    %% Styles
    classDef build fill:#cce5ff,stroke:#004085,color:#004085
    classDef runtime fill:#d4edda,stroke:#155724,color:#155724
    classDef asset fill:#fff3cd,stroke:#856404,color:#856404
    classDef lib fill:#e2e3e5,stroke:#6c757d,color:#6c757d
    classDef test fill:#e2ccff,stroke:#4a148c,color:#4a148c
```
