38 lines
758 B
Go
38 lines
758 B
Go
package router
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
e "github.com/iliadenisov/galaxy/internal/error"
|
|
"github.com/iliadenisov/galaxy/internal/game"
|
|
"github.com/iliadenisov/galaxy/internal/model/rest"
|
|
)
|
|
|
|
func StatusHandler(c *gin.Context) {
|
|
g, err := game.LoadState(param())
|
|
|
|
if err == nil {
|
|
c.JSON(http.StatusOK, rest.Status{
|
|
Turn: g.Age,
|
|
Players: len(g.Race),
|
|
})
|
|
return
|
|
}
|
|
|
|
var ge = new(e.GenericError)
|
|
|
|
if errors.As(err, ge) {
|
|
switch ge.Code {
|
|
case e.ErrGameNotInitialized:
|
|
c.Status(http.StatusNotImplemented)
|
|
default:
|
|
c.JSON(http.StatusInternalServerError, gin.H{"generic_error": ge.Error(), "code": ge.Code})
|
|
}
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
}
|