feat: game order api methods
This commit is contained in:
@@ -41,7 +41,7 @@ func CommandHandler(c *gin.Context, executor CommandExecutor) {
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
c.Status(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func parseCommand(actor string, c json.RawMessage) (Command, error) {
|
||||
|
||||
@@ -25,8 +25,10 @@ type CommandExecutor interface {
|
||||
GameState() (rest.StateResponse, error)
|
||||
BanishRace(string) error
|
||||
LoadReport(actor string, turn uint) (*report.Report, error)
|
||||
// Execute is reserved for future use; any API request for orders should use ValidateOrder
|
||||
Execute(cmd ...Command) error
|
||||
ValidateOrder(actor string, cmd ...order.DecodableCommand) error
|
||||
ValidateOrder(actor string, cmd ...order.DecodableCommand) (*order.UserGamesOrder, error)
|
||||
FetchOrder(actor string, turn uint) (*order.UserGamesOrder, bool, error)
|
||||
}
|
||||
|
||||
type Command func(controller.Ctrl) error
|
||||
@@ -76,10 +78,14 @@ func (e *executor) Execute(cmd ...Command) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (e *executor) ValidateOrder(actor string, cmd ...order.DecodableCommand) error {
|
||||
func (e *executor) ValidateOrder(actor string, cmd ...order.DecodableCommand) (*order.UserGamesOrder, error) {
|
||||
return controller.ValidateOrder(e.cfg, actor, cmd...)
|
||||
}
|
||||
|
||||
func (e *executor) FetchOrder(actor string, turn uint) (*order.UserGamesOrder, bool, error) {
|
||||
return controller.FetchOrder(e.cfg, actor, turn)
|
||||
}
|
||||
|
||||
func (e *executor) GenerateGame(races []string) (rest.StateResponse, error) {
|
||||
s, err := controller.GenerateGame(e.cfg, races)
|
||||
if err != nil {
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func OrderHandler(c *gin.Context, executor CommandExecutor) {
|
||||
func PutOrderHandler(c *gin.Context, executor CommandExecutor) {
|
||||
var cmd rest.Command
|
||||
if errorResponse(c, c.ShouldBindJSON(&cmd)) {
|
||||
return
|
||||
@@ -31,9 +31,38 @@ func OrderHandler(c *gin.Context, executor CommandExecutor) {
|
||||
return
|
||||
}
|
||||
|
||||
if errorResponse(c, executor.ValidateOrder(cmd.Actor, commands...)) {
|
||||
result, err := executor.ValidateOrder(cmd.Actor, commands...)
|
||||
if errorResponse(c, err) {
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
c.JSON(http.StatusAccepted, result)
|
||||
}
|
||||
|
||||
type orderParam struct {
|
||||
Player string `form:"player" binding:"required,notblank"`
|
||||
Turn int `form:"turn" binding:"gte=0"`
|
||||
}
|
||||
|
||||
func GetOrderHandler(c *gin.Context, executor CommandExecutor) {
|
||||
p := &orderParam{}
|
||||
err := c.ShouldBindQuery(p)
|
||||
if errorResponse(c, err) {
|
||||
return
|
||||
}
|
||||
|
||||
order, ok, err := executor.FetchOrder(p.Player, uint(p.Turn))
|
||||
if errorResponse(c, err) {
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
// there was no order previously sent by player
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
var cmd rest.Command
|
||||
if errorResponse(c, c.ShouldBindJSON(&cmd)) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, order)
|
||||
}
|
||||
|
||||
@@ -74,8 +74,11 @@ func setupRouter(executor handler.CommandExecutor) *gin.Engine {
|
||||
groupAdmin.POST("/race/banish", func(ctx *gin.Context) { handler.BanishHandler(ctx, executor) })
|
||||
|
||||
groupV1.GET("/report", func(ctx *gin.Context) { handler.ReportHandler(ctx, executor) })
|
||||
groupV1.PUT("/order", func(ctx *gin.Context) { handler.PutOrderHandler(ctx, executor) })
|
||||
groupV1.GET("/order", func(ctx *gin.Context) { handler.GetOrderHandler(ctx, executor) })
|
||||
|
||||
// /command is reserved for future use; any API request for orders should use /order
|
||||
groupV1.PUT("/command", LimitMiddleware(1), func(ctx *gin.Context) { handler.CommandHandler(ctx, executor) })
|
||||
groupV1.PUT("/order", func(ctx *gin.Context) { handler.OrderHandler(ctx, executor) })
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
commandNoErrorsStatus = http.StatusNoContent
|
||||
commandNoErrorsStatus = http.StatusAccepted
|
||||
commandDefaultActor = "Gorlum"
|
||||
apiCommandMethod = "PUT"
|
||||
apiCommandPath = "/api/v1/command"
|
||||
@@ -34,9 +34,13 @@ type dummyExecutor struct {
|
||||
CommandsExecuted int
|
||||
}
|
||||
|
||||
func (e *dummyExecutor) ValidateOrder(actor string, cmd ...order.DecodableCommand) error {
|
||||
func (e *dummyExecutor) ValidateOrder(actor string, cmd ...order.DecodableCommand) (*order.UserGamesOrder, error) {
|
||||
e.CommandsExecuted = len(cmd)
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (e *dummyExecutor) FetchOrder(actor string, turn uint) (*order.UserGamesOrder, bool, error) {
|
||||
return nil, true, nil
|
||||
}
|
||||
|
||||
func (e *dummyExecutor) Execute(command ...handler.Command) error {
|
||||
|
||||
Reference in New Issue
Block a user