handler.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package handler
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/micros/guess/handler/match"
  5. "bet24.com/servers/micros/guess/handler/team"
  6. pb "bet24.com/servers/micros/guess/proto"
  7. "golang.org/x/net/context"
  8. "math/rand"
  9. "time"
  10. )
  11. var instance *guess
  12. type guess struct {
  13. logPrint bool // 日志打印
  14. }
  15. func GetInstance() *guess {
  16. if instance == nil {
  17. instance = newHandler()
  18. }
  19. return instance
  20. }
  21. func newHandler() *guess {
  22. ret := new(guess)
  23. ret.ctor()
  24. return ret
  25. }
  26. func (g *guess) ctor() {
  27. rand.Seed(time.Now().Unix())
  28. // 球队
  29. team.Run()
  30. // 赛事
  31. match.Run()
  32. }
  33. func Dump(cmd, param1, param2 string) {
  34. GetInstance().dump(cmd, param1, param2)
  35. }
  36. func (this *guess) dump(cmd, param1, param2 string) {
  37. switch cmd {
  38. case "match": // 打印赛事
  39. match.Dump(param1, param2)
  40. case "team": // 打印球队
  41. team.Dump(param1, param2)
  42. case "log":
  43. if param1 == "open" {
  44. this.logPrint = true
  45. log.Debug("消息指令跟踪已开启...")
  46. } else {
  47. this.logPrint = false
  48. log.Debug("消息指令跟踪已关闭!")
  49. }
  50. case "exit":
  51. // do nothing
  52. default:
  53. log.Release("guess.Dump unhandled cmd %s", cmd)
  54. }
  55. }
  56. // 指令消息处理
  57. func (this *guess) OnGuessMsg(ctx context.Context, req *pb.Request, resp *pb.Response) error {
  58. resp.Data = this.messageHandler(req.UserId, req.Msg, req.Data)
  59. // 打印日志
  60. if this.logPrint {
  61. switch req.Msg {
  62. case "guessGetMatchList":
  63. log.Debug("OnAudioRoomMsg request=%+v ==> resp.Data=%s", req, req.Data)
  64. default:
  65. log.Debug("OnAudioRoomMsg request=%+v", req)
  66. }
  67. }
  68. return nil
  69. }
  70. // 消息分发处理器
  71. func (this *guess) messageHandler(userId int, msg, data string) string {
  72. switch msg {
  73. case "guessGetConfig": // 配置
  74. return match.GetConfig(userId, data)
  75. case "guessGetTeam": // 获取球队
  76. return team.GetTeamJson(userId, data)
  77. case "guessGetTeamList": // 球队列表
  78. return team.GetTeamList(userId, data)
  79. case "guessAddTeam": // 添加球队
  80. return team.AddTeam(userId, data)
  81. case "guessUpdateTeam": // 修改球队
  82. return team.UpdateTeam(userId, data, match.RefreshMatchTeam)
  83. case "guessGetMatchList": // 获取赛事列表
  84. return match.GetMatchList(userId, data)
  85. case "guessGetMatchInfo": // 获取赛事信息
  86. return match.GetMatchInfo(userId, data)
  87. case "guessBet": // 投注
  88. return match.Bet(userId, data)
  89. case "guessSetResult": // 设置赛事结果
  90. return match.SetResult(userId, data)
  91. case "guessAward": // 派奖
  92. return match.Award(userId, data)
  93. case "guessAddMatch": // 添加赛事
  94. return match.AddMatch(userId, data)
  95. case "guessUpdateMatch": // 修改赛事
  96. return match.UpdateMatch(userId, data)
  97. case "guessAddMatchTeam": // 添加赛事球队
  98. return match.AddMatchTeam(userId, data)
  99. case "guessUpdateMatchTeam": // 修改赛事球队
  100. return match.UpdateMatchTeam(userId, data)
  101. case "guessAddMatchBet": // 添加赛事投注选项
  102. return match.AddMatchBet(userId, data)
  103. case "guessUpdateMatchBet": // 修改赛事投注选项
  104. return match.UpdateMatchBet(userId, data)
  105. case "guessSetMatchOpen": // 设置赛事开启
  106. return match.SetMatchOpen(userId, data)
  107. case "guessGetUserBetRecordList": // 用户投注记录
  108. return match.GetUserBetRecordList(userId, data)
  109. default:
  110. // do nothing
  111. }
  112. log.Error("handler.messageHandler unprocessed userId=%d msg=%s data=%s", userId, msg, data)
  113. return ""
  114. }