notifyuseronlinelogic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.CONNECT_TYPE_PLAYER:
  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. PlayerId: in.Uid,
  46. GameId: in.GameId,
  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. l.Logger.Infof("111111")
  57. } else {
  58. onlinePlayerMap := treemap.New(treemap.WithGoroutineSafe())
  59. // 判断是不是vip玩家
  60. if playerInfo := ext.GetVipPlayer(in.GameId, in.Uid); playerInfo != nil {
  61. playerInfo.ConnectTs = time.Now().Unix()
  62. onlinePlayerMap.Insert(in.Uid, playerInfo)
  63. } else {
  64. // 不是vip
  65. ts := time.Now().Unix()
  66. playerInfo := model.PlayerInfo{
  67. PlayerId: in.Uid,
  68. GameId: in.GameId,
  69. ConnectTs: ts,
  70. EnqueueTs: ts,
  71. }
  72. onlinePlayerMap.Insert(in.Uid, &playerInfo)
  73. // 放入等待队列
  74. ext.WaitingList.PushBack(&playerInfo)
  75. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingList.String())
  76. }
  77. ext.GameOnlinePlayerMap.Insert(in.GameId, onlinePlayerMap)
  78. l.Logger.Infof("22222")
  79. }
  80. case globalkey.CONNECT_TYPE_CS:
  81. if csInfo := ext.GetCsInfo(in.Uid); csInfo != nil {
  82. csInfo.OnlineStatus = 1
  83. } else {
  84. return nil, errors.Wrap(result.NewErrMsg("no such user"), "")
  85. }
  86. default:
  87. return nil, errors.Wrap(result.NewErrMsg("no such user type"), "")
  88. }
  89. return &pb.NotifyUserStatusResp{}, nil
  90. }