xonecas.ioDon't click! Sean Caetano Martin Sign in

tiny-adventure / commits / ccbe09e

Scaffold Go module and shared game model contract

Co-Authored-By: Claude Sonnet 5.5

sacenox3 days agoccbe09e552ac93622d23276c6211717c9fa2f7a3parent 59ffd7e14 files changed

cmd/server/main.go+14-0
@@ -0,0 +1,14 @@
1+package main
2+
3+import (
4+ "flag"
5+ "fmt"
6+
7+ "tinyadventure/internal/model"
8+)
9+
10+func main() {
11+ addr := flag.String("addr", ":8080", "listen address")
12+ flag.Parse()
13+ fmt.Println("tiny adventure server stub", *addr, model.Version)
14+}
docs/api.md+150-0
@@ -0,0 +1,150 @@
1+# HTTP API
2+
3+All bodies are JSON. Types live in `internal/model`. Single player first: the player id is `local`, sent as the `X-Player` header (optional, defaults to `local`). Server state is keyed by player id so multiplayer can be added later.
4+
5+Errors return a non-2xx status and `{"error": "message"}` (`ErrorResponse`).
6+
7+Every endpoint except `/api/seed` and `/api/combat/log` returns a full `GameState`. `phase` is one of `explore`, `combat`, `victory`, `defeat`.
8+
9+## GameState example
10+
11+```json
12+{
13+ "version": 1,
14+ "playerId": "local",
15+ "seed": 42,
16+ "campaign": {
17+ "seed": 42,
18+ "systems": ["factions", "sidequests"],
19+ "factions": [{"id": "f1", "name": "Vampires", "theme": "undead", "power": 3}],
20+ "main": {
21+ "template": "fetch-key",
22+ "name": "The Crimson Key",
23+ "steps": [
24+ {"id": "s1", "kind": "reach", "text": "Find the mine", "slots": {"poiKind": "mine", "faction": "f1", "item": "i2"}, "poi": "poi1", "done": false}
25+ ]
26+ },
27+ "sideQuests": []
28+ },
29+ "map": {
30+ "width": 1,
31+ "height": 1,
32+ "tiles": [[{"terrain": "grass", "walkable": true}]],
33+ "pois": [{"id": "poi1", "kind": "mine", "name": "Dark Mine", "x": 0, "y": 0, "faction": "f1", "cleared": false}],
34+ "spawns": [{"id": "sp1", "kind": "inn", "x": 0, "y": 0, "hint": "Ask the innkeeper"}],
35+ "loot": [{"id": "l1", "x": 0, "y": 0, "item": {"id": "i1", "name": "Rusty Sword", "slot": "weapon", "rarity": "common", "bonus": {"hp": 0, "maxHp": 0, "attack": 2, "defense": 0, "magic": 0, "speed": 0, "crit": 0}}, "collected": false}]
36+ },
37+ "party": {
38+ "playerId": "local",
39+ "units": [
40+ {
41+ "id": "u1", "name": "Ayla", "class": "fighter", "level": 1, "xp": 0,
42+ "stats": {"hp": 10, "maxHp": 10, "attack": 3, "defense": 1, "magic": 0, "speed": 4, "crit": 5},
43+ "abilities": [{"id": "a1", "name": "Slash", "kind": "damage", "power": 1.2, "cooldown": 2, "target": "enemy"}],
44+ "gear": {}
45+ }
46+ ],
47+ "inventory": [],
48+ "x": 0,
49+ "y": 0,
50+ "gold": 0
51+ },
52+ "log": [],
53+ "phase": "explore",
54+ "summary": {"battles": 0, "kills": 0, "loot": 0, "steps": 0}
55+}
56+```
57+
58+Optional fields: `encounter` (`Encounter`, present while `phase` is `combat`), `rewards` (`Rewards`, present after a won fight), `winner` (player id, set at `victory`).
59+
60+Item slots: `weapon`, `armor`, `helm`, `trinket`. `gear` maps slot to `Item`.
61+
62+## GET /api/state
63+
64+Returns the current `GameState`.
65+
66+```
67+GET /api/state
68+```
69+
70+Response: `GameState` (see above).
71+
72+## POST /api/new
73+
74+Starts a new campaign. `seed` is optional; the server picks a random one when omitted.
75+
76+```json
77+{"seed": 42}
78+```
79+
80+Response: `GameState` with a fresh campaign.
81+
82+## POST /api/move
83+
84+Moves the party toward tile `x,y`. The server paths over walkable tiles, may trigger an encounter on arrival, and returns the new state. Rejected with 400 during `combat` or for an unreachable tile.
85+
86+```json
87+{"x": 12, "y": 7}
88+```
89+
90+Response: `GameState` with updated `party.x`, `party.y`, and possibly `phase: "combat"` plus `encounter`.
91+
92+## POST /api/interact
93+
94+Interacts with a POI, loot node, or spawn structure by id (`poi1`, `l1`, `sp1`). Collects loot, advances mission steps, or starts the POI encounter.
95+
96+```json
97+{"id": "l1"}
98+```
99+
100+Response: `GameState`. Collected loot appears in `party.inventory` and `map.loot[].collected` becomes `true`.
101+
102+## POST /api/equip
103+
104+Equips an inventory item on a unit, or unequips the slot item when `unequip` is true.
105+
106+```json
107+{"unit": "u1", "item": "i1"}
108+```
109+
110+```json
111+{"unit": "u1", "item": "i1", "unequip": true}
112+```
113+
114+Response: `GameState` with updated `party.units[].gear` and `party.inventory`.
115+
116+## POST /api/ability
117+
118+Assigns an ability to the unit's equipped ability slot, used by auto-combat.
119+
120+```json
121+{"unit": "u1", "ability": "a1", "slot": 0}
122+```
123+
124+Response: `GameState` with updated `party.units[].abilities`.
125+
126+## GET /api/combat/log
127+
128+Returns the combat log of the latest fight.
129+
130+Response:
131+
132+```json
133+{
134+ "log": [
135+ {"tick": 1, "actor": "u1", "target": "m1", "ability": "a1", "amount": 3, "crit": false, "killed": false, "text": "Ayla slashes Bat for 3"}
136+ ]
137+}
138+```
139+
140+## GET /api/seed
141+
142+Returns the seed and the enabled systems.
143+
144+```json
145+{"seed": 42, "systems": ["factions", "sidequests"]}
146+```
147+
148+## Mission templates
149+
150+A mission is a list of `MissionStep`. Each step has typed `slots` (`poiKind`, `faction`, `item`) filled from the seed, so the `mine`/`vampires`/`key` template becomes `beach`/`lizardfolk`/`key` by changing slot values only.
go.mod+3-0
@@ -0,0 +1,3 @@
1+module tinyadventure
2+
3+go 1.27
internal/model/ability.go+10-0
@@ -0,0 +1,10 @@
1+package model
2+
3+type Ability struct {
4+ ID string `json:"id"`
5+ Name string `json:"name"`
6+ Kind string `json:"kind"`
7+ Power float64 `json:"power"`
8+ Cooldown int `json:"cooldown"`
9+ Target string `json:"target"`
10+}
internal/model/api.go+39-0
@@ -0,0 +1,39 @@
1+package model
2+
3+type NewRequest struct {
4+ Seed *Seed `json:"seed,omitempty"`
5+}
6+
7+type MoveRequest struct {
8+ X int `json:"x"`
9+ Y int `json:"y"`
10+}
11+
12+type InteractRequest struct {
13+ ID string `json:"id"`
14+}
15+
16+type EquipRequest struct {
17+ Unit string `json:"unit"`
18+ Item string `json:"item"`
19+ Unequip bool `json:"unequip,omitempty"`
20+}
21+
22+type AbilityRequest struct {
23+ Unit string `json:"unit"`
24+ Ability string `json:"ability"`
25+ Slot int `json:"slot"`
26+}
27+
28+type SeedResponse struct {
29+ Seed Seed `json:"seed"`
30+ Systems []string `json:"systems"`
31+}
32+
33+type CombatLogResponse struct {
34+ Log []CombatEvent `json:"log"`
35+}
36+
37+type ErrorResponse struct {
38+ Error string `json:"error"`
39+}
internal/model/campaign.go+43-0
@@ -0,0 +1,43 @@
1+package model
2+
3+type Faction struct {
4+ ID string `json:"id"`
5+ Name string `json:"name"`
6+ Theme string `json:"theme"`
7+ Power int `json:"power"`
8+}
9+
10+type Slots struct {
11+ POIKind string `json:"poiKind,omitempty"`
12+ Faction string `json:"faction,omitempty"`
13+ Item string `json:"item,omitempty"`
14+}
15+
16+type MissionStep struct {
17+ ID string `json:"id"`
18+ Kind string `json:"kind"`
19+ Text string `json:"text"`
20+ Slots Slots `json:"slots"`
21+ POI string `json:"poi,omitempty"`
22+ Done bool `json:"done"`
23+}
24+
25+type MissionTemplate struct {
26+ ID string `json:"id"`
27+ Name string `json:"name"`
28+ Steps []MissionStep `json:"steps"`
29+}
30+
31+type Mission struct {
32+ Template string `json:"template"`
33+ Name string `json:"name"`
34+ Steps []MissionStep `json:"steps"`
35+}
36+
37+type Campaign struct {
38+ Seed Seed `json:"seed"`
39+ Systems []string `json:"systems"`
40+ Factions []Faction `json:"factions"`
41+ Main Mission `json:"main"`
42+ SideQuests []Mission `json:"sideQuests"`
43+}
internal/model/combat.go+25-0
@@ -0,0 +1,25 @@
1+package model
2+
3+type Encounter struct {
4+ ID string `json:"id"`
5+ POI string `json:"poi,omitempty"`
6+ Enemies []Unit `json:"enemies"`
7+ Boss bool `json:"boss,omitempty"`
8+}
9+
10+type CombatEvent struct {
11+ Tick int `json:"tick"`
12+ Actor string `json:"actor"`
13+ Target string `json:"target"`
14+ Ability string `json:"ability"`
15+ Amount int `json:"amount"`
16+ Crit bool `json:"crit,omitempty"`
17+ Killed bool `json:"killed,omitempty"`
18+ Text string `json:"text"`
19+}
20+
21+type Rewards struct {
22+ XP int `json:"xp"`
23+ Gold int `json:"gold"`
24+ Items []Item `json:"items"`
25+}
internal/model/item.go+22-0
@@ -0,0 +1,22 @@
1+package model
2+
3+type Slot string
4+
5+const (
6+ SlotWeapon Slot = "weapon"
7+ SlotArmor Slot = "armor"
8+ SlotHelm Slot = "helm"
9+ SlotTrinket Slot = "trinket"
10+)
11+
12+type Item struct {
13+ ID string `json:"id"`
14+ Name string `json:"name"`
15+ Slot Slot `json:"slot"`
16+ Rarity string `json:"rarity"`
17+ Bonus Stats `json:"bonus"`
18+ Abilities []string `json:"abilities,omitempty"`
19+ Key bool `json:"key,omitempty"`
20+}
21+
22+type Gear map[Slot]*Item
internal/model/state.go+23-0
@@ -0,0 +1,23 @@
1+package model
2+
3+type Summary struct {
4+ Battles int `json:"battles"`
5+ Kills int `json:"kills"`
6+ Loot int `json:"loot"`
7+ Steps int `json:"steps"`
8+}
9+
10+type GameState struct {
11+ Version int `json:"version"`
12+ PlayerID string `json:"playerId"`
13+ Seed Seed `json:"seed"`
14+ Campaign Campaign `json:"campaign"`
15+ Map Map `json:"map"`
16+ Party Party `json:"party"`
17+ Encounter *Encounter `json:"encounter,omitempty"`
18+ Log []CombatEvent `json:"log"`
19+ Rewards *Rewards `json:"rewards,omitempty"`
20+ Phase string `json:"phase"`
21+ Winner string `json:"winner,omitempty"`
22+ Summary Summary `json:"summary"`
23+}
internal/model/state_test.go+56-0
@@ -0,0 +1,56 @@
1+package model
2+
3+import (
4+ "encoding/json"
5+ "reflect"
6+ "testing"
7+)
8+
9+func TestGameStateRoundTrip(t *testing.T) {
10+ sword := Item{ID: "i1", Name: "Rusty Sword", Slot: SlotWeapon, Rarity: "common", Bonus: Stats{Attack: 2}}
11+ in := GameState{
12+ Version: Version,
13+ PlayerID: "p1",
14+ Seed: 42,
15+ Campaign: Campaign{
16+ Seed: 42,
17+ Systems: []string{"factions", "sidequests"},
18+ Factions: []Faction{{ID: "f1", Name: "Vampires", Theme: "undead", Power: 3}},
19+ Main: Mission{Template: "fetch", Name: "The Key", Steps: []MissionStep{
20+ {ID: "s1", Kind: "reach", Text: "Find the mine", Slots: Slots{POIKind: "mine", Faction: "f1", Item: "i2"}, POI: "poi1"},
21+ }},
22+ SideQuests: []Mission{},
23+ },
24+ Map: Map{
25+ Width: 1, Height: 1,
26+ Tiles: [][]Tile{{{Terrain: "grass", Walkable: true}}},
27+ POIs: []POI{{ID: "poi1", Kind: "mine", Name: "Dark Mine", X: 0, Y: 0, Faction: "f1"}},
28+ Spawns: []SpawnPoint{{ID: "sp1", Kind: "inn", Hint: "Ask the innkeeper"}},
29+ Loot: []LootNode{{ID: "l1", Item: sword}},
30+ },
31+ Party: Party{
32+ PlayerID: "p1",
33+ Units: []Unit{{ID: "u1", Name: "Ayla", Class: "fighter", Level: 1,
34+ Stats: Stats{HP: 10, MaxHP: 10, Attack: 3},
35+ Abilities: []Ability{{ID: "a1", Name: "Slash", Kind: "damage", Power: 1.2, Target: "enemy"}},
36+ Gear: Gear{SlotWeapon: &sword}}},
37+ Inventory: []Item{sword},
38+ },
39+ Encounter: &Encounter{ID: "e1", Enemies: []Unit{{ID: "m1", Name: "Bat"}}},
40+ Log: []CombatEvent{{Tick: 1, Actor: "u1", Target: "m1", Ability: "a1", Amount: 3, Text: "hit"}},
41+ Rewards: &Rewards{XP: 5, Gold: 2, Items: []Item{sword}},
42+ Phase: "explore",
43+ Summary: Summary{Battles: 1},
44+ }
45+ b, err := json.Marshal(in)
46+ if err != nil {
47+ t.Fatal(err)
48+ }
49+ var out GameState
50+ if err := json.Unmarshal(b, &out); err != nil {
51+ t.Fatal(err)
52+ }
53+ if !reflect.DeepEqual(in, out) {
54+ t.Fatalf("mismatch\n%s", b)
55+ }
56+}
internal/model/stats.go+11-0
@@ -0,0 +1,11 @@
1+package model
2+
3+type Stats struct {
4+ HP int `json:"hp"`
5+ MaxHP int `json:"maxHp"`
6+ Attack int `json:"attack"`
7+ Defense int `json:"defense"`
8+ Magic int `json:"magic"`
9+ Speed int `json:"speed"`
10+ Crit int `json:"crit"`
11+}
internal/model/unit.go+22-0
@@ -0,0 +1,22 @@
1+package model
2+
3+type Unit struct {
4+ ID string `json:"id"`
5+ Name string `json:"name"`
6+ Class string `json:"class"`
7+ Faction string `json:"faction,omitempty"`
8+ Level int `json:"level"`
9+ XP int `json:"xp"`
10+ Stats Stats `json:"stats"`
11+ Abilities []Ability `json:"abilities"`
12+ Gear Gear `json:"gear,omitempty"`
13+}
14+
15+type Party struct {
16+ PlayerID string `json:"playerId"`
17+ Units []Unit `json:"units"`
18+ Inventory []Item `json:"inventory"`
19+ X int `json:"x"`
20+ Y int `json:"y"`
21+ Gold int `json:"gold"`
22+}
internal/model/version.go+5-0
@@ -0,0 +1,5 @@
1+package model
2+
3+const Version = 1
4+
5+type Seed int64
internal/model/worldmap.go+41-0
@@ -0,0 +1,41 @@
1+package model
2+
3+type Tile struct {
4+ Terrain string `json:"terrain"`
5+ Walkable bool `json:"walkable"`
6+}
7+
8+type POI struct {
9+ ID string `json:"id"`
10+ Kind string `json:"kind"`
11+ Name string `json:"name"`
12+ X int `json:"x"`
13+ Y int `json:"y"`
14+ Faction string `json:"faction,omitempty"`
15+ Cleared bool `json:"cleared"`
16+}
17+
18+type SpawnPoint struct {
19+ ID string `json:"id"`
20+ Kind string `json:"kind"`
21+ X int `json:"x"`
22+ Y int `json:"y"`
23+ Hint string `json:"hint"`
24+}
25+
26+type LootNode struct {
27+ ID string `json:"id"`
28+ X int `json:"x"`
29+ Y int `json:"y"`
30+ Item Item `json:"item"`
31+ Collected bool `json:"collected"`
32+}
33+
34+type Map struct {
35+ Width int `json:"width"`
36+ Height int `json:"height"`
37+ Tiles [][]Tile `json:"tiles"`
38+ POIs []POI `json:"pois"`
39+ Spawns []SpawnPoint `json:"spawns"`
40+ Loot []LootNode `json:"loot"`
41+}