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

  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. err := c.BindJSON(&registerMsg)
  33. if err != nil {
  34. fail(c, err, "error on json")
  35. return
  36. }
  37. user, err := userservice.Register(registerMsg.Name, registerMsg.Password, registerMsg.Email)
  38. if err != nil {
  39. fail(c, err, "error on register")
  40. return
  41. }
  42. c.JSON(200, gin.H{
  43. "user": user,
  44. })
  45. }
  46. type LoginMsg struct {
  47. Email string `binding:"required"`
  48. Password string `binding:"required"`
  49. }
  50. func handleLogin(c *gin.Context) {
  51. var loginMsg LoginMsg
  52. err := c.BindJSON(&loginMsg)
  53. if err != nil {
  54. fail(c, err, "error on json")
  55. return
  56. }
  57. token, user, err := userservice.Login(loginMsg.Email, loginMsg.Password)
  58. if err != nil {
  59. fail(c, err, "error on login")
  60. return
  61. }
  62. c.JSON(200, gin.H{
  63. "token": token,
  64. "user": user,
  65. })
  66. }
  67. func handleGetUser(c *gin.Context) {
  68. claims := jwt.ExtractClaims(c)
  69. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  70. user, err := userservice.GetUserById(userid)
  71. if err != nil {
  72. fail(c, err, "error on getting user")
  73. return
  74. }
  75. c.JSON(200, gin.H{
  76. "user": user,
  77. })
  78. }
  79. func handleGetResources(c *gin.Context) {
  80. claims := jwt.ExtractClaims(c)
  81. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  82. user, err := userservice.GetUserById(userid)
  83. if err != nil {
  84. fail(c, err, "error on getting user")
  85. return
  86. }
  87. c.JSON(200, gin.H{
  88. "user": user,
  89. })
  90. }
  91. func handleGetUserPlanets(c *gin.Context) {
  92. claims := jwt.ExtractClaims(c)
  93. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  94. planets, err := userservice.GetUserPlanetsById(userid)
  95. if err != nil {
  96. fail(c, err, "error on getting user planets")
  97. return
  98. }
  99. c.JSON(200, gin.H{
  100. "planets": planets,
  101. })
  102. }
  103. func handleGetPlanet(c *gin.Context) {
  104. claims := jwt.ExtractClaims(c)
  105. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  106. planetid := c.Param("planetid")
  107. user, err := userservice.GetUserById(userid)
  108. if err != nil {
  109. fail(c, err, "error on getting user")
  110. return
  111. }
  112. planet, err := gameservice.GetPlanet(user, bson.ObjectIdHex(planetid))
  113. if err != nil {
  114. fail(c, err, "error upgrading building")
  115. return
  116. }
  117. c.JSON(200, gin.H{
  118. "planet": planet,
  119. })
  120. }
  121. type BuildMsg struct {
  122. PlanetId string
  123. Building string
  124. }
  125. func handlePostUpgradeBuilding(c *gin.Context) {
  126. claims := jwt.ExtractClaims(c)
  127. userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
  128. var buildMsg BuildMsg
  129. err := c.BindJSON(&buildMsg)
  130. if err != nil {
  131. fail(c, err, "error parsing json")
  132. return
  133. }
  134. user, err := userservice.GetUserById(userid)
  135. if err != nil {
  136. fail(c, err, "error on getting user")
  137. return
  138. }
  139. planet, err := gameservice.UpgradeBuilding(user, bson.ObjectIdHex(buildMsg.PlanetId), buildMsg.Building)
  140. if err != nil {
  141. fail(c, err, "error upgrading building")
  142. return
  143. }
  144. c.JSON(200, gin.H{
  145. "planet": planet,
  146. })
  147. }