saving new turn
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user