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
+15 -10
View File
@@ -17,6 +17,8 @@ import (
"galaxy/model/order"
"galaxy/model/report"
"galaxy/util"
"github.com/google/uuid"
)
const (
@@ -62,7 +64,8 @@ type fsStorage struct {
}
type storedOrder struct {
UpdatedAt int `json:"updatedAt"`
GameID uuid.UUID `json:"game_id"`
UpdatedAt int64 `json:"updatedAt"`
Commands []json.RawMessage `json:"cmd"`
}
@@ -155,7 +158,7 @@ func (s *fsStorage) SaveReportAsync(id client.GameID, turn uint, rep report.Repo
}()
}
func (s *fsStorage) LoadOrderAsync(id client.GameID, turn uint, callback func(order.Order, error)) {
func (s *fsStorage) LoadOrderAsync(id client.GameID, turn uint, callback func(order.UserGamesOrder, error)) {
go func() {
o, err := s.loadOrderSync(id, turn)
if callback != nil {
@@ -164,7 +167,7 @@ func (s *fsStorage) LoadOrderAsync(id client.GameID, turn uint, callback func(or
}()
}
func (s *fsStorage) SaveOrderAsync(id client.GameID, turn uint, o order.Order, callback func(error)) {
func (s *fsStorage) SaveOrderAsync(id client.GameID, turn uint, o order.UserGamesOrder, callback func(error)) {
go func() {
err := s.saveOrderSync(id, turn, o)
if callback != nil {
@@ -320,18 +323,18 @@ func (s *fsStorage) saveReportSync(id client.GameID, turn uint, rep report.Repor
}))
}
func (s *fsStorage) loadOrderSync(id client.GameID, turn uint) (order.Order, error) {
func (s *fsStorage) loadOrderSync(id client.GameID, turn uint) (order.UserGamesOrder, error) {
gameData, err := s.loadGameDataSync(id, turn)
if err != nil {
return order.Order{}, classifyStorageError(err)
return order.UserGamesOrder{}, classifyStorageError(err)
}
if gameData.Order == nil {
return order.Order{}, classifyStorageError(fmt.Errorf("load order for game %q turn %d: %w", id, turn, os.ErrNotExist))
return order.UserGamesOrder{}, classifyStorageError(fmt.Errorf("load order for game %q turn %d: %w", id, turn, os.ErrNotExist))
}
return *gameData.Order, nil
}
func (s *fsStorage) saveOrderSync(id client.GameID, turn uint, o order.Order) error {
func (s *fsStorage) saveOrderSync(id client.GameID, turn uint, o order.UserGamesOrder) error {
absPath, err := s.resolvePath(gameTurnFilePath(id, turn))
if err != nil {
return classifyStorageError(err)
@@ -474,8 +477,9 @@ func (d storedGameData) toGameData() (client.GameData, error) {
return gameData, nil
}
func makeStoredOrder(o order.Order) (storedOrder, error) {
func makeStoredOrder(o order.UserGamesOrder) (storedOrder, error) {
result := storedOrder{
GameID: o.GameID,
UpdatedAt: o.UpdatedAt,
Commands: make([]json.RawMessage, len(o.Commands)),
}
@@ -489,12 +493,13 @@ func makeStoredOrder(o order.Order) (storedOrder, error) {
return result, nil
}
func (o *storedOrder) toOrder() (*order.Order, error) {
func (o *storedOrder) toOrder() (*order.UserGamesOrder, error) {
if o == nil {
return nil, nil
}
result := &order.Order{
result := &order.UserGamesOrder{
GameID: o.GameID,
UpdatedAt: o.UpdatedAt,
Commands: make([]order.DecodableCommand, len(o.Commands)),
}