saving new turn
This commit is contained in:
@@ -75,6 +75,10 @@ func (f *fs) Lock() (func() error, error) {
|
||||
return unlock, nil
|
||||
}
|
||||
|
||||
func (f *fs) Exist(path string) (bool, error) {
|
||||
return fileExists(filepath.Join(f.root, path))
|
||||
}
|
||||
|
||||
func (f *fs) Write(path string, v encoding.BinaryMarshaler) error {
|
||||
if v == nil {
|
||||
return errors.New("cant't marshal from nil object")
|
||||
|
||||
@@ -33,6 +33,27 @@ func TestLock(t *testing.T) {
|
||||
assert.False(t, exists, "lock file must be removed")
|
||||
}
|
||||
|
||||
func TestExist(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
defer cleanup()
|
||||
|
||||
fileName := "some-file.ext"
|
||||
if err := os.WriteFile(filepath.Join(root, fileName), []byte{1, 2, 3, 4}, os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fs, err := NewFileStorage(root)
|
||||
assert.NoError(t, err, "create file storage")
|
||||
|
||||
exist, err := fs.Exist(fileName)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, exist)
|
||||
|
||||
exist, err = fs.Exist("random/path")
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, exist)
|
||||
}
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
root, cleanup := createWorkDir(t)
|
||||
defer cleanup()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -2,12 +2,26 @@ package repo
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"errors"
|
||||
|
||||
"github.com/iliadenisov/galaxy/pkg/repo/fs"
|
||||
)
|
||||
|
||||
type StorageError error
|
||||
|
||||
func NewStorageError(err error) error {
|
||||
return StorageError(err)
|
||||
}
|
||||
|
||||
type StateError error
|
||||
|
||||
func NewStateError(msg string) error {
|
||||
return StateError(errors.New(msg))
|
||||
}
|
||||
|
||||
type Storage interface {
|
||||
Lock() (func() error, error)
|
||||
Exist(string) (bool, error)
|
||||
Write(string, encoding.BinaryMarshaler) error
|
||||
Read(string, encoding.BinaryUnmarshaler) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user