client.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package client
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/micros/common"
  5. "context"
  6. _ "encoding/json"
  7. rpcx_client "github.com/smallnest/rpcx/client"
  8. _ "github.com/smallnest/rpcx/protocol"
  9. )
  10. var consulAddr = common.Default_Consul_Addr
  11. func getClient(serviceName string) rpcx_client.XClient {
  12. return common.GetClientPool().GetClient(serviceName, consulAddr)
  13. }
  14. type Reply struct {
  15. }
  16. func SetConsulAddr(addr string) {
  17. consulAddr = addr
  18. }
  19. type ServerStatus struct {
  20. GameId int
  21. GameRule string
  22. Desc string
  23. TargetOptions []int
  24. UserOptions []int
  25. PlayTimeOptions []int
  26. }
  27. const (
  28. Roomstatus_start = "roomstatus_start"
  29. Roomstatus_end = "roomstatus_end"
  30. Roomstatus_scorechanged = "roomstatus_scorechanged"
  31. Roomstatus_statuschanged = "roomstatus_statuschanged"
  32. )
  33. type RoomStatus struct {
  34. Method string
  35. RoomNo int
  36. UserId int `json:",omitempty"`
  37. Score int `json:",omitempty"`
  38. OldStatus int `json:",omitempty"`
  39. NewStatus int `json:",omitempty"`
  40. Winners []int `json:",omitempty"`
  41. }
  42. func OnGameRuleRegistered(addr string, gameId int, gameRule, desc string, targetOptions, userOptions, playTimeOpions []int) bool {
  43. //log.Debug("OnGameRuleRegistered %s", addr)
  44. xclient := getClient(addr)
  45. err := xclient.Call(context.Background(), "OnGameRuleRegistered", &ServerStatus{
  46. GameId: gameId,
  47. GameRule: gameRule,
  48. Desc: desc,
  49. TargetOptions: targetOptions,
  50. UserOptions: userOptions,
  51. PlayTimeOptions: playTimeOpions,
  52. }, nil)
  53. if err != nil {
  54. log.Debug("OnGameRuleRegistered failed to call: %v", err)
  55. common.GetClientPool().RemoveClient(addr)
  56. return false
  57. }
  58. return true
  59. }
  60. func OnGameRuleDeregistered(addr string, gameId int, gameRule string) bool {
  61. //log.Debug("OnGameRuleRegistered %s", addr)
  62. xclient := getClient(addr)
  63. err := xclient.Call(context.Background(), "OnGameRuleDeregistered", &ServerStatus{
  64. GameId: gameId,
  65. GameRule: gameRule,
  66. }, nil)
  67. if err != nil {
  68. log.Debug("OnGameRuleDeregistered failed to call: %v", err)
  69. common.GetClientPool().RemoveClient(addr)
  70. return false
  71. }
  72. return true
  73. }
  74. func OnRoomStatusInfo(addr string, method string, roomNo int, userId int, score int, oldStatus, newStatus int, winners []int) bool {
  75. xclient := getClient(addr)
  76. err := xclient.Call(context.Background(), "OnRoomStatusInfo", &RoomStatus{
  77. Method: method,
  78. RoomNo: roomNo,
  79. UserId: userId,
  80. Score: score,
  81. OldStatus: oldStatus,
  82. NewStatus: newStatus,
  83. Winners: winners,
  84. }, nil)
  85. if err != nil {
  86. log.Debug("OnRoomStatus failed to call: %v", err)
  87. common.GetClientPool().RemoveClient(addr)
  88. return false
  89. }
  90. return true
  91. }