notifyuseronlinelogic.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package logic
  2. import (
  3. "context"
  4. treemap "github.com/liyue201/gostl/ds/map"
  5. "github.com/pkg/errors"
  6. "time"
  7. "ylink/comm/globalkey"
  8. "ylink/comm/model"
  9. "ylink/comm/result"
  10. "ylink/core/inner/rpc/internal/ext"
  11. "ylink/core/inner/rpc/internal/svc"
  12. "ylink/core/inner/rpc/pb"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type NotifyUserOnlineLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewNotifyUserOnlineLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NotifyUserOnlineLogic {
  21. return &NotifyUserOnlineLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. func (l *NotifyUserOnlineLogic) NotifyUserOnline(in *pb.NotifyUserStatusReq) (*pb.NotifyUserStatusResp, error) {
  28. switch in.Type {
  29. case globalkey.ConnectTypeNormalPlayer:
  30. // 修改玩家在线状态
  31. if ext.GameOnlinePlayerMap.Contains(in.GameId) {
  32. // 有则取出玩家的map
  33. onlinePlayerMap := ext.GameOnlinePlayerMap.Get(in.GameId).(*treemap.Map)
  34. if onlinePlayerMap.Contains(in.Uid) {
  35. l.Logger.Error("该玩家已在线")
  36. // TODO 单点在线
  37. } else {
  38. ts := time.Now().Unix()
  39. playerInfo := &model.PlayerInfo{
  40. GameId: in.GameId,
  41. PlayerId: in.Uid,
  42. IsVip: 0,
  43. CsId: "",
  44. ConnectTs: ts,
  45. LastChatTs: 0,
  46. EnqueueTs: ts,
  47. DequeueTs: 0,
  48. }
  49. //if playerInfo == nil {
  50. // l.Logger.Infof("playerInfo is nil")
  51. //}
  52. onlinePlayerMap.Insert(in.Uid, playerInfo)
  53. // 放入等待队列
  54. ext.WaitingQueue.Insert(in.GameId+"_"+in.Uid, playerInfo)
  55. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingQueue)
  56. //TODO 返回等待信息
  57. }
  58. } else {
  59. onlinePlayerMap := treemap.New(treemap.WithGoroutineSafe())
  60. ts := time.Now().Unix()
  61. playerInfo := &model.PlayerInfo{
  62. GameId: in.GameId,
  63. PlayerId: in.Uid,
  64. ConnectTs: ts,
  65. EnqueueTs: ts,
  66. }
  67. onlinePlayerMap.Insert(in.Uid, playerInfo)
  68. // 放入等待队列
  69. ext.WaitingQueue.Insert(in.GameId+"_"+in.Uid, playerInfo)
  70. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingQueue)
  71. ext.GameOnlinePlayerMap.Insert(in.GameId, onlinePlayerMap)
  72. //TODO 返回等待信息
  73. }
  74. case globalkey.ConnectTypeVipPlayer:
  75. var onlinePlayerMap *treemap.Map
  76. if ext.GameOnlinePlayerMap.Contains(in.GameId) {
  77. onlinePlayerMap = ext.GameOnlinePlayerMap.Get(in.GameId).(*treemap.Map)
  78. } else {
  79. onlinePlayerMap = treemap.New(treemap.WithGoroutineSafe())
  80. }
  81. if playerInfo := ext.GetVipPlayer(in.GameId, in.Uid); playerInfo != nil {
  82. playerInfo.ConnectTs = time.Now().Unix()
  83. onlinePlayerMap.Insert(in.Uid, playerInfo)
  84. ext.GameOnlinePlayerMap.Insert(in.GameId, onlinePlayerMap)
  85. } else {
  86. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  87. }
  88. case globalkey.ConnectTypeCs:
  89. if csInfo := ext.GetCsInfo(in.Uid); csInfo != nil {
  90. csInfo.OnlineStatus = 1
  91. //TODO 返回等待信息
  92. } else {
  93. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  94. }
  95. default:
  96. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  97. }
  98. return &pb.NotifyUserStatusResp{}, nil
  99. }