notifyuserofflinelogic.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package logic
  2. import (
  3. "context"
  4. "github.com/bytedance/sonic"
  5. treemap "github.com/liyue201/gostl/ds/map"
  6. "github.com/pkg/errors"
  7. "time"
  8. "ylink/comm/globalkey"
  9. "ylink/comm/model"
  10. "ylink/comm/result"
  11. "ylink/core/inner/rpc/internal/ext"
  12. "ylink/core/inner/rpc/internal/svc"
  13. "ylink/core/inner/rpc/pb"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type NotifyUserOfflineLogic struct {
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. logx.Logger
  20. }
  21. func NewNotifyUserOfflineLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NotifyUserOfflineLogic {
  22. return &NotifyUserOfflineLogic{
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. Logger: logx.WithContext(ctx),
  26. }
  27. }
  28. func (l *NotifyUserOfflineLogic) NotifyUserOffline(in *pb.NotifyUserStatusReq) (*pb.NotifyUserStatusResp, error) {
  29. l.Logger.Infof("NotifyUserOffline")
  30. switch in.Type {
  31. case globalkey.ConnectTypeNormalPlayer:
  32. // 修改玩家在线状态
  33. if ext.GameOnlinePlayerMap.Contains(in.GameId) {
  34. // 有则取出玩家
  35. onlinePlayerMap := ext.GameOnlinePlayerMap.Get(in.GameId).(*treemap.Map)
  36. if onlinePlayerMap.Contains(in.Uid) {
  37. // 有则清除,代表下线
  38. onlinePlayerMap.Erase(in.Uid)
  39. l.Logger.Infof("清除玩家在线状态")
  40. }
  41. }
  42. uniqueId := in.GameId + "_" + in.Uid
  43. if ext.WaitingQueue.Contains(uniqueId) {
  44. l.Logger.Infof("remove the player from the queue, game_id: %s, player_id: %s", in.GameId, in.Uid)
  45. ext.WaitingQueue.Erase(uniqueId)
  46. // 广播客户端更新等待队列信息
  47. payload, _ := sonic.MarshalString(&model.CommandMessage{
  48. CmdInfo: map[string]interface{}{
  49. "queue_size": ext.WaitingQueue.Size(),
  50. },
  51. })
  52. kMsg, _ := sonic.MarshalString(&model.KqMessage{
  53. Opt: model.CMD_UPDATE_WAITING_QUEUE,
  54. CreateTs: time.Now().Unix(),
  55. Payload: payload,
  56. SenderId: uniqueId,
  57. ReceiverId: globalkey.AllNormalPlayer,
  58. GameId: in.GameId,
  59. Uid: in.Uid,
  60. Ext: "",
  61. })
  62. l.svcCtx.KqCmdBoxProducer.SendMessage(l.ctx, kMsg, globalkey.AllNormalPlayer)
  63. }
  64. case globalkey.ConnectTypeVipPlayer:
  65. // 修改玩家在线状态
  66. if ext.GameOnlinePlayerMap.Contains(in.GameId) {
  67. // 有则取出玩家
  68. onlinePlayerMap := ext.GameOnlinePlayerMap.Get(in.GameId).(*treemap.Map)
  69. if onlinePlayerMap.Contains(in.Uid) {
  70. // 有则清除,代表下线
  71. onlinePlayerMap.Erase(in.Uid)
  72. l.Logger.Infof("清除玩家在线状态")
  73. }
  74. }
  75. case globalkey.ConnectTypeCs:
  76. // 修改客服在线状态
  77. if csInfo := ext.GetCsInfo(in.Uid); csInfo != nil {
  78. csInfo.OnlineStatus = 0
  79. } else {
  80. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  81. }
  82. default:
  83. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  84. }
  85. return &pb.NotifyUserStatusResp{}, nil
  86. }