package controller import ( "net/http" "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 int) error { user := contextlib.GetUser(c) game, err := ec.GameService.GetByID(c.Request().Context(), uint(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)) } // (POST /engine/{uid}/code) func (ec *Engine) EnterCode(c echo.Context, uid int) error { user := contextlib.GetUser(c) ctx := c.Request().Context() game, err := ec.GameService.GetByID(ctx, uint(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) if err != nil { return err } return c.JSON(http.StatusOK, mapCursorToTask(cursor)) } func mapCursorToTask(cursor *models.GameCursor) *api.TaskView { resp := &api.TaskResponse{ Codes: make([]api.CodeView, 0, len(cursor.Task.Codes)), Entered: make([]api.CodeView, 0, len(cursor.Codes)), Solutions: []api.SolutionView{}, Text: cursor.Task.Text, Title: cursor.Task.Title, } for _, code := range cursor.Task.Codes { resp.Codes = append(resp.Codes, api.CodeView{ Description: code.Description, }) } for _, code := range cursor.Codes { resp.Entered = append(resp.Entered, api.CodeView{ Code: &code.Code, Description: code.Description, }) } return resp }