You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

172 lines
3.5 KiB

package endpoint
import (
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/arnaucube/gogame/constants"
"github.com/fatih/color"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
)
func fail(c *gin.Context, err error, msg string) {
color.Red(msg + ": " + err.Error())
c.JSON(400, gin.H{
"error": msg,
})
}
func handleMain(c *gin.Context) {
c.JSON(200, gin.H{
"status": "wellcome to gogame",
})
}
func handleInfo(c *gin.Context) {
c.JSON(200, gin.H{
"info": "info",
})
}
type RegisterMsg struct {
Name string `json:"name" binding:"required"`
Password string `json:"password" binding:"required"`
Email string `json:"email" binding:"required"`
}
func handleRegister(c *gin.Context) {
var registerMsg RegisterMsg
err := c.BindJSON(&registerMsg)
if err != nil {
fail(c, err, "error on json")
return
}
user, err := userservice.Register(registerMsg.Name, registerMsg.Password, registerMsg.Email)
if err != nil {
fail(c, err, "error on register")
return
}
c.JSON(200, gin.H{
"user": user,
})
}
type LoginMsg struct {
Email string `binding:"required"`
Password string `binding:"required"`
}
func handleLogin(c *gin.Context) {
var loginMsg LoginMsg
err := c.BindJSON(&loginMsg)
if err != nil {
fail(c, err, "error on json")
return
}
token, user, err := userservice.Login(loginMsg.Email, loginMsg.Password)
if err != nil {
fail(c, err, "error on login")
return
}
c.JSON(200, gin.H{
"token": token,
"user": user,
})
}
func handleGetUser(c *gin.Context) {
claims := jwt.ExtractClaims(c)
userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
user, err := userservice.GetUserById(userid)
if err != nil {
fail(c, err, "error on getting user")
return
}
c.JSON(200, gin.H{
"user": user,
})
}
func handleGetResources(c *gin.Context) {
claims := jwt.ExtractClaims(c)
userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
user, err := userservice.GetUserById(userid)
if err != nil {
fail(c, err, "error on getting user")
return
}
c.JSON(200, gin.H{
"user": user,
})
}
func handleGetUserPlanets(c *gin.Context) {
claims := jwt.ExtractClaims(c)
userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
planets, err := userservice.GetUserPlanetsById(userid)
if err != nil {
fail(c, err, "error on getting user planets")
return
}
c.JSON(200, gin.H{
"planets": planets,
})
}
func handleGetPlanet(c *gin.Context) {
claims := jwt.ExtractClaims(c)
userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
planetid := c.Param("planetid")
user, err := userservice.GetUserById(userid)
if err != nil {
fail(c, err, "error on getting user")
return
}
planet, err := gameservice.GetPlanet(user, bson.ObjectIdHex(planetid))
if err != nil {
fail(c, err, "error upgrading building")
return
}
c.JSON(200, gin.H{
"planet": planet,
})
}
type BuildMsg struct {
PlanetId string
Building string
}
func handlePostUpgradeBuilding(c *gin.Context) {
claims := jwt.ExtractClaims(c)
userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
var buildMsg BuildMsg
err := c.BindJSON(&buildMsg)
if err != nil {
fail(c, err, "error parsing json")
return
}
user, err := userservice.GetUserById(userid)
if err != nil {
fail(c, err, "error on getting user")
return
}
planet, err := gameservice.UpgradeBuilding(user, bson.ObjectIdHex(buildMsg.PlanetId), buildMsg.Building)
if err != nil {
fail(c, err, "error upgrading building")
return
}
c.JSON(200, gin.H{
"planet": planet,
})
}