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. onlinePlayerMap.Insert(in.Uid, &model.PlayerInfo{
  44. PlayerId: in.Uid,
  45. GameId: in.GameId,
  46. ConnectTs: time.Now().Unix(),
  47. })
  48. // 放入等待队列
  49. ext.WaitingQueue.PushBack(&model.PlayerWaitingInfo{
  50. PlayerId: in.Uid,
  51. GameId: in.GameId,
  52. EnqueueTime: time.Now().Unix(),
  53. })
  54. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingQueue.String())
  55. }
  56. }
  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. onlinePlayerMap.Insert(in.Uid, &model.PlayerInfo{
  66. PlayerId: in.Uid,
  67. GameId: in.GameId,
  68. ConnectTs: time.Now().Unix(),
  69. })
  70. // 放入等待队列
  71. ext.WaitingQueue.PushBack(&model.PlayerWaitingInfo{
  72. PlayerId: in.Uid,
  73. GameId: in.GameId,
  74. EnqueueTime: time.Now().Unix(),
  75. })
  76. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingQueue.String())
  77. }
  78. ext.GameOnlinePlayerMap.Insert(in.GameId, onlinePlayerMap)
  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. }