notifyuseronlinelogic.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.ConnectTypePlayer:
  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("such player has been connected")
  36. } else {
  37. // 不存在换这个玩家,判断是否vip
  38. if playerInfo := ext.GetVipPlayer(in.GameId, in.Uid); playerInfo != nil {
  39. playerInfo.ConnectTs = time.Now().Unix()
  40. onlinePlayerMap.Insert(in.Uid, playerInfo)
  41. } else {
  42. // 不是vip
  43. ts := time.Now().Unix()
  44. playerInfo := model.PlayerInfo{
  45. GameId: in.GameId,
  46. PlayerId: in.Uid,
  47. ConnectTs: ts,
  48. EnqueueTs: ts,
  49. }
  50. onlinePlayerMap.Insert(in.Uid, &playerInfo)
  51. // 放入等待队列
  52. ext.WaitingList.PushBack(&playerInfo)
  53. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingList.String())
  54. }
  55. }
  56. } else {
  57. onlinePlayerMap := treemap.New(treemap.WithGoroutineSafe())
  58. // 判断是不是vip玩家
  59. if playerInfo := ext.GetVipPlayer(in.GameId, in.Uid); playerInfo != nil {
  60. playerInfo.ConnectTs = time.Now().Unix()
  61. onlinePlayerMap.Insert(in.Uid, playerInfo)
  62. } else {
  63. // 不是vip
  64. ts := time.Now().Unix()
  65. playerInfo := model.PlayerInfo{
  66. GameId: in.GameId,
  67. PlayerId: in.Uid,
  68. ConnectTs: ts,
  69. EnqueueTs: ts,
  70. }
  71. onlinePlayerMap.Insert(in.Uid, &playerInfo)
  72. // 放入等待队列
  73. ext.WaitingList.PushBack(&playerInfo)
  74. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingList.String())
  75. }
  76. ext.GameOnlinePlayerMap.Insert(in.GameId, onlinePlayerMap)
  77. }
  78. case globalkey.ConnectTypeCs:
  79. if csInfo := ext.GetCsInfo(in.Uid); csInfo != nil {
  80. csInfo.OnlineStatus = 1
  81. } else {
  82. return nil, errors.Wrap(result.NewErrMsg("no such user"), "")
  83. }
  84. default:
  85. return nil, errors.Wrap(result.NewErrMsg("no such user type"), "")
  86. }
  87. return &pb.NotifyUserStatusResp{}, nil
  88. }