1package model 2 3import ( 4 "encoding/json" 5 "reflect" 6 "testing" 7) 8 9func 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 Game: "g1", 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 Hexes: []byte{ShapePlain, 4, 1, 0}, 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 Units: []Unit{{ID: "u1", Name: "Ayla", Class: "fighter", Level: 1, 33 Stats: Stats{HP: 10, MaxHP: 10, Attack: 3}, 34 Abilities: []Ability{{ID: "a1", Name: "Slash", Kind: "damage", Power: 1.2, Target: "enemy"}}, 35 Gear: Gear{SlotWeapon: &sword}}}, 36 Inventory: []Item{sword}, 37 }, 38 Encounter: &Encounter{ID: "e1", Enemies: []Unit{{ID: "m1", Name: "Bat"}}}, 39 Log: []CombatEvent{{Tick: 1, Actor: "u1", Target: "m1", Ability: "a1", Amount: 3, Text: "hit"}}, 40 Rewards: &Rewards{XP: 5, Gold: 2, Items: []Item{sword}}, 41 Phase: "explore", 42 Summary: Summary{Battles: 1}, 43 } 44 b, err := json.Marshal(in) 45 if err != nil { 46 t.Fatal(err) 47 } 48 var out GameState 49 if err := json.Unmarshal(b, &out); err != nil { 50 t.Fatal(err) 51 } 52 if !reflect.DeepEqual(in, out) { 53 t.Fatalf("mismatch\n%s", b) 54 } 55}