notifyuseronlinelogic.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. } else {
  37. ts := time.Now().Unix()
  38. playerInfo := &model.PlayerInfo{
  39. GameId: in.GameId,
  40. PlayerId: in.Uid,
  41. IsVip: 0,
  42. CsId: "",
  43. ConnectTs: ts,
  44. LastChatTs: 0,
  45. EnqueueTs: ts,
  46. DequeueTs: 0,
  47. }
  48. //if playerInfo == nil {
  49. // l.Logger.Infof("playerInfo is nil")
  50. //}
  51. onlinePlayerMap.Insert(in.Uid, playerInfo)
  52. // 放入等待队列
  53. ext.WaitingQueue.Insert(in.GameId+"_"+in.Uid, playerInfo)
  54. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingQueue)
  55. //TODO 返回等待信息
  56. }
  57. } else {
  58. onlinePlayerMap := treemap.New(treemap.WithGoroutineSafe())
  59. ts := time.Now().Unix()
  60. playerInfo := &model.PlayerInfo{
  61. GameId: in.GameId,
  62. PlayerId: in.Uid,
  63. ConnectTs: ts,
  64. EnqueueTs: ts,
  65. }
  66. onlinePlayerMap.Insert(in.Uid, playerInfo)
  67. // 放入等待队列
  68. ext.WaitingQueue.Insert(in.GameId+"_"+in.Uid, playerInfo)
  69. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingQueue)
  70. ext.GameOnlinePlayerMap.Insert(in.GameId, onlinePlayerMap)
  71. //TODO 返回等待信息
  72. }
  73. case globalkey.ConnectTypeVipPlayer:
  74. var onlinePlayerMap *treemap.Map
  75. if ext.GameOnlinePlayerMap.Contains(in.GameId) {
  76. onlinePlayerMap = ext.GameOnlinePlayerMap.Get(in.GameId).(*treemap.Map)
  77. } else {
  78. onlinePlayerMap = treemap.New(treemap.WithGoroutineSafe())
  79. }
  80. if playerInfo := ext.GetVipPlayer(in.GameId, in.Uid); playerInfo != nil {
  81. playerInfo.ConnectTs = time.Now().Unix()
  82. onlinePlayerMap.Insert(in.Uid, playerInfo)
  83. ext.GameOnlinePlayerMap.Insert(in.GameId, onlinePlayerMap)
  84. } else {
  85. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  86. }
  87. case globalkey.ConnectTypeCs:
  88. if csInfo := ext.GetCsInfo(in.Uid); csInfo != nil {
  89. csInfo.OnlineStatus = 1
  90. //TODO 返回等待信息
  91. } else {
  92. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  93. }
  94. default:
  95. return nil, errors.Wrap(result.NewErrMsg("用户不存在"), "")
  96. }
  97. return &pb.NotifyUserStatusResp{}, nil
  98. }