BlueGit

blue-star tribegen

Files

tribegen / web / .github / copilot-instructions.md

copilot-instructions.md

initial generation
timfa Tim · pushed by timfa · 9 months ago
0b655138 History
copilot-instructions.md 5.4 KiB Download Raw

TribeGen - AI Coding Agent Instructions

Architecture Overview

TribeGen is a browser-based strategy game where players manage a Kobold tribe. The codebase uses file-based JSON storage (refactored from SQLite) with PHP backends and vanilla JavaScript frontend.

Storage Architecture

  • Authentication: SQLite database (users table only) — see src/Database/Database.php
  • Game State: JSON files organized by user → save → entities (tribe, kobolds, items, NPCs)
  • SaveManager: Single abstraction for all file I/O — see src/System/SaveManager.php
  • Path pattern: data/saves/{userId}/{saveId}/ containing save.json, kobolds.json, items.json, npcs.json

Critical Pattern: All game classes use fromArray()/toArray() for JSON serialization. Changes to class properties require updating both methods.

Core Entity Classes

1. Tribe (src/Classes/Tribe.php): Manages kobolds, food/storage, month/season progression, loads/saves via SaveManager 2. Kobold (src/Classes/Kobold.php): Individual stats (vitality, intelligence, tinkering, medicine, crafting, personality, will), injuries, items, genetic lineage 3. Item (src/Classes/Item.php): Named items with stat modifications (each mod is -5 to +5 per stat) 4. NPC (src/Classes/NPC.php): Non-player characters/tribes (extensible via JSON data field) 5. Injury (src/Classes/Injury.php): Injury type, category (wound/fracture), severity 0-10

Event System

  • Base Class: Event — abstract execute() method that modifies tribe state and sets $this->story
  • EventManager (src/Events/EventManager.php): Loads enabled events per save, executes them
  • Implementations: HuntingEvent, ExplorationEvent, FarmingEvent
  • Creating New Events: Extend Event, implement execute(), return narrative story via $this->story
  • Example: ExplorationEvent — shows stat-based mechanics (intelligence affects food discovery, vitality reduces injury chance), food addition via $tribe->addFood(), and injury creation as array appended to kobold's injuries list

API Endpoints

All endpoints in src/api/game.php require userId and saveId in POST body:

  • POST /api/game/generation: Create new tribe with 20 random kobolds
  • POST /api/game/load?saveId=X: Load tribe state
  • POST /api/game/save: Save tribe setup (currently a TODO)
  • POST /api/game/event: Execute single event
  • POST /api/game/endMonth: Progress month, age kobolds, apply injuries, execute month events

Pattern: All responses use { success: boolean, error?: string, ...data }

Key Conventions

IDs and Naming

  • User IDs: Database user.id (integer)
  • Save IDs: Numeric string generated by SaveManager::generateSaveId()
  • Kobold IDs: Prefixed with kob_ + uniqid (example: kob_678abc123)
  • Method Names: Use camelCase, boolean getters are is*() or get*() followed by value

Stats System

  • All stats are 0-100 internally, displayed as text ranges (0-10: "terrible", 90-100: "godlike")
  • Stat checks: Get base value → apply item modifications → check result
  • Items can boost/reduce any stat independently

Kobold Lifecycle

  • Age Tiers: egg → baby → adult → old → very_old (affects food consumption and sprite)
  • Sprite Morphs: Loaded from assets/sprites/{morph}/ directory, copied to save folder on generation
  • Genetic Inheritance: Each kobold tracks fatherId, motherId for generation tracking

Food System

  • Tribe::foodCount tracks stored food
  • Age tiers have food requirements (egg: 0, baby: 0.5, adult: 1.0, old: 0.6, very_old: 0.4 units)
  • endMonth() deducts food and reports shortage via story

Development Workflow

1. Loading Data: $tribe = Tribe::load($userId, $saveId); — automatically loads kobolds, storage, items 2. Modifying Tribe: Add kobolds with $tribe->addKobold($kobold), modify state, then $tribe->save() writes all JSON 3. Adding Features: Update entity class → add fromArray()/toArray() support → update API endpoint 4. Testing Events: Create event class extending Event, test via EventManager or direct instantiation

Common File Locations

PurposeFile
Database setupsrc/Database/Database.php
User authsrc/Classes/User.php, src/api/auth.php
Game statesrc/Classes/Tribe.php
Kobold logicsrc/Classes/Kobold.php
Storage abstractionsrc/System/SaveManager.php
Game routessrc/api/game.php
Frontendpublic/js/app.js, public/index.html

Documentation References

  • Architecture Details: ARCHITECTURE.md — database schema, class hierarchy, API flow
  • Quick Reference: QUICK_REFERENCE.md — refactoring summary, file changes
  • Getting Started: README.md — installation, features, event creation guide
Source
1# TribeGen - AI Coding Agent Instructions
2
3## Architecture Overview
4
5**TribeGen** is a browser-based strategy game where players manage a Kobold tribe. The codebase uses **file-based JSON storage** (refactored from SQLite) with PHP backends and vanilla JavaScript frontend.
6
7### Storage Architecture
8- **Authentication**: SQLite database (users table only) — see [src/Database/Database.php](src/Database/Database.php)
9- **Game State**: JSON files organized by user → save → entities (tribe, kobolds, items, NPCs)
10- **SaveManager**: Single abstraction for all file I/O — see [src/System/SaveManager.php](src/System/SaveManager.php)
11 - Path pattern: `data/saves/{userId}/{saveId}/` containing `save.json`, `kobolds.json`, `items.json`, `npcs.json`
12
13**Critical Pattern**: All game classes use `fromArray()/toArray()` for JSON serialization. Changes to class properties require updating both methods.
14
15### Core Entity Classes
161. **Tribe** ([src/Classes/Tribe.php](src/Classes/Tribe.php#L1-L100)): Manages kobolds, food/storage, month/season progression, loads/saves via SaveManager
172. **Kobold** ([src/Classes/Kobold.php](src/Classes/Kobold.php#L1-L100)): Individual stats (vitality, intelligence, tinkering, medicine, crafting, personality, will), injuries, items, genetic lineage
183. **Item** ([src/Classes/Item.php](src/Classes/Item.php)): Named items with stat modifications (each mod is -5 to +5 per stat)
194. **NPC** ([src/Classes/NPC.php](src/Classes/NPC.php)): Non-player characters/tribes (extensible via JSON `data` field)
205. **Injury** ([src/Classes/Injury.php](src/Classes/Injury.php)): Injury type, category (wound/fracture), severity 0-10
21
22### Event System
23- **Base Class**: `Event` — abstract execute() method that modifies tribe state and sets `$this->story`
24- **EventManager** ([src/Events/EventManager.php](src/Events/EventManager.php)): Loads enabled events per save, executes them
25- **Implementations**: [HuntingEvent](src/Events/HuntingEvent.php), [ExplorationEvent](src/Events/ExplorationEvent.php), [FarmingEvent](src/Events/FarmingEvent.php)
26- **Creating New Events**: Extend `Event`, implement `execute()`, return narrative story via `$this->story`
27- **Example**: [ExplorationEvent](src/Events/ExplorationEvent.php) — shows stat-based mechanics (intelligence affects food discovery, vitality reduces injury chance), food addition via `$tribe->addFood()`, and injury creation as array appended to kobold's injuries list
28
29## API Endpoints
30
31All endpoints in [src/api/game.php](src/api/game.php) require `userId` and `saveId` in POST body:
32
33- **POST `/api/game/generation`**: Create new tribe with 20 random kobolds
34- **POST `/api/game/load?saveId=X`**: Load tribe state
35- **POST `/api/game/save`**: Save tribe setup (currently a TODO)
36- **POST `/api/game/event`**: Execute single event
37- **POST `/api/game/endMonth`**: Progress month, age kobolds, apply injuries, execute month events
38
39**Pattern**: All responses use `{ success: boolean, error?: string, ...data }`
40
41## Key Conventions
42
43### IDs and Naming
44- **User IDs**: Database `user.id` (integer)
45- **Save IDs**: Numeric string generated by `SaveManager::generateSaveId()`
46- **Kobold IDs**: Prefixed with `kob_` + uniqid (example: `kob_678abc123`)
47- **Method Names**: Use camelCase, boolean getters are `is*()` or `get*()` followed by value
48
49### Stats System
50- All stats are 0-100 internally, displayed as text ranges (0-10: "terrible", 90-100: "godlike")
51- Stat checks: Get base value → apply item modifications → check result
52- Items can boost/reduce any stat independently
53
54### Kobold Lifecycle
55- **Age Tiers**: egg → baby → adult → old → very_old (affects food consumption and sprite)
56- **Sprite Morphs**: Loaded from `assets/sprites/{morph}/` directory, copied to save folder on generation
57- **Genetic Inheritance**: Each kobold tracks `fatherId`, `motherId` for generation tracking
58
59### Food System
60- `Tribe::foodCount` tracks stored food
61- Age tiers have food requirements (egg: 0, baby: 0.5, adult: 1.0, old: 0.6, very_old: 0.4 units)
62- `endMonth()` deducts food and reports shortage via story
63
64## Development Workflow
65
661. **Loading Data**: `$tribe = Tribe::load($userId, $saveId);` — automatically loads kobolds, storage, items
672. **Modifying Tribe**: Add kobolds with `$tribe->addKobold($kobold)`, modify state, then `$tribe->save()` writes all JSON
683. **Adding Features**: Update entity class → add `fromArray()/toArray()` support → update API endpoint
694. **Testing Events**: Create event class extending `Event`, test via EventManager or direct instantiation
70
71## Common File Locations
72
73| Purpose | File |
74|---------|------|
75| Database setup | [src/Database/Database.php](src/Database/Database.php) |
76| User auth | [src/Classes/User.php](src/Classes/User.php), [src/api/auth.php](src/api/auth.php) |
77| Game state | [src/Classes/Tribe.php](src/Classes/Tribe.php) |
78| Kobold logic | [src/Classes/Kobold.php](src/Classes/Kobold.php) |
79| Storage abstraction | [src/System/SaveManager.php](src/System/SaveManager.php) |
80| Game routes | [src/api/game.php](src/api/game.php) |
81| Frontend | [public/js/app.js](public/js/app.js), [public/index.html](public/index.html) |
82
83## Documentation References
84
85- **Architecture Details**: [ARCHITECTURE.md](ARCHITECTURE.md) — database schema, class hierarchy, API flow
86- **Quick Reference**: [QUICK_REFERENCE.md](QUICK_REFERENCE.md) — refactoring summary, file changes
87- **Getting Started**: [README.md](README.md) — installation, features, event creation guide