feat: game order api methods
This commit is contained in:
+15
-10
@@ -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)),
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ import (
|
||||
"galaxy/model/client"
|
||||
"galaxy/model/order"
|
||||
"galaxy/model/report"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const testTimeout = time.Second
|
||||
@@ -137,9 +139,9 @@ func TestReportAndOrderRoundTripAsync(t *testing.T) {
|
||||
t.Fatalf("loaded report mismatch\nwant: %#v\ngot: %#v", updatedReport, gotReport.value)
|
||||
}
|
||||
|
||||
loadOrderDone := make(chan callbackResult[order.Order], 1)
|
||||
s.LoadOrderAsync(id, turn, func(got order.Order, err error) {
|
||||
loadOrderDone <- callbackResult[order.Order]{value: got, err: err}
|
||||
loadOrderDone := make(chan callbackResult[order.UserGamesOrder], 1)
|
||||
s.LoadOrderAsync(id, turn, func(got order.UserGamesOrder, err error) {
|
||||
loadOrderDone <- callbackResult[order.UserGamesOrder]{value: got, err: err}
|
||||
})
|
||||
gotOrder := waitResult(t, loadOrderDone)
|
||||
if gotOrder.err != nil {
|
||||
@@ -529,8 +531,9 @@ func sampleReport(turn uint, race string) report.Report {
|
||||
}
|
||||
}
|
||||
|
||||
func sampleOrder() order.Order {
|
||||
return order.Order{
|
||||
func sampleOrder() order.UserGamesOrder {
|
||||
return order.UserGamesOrder{
|
||||
GameID: uuid.New(),
|
||||
UpdatedAt: 1700,
|
||||
Commands: []order.DecodableCommand{
|
||||
&order.CommandPlanetRename{
|
||||
|
||||
Reference in New Issue
Block a user