notifyuserofflinelogic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. switch in.Type {
  30. case globalkey.ConnectTypeNormalPlayer:
  31. // 修改玩家在线状态
  32. if ext.GameOnlinePlayerMap.Contains(in.GameId) {
  33. // 有则取出玩家
  34. onlinePlayerMap := ext.GameOnlinePlayerMap.Get(in.GameId).(*treemap.Map)
  35. if onlinePlayerMap.Contains(in.Uid) {
  36. // 有则清除,代表下线
  37. onlinePlayerMap.Erase(in.Uid)
  38. l.Logger.Infof("清除玩家在线状态")
  39. }
  40. }
  41. uniqueId := in.GameId + "_" + in.Uid
  42. if ext.WaitingQueue.Contains(uniqueId) {
  43. l.Logger.Infof("remove the player from the queue, game_id: %s, player_id: %s", in.GameId, in.Uid)
  44. ext.WaitingQueue.Erase(uniqueId)
  45. // 广播客户端更新等待队列信息
  46. payload, _ := sonic.MarshalString(&model.CommandMessage{
  47. CmdInfo: map[string]interface{}{
  48. "queue_size": ext.WaitingQueue.Size(),
  49. },
  50. })
  51. kMsg, _ := sonic.MarshalString(&model.KqMessage{
  52. Opt: model.CMD_UPDATE_WAITING_QUEUE,
  53. CreateTs: time.Now().Unix(),
  54. Payload: payload,
  55. SenderId: uniqueId,
  56. ReceiverId: globalkey.AllNormalPlayer,
  57. GameId: in.GameId,
  58. Uid: in.Uid,
  59. Ext: "",
  60. })
  61. l.svcCtx.KqCmdBoxProducer.SendMessage(l.ctx, kMsg, globalkey.AllNormalPlayer)
  62. }
  63. case globalkey.ConnectTypeVipPlayer:
  64. // 修改玩家在线状态
  65. if ext.GameOnlinePlayerMap.Contains(in.GameId) {
  66. // 有则取出玩家
  67. onlinePlayerMap := ext.GameOnlinePlayerMap.Get(in.GameId).(*treemap.Map)
  68. if onlinePlayerMap.Contains(in.Uid) {
  69. // 有则清除,代表下线
  70. onlinePlayerMap.Erase(in.Uid)
  71. l.Logger.Infof("清除玩家在线状态")
  72. }
  73. }
  74. case globalkey.ConnectTypeCs:
  75. // 修改客服在线状态
  76. if csInfo := ext.GetCsInfo(in.Uid); csInfo != nil {
  77. csInfo.OnlineStatus = 0
  78. } else {
  79. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  80. }
  81. default:
  82. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  83. }
  84. return &pb.NotifyUserStatusResp{}, nil
  85. }