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)
}
+3 -2
View File
@@ -28,9 +28,10 @@ func TestInit(t *testing.T) {
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code, w.Body)
var initResponse rest.InitResponse
var initResponse rest.StateResponse
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &initResponse))
assert.NoError(t, uuid.Validate(initResponse.UUID.String()))
assert.NoError(t, uuid.Validate(initResponse.ID.String()))
assert.NotEqual(t, uuid.Nil, uuid.MustParse(initResponse.ID.String()))
}
func TestInitValidators(t *testing.T) {
+1
View File
@@ -67,6 +67,7 @@ func setupRouter(executor handler.CommandExecutor) *gin.Engine {
groupV1.GET("/status", func(ctx *gin.Context) { handler.StatusHandler(ctx, executor) })
groupV1.POST("/init", func(ctx *gin.Context) { handler.InitHandler(ctx, executor) })
groupV1.PUT("/command", LimitMiddleware(1), func(ctx *gin.Context) { handler.CommandHandler(ctx, executor) })
groupV1.PUT("/turn", func(ctx *gin.Context) { handler.TurnHandler(ctx, executor) })
return r
}
+1
View File
@@ -6,5 +6,6 @@ import (
)
func SetupRouter(e handler.CommandExecutor) *gin.Engine {
gin.SetMode(gin.TestMode)
return setupRouter(e)
}
+6 -4
View File
@@ -4,7 +4,6 @@ import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router"
"github.com/iliadenisov/galaxy/internal/router/handler"
@@ -19,8 +18,12 @@ func (e *dummyExecutor) Execute(command ...handler.Command) error {
return nil
}
func (e *dummyExecutor) GenerateGame(races []string) (uuid.UUID, error) {
return uuid.New(), nil
func (e *dummyExecutor) GenerateGame(races []string) (rest.StateResponse, error) {
return rest.StateResponse{}, nil
}
func (e *dummyExecutor) GenerateTurn() (rest.StateResponse, error) {
return rest.StateResponse{}, nil
}
func (e *dummyExecutor) GameState() (rest.StateResponse, error) {
@@ -32,7 +35,6 @@ func setupRouter() *gin.Engine {
}
func setupRouterExecutor(e handler.CommandExecutor) *gin.Engine {
gin.SetMode(gin.TestMode)
return router.SetupRouter(e)
}
+4 -2
View File
@@ -28,9 +28,10 @@ func TestGetStatus(t *testing.T) {
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code, w.Body)
var initResponse rest.InitResponse
var initResponse rest.StateResponse
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &initResponse))
assert.NoError(t, uuid.Validate(initResponse.UUID.String()))
assert.NoError(t, uuid.Validate(initResponse.ID.String()))
assert.NotEqual(t, uuid.Nil, uuid.MustParse(initResponse.ID.String()))
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/api/v1/status", nil)
@@ -40,6 +41,7 @@ func TestGetStatus(t *testing.T) {
var stateResponse rest.StateResponse
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &stateResponse))
assert.NoError(t, uuid.Validate(stateResponse.ID.String()))
assert.Equal(t, initResponse.ID, stateResponse.ID)
assert.Equal(t, uint(0), stateResponse.Turn)
assert.Equal(t, uint(0), stateResponse.Stage)
assert.Len(t, stateResponse.Players, 10)
+67
View File
@@ -0,0 +1,67 @@
package router_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/uuid"
"github.com/iliadenisov/galaxy/internal/controller"
"github.com/iliadenisov/galaxy/internal/model/rest"
"github.com/iliadenisov/galaxy/internal/router"
"github.com/iliadenisov/galaxy/internal/router/handler"
"github.com/iliadenisov/galaxy/internal/util"
"github.com/stretchr/testify/assert"
)
func TestGetTurn(t *testing.T) {
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
r := router.SetupRouter(handler.NewDefaultConfigExecutor(func(p *controller.Param) { p.StoragePath = root }))
// create game
payload := generateInitRequest(10)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/init", asBody(payload))
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code, w.Body)
var initResponse rest.StateResponse
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &initResponse))
assert.NoError(t, uuid.Validate(initResponse.ID.String()))
assert.NotEqual(t, uuid.Nil, uuid.MustParse(initResponse.ID.String()))
assert.Equal(t, uint(0), initResponse.Turn)
assert.Equal(t, uint(0), initResponse.Stage)
// generate next turn
w = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/api/v1/turn", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, w.Body)
var turnResponse rest.StateResponse
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &turnResponse))
assert.NoError(t, uuid.Validate(turnResponse.ID.String()))
assert.Equal(t, initResponse.ID, turnResponse.ID)
assert.Equal(t, uint(1), turnResponse.Turn)
assert.Equal(t, uint(0), turnResponse.Stage)
// validate status
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/api/v1/status", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, w.Body)
var stateResponse rest.StateResponse
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &stateResponse))
assert.Equal(t, initResponse.ID, stateResponse.ID)
assert.Equal(t, uint(1), stateResponse.Turn)
assert.Equal(t, uint(0), stateResponse.Stage)
}