client.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package client
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/micros/common"
  5. "context"
  6. rpcx_client "github.com/smallnest/rpcx/client"
  7. )
  8. var consulAddr = common.Default_Consul_Addr
  9. func getClient(serviceName string) rpcx_client.XClient {
  10. return common.GetClientPool().GetClient(serviceName, consulAddr)
  11. }
  12. func SetConsulAddr(addr string) {
  13. consulAddr = addr
  14. }
  15. type Reply struct {
  16. Data string
  17. RetCode int
  18. }
  19. type Request_Create struct {
  20. Creator int // 创建者
  21. GameRule string // 游戏规则
  22. Target int // 局数或分数
  23. UserCount int // 对局人数
  24. //Fee int // 门票费用
  25. PlayTimeout int
  26. }
  27. type Request_StatusChanged struct {
  28. RoomNo int
  29. OldStatus int
  30. NewStatus int
  31. }
  32. type Request_ForceUserEnter struct {
  33. UserId int
  34. RoomNo int
  35. ChairId int
  36. }
  37. type Request_ExtraParams struct {
  38. RoomNo int
  39. UserId int
  40. ParamId int
  41. Data string
  42. }
  43. func CreatePrivateRoom(addr string, param Request_Create) int {
  44. log.Debug("CreatePrivateRoom addr = %s,Game = %s", addr, param.GameRule)
  45. xclient := getClient(addr)
  46. reply := &Reply{}
  47. err := xclient.Call(context.Background(), "CreatePrivateRoom", &param, reply)
  48. if err != nil {
  49. log.Debug("CreatePrivateRoom failed to call: %v", err)
  50. common.GetClientPool().RemoveClient(addr)
  51. return 0
  52. }
  53. return reply.RetCode
  54. }
  55. func OnRoomStatusChanged(addr string, roomNo, oldStatus, newStatus int) {
  56. log.Debug("OnRoomStatusChanged %d %d->%d", roomNo, oldStatus, newStatus)
  57. xclient := getClient(addr)
  58. param := &Request_StatusChanged{
  59. RoomNo: roomNo,
  60. OldStatus: oldStatus,
  61. NewStatus: newStatus,
  62. }
  63. err := xclient.Call(context.Background(), "OnRoomStatusChanged", param, nil)
  64. if err != nil {
  65. log.Debug("OnRoomStatusChanged failed to call: %v", err)
  66. common.GetClientPool().RemoveClient(addr)
  67. }
  68. }
  69. func OnRoomDismissed(addr string, roomNo int) {
  70. xclient := getClient(addr)
  71. param := &Request_StatusChanged{
  72. RoomNo: roomNo,
  73. }
  74. err := xclient.Call(context.Background(), "OnRoomDismissed", param, nil)
  75. if err != nil {
  76. log.Debug("OnRoomDismissed failed to call: %v", err)
  77. common.GetClientPool().RemoveClient(addr)
  78. }
  79. }
  80. // 强制玩家进入,比赛撮合用
  81. func ForceEnterUser(addr string, userId int, roomNo int, chairId int) {
  82. xclient := getClient(addr)
  83. param := &Request_ForceUserEnter{
  84. UserId: userId,
  85. RoomNo: roomNo,
  86. ChairId: chairId,
  87. }
  88. err := xclient.Call(context.Background(), "ForceEnterUser", param, nil)
  89. if err != nil {
  90. log.Debug("ForceEnterUser failed to call: %v", err)
  91. common.GetClientPool().RemoveClient(addr)
  92. }
  93. }
  94. func SetExtraParam(addr string, roomNo int, userId, paramId int, data string) {
  95. xclient := getClient(addr)
  96. param := &Request_ExtraParams{
  97. RoomNo: roomNo,
  98. ParamId: paramId,
  99. UserId: userId,
  100. Data: data,
  101. }
  102. err := xclient.Call(context.Background(), "SetExtraParam", param, nil)
  103. if err != nil {
  104. log.Debug("SetExtraParam failed to call: %v", err)
  105. common.GetClientPool().RemoveClient(addr)
  106. }
  107. }