game.pb.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package proto
  2. import (
  3. "context"
  4. "bet24.com/log"
  5. "bet24.com/servers/micros/common"
  6. "github.com/smallnest/rpcx/client"
  7. )
  8. const ServiceName = "game"
  9. var consulAddr = common.Default_Consul_Addr
  10. func getClient() client.XClient {
  11. return common.GetClientPool().GetClient(ServiceName, consulAddr)
  12. }
  13. type Request struct {
  14. Name string
  15. }
  16. type Response struct {
  17. Data string
  18. }
  19. func SetConsulAddr(addr string) {
  20. consulAddr = addr
  21. }
  22. func SayHello(name string) string {
  23. xclient := getClient()
  24. //defer xclient.Close()
  25. args := &Request{
  26. Name: name,
  27. }
  28. reply := &Response{}
  29. err := xclient.Call(context.Background(), "SayHello", args, reply)
  30. if err != nil {
  31. log.Debug("game failed to call: %v", err)
  32. common.GetClientPool().RemoveClient(ServiceName)
  33. return ""
  34. }
  35. log.Debug("SayHello return %s", reply.Data)
  36. return reply.Data
  37. }
  38. func GetGame(gameId int) *GameInfo {
  39. xclient := getClient()
  40. //defer xclient.Close()
  41. args := &Request_GetGame{
  42. GameID: gameId,
  43. }
  44. reply := &Response_GetGame{}
  45. err := xclient.Call(context.Background(), "GetGame", args, reply)
  46. if err != nil {
  47. log.Debug("game.GetGame failed to call: %v", err)
  48. common.GetClientPool().RemoveClient(ServiceName)
  49. return nil
  50. }
  51. return reply.Info
  52. }
  53. func GetGameJson(userId int, userIp string, partnerId, versionCode int) string {
  54. xclient := getClient()
  55. //defer xclient.Close()
  56. args := &Request_GetGameJson{
  57. UserId: userId,
  58. UserIp: userIp,
  59. PartnerId: partnerId,
  60. VersionCode: versionCode,
  61. }
  62. reply := &Response{}
  63. err := xclient.Call(context.Background(), "GetGameJson", args, reply)
  64. if err != nil {
  65. log.Debug("game.GetGameJson failed to call: %v", err)
  66. common.GetClientPool().RemoveClient(ServiceName)
  67. return reply.Data
  68. }
  69. return reply.Data
  70. }
  71. // 获取游戏地址
  72. func GetGameAddr(userId int) string {
  73. xclient := getClient()
  74. //defer xclient.Close()
  75. args := &Request_GetGameAddr{
  76. UserId: userId,
  77. }
  78. reply := &Response{}
  79. err := xclient.Call(context.Background(), "GetGameAddr", args, reply)
  80. if err != nil {
  81. log.Debug("game.GetGameAddr failed to call: %v", err)
  82. common.GetClientPool().RemoveClient(ServiceName)
  83. return reply.Data
  84. }
  85. return reply.Data
  86. }
  87. // 游戏轮询
  88. func GamePolling(addr string, players int) {
  89. xclient := getClient()
  90. //defer xclient.Close()
  91. args := &Request_GamePolling{
  92. Addr: addr,
  93. Players: players,
  94. }
  95. reply := &Response{}
  96. err := xclient.Call(context.Background(), "GamePolling", args, reply)
  97. if err != nil {
  98. log.Debug("game.GamePolling failed to call: %v", err)
  99. common.GetClientPool().RemoveClient(ServiceName)
  100. return
  101. }
  102. return
  103. }
  104. // GetReviewGame
  105. func GetReviewGame(ip string, partnerId, versionCode int) string {
  106. xclient := getClient()
  107. args := &Request_IsInReview{
  108. UserIp: ip,
  109. PartnerId: partnerId,
  110. VersionCode: versionCode,
  111. }
  112. reply := &Response{}
  113. err := xclient.Call(context.Background(), "GetReviewGame", args, reply)
  114. if err != nil {
  115. log.Debug("game.GamePolling failed to call: %v", err)
  116. common.GetClientPool().RemoveClient(ServiceName)
  117. return ""
  118. }
  119. return reply.Data
  120. }
  121. func GetGameList() []GameInfo {
  122. xclient := getClient()
  123. args := &Request{}
  124. reply := &Response_GetGameList{}
  125. err := xclient.Call(context.Background(), "GetGameList", args, reply)
  126. if err != nil {
  127. log.Debug("game.GamePolling failed to call: %v", err)
  128. common.GetClientPool().RemoveClient(ServiceName)
  129. }
  130. return reply.List
  131. }
  132. func GetGameNameByGameId(gameId int) string {
  133. g := GetGame(gameId)
  134. if g == nil {
  135. return ""
  136. }
  137. return g.EnglishName
  138. }
  139. // 根据IP地址获取国家地区地理位置
  140. func GetCountryAndRegion(ipAddress string) (country, region string) {
  141. xClient := getClient()
  142. args := &Request_GetCountryAndRegion{
  143. IpAddress: ipAddress,
  144. }
  145. reply := &Response_GetCountryAndRegion{}
  146. err := xClient.Call(context.Background(), "GetCountryAndRegion", args, reply)
  147. if err != nil {
  148. log.Debug("game.GamePolling failed to call: %v", err)
  149. common.GetClientPool().RemoveClient(ServiceName)
  150. return
  151. }
  152. country = reply.Country
  153. region = reply.Region
  154. return
  155. }