notifyuseronlinelogic.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package logic
  2. import (
  3. "context"
  4. treemap "github.com/liyue201/gostl/ds/map"
  5. "github.com/liyue201/gostl/ds/set"
  6. "github.com/pkg/errors"
  7. "time"
  8. "ylink/comm/globalkey"
  9. "ylink/comm/model"
  10. "ylink/comm/result"
  11. "ylink/core/inner/rpc/internal/ext"
  12. "ylink/core/inner/rpc/internal/svc"
  13. "ylink/core/inner/rpc/pb"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type NotifyUserOnlineLogic struct {
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. logx.Logger
  20. }
  21. func NewNotifyUserOnlineLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NotifyUserOnlineLogic {
  22. return &NotifyUserOnlineLogic{
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. Logger: logx.WithContext(ctx),
  26. }
  27. }
  28. func (l *NotifyUserOnlineLogic) NotifyUserOnline(in *pb.NotifyUserStatusReq) (*pb.NotifyUserStatusResp, error) {
  29. switch in.Type {
  30. case globalkey.CONNECT_TYPE_PLAYER:
  31. // 修改玩家在线状态
  32. if ext.Game2PlayerStatusMap.Contains(in.GameId) {
  33. // 有则取出玩家的set
  34. playerStatSet := ext.Game2PlayerStatusMap.Get(in.GameId).(*set.Set)
  35. if !playerStatSet.Contains(in.Uid) {
  36. playerStatSet.Insert(in.Uid)
  37. }
  38. } else {
  39. playerStatSet := set.New()
  40. playerStatSet.Insert(in.Uid)
  41. ext.Game2PlayerStatusMap.Insert(in.GameId, playerStatSet)
  42. }
  43. // 判断是否有专属客服,没有则放入等待队列
  44. if ext.GameVipMap.Contains(in.GameId) {
  45. p2cMap := ext.GameVipMap.Get(in.GameId).(*treemap.Map)
  46. if !p2cMap.Contains(in.Uid) {
  47. ext.WaitingQueue.PushBack(&model.PlayerWaitingInfo{
  48. PlayerId: in.Uid,
  49. GameId: in.GameId,
  50. EnqueueTime: time.Now().Unix(),
  51. })
  52. }
  53. } else {
  54. ext.WaitingQueue.PushBack(&model.PlayerWaitingInfo{
  55. PlayerId: in.Uid,
  56. GameId: in.GameId,
  57. EnqueueTime: time.Now().Unix(),
  58. })
  59. }
  60. l.Logger.Infof("enqueue waiting list: %s", ext.WaitingQueue.String())
  61. case globalkey.CONNECT_TYPE_CS:
  62. // 修改客服在线状态
  63. csInfo := ext.CsInfoMap.Get(in.Uid).(*model.CsInfo)
  64. csInfo.OnlineStatus = 1
  65. default:
  66. return nil, errors.Wrap(result.NewErrMsg("no such user type"), "")
  67. }
  68. return &pb.NotifyUserStatusResp{}, nil
  69. }