98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
"gitrepo.ru/neonxp/nquest/api"
|
|
"gitrepo.ru/neonxp/nquest/pkg/contextlib"
|
|
"gitrepo.ru/neonxp/nquest/pkg/models"
|
|
"gitrepo.ru/neonxp/nquest/pkg/service"
|
|
)
|
|
|
|
type Engine struct {
|
|
GameService *service.Game
|
|
EngineService *service.Engine
|
|
}
|
|
|
|
// (GET /engine/{uid})
|
|
func (ec *Engine) GameEngine(c echo.Context, uid uuid.UUID) error {
|
|
user := contextlib.GetUser(c)
|
|
|
|
game, err := ec.GameService.GetByID(c.Request().Context(), uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cursor, err := ec.EngineService.GetState(c.Request().Context(), game, user)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, mapCursorToTask(cursor, nil))
|
|
}
|
|
|
|
// (POST /engine/{uid}/code)
|
|
func (ec *Engine) EnterCode(c echo.Context, uid uuid.UUID) error {
|
|
user := contextlib.GetUser(c)
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
game, err := ec.GameService.GetByID(ctx, uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req := &api.EnterCodeJSONRequestBody{}
|
|
if err := c.Bind(req); err != nil {
|
|
return err
|
|
}
|
|
|
|
cursor, err := ec.EngineService.EnterCode(ctx, game, user, req.Code)
|
|
message := api.OkCode
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, service.ErrGameFinished):
|
|
message = api.GameComplete
|
|
case errors.Is(err, service.ErrInvalidCode):
|
|
message = api.InvalidCode
|
|
case errors.Is(err, service.ErrOldCode):
|
|
message = api.OldCode
|
|
case errors.Is(err, service.ErrNextLevel):
|
|
message = api.NextLevel
|
|
default:
|
|
return c.JSON(http.StatusBadRequest, &api.ErrorResponse{
|
|
Code: http.StatusBadRequest,
|
|
Message: err.Error(),
|
|
})
|
|
}
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, mapCursorToTask(cursor, &message))
|
|
}
|
|
|
|
func mapCursorToTask(cursor *models.GameCursor, message *api.TaskViewMessage) *api.TaskView {
|
|
resp := &api.TaskResponse{
|
|
Message: message,
|
|
Codes: make([]api.CodeView, 0, len(cursor.Task.Codes)),
|
|
Solutions: []api.SolutionView{},
|
|
Text: cursor.Task.Text,
|
|
Title: cursor.Task.Title,
|
|
}
|
|
for _, code := range cursor.Task.Codes {
|
|
c := api.CodeView{
|
|
Description: code.Description,
|
|
}
|
|
for _, cd := range cursor.Codes {
|
|
if cd.ID == code.ID {
|
|
c.Code = &cd.Code
|
|
break
|
|
}
|
|
}
|
|
resp.Codes = append(resp.Codes, c)
|
|
}
|
|
|
|
return resp
|
|
}
|