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.

176 lines
3.7 KiB

  1. package endpoint
  2. import (
  3. jwt "github.com/appleboy/gin-jwt/v2"
  4. "github.com/arnaucube/gogame/constants"
  5. "github.com/fatih/color"
  6. "github.com/gin-gonic/gin"
  7. "gopkg.in/mgo.v2/bson"
  8. )
  9. func fail(c *gin.Context, err error, msg string) {
  10. color.Red(msg + ": " + err.Error())
  11. c.JSON(400, gin.H{
  12. "error": msg,
  13. })
  14. }
  15. func handleMain(c *gin.Context) {
  16. c.JSON(200, gin.H{
  17. "status": "wellcome to gogame",
  18. })
  19. }
  20. func handleInfo(c *gin.Context) {
  21. c.JSON(200, gin.H{
  22. "info": "info",
  23. })
  24. }
  25. type RegisterMsg struct {
  26. Name string `json:"name" binding:"required"`
  27. Password string `json:"password" binding:"required"`
  28. Email string `json:"email" binding:"required"`
  29. }
  30. func handleRegister(c *gin.Context) {
  31. var registerMsg RegisterMsg
  32. c.BindJSON(&registerMsg)
  33. user, err := userservice.Register(registerMsg.Name, registerMsg.Password, registerMsg.Email)
  34. if err != nil {
  35. fail(c, err, "error on register")
  36. return
  37. }
  38. c.JSON(200, gin.H{
  39. "user": user,
  40. })
  41. }
  42. type LoginMsg struct {
  43. Email string `binding:"required"`
  44. Password string `binding:"required"`
  45. }
  46. func handleLogin(c *gin.Context) {
  47. var loginMsg LoginMsg
  48. c.BindJSON(&loginMsg)
  49. token, user, err := userservice.Login(loginMsg.Email, loginMsg.Password)
  50. if err != nil {
  51. fail(c, err, "error on login")
  52. return
  53. }
  54. c.JSON(200, gin.H{
  55. "token": token,
  56. "user": user,
  57. })
  58. }
  59. func handleGetUser(c *gin.Context) {
  60. claims := jwt.ExtractClaims(c)
  61. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  62. user, err := userservice.GetUserById(userid)
  63. if err != nil {
  64. fail(c, err, "error on getting user")
  65. return
  66. }
  67. // resources, err := user.GetResources()
  68. // if err != nil {
  69. // fail(c, err, "error on getting user resources")
  70. // return
  71. // }
  72. c.JSON(200, gin.H{
  73. "user": user,
  74. // "resources": resources,
  75. })
  76. }
  77. func handleGetResources(c *gin.Context) {
  78. claims := jwt.ExtractClaims(c)
  79. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  80. user, err := userservice.GetUserById(userid)
  81. if err != nil {
  82. fail(c, err, "error on getting user")
  83. return
  84. }
  85. resources, err := user.GetResources()
  86. if err != nil {
  87. fail(c, err, "error on getting user resources")
  88. return
  89. }
  90. c.JSON(200, gin.H{
  91. "user": user,
  92. "resources": resources,
  93. })
  94. }
  95. func handleGetUserPlanets(c *gin.Context) {
  96. claims := jwt.ExtractClaims(c)
  97. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  98. planets, err := userservice.GetUserPlanetsById(userid)
  99. if err != nil {
  100. fail(c, err, "error on getting user planets")
  101. return
  102. }
  103. c.JSON(200, gin.H{
  104. "planets": planets,
  105. })
  106. }
  107. func handleGetPlanet(c *gin.Context) {
  108. claims := jwt.ExtractClaims(c)
  109. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  110. planetid := c.Param("planetid")
  111. user, err := userservice.GetUserById(userid)
  112. if err != nil {
  113. fail(c, err, "error on getting user")
  114. return
  115. }
  116. planet, err := gameservice.GetBuildings(user, bson.ObjectIdHex(planetid))
  117. if err != nil {
  118. fail(c, err, "error upgrading building")
  119. return
  120. }
  121. c.JSON(200, gin.H{
  122. "planet": planet,
  123. })
  124. }
  125. type BuildMsg struct {
  126. PlanetId string
  127. Building string
  128. }
  129. func handlePostUpgradeBuilding(c *gin.Context) {
  130. claims := jwt.ExtractClaims(c)
  131. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  132. var buildMsg BuildMsg
  133. err := c.BindJSON(&buildMsg)
  134. if err != nil {
  135. fail(c, err, "error parsing json")
  136. return
  137. }
  138. user, err := userservice.GetUserById(userid)
  139. if err != nil {
  140. fail(c, err, "error on getting user")
  141. return
  142. }
  143. planet, err := gameservice.UpgradeBuilding(user, bson.ObjectIdHex(buildMsg.PlanetId), buildMsg.Building)
  144. if err != nil {
  145. fail(c, err, "error upgrading building")
  146. return
  147. }
  148. c.JSON(200, gin.H{
  149. "planet": planet,
  150. })
  151. }