feat: game order api methods

This commit is contained in:
Ilia Denisov
2026-05-09 10:36:44 +02:00
parent f2a7f2b515
commit 2a1e80053a
17 changed files with 166 additions and 75 deletions
+17 -11
View File
@@ -29,7 +29,9 @@ const (
)
type storedOrder struct {
Commands []json.RawMessage `json:"cmd"`
GameID uuid.UUID `json:"game_id"`
UpdatedAt int64 `json:"updatedAt"`
Commands []json.RawMessage `json:"cmd"`
}
func (o storedOrder) MarshalBinary() (data []byte, err error) {
@@ -201,11 +203,11 @@ func loadReport(s Storage, t uint, id uuid.UUID) (*report.Report, error) {
return result, nil
}
func (r *repo) SaveOrder(t uint, id uuid.UUID, o *order.Order) error {
func (r *repo) SaveOrder(t uint, id uuid.UUID, o *order.UserGamesOrder) error {
return saveOrder(r.s, t, id, o)
}
func saveOrder(s Storage, t uint, id uuid.UUID, o *order.Order) error {
func saveOrder(s Storage, t uint, id uuid.UUID, o *order.UserGamesOrder) error {
path := OrderDir(t, id)
if err := s.WriteSafe(path, o); err != nil {
return NewStorageError(err)
@@ -213,11 +215,11 @@ func saveOrder(s Storage, t uint, id uuid.UUID, o *order.Order) error {
return nil
}
func (r *repo) LoadOrder(t uint, id uuid.UUID) (*order.Order, bool, error) {
func (r *repo) LoadOrder(t uint, id uuid.UUID) (*order.UserGamesOrder, bool, error) {
return loadOrder(r.s, t, id)
}
func loadOrder(s Storage, t uint, id uuid.UUID) (*order.Order, bool, error) {
func loadOrder(s Storage, t uint, id uuid.UUID) (*order.UserGamesOrder, bool, error) {
path := OrderDir(t, id)
exist, err := s.Exists(path)
@@ -228,17 +230,21 @@ func loadOrder(s Storage, t uint, id uuid.UUID) (*order.Order, bool, error) {
return nil, false, nil
}
cmd := new(storedOrder)
if err := s.ReadSafe(path, cmd); err != nil {
stored := new(storedOrder)
if err := s.ReadSafe(path, stored); err != nil {
return nil, false, NewStorageError(err)
}
result := &order.Order{Commands: make([]order.DecodableCommand, len(cmd.Commands))}
if len(cmd.Commands) == 0 {
result := &order.UserGamesOrder{
GameID: stored.GameID,
UpdatedAt: stored.UpdatedAt,
Commands: make([]order.DecodableCommand, len(stored.Commands)),
}
if len(stored.Commands) == 0 {
return nil, false, errors.New("no commands were stored")
}
for i := range cmd.Commands {
command, err := ParseOrder(cmd.Commands[i], nil)
for i := range stored.Commands {
command, err := ParseOrder(stored.Commands[i], nil)
if err != nil {
return nil, false, err
}
+2 -2
View File
@@ -6,10 +6,10 @@ import (
"github.com/google/uuid"
)
func LoadOrder_T(s Storage, t uint, id uuid.UUID) (*order.Order, bool, error) {
func LoadOrder_T(s Storage, t uint, id uuid.UUID) (*order.UserGamesOrder, bool, error) {
return loadOrder(s, t, id)
}
func SaveOrder_T(s Storage, t uint, id uuid.UUID, o *order.Order) error {
func SaveOrder_T(s Storage, t uint, id uuid.UUID, o *order.UserGamesOrder) error {
return saveOrder(s, t, id, o)
}
+10 -3
View File
@@ -3,6 +3,7 @@ package repo_test
import (
"path/filepath"
"testing"
"time"
"galaxy/model/order"
@@ -18,7 +19,11 @@ func TestSaveOrder(t *testing.T) {
s, err := fs.NewFileStorage(root)
assert.NoError(t, err)
id := uuid.New()
o := &order.Order{
gameID := uuid.New()
now := time.Now().UTC().UnixMilli()
o := &order.UserGamesOrder{
GameID: gameID,
UpdatedAt: now,
Commands: []order.DecodableCommand{
&order.CommandRaceVote{
CommandMeta: order.CommandMeta{
@@ -87,17 +92,19 @@ func TestSaveOrder(t *testing.T) {
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) {
func LoadOrderTest(t *testing.T, s repo.Storage, root string, turn uint, id uuid.UUID, expected *order.UserGamesOrder) {
o, ok, err := repo.LoadOrder_T(s, turn, id)
assert.NoError(t, err)
assert.True(t, ok)
assert.Len(t, o.Commands, 5)
assert.Equal(t, expected.GameID, o.GameID)
assert.Equal(t, expected.UpdatedAt, o.UpdatedAt)
assert.ElementsMatch(t, expected.Commands, o.Commands)
CommandResultTest(t, o)
}
func CommandResultTest(t *testing.T, o *order.Order) {
func CommandResultTest(t *testing.T, o *order.UserGamesOrder) {
assert.NotEmpty(t, o.Commands)
for i := range o.Commands {
if v, ok := order.AsCommand[*order.CommandRaceVote](o.Commands[i]); ok {