feat: turn generate api

This commit is contained in:
IliaDenisov
2026-02-12 14:27:56 +03:00
parent 67f0fdef61
commit 87291d2760
16 changed files with 192 additions and 51 deletions
+22 -5
View File
@@ -7,14 +7,15 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/iliadenisov/galaxy/internal/controller"
e "github.com/iliadenisov/galaxy/internal/error"
"github.com/iliadenisov/galaxy/internal/model/game"
"github.com/iliadenisov/galaxy/internal/model/rest"
)
type CommandExecutor interface {
GenerateGame([]string) (uuid.UUID, error)
GenerateGame([]string) (rest.StateResponse, error)
GenerateTurn() (rest.StateResponse, error)
GameState() (rest.StateResponse, error)
Execute(cmd ...Command) error
}
@@ -50,8 +51,20 @@ func (e *executor) Execute(command ...Command) error {
})
}
func (e *executor) GenerateGame(races []string) (uuid.UUID, error) {
return controller.GenerateGame(e.cfg, races)
func (e *executor) GenerateGame(races []string) (rest.StateResponse, error) {
s, err := controller.GenerateGame(e.cfg, races)
if err != nil {
return rest.StateResponse{}, err
}
return stateResponse(s), nil
}
func (e *executor) GenerateTurn() (rest.StateResponse, error) {
err := controller.GenerateTurn(e.cfg)
if err != nil {
return rest.StateResponse{}, err
}
return e.GameState()
}
func (e *executor) GameState() (rest.StateResponse, error) {
@@ -59,6 +72,10 @@ func (e *executor) GameState() (rest.StateResponse, error) {
if err != nil {
return rest.StateResponse{}, err
}
return stateResponse(s), nil
}
func stateResponse(s game.State) rest.StateResponse {
result := &rest.StateResponse{
ID: s.ID,
Turn: s.Turn,
@@ -70,7 +87,7 @@ func (e *executor) GameState() (rest.StateResponse, error) {
result.Players[i].Name = s.Players[i].Name
result.Players[i].Extinct = s.Players[i].Extinct
}
return *result, nil
return *result
}
func errorResponded(c *gin.Context, err error) bool {
+2 -4
View File
@@ -18,12 +18,10 @@ func InitHandler(c *gin.Context, executor CommandExecutor) {
races[i] = init.Races[i].Name
}
uuid, err := executor.GenerateGame(races)
s, err := executor.GenerateGame(races)
if errorResponded(c, err) {
return
}
c.JSON(http.StatusCreated, rest.InitResponse{
UUID: uuid,
})
c.JSON(http.StatusCreated, s)
}
+17
View File
@@ -0,0 +1,17 @@
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
)
func TurnHandler(c *gin.Context, executor CommandExecutor) {
state, err := executor.GenerateTurn()
if errorResponded(c, err) {
return
}
c.JSON(http.StatusOK, state)
}