feat: order processing

feat: order commands result save/load
This commit is contained in:
Ilia Denisov
2026-02-20 17:44:41 +02:00
committed by GitHub
parent 0c0df976bd
commit 233c9ebc2a
26 changed files with 2028 additions and 385 deletions
+179 -32
View File
@@ -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
}