feat: order processing
feat: order commands result save/load
This commit is contained in:
+10
-6
@@ -79,15 +79,11 @@ func (f *fs) Exists(path string) (bool, error) {
|
||||
return fileExists(filepath.Join(f.root, path))
|
||||
}
|
||||
|
||||
func (f *fs) Write(path string, v encoding.BinaryMarshaler) error {
|
||||
func (f *fs) WriteSafe(path string, v encoding.BinaryMarshaler) error {
|
||||
if v == nil {
|
||||
return errors.New("cant't marshal from nil object")
|
||||
}
|
||||
|
||||
if f.lock == nil {
|
||||
return errors.New("lock must be acquired before write")
|
||||
}
|
||||
|
||||
targetFilePath := filepath.Join(f.root, path)
|
||||
if targetFilePath == f.lockFilePath() {
|
||||
return errors.New("can't write to the lock file")
|
||||
@@ -160,6 +156,14 @@ func (f *fs) Write(path string, v encoding.BinaryMarshaler) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fs) Write(path string, v encoding.BinaryMarshaler) error {
|
||||
if f.lock == nil {
|
||||
return errors.New("lock must be acquired before write")
|
||||
}
|
||||
|
||||
return f.WriteSafe(path, v)
|
||||
}
|
||||
|
||||
func (f *fs) Read(path string, v encoding.BinaryUnmarshaler) error {
|
||||
if f.lock == nil {
|
||||
return errors.New("lock must be acquired before read")
|
||||
@@ -183,7 +187,7 @@ func (f *fs) ReadSafe(path string, v encoding.BinaryUnmarshaler) error {
|
||||
}
|
||||
case <-timeout.C:
|
||||
checker.Stop()
|
||||
return errors.New("lock acquired, timeout waiting for release")
|
||||
return errors.New("timeout waiting for lock release")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+179
-32
@@ -4,16 +4,20 @@ package repo
|
||||
/state.json
|
||||
/0001/state.json
|
||||
/0001/meta.json
|
||||
/0000/order/{UUID}.json
|
||||
/0001/bombing.json
|
||||
/0001/battle/{UUID}.json
|
||||
/0001/report/{UUID}.json
|
||||
*/
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/iliadenisov/galaxy/internal/model/order"
|
||||
"github.com/iliadenisov/galaxy/internal/model/report"
|
||||
)
|
||||
|
||||
@@ -22,36 +26,16 @@ const (
|
||||
metaPath = "meta.json"
|
||||
)
|
||||
|
||||
func (r *repo) SaveReport(t uint, rep *report.Report) error {
|
||||
return saveReport(r.s, t, rep)
|
||||
type storedOrder struct {
|
||||
Commands []json.RawMessage `json:"cmd"`
|
||||
}
|
||||
|
||||
func saveReport(s Storage, t uint, v *report.Report) error {
|
||||
path := repDir(t, v.RaceID)
|
||||
if err := s.Write(path, v); err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
return nil
|
||||
func (o storedOrder) MarshalBinary() (data []byte, err error) {
|
||||
return json.Marshal(&o)
|
||||
}
|
||||
|
||||
func (r *repo) LoadReport(t uint, id uuid.UUID) (*report.Report, error) {
|
||||
return loadReport(r.s, t, id)
|
||||
}
|
||||
|
||||
func loadReport(s Storage, t uint, id uuid.UUID) (*report.Report, error) {
|
||||
path := repDir(t, id)
|
||||
result := new(report.Report)
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
if !exist {
|
||||
return nil, NewReportNotFoundError()
|
||||
}
|
||||
if err := s.ReadSafe(path, result); err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
return result, nil
|
||||
func (o *storedOrder) UnmarshalBinary(data []byte) error {
|
||||
return json.Unmarshal(data, o)
|
||||
}
|
||||
|
||||
func (r *repo) SaveNewTurn(t uint, g *game.Game) error {
|
||||
@@ -59,7 +43,7 @@ func (r *repo) SaveNewTurn(t uint, g *game.Game) error {
|
||||
}
|
||||
|
||||
func saveNewTurn(s Storage, t uint, g *game.Game) error {
|
||||
path := fmt.Sprintf("%s/state.json", turnDir(t))
|
||||
path := fmt.Sprintf("%s/state.json", TurnDir(t))
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return NewStorageError(err)
|
||||
@@ -132,7 +116,7 @@ func loadMeta(s Storage) (*game.GameMeta, error) {
|
||||
|
||||
func saveMeta(s Storage, t uint, gm *game.GameMeta) error {
|
||||
// save turn's meta
|
||||
path := fmt.Sprintf("%s/%s", turnDir(t), metaPath)
|
||||
path := fmt.Sprintf("%s/%s", TurnDir(t), metaPath)
|
||||
if err := s.Write(path, gm); err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
@@ -158,7 +142,7 @@ func (r *repo) SaveBattle(t uint, b *report.BattleReport, m *game.BattleMeta) er
|
||||
}
|
||||
|
||||
func saveBattle(s Storage, t uint, b *report.BattleReport) error {
|
||||
path := fmt.Sprintf("%s/battle/%s.json", turnDir(t), b.ID.String())
|
||||
path := fmt.Sprintf("%s/battle/%s.json", TurnDir(t), b.ID.String())
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return NewStorageError(err)
|
||||
@@ -183,10 +167,173 @@ func (r *repo) SaveBombings(t uint, b []*game.Bombing) error {
|
||||
return saveMeta(r.s, t, meta)
|
||||
}
|
||||
|
||||
func repDir(t uint, id uuid.UUID) string {
|
||||
return fmt.Sprintf("%s/report/%s.json", turnDir(t), id.String())
|
||||
func (r *repo) SaveReport(t uint, rep *report.Report) error {
|
||||
return saveReport(r.s, t, rep)
|
||||
}
|
||||
|
||||
func turnDir(t uint) string {
|
||||
func saveReport(s Storage, t uint, v *report.Report) error {
|
||||
path := ReportDir(t, v.RaceID)
|
||||
if err := s.Write(path, v); err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) LoadReport(t uint, id uuid.UUID) (*report.Report, error) {
|
||||
return loadReport(r.s, t, id)
|
||||
}
|
||||
|
||||
func loadReport(s Storage, t uint, id uuid.UUID) (*report.Report, error) {
|
||||
path := ReportDir(t, id)
|
||||
result := new(report.Report)
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
if !exist {
|
||||
return nil, NewReportNotFoundError()
|
||||
}
|
||||
if err := s.ReadSafe(path, result); err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *repo) SaveOrder(t uint, id uuid.UUID, o *order.Order) error {
|
||||
return saveOrder(r.s, t, id, o)
|
||||
}
|
||||
|
||||
func saveOrder(s Storage, t uint, id uuid.UUID, o *order.Order) error {
|
||||
path := OrderDir(t, id)
|
||||
if err := s.WriteSafe(path, o); err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) LoadOrder(t uint, id uuid.UUID) (*order.Order, bool, error) {
|
||||
return loadOrder(r.s, t, id)
|
||||
}
|
||||
|
||||
func loadOrder(s Storage, t uint, id uuid.UUID) (*order.Order, bool, error) {
|
||||
path := OrderDir(t, id)
|
||||
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return nil, false, NewStorageError(err)
|
||||
}
|
||||
if !exist {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
cmd := new(storedOrder)
|
||||
if err := s.ReadSafe(path, cmd); err != nil {
|
||||
return nil, false, NewStorageError(err)
|
||||
}
|
||||
result := &order.Order{Commands: make([]order.DecodableCommand, len(cmd.Commands))}
|
||||
if len(cmd.Commands) == 0 {
|
||||
return nil, false, errors.New("no commands were stored")
|
||||
}
|
||||
|
||||
for i := range cmd.Commands {
|
||||
command, err := ParseOrder(cmd.Commands[i], nil)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
result.Commands[i] = command
|
||||
}
|
||||
|
||||
return result, true, nil
|
||||
}
|
||||
|
||||
// Helper funcs
|
||||
|
||||
func OrderDir(t uint, id uuid.UUID) string {
|
||||
return fmt.Sprintf("%s/order/%s.json", TurnDir(t), id.String())
|
||||
}
|
||||
|
||||
func ReportDir(t uint, id uuid.UUID) string {
|
||||
return fmt.Sprintf("%s/report/%s.json", TurnDir(t), id.String())
|
||||
}
|
||||
|
||||
func TurnDir(t uint) string {
|
||||
return fmt.Sprintf("%04d", t)
|
||||
}
|
||||
|
||||
func ParseOrder(c json.RawMessage, validator func(order.DecodableCommand) error) (order.DecodableCommand, error) {
|
||||
meta := new(order.CommandMeta)
|
||||
if err := json.Unmarshal(c, meta); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch t := meta.CmdType; t {
|
||||
case order.CommandTypeRaceQuit:
|
||||
return decodeCommand(c, new(order.CommandRaceQuit), validator)
|
||||
case order.CommandTypeRaceVote:
|
||||
return decodeCommand(c, new(order.CommandRaceVote), validator)
|
||||
case order.CommandTypeRaceRelation:
|
||||
return decodeCommand(c, new(order.CommandRaceRelation), validator)
|
||||
case order.CommandTypeShipClassCreate:
|
||||
return decodeCommand(c, new(order.CommandShipClassCreate), validator)
|
||||
case order.CommandTypeShipClassMerge:
|
||||
return decodeCommand(c, new(order.CommandShipClassMerge), validator)
|
||||
case order.CommandTypeShipClassRemove:
|
||||
return decodeCommand(c, new(order.CommandShipClassRemove), validator)
|
||||
case order.CommandTypeShipGroupBreak:
|
||||
return decodeCommand(c, new(order.CommandShipGroupBreak), validator)
|
||||
case order.CommandTypeShipGroupLoad:
|
||||
return decodeCommand(c, new(order.CommandShipGroupLoad), validator)
|
||||
case order.CommandTypeShipGroupUnload:
|
||||
return decodeCommand(c, new(order.CommandShipGroupUnload), validator)
|
||||
case order.CommandTypeShipGroupSend:
|
||||
return decodeCommand(c, new(order.CommandShipGroupSend), validator)
|
||||
case order.CommandTypeShipGroupUpgrade:
|
||||
return decodeCommand(c, new(order.CommandShipGroupUpgrade), validator)
|
||||
case order.CommandTypeShipGroupMerge:
|
||||
return decodeCommand(c, new(order.CommandShipGroupMerge), validator)
|
||||
case order.CommandTypeShipGroupDismantle:
|
||||
return decodeCommand(c, new(order.CommandShipGroupDismantle), validator)
|
||||
case order.CommandTypeShipGroupTransfer:
|
||||
return decodeCommand(c, new(order.CommandShipGroupTransfer), validator)
|
||||
case order.CommandTypeShipGroupJoinFleet:
|
||||
return decodeCommand(c, new(order.CommandShipGroupJoinFleet), validator)
|
||||
case order.CommandTypeFleetMerge:
|
||||
return decodeCommand(c, new(order.CommandFleetMerge), validator)
|
||||
case order.CommandTypeFleetSend:
|
||||
return decodeCommand(c, new(order.CommandFleetSend), validator)
|
||||
case order.CommandTypeScienceCreate:
|
||||
return decodeCommand(c, new(order.CommandScienceCreate), validator)
|
||||
case order.CommandTypeScienceRemove:
|
||||
return decodeCommand(c, new(order.CommandScienceRemove), validator)
|
||||
case order.CommandTypePlanetRename:
|
||||
return decodeCommand(c, new(order.CommandPlanetRename), validator)
|
||||
case order.CommandTypePlanetProduce:
|
||||
return decodeCommand(c, new(order.CommandPlanetProduce), validator)
|
||||
case order.CommandTypePlanetRouteSet:
|
||||
return decodeCommand(c, new(order.CommandPlanetRouteSet), validator)
|
||||
case order.CommandTypePlanetRouteRemove:
|
||||
return decodeCommand(c, new(order.CommandPlanetRouteRemove), validator)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown comman type: %s", t)
|
||||
}
|
||||
}
|
||||
|
||||
func decodeCommand(m json.RawMessage, c order.DecodableCommand, validator func(order.DecodableCommand) error) (order.DecodableCommand, error) {
|
||||
v, err := unmarshallCommand(m, c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if validator != nil {
|
||||
err = validator(v)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func unmarshallCommand[T order.DecodableCommand](c json.RawMessage, v T) (T, error) {
|
||||
if err := json.Unmarshal(c, v); err != nil {
|
||||
return v, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ type Storage interface {
|
||||
Lock() (func() error, error)
|
||||
Exists(string) (bool, error)
|
||||
Write(string, encoding.BinaryMarshaler) error
|
||||
WriteSafe(string, encoding.BinaryMarshaler) error
|
||||
Read(string, encoding.BinaryUnmarshaler) error
|
||||
ReadSafe(string, encoding.BinaryUnmarshaler) error
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/iliadenisov/galaxy/internal/model/order"
|
||||
)
|
||||
|
||||
func LoadOrder_T(s Storage, t uint, id uuid.UUID) (*order.Order, bool, error) {
|
||||
return loadOrder(s, t, id)
|
||||
}
|
||||
|
||||
func SaveOrder_T(s Storage, t uint, id uuid.UUID, o *order.Order) error {
|
||||
return saveOrder(s, t, id, o)
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package repo_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/iliadenisov/galaxy/internal/model/order"
|
||||
"github.com/iliadenisov/galaxy/internal/repo"
|
||||
"github.com/iliadenisov/galaxy/internal/repo/fs"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSaveOrder(t *testing.T) {
|
||||
root := t.ArtifactDir()
|
||||
s, err := fs.NewFileStorage(root)
|
||||
assert.NoError(t, err)
|
||||
id := uuid.New()
|
||||
o := &order.Order{
|
||||
Commands: []order.DecodableCommand{
|
||||
&order.CommandRaceVote{
|
||||
CommandMeta: order.CommandMeta{
|
||||
CmdType: order.CommandTypeRaceVote,
|
||||
CmdID: uuid.New().String(),
|
||||
},
|
||||
Acceptor: "Race_acc",
|
||||
},
|
||||
&order.CommandShipClassCreate{
|
||||
CommandMeta: order.CommandMeta{
|
||||
CmdType: order.CommandTypeShipClassCreate,
|
||||
CmdID: uuid.New().String(),
|
||||
},
|
||||
Name: "Fighter",
|
||||
Drive: 20.5,
|
||||
Armament: 5,
|
||||
Weapons: 20,
|
||||
Shields: 15.5,
|
||||
Cargo: 0,
|
||||
},
|
||||
&order.CommandShipGroupMerge{
|
||||
CommandMeta: order.CommandMeta{
|
||||
CmdType: order.CommandTypeShipGroupMerge,
|
||||
CmdID: uuid.New().String(),
|
||||
},
|
||||
},
|
||||
&order.CommandShipClassCreate{
|
||||
CommandMeta: order.CommandMeta{
|
||||
CmdType: order.CommandTypeShipClassCreate,
|
||||
CmdID: uuid.New().String(),
|
||||
},
|
||||
Name: "Freighter",
|
||||
Drive: 30.33,
|
||||
Armament: 1,
|
||||
Weapons: 1,
|
||||
Shields: 10.1,
|
||||
Cargo: 0,
|
||||
},
|
||||
&order.CommandRaceQuit{
|
||||
CommandMeta: order.CommandMeta{
|
||||
CmdType: order.CommandTypeRaceQuit,
|
||||
CmdID: uuid.New().String(),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
var turn uint = 2
|
||||
|
||||
for i := range o.Commands {
|
||||
if v, ok := order.AsCommand[*order.CommandRaceVote](o.Commands[i]); ok {
|
||||
m := &v.CommandMeta
|
||||
m.Result(0)
|
||||
} else if v, ok := order.AsCommand[*order.CommandRaceQuit](o.Commands[i]); ok {
|
||||
v.Result(10)
|
||||
} else if v, ok := order.AsCommand[*order.CommandShipClassCreate](o.Commands[i]); ok {
|
||||
m := &v.CommandMeta
|
||||
m.Result(33)
|
||||
} else if v, ok := order.AsCommand[*order.CommandShipGroupMerge](o.Commands[i]); ok {
|
||||
v.Result(0)
|
||||
}
|
||||
}
|
||||
|
||||
assert.NoError(t, repo.SaveOrder_T(s, turn, id, o))
|
||||
assert.FileExists(t, filepath.Join(root, repo.OrderDir(turn, id)))
|
||||
|
||||
LoadOrderTest(t, s, root, turn, id, o)
|
||||
}
|
||||
|
||||
func LoadOrderTest(t *testing.T, s repo.Storage, root string, turn uint, id uuid.UUID, expected *order.Order) {
|
||||
o, ok, err := repo.LoadOrder_T(s, turn, id)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, ok)
|
||||
assert.Len(t, o.Commands, 5)
|
||||
assert.ElementsMatch(t, expected.Commands, o.Commands)
|
||||
|
||||
CommandResultTest(t, o)
|
||||
}
|
||||
|
||||
func CommandResultTest(t *testing.T, o *order.Order) {
|
||||
assert.NotEmpty(t, o.Commands)
|
||||
for i := range o.Commands {
|
||||
if v, ok := order.AsCommand[*order.CommandRaceVote](o.Commands[i]); ok {
|
||||
assert.NotNil(t, v.CmdApplied)
|
||||
assert.True(t, *v.CmdApplied)
|
||||
assert.Equal(t, 0, *v.CmdErrCode)
|
||||
} else if v, ok := order.AsCommand[*order.CommandRaceQuit](o.Commands[i]); ok {
|
||||
assert.NotNil(t, v.CmdApplied)
|
||||
assert.False(t, *v.CmdApplied)
|
||||
assert.Equal(t, 10, *v.CmdErrCode)
|
||||
} else if v, ok := order.AsCommand[*order.CommandShipClassCreate](o.Commands[i]); ok {
|
||||
assert.NotNil(t, v.CmdApplied)
|
||||
assert.False(t, *v.CmdApplied)
|
||||
assert.Equal(t, 33, *v.CmdErrCode)
|
||||
} else if v, ok := order.AsCommand[*order.CommandShipGroupMerge](o.Commands[i]); ok {
|
||||
assert.NotNil(t, v.CmdApplied)
|
||||
assert.True(t, *v.CmdApplied)
|
||||
assert.Equal(t, 0, *v.CmdErrCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user