feat: store battles and bombings
This commit is contained in:
+63
-9
@@ -1,20 +1,25 @@
|
||||
package repo
|
||||
|
||||
/*
|
||||
/state.json
|
||||
TODO: only state will be saved once (current, turn); meta and bombings are saved at turn generation and saved twice
|
||||
/state.json
|
||||
/0001/state.json
|
||||
/0001/race/{UUID}/report.json
|
||||
/0001/meta.json
|
||||
/0001/bombing.json
|
||||
/0001/battle/{UUID}.json
|
||||
/0001/report/{UUID}.json
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/iliadenisov/galaxy/internal/model/report"
|
||||
)
|
||||
|
||||
const (
|
||||
statePath = "state.json"
|
||||
metaPath = "meta.json"
|
||||
)
|
||||
|
||||
func (r *repo) SaveTurn(t uint, g *game.Game) error {
|
||||
@@ -65,7 +70,7 @@ func (r *repo) LoadStateSafe() (*game.Game, error) {
|
||||
}
|
||||
|
||||
func loadState(s Storage, locked bool) (*game.Game, error) {
|
||||
var g *game.Game = new(game.Game)
|
||||
var result *game.Game = new(game.Game)
|
||||
path := statePath
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
@@ -75,22 +80,62 @@ func loadState(s Storage, locked bool) (*game.Game, error) {
|
||||
return nil, NewGameNotInitializedError()
|
||||
}
|
||||
if locked {
|
||||
if err := s.Read(path, g); err != nil {
|
||||
if err := s.Read(path, result); err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
} else {
|
||||
if err := s.ReadSafe(path, g); err != nil {
|
||||
if err := s.ReadSafe(path, result); err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
}
|
||||
return g, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *repo) SaveBattle(t uint, b *game.BattleReport) error {
|
||||
return saveBattle(r.s, t, b)
|
||||
func loadMeta(s Storage) (*game.GameMeta, error) {
|
||||
var result *game.GameMeta = new(game.GameMeta)
|
||||
path := metaPath
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
if !exist {
|
||||
return result, nil
|
||||
}
|
||||
// TODO: create separate Read func for meta ops
|
||||
if err := s.ReadSafe(path, result); err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func saveBattle(s Storage, t uint, b *game.BattleReport) error {
|
||||
func saveMeta(s Storage, t uint, gm *game.GameMeta) error {
|
||||
// save turn's meta
|
||||
path := fmt.Sprintf("%s/%s", turnDir(t), metaPath)
|
||||
if err := s.Write(path, gm); err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
// also save as latest meta
|
||||
path = metaPath
|
||||
if err := s.Write(path, gm); err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) SaveBattle(t uint, b *report.BattleReport, m *game.BattleMeta) error {
|
||||
meta, err := loadMeta(r.s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = saveBattle(r.s, t, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
meta.Battles = append(meta.Battles, *m)
|
||||
return saveMeta(r.s, t, meta)
|
||||
}
|
||||
|
||||
func saveBattle(s Storage, t uint, b *report.BattleReport) error {
|
||||
path := fmt.Sprintf("%s/battle/%s.json", turnDir(t), b.ID)
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
@@ -105,6 +150,15 @@ func saveBattle(s Storage, t uint, b *game.BattleReport) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) SaveBombings(t uint, b []report.BombingPlanetReport) error {
|
||||
meta, err := loadMeta(r.s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
meta.Bombings = b
|
||||
return saveMeta(r.s, t, meta)
|
||||
}
|
||||
|
||||
func turnDir(t uint) string {
|
||||
return fmt.Sprintf("%04d", t)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user