saving new turn

This commit is contained in:
Ilia Denisov
2025-09-26 01:38:49 +03:00
parent 6d87ea6086
commit 282150a253
13 changed files with 170 additions and 57 deletions
+48
View File
@@ -0,0 +1,48 @@
package repo
/*
/state.json
/000/state.json
/000/race/{UUID}/order/001.json
/000/race/{UUID}/report.json
/000/battle/{planet_UUID}
*/
import (
"fmt"
"github.com/iliadenisov/galaxy/pkg/model/game"
)
func (r *repo) SaveTurn(t uint, g game.Game) error {
return saveTurn(r.s, t, g)
}
func saveTurn(s Storage, t uint, g game.Game) error {
path := fmt.Sprintf("%03d/state.json", t)
exist, err := s.Exist(path)
if err != nil {
return NewStorageError(err)
}
if exist {
return NewStateError(fmt.Sprintf("state for turn %d already saved", t))
}
if err := s.Write(path, g); err != nil {
return NewStorageError(err)
}
// TODO: save reports
// TODO: save battles
return saveState(s, g)
}
func (r *repo) SaveState(g game.Game) error {
return saveState(r.s, g)
}
func saveState(s Storage, g game.Game) error {
path := "state.json"
if err := s.Write(path, g); err != nil {
return NewStorageError(err)
}
return nil
}