game_handler.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package handler
  2. import (
  3. "bet24.com/database"
  4. "bet24.com/log"
  5. "bet24.com/servers/cz88ip"
  6. dbengine "bet24.com/servers/micros/dbengine/proto"
  7. pb "bet24.com/servers/micros/game/proto"
  8. "bet24.com/servers/transaction"
  9. "bet24.com/utils"
  10. "context"
  11. "encoding/json"
  12. "fmt"
  13. "sort"
  14. "sync"
  15. "time"
  16. )
  17. var instance *Game
  18. var mgr *gamemgr
  19. func GetInstance() *Game {
  20. if instance == nil {
  21. instance = newHandler()
  22. }
  23. return instance
  24. }
  25. func Dump(cmd, param1, param2 string) {
  26. GetInstance().dump(cmd, param1, param2)
  27. }
  28. func (g *Game) dump(cmd, param1, param2 string) {
  29. switch cmd {
  30. case "list":
  31. for _, v := range g.games {
  32. log.Release("%+v", v)
  33. }
  34. default:
  35. log.Release("game.Dump unhandled cmd %s", cmd)
  36. }
  37. }
  38. func (g *Game) SayHello(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
  39. rsp.Data = fmt.Sprintf("Hello from %s:%s", pb.ServiceName, req.Name)
  40. return nil
  41. }
  42. func (g *Game) GetGame(ctx context.Context, req *pb.Request_GetGame, rsp *pb.Response_GetGame) error {
  43. g.lock.RLock()
  44. defer g.lock.RUnlock()
  45. for k, v := range g.games {
  46. if req.GameID == v.GameID {
  47. rsp.Info = &g.games[k]
  48. return nil
  49. }
  50. }
  51. return nil
  52. }
  53. func (g *Game) GetGameJson(ctx context.Context, req *pb.Request_GetGameJson, rsp *pb.Response) error {
  54. g.lock.RLock()
  55. defer g.lock.RUnlock()
  56. if len(g.games) <= 0 {
  57. return nil
  58. }
  59. var games []pb.GameInfo
  60. games = append(games, g.games...)
  61. /* 暂时注释
  62. // 非元宝场才显示元宝大厅
  63. if config.Server.IsChipRoom == 0 && GameReq.GetReviewGame(userIp, partnerId, versionCode) == "" {
  64. if addr := coreservice.GetGameAddr(userId); addr != "" {
  65. games = append(games, transaction.GameInfo{
  66. GameID: chip_gameid,
  67. ChineseName: "chiphall",
  68. EnglishName: "chiphall",
  69. ServerAddr: addr,
  70. })
  71. }
  72. }
  73. */
  74. retData, err := json.Marshal(games)
  75. if err != nil {
  76. log.Release("GetGameJson GetJson Marshal failed")
  77. return err
  78. }
  79. rsp.Data = string(retData)
  80. return nil
  81. }
  82. func (g *Game) GetGameList(ctx context.Context, req *pb.Request, rsp *pb.Response_GetGameList) error {
  83. g.lock.RLock()
  84. defer g.lock.RUnlock()
  85. rsp.List = g.games
  86. return nil
  87. }
  88. func newHandler() *Game {
  89. ret := new(Game)
  90. ret.ctor()
  91. return ret
  92. }
  93. type Game struct {
  94. games []pb.GameInfo
  95. lock *sync.RWMutex
  96. gameRequests []transaction.GameRequestInfo
  97. }
  98. func (g *Game) ctor() {
  99. mgr = newGameMgr()
  100. g.lock = &sync.RWMutex{}
  101. g.refreshData()
  102. }
  103. func (g *Game) refreshData() {
  104. go g.doRefresh()
  105. time.AfterFunc(5*time.Minute, g.refreshData)
  106. }
  107. func (g *Game) doRefresh() {
  108. gameList := g.getGameListFromDB(false)
  109. objList := transaction.NewTransGameRequest()
  110. objList.DoAction(nil)
  111. g.lock.Lock()
  112. defer g.lock.Unlock()
  113. g.games = gameList
  114. if len(objList.OUT) > 0 {
  115. g.gameRequests = objList.OUT
  116. } else {
  117. g.gameRequests = []transaction.GameRequestInfo{}
  118. }
  119. }
  120. func (g *Game) getGameList(partnerId int) []pb.GameInfo {
  121. statement := database.NewStatement()
  122. statement.SetNeedReturnValue(false)
  123. statement.SetOpenRecordSet(true)
  124. statement.SetProcName("WS_AllGame_GetList")
  125. statement.AddParamter("@PartnerID", database.AdParamInput, database.AdInteger, 4, partnerId)
  126. sqlstring := statement.GenSql()
  127. //log.Debug(sqlstring)
  128. jsonData := dbengine.Execute(sqlstring)
  129. var list []pb.GameInfo
  130. json.Unmarshal([]byte(jsonData), &list)
  131. for i := 0; i < len(list); i++ {
  132. displaySort := list[i].DisplaySort
  133. if len(displaySort) >= 5 {
  134. list[i].GoldSort = displaySort[1:3]
  135. list[i].ChipSort = displaySort[3:]
  136. }
  137. }
  138. return list
  139. }
  140. func (g *Game) getGameListFromDB(isChipRoom bool) []pb.GameInfo {
  141. act := g.getGameList(0)
  142. var ret []pb.GameInfo
  143. for _, v := range act {
  144. if isChipRoom && v.GameType == pb.GameType_Gold {
  145. continue
  146. }
  147. if !isChipRoom && v.GameType == pb.GameType_Chip {
  148. continue
  149. }
  150. ret = append(ret, v)
  151. }
  152. if isChipRoom {
  153. sort.SliceStable(ret, func(i, j int) bool {
  154. return ret[i].ChipSort < ret[j].ChipSort
  155. })
  156. } else {
  157. sort.SliceStable(ret, func(i, j int) bool {
  158. return ret[i].GoldSort < ret[j].GoldSort
  159. })
  160. }
  161. return ret
  162. }
  163. // 获取游戏地址
  164. func (g *Game) GetGameAddr(ctx context.Context, req *pb.Request_GetGameAddr, rsp *pb.Response) error {
  165. rsp.Data = mgr.getAddr(req.UserId)
  166. return nil
  167. }
  168. // 游戏轮询
  169. func (g *Game) GamePolling(ctx context.Context, req *pb.Request_GamePolling, rsp *pb.Response) error {
  170. mgr.polling(req.Addr, req.Players)
  171. return nil
  172. }
  173. func (g *Game) GetReviewGame(ctx context.Context, req *pb.Request_IsInReview, rsp *pb.Response) error {
  174. rsp.Data = g.getReviewGame(req.UserIp, req.PartnerId, req.VersionCode)
  175. return nil
  176. }
  177. func (g *Game) getReviewGame(userIp string, PartnerID, VersionCode int) string {
  178. defer utils.TimeCost(fmt.Sprintf("game.getReviewGame"))()
  179. country, _ := cz88ip.GetCountryAndRegion(userIp)
  180. if country == "美国" {
  181. log.Debug("userIp[%s] is USA", userIp)
  182. return "game"
  183. }
  184. g.lock.Lock()
  185. defer g.lock.Unlock()
  186. for _, val := range g.gameRequests {
  187. if val.PartnerID == PartnerID && val.VersionCode == VersionCode {
  188. log.Debug("GameRequest[%s,%d,%d] return %s", userIp, PartnerID, VersionCode, val.GameName)
  189. return val.GameName
  190. }
  191. }
  192. return ""
  193. }
  194. // 根据IP获取地理位置
  195. func (g *Game) GetCountryAndRegion(ctx context.Context, req *pb.Request_GetCountryAndRegion, rsp *pb.Response_GetCountryAndRegion) error {
  196. rsp.Country, rsp.Region = cz88ip.GetCountryAndRegion(req.IpAddress)
  197. return nil
  198. }