api.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package game
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. "github.com/sirupsen/logrus"
  8. "github.com/spf13/viper"
  9. "gogs.daxia.dev/huanan/pkg.daxia.dev/webutils"
  10. )
  11. //对接游戏用的API
  12. type QznnApi struct {
  13. }
  14. func (p QznnApi) Ready(roomID, userID uint32) (gin.H, error) {
  15. gameBaseUrl := viper.GetString("game.base_url")
  16. reqUrl := gameBaseUrl + "/api/bs/qznn/ready"
  17. readyReq := ReadyReq{
  18. RoomID: roomID,
  19. UserID: userID,
  20. }
  21. readyReqData, err := json.Marshal(readyReq)
  22. if err != nil {
  23. logrus.Error(err)
  24. return nil, err
  25. }
  26. respData, err := webutils.PostResp[gin.H](reqUrl, string(readyReqData), time.Second*3)
  27. if err != nil {
  28. logrus.Error(err)
  29. return nil, err
  30. }
  31. if respData.Code != webutils.CodeSuccess {
  32. logrus.Error(respData)
  33. return nil, errors.New(respData.Msg)
  34. }
  35. return respData.Data, nil
  36. }
  37. func (p QznnApi) ChooseMaster(roomID, userID uint32, mul int) (gin.H, error) {
  38. gameBaseUrl := viper.GetString("game.base_url")
  39. reqUrl := gameBaseUrl + "/api/bs/qznn/choose_master"
  40. req := ChooseMasterReq{
  41. RoomID: roomID,
  42. UserID: userID,
  43. Mul: mul,
  44. }
  45. readyReqData, err := json.Marshal(req)
  46. if err != nil {
  47. logrus.Error(err)
  48. return nil, err
  49. }
  50. respData, err := webutils.PostResp[gin.H](reqUrl, string(readyReqData), time.Second*3)
  51. if err != nil {
  52. logrus.Error(err)
  53. return nil, err
  54. }
  55. if respData.Code != webutils.CodeSuccess {
  56. logrus.Error(respData)
  57. return nil, errors.New(respData.Msg)
  58. }
  59. return respData.Data, nil
  60. }
  61. func (p QznnApi) ChooseMul(roomID, userID uint32, mul int) (gin.H, error) {
  62. gameBaseUrl := viper.GetString("game.base_url")
  63. reqUrl := gameBaseUrl + "/api/bs/qznn/choose_mul"
  64. req := ChooseMulReq{
  65. RoomID: roomID,
  66. UserID: userID,
  67. Mul: mul,
  68. }
  69. readyReqData, err := json.Marshal(req)
  70. if err != nil {
  71. logrus.Error(err)
  72. return nil, err
  73. }
  74. respData, err := webutils.PostResp[gin.H](reqUrl, string(readyReqData), time.Second*3)
  75. if err != nil {
  76. logrus.Error(err)
  77. return nil, err
  78. }
  79. if respData.Code != webutils.CodeSuccess {
  80. logrus.Error(respData)
  81. return nil, errors.New(respData.Msg)
  82. }
  83. return respData.Data, nil
  84. }
  85. func (p QznnApi) Open(roomID, userID uint32) (gin.H, error) {
  86. gameBaseUrl := viper.GetString("game.base_url")
  87. reqUrl := gameBaseUrl + "/api/bs/qznn/open"
  88. req := OpenReq{
  89. RoomID: roomID,
  90. UserID: userID,
  91. }
  92. readyReqData, err := json.Marshal(req)
  93. if err != nil {
  94. logrus.Error(err)
  95. return nil, err
  96. }
  97. respData, err := webutils.PostResp[gin.H](reqUrl, string(readyReqData), time.Second*3)
  98. if err != nil {
  99. logrus.Error(err)
  100. return nil, err
  101. }
  102. if respData.Code != webutils.CodeSuccess {
  103. logrus.Error(respData)
  104. return nil, errors.New(respData.Msg)
  105. }
  106. return respData.Data, nil
  107. }