# mg-tokyo/qpm-gr architecture

> Architecture diagram of the GitHub repository mg-tokyo/qpm-gr, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mg-tokyo/qpm-gr
- Repository: https://github.com/mg-tokyo/qpm-gr

## Overview

QPM is a browser userscript mod for Magic Garden and related sites, including the browser-based Discord activity. Players load it alongside the game and use its panel for shop, pet, garden, protection, journal, and camera features. The graph highlights the documented restock-to-purchase workflow, shared game-state and catalog layers, and selected major capabilities. README statements support product behavior; source excerpts and the index support only the cited internal links, so unsampled feature wiring is not inferred.

Main entry points: [`src`](https://github.com/mg-tokyo/qpm-gr/tree/master/src) (QPM panel), [`src/ui/shop/restockAlerts`](https://github.com/mg-tokyo/qpm-gr/tree/master/src/ui/shop/restockAlerts) (Restock alerts), [`src/main.ts`](https://github.com/mg-tokyo/qpm-gr/blob/master/src/main.ts) (QPM userscript), [`src/ui/shop/restockAlerts/purchasePipeline.ts`](https://github.com/mg-tokyo/qpm-gr/blob/master/src/ui/shop/restockAlerts/purchasePipeline.ts) (Purchase flow)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime and game data"]
  node_qpm["QPM userscript<br/>[main.ts]"]
  node_initialization["Mod initialization<br/>[init.ts]"]
  node_page_context["Page context<br/>[pageContext.ts]"]
  node_game_state["Game state<br/>[index.ts]"]
  node_catalogs["Game catalogs<br/>[gameCatalogs.ts]"]
  node_panel["QPM panel"]
end

subgraph group_shops["Shop restocks"]
  node_stock_tracking["Stock tracking"]
  node_restock_alerts["Restock alerts"]
  node_purchase["Purchase flow"]
end

subgraph group_player_tools["Player tools"]
  node_pet_management["Pet teams and optimizer"]
  node_activity_log["Activity log"]
  node_protection["Harvest protection"]
end

subgraph group_gameplay["Gameplay features"]
  node_garden_tools["Garden tools"]
  node_journal["Journal checker"]
  node_pity["Bad-luck protection<br/>[pityThresholds.ts]"]
  node_camera["3D camera"]
  node_battleship["Garden Battleship"]
end

subgraph group_support["Shared services"]
  node_diagnostics["Diagnostics"]
  node_storage[("Persistent storage<br/>[storage.ts]")]
  node_timers["Timer manager<br/>[timerManager.ts]"]
  node_sprite_service["Sprite service<br/>[index.ts]"]
end

node_player(("Player"))
node_game["Magic Garden"]

node_player -->|"plays"| node_game
node_player -->|"installs and loads"| node_qpm
node_qpm -->|"starts"| node_initialization
node_qpm -->|"initializes"| node_sprite_service
node_player -->|"opens"| node_panel
node_panel -->|"offers"| node_restock_alerts
node_stock_tracking -->|"predicts restocks for"| node_restock_alerts
node_restock_alerts -->|"starts buy-all"| node_purchase
node_purchase -->|"checks readiness and balance"| node_game_state
node_purchase -->|"gets bag limit"| node_catalogs
node_panel -->|"offers"| node_pet_management
node_panel -->|"offers"| node_activity_log
node_panel -->|"offers"| node_protection
node_panel -->|"offers"| node_garden_tools
node_panel -->|"offers"| node_journal
node_panel -->|"offers"| node_pity
node_panel -->|"offers"| node_camera
node_panel -->|"offers"| node_battleship
node_player -->|"opens for troubleshooting"| node_diagnostics
node_initialization -->|"uses"| node_page_context
node_game_state --> node_catalogs
node_restock_alerts --> node_storage

click node_qpm "https://github.com/mg-tokyo/qpm-gr/blob/master/src/main.ts"
click node_initialization "https://github.com/mg-tokyo/qpm-gr/blob/master/src/main/init.ts"
click node_page_context "https://github.com/mg-tokyo/qpm-gr/blob/master/src/core/pageContext.ts"
click node_game_state "https://github.com/mg-tokyo/qpm-gr/blob/master/src/core/gameState/index.ts"
click node_catalogs "https://github.com/mg-tokyo/qpm-gr/blob/master/src/catalogs/gameCatalogs.ts"
click node_panel "https://github.com/mg-tokyo/qpm-gr/tree/master/src"
click node_stock_tracking "https://github.com/mg-tokyo/qpm-gr/blob/master/src/store/shopStockParsers.ts"
click node_restock_alerts "https://github.com/mg-tokyo/qpm-gr/tree/master/src/ui/shop/restockAlerts"
click node_purchase "https://github.com/mg-tokyo/qpm-gr/blob/master/src/ui/shop/restockAlerts/purchasePipeline.ts"
click node_pet_management "https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/pets"
click node_activity_log "https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/activity/activityLogNativeEnhancer"
click node_protection "https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/locker"
click node_garden_tools "https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/garden"
click node_journal "https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/journal"
click node_pity "https://github.com/mg-tokyo/qpm-gr/blob/master/src/catalogs/pityThresholds.ts"
click node_camera "https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/camera3d"
click node_battleship "https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/battleship"
click node_diagnostics "https://github.com/mg-tokyo/qpm-gr/tree/master/src/diagnostics"
click node_storage "https://github.com/mg-tokyo/qpm-gr/blob/master/src/utils/storage.ts"
click node_timers "https://github.com/mg-tokyo/qpm-gr/blob/master/src/utils/scheduling/timerManager.ts"
click node_sprite_service "https://github.com/mg-tokyo/qpm-gr/blob/master/src/sprite-v2/index.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_qpm,node_initialization,node_page_context,node_game_state,node_catalogs,node_panel toneBlue
class node_stock_tracking,node_restock_alerts,node_purchase toneAmber
class node_pet_management,node_activity_log,node_protection toneMint
class node_garden_tools,node_journal,node_pity,node_camera,node_battleship toneRose
class node_diagnostics,node_storage,node_timers,node_sprite_service,node_player,node_game toneIndigo
```

## Components

### Runtime and game data

- [QPM userscript](https://github.com/mg-tokyo/qpm-gr/blob/master/src/main.ts): `src/main.ts`
- [Mod initialization](https://github.com/mg-tokyo/qpm-gr/blob/master/src/main/init.ts): `src/main/init.ts`
- [Page context](https://github.com/mg-tokyo/qpm-gr/blob/master/src/core/pageContext.ts): `src/core/pageContext.ts`
- [Game state](https://github.com/mg-tokyo/qpm-gr/blob/master/src/core/gameState/index.ts): `src/core/gameState/index.ts`
- [Game catalogs](https://github.com/mg-tokyo/qpm-gr/blob/master/src/catalogs/gameCatalogs.ts): `src/catalogs/gameCatalogs.ts`
- [QPM panel](https://github.com/mg-tokyo/qpm-gr/tree/master/src): `src`

### Shop restocks

- [Stock tracking](https://github.com/mg-tokyo/qpm-gr/blob/master/src/store/shopStockParsers.ts): `src/store/shopStockParsers.ts`
- [Restock alerts](https://github.com/mg-tokyo/qpm-gr/tree/master/src/ui/shop/restockAlerts): `src/ui/shop/restockAlerts`
- [Purchase flow](https://github.com/mg-tokyo/qpm-gr/blob/master/src/ui/shop/restockAlerts/purchasePipeline.ts): `src/ui/shop/restockAlerts/purchasePipeline.ts`

### Player tools

- [Pet teams and optimizer](https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/pets): `src/features/pets`
- [Activity log](https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/activity/activityLogNativeEnhancer): `src/features/activity/activityLogNativeEnhancer`
- [Harvest protection](https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/locker): `src/features/locker`

### Gameplay features

- [Garden tools](https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/garden): `src/features/garden`
- [Journal checker](https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/journal): `src/features/journal`
- [Bad-luck protection](https://github.com/mg-tokyo/qpm-gr/blob/master/src/catalogs/pityThresholds.ts): `src/catalogs/pityThresholds.ts`
- [3D camera](https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/camera3d): `src/features/camera3d`
- [Garden Battleship](https://github.com/mg-tokyo/qpm-gr/tree/master/src/features/battleship): `src/features/battleship`

### Shared services

- [Diagnostics](https://github.com/mg-tokyo/qpm-gr/tree/master/src/diagnostics): `src/diagnostics`
- [Persistent storage](https://github.com/mg-tokyo/qpm-gr/blob/master/src/utils/storage.ts): `src/utils/storage.ts`
- [Timer manager](https://github.com/mg-tokyo/qpm-gr/blob/master/src/utils/scheduling/timerManager.ts): `src/utils/scheduling/timerManager.ts`
- [Sprite service](https://github.com/mg-tokyo/qpm-gr/blob/master/src/sprite-v2/index.ts): `src/sprite-v2/index.ts`

### Other components

- **Player**
- **Magic Garden**

## Connections

- Player → Magic Garden: plays
- Player → QPM userscript: installs and loads
- QPM userscript → Mod initialization: starts (evidence: `src/main.ts`)
- QPM userscript → Sprite service: initializes (evidence: `src/main.ts`)
- Player → QPM panel: opens
- QPM panel → Restock alerts: offers (evidence: `src/ui/shop/restockAlerts/purchasePipeline.ts`)
- Stock tracking → Restock alerts: predicts restocks for
- Restock alerts → Purchase flow: starts buy-all (evidence: `src/ui/shop/restockAlerts/purchasePipeline.ts`)
- Purchase flow → Game state: checks readiness and balance (evidence: `src/ui/shop/restockAlerts/purchasePipeline.ts`)
- Purchase flow → Game catalogs: gets bag limit (evidence: `src/ui/shop/restockAlerts/purchasePipeline.ts`)
- QPM panel → Pet teams and optimizer: offers
- QPM panel → Activity log: offers
- QPM panel → Harvest protection: offers
- QPM panel → Garden tools: offers (evidence: `src/features/economy/storageValue.ts`)
- QPM panel → Journal checker: offers
- QPM panel → Bad-luck protection: offers
- QPM panel → 3D camera: offers
- QPM panel → Garden Battleship: offers
- Player → Diagnostics: opens for troubleshooting
- Mod initialization → Page context: uses (evidence: `src/core/pageContext.ts`)
- Game state → Game catalogs
- Restock alerts → Persistent storage
