csconnectplayerlogic.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package logic
  2. import (
  3. "context"
  4. "github.com/bytedance/sonic"
  5. "github.com/gookit/event"
  6. treemap "github.com/liyue201/gostl/ds/map"
  7. "github.com/pkg/errors"
  8. "github.com/robfig/cron/v3"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. "time"
  11. "ylink/comm/ctxdata"
  12. "ylink/comm/globalkey"
  13. "ylink/comm/model"
  14. "ylink/comm/result"
  15. "ylink/comm/trace"
  16. "ylink/core/inner/rpc/internal/ext"
  17. "ylink/core/inner/rpc/internal/svc"
  18. "ylink/core/inner/rpc/pb"
  19. )
  20. type CsConnectPlayerLogic struct {
  21. ctx context.Context
  22. svcCtx *svc.ServiceContext
  23. logx.Logger
  24. }
  25. func NewCsConnectPlayerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CsConnectPlayerLogic {
  26. return &CsConnectPlayerLogic{
  27. ctx: ctx,
  28. svcCtx: svcCtx,
  29. Logger: logx.WithContext(ctx),
  30. }
  31. }
  32. func (l *CsConnectPlayerLogic) CsConnectPlayer(in *pb.InnerCsConnectPlayerReq) (*pb.InnerCsConnectPlayerResp, error) {
  33. playerInfo := ext.GetOnlinePlayerInfo(in.GameId, in.PlayerId)
  34. if playerInfo == nil {
  35. return nil, errors.Wrapf(result.NewErrMsg("玩家不在线"), "")
  36. }
  37. playerInfo.CsId = in.CsId
  38. playerInfo.DequeueTs = time.Now().Unix()
  39. if ext.GameConnectedMap.Contains(in.GameId) {
  40. playerConnectedMap := ext.GameConnectedMap.Get(in.GameId).(*treemap.Map)
  41. playerConnectedMap.Insert(in.PlayerId, playerInfo)
  42. } else {
  43. playerConnectedMap := treemap.New(treemap.WithGoroutineSafe())
  44. playerConnectedMap.Insert(in.PlayerId, playerInfo)
  45. ext.GameConnectedMap.Insert(in.GameId, playerConnectedMap)
  46. }
  47. // 移除WaitingQueue
  48. uniqueId := in.GameId + "_" + in.PlayerId
  49. if ext.WaitingQueue.Contains(uniqueId) {
  50. l.Logger.Infof("remove the player from the queue, game_id: %s, player_id: %s", in.GameId, in.PlayerId)
  51. ext.WaitingQueue.Erase(uniqueId)
  52. // 广播客户端更新等待队列信息
  53. //TODO 通知客服也更新
  54. payload, _ := sonic.MarshalString(&model.CommandMessage{
  55. CmdInfo: map[string]interface{}{
  56. "queue_size": ext.WaitingQueue.Size(),
  57. },
  58. })
  59. kMsg, _ := sonic.MarshalString(&model.KqMessage{
  60. Opt: model.CMD_UPDATE_WAITING_QUEUE,
  61. CreateTs: time.Now().Unix(),
  62. Payload: payload,
  63. SenderId: uniqueId,
  64. ReceiverId: globalkey.AllNormalPlayer,
  65. GameId: in.GameId,
  66. Uid: in.PlayerId,
  67. Ext: "",
  68. })
  69. l.svcCtx.KqCmdBoxProducer.SendMessage(l.ctx, kMsg, globalkey.AllNormalPlayer)
  70. }
  71. traceId := ctxdata.GetTraceIdFromCtx(l.ctx)
  72. trace.RunOnTracing(traceId, func(ctx context.Context) {
  73. var entryId cron.EntryID
  74. entryId, _ = l.svcCtx.TimeoutCron.AddFunc("@every 1m", func() {
  75. var timeoutTs int64
  76. if playerInfo.LastChatTs == 0 {
  77. timeoutTs = time.Now().Unix() - playerInfo.ConnectTs
  78. } else {
  79. timeoutTs = time.Now().Unix() - playerInfo.LastChatTs
  80. }
  81. if timeoutTs >= 3600 {
  82. // 释放计时器任务
  83. _ = event.MustFire(globalkey.EventRemoveTimeoutJob, event.M{"entry_id": entryId})
  84. l.Logger.Infof("trigger timeout event, remove cron job, entry id: %d", entryId)
  85. trace.StartTrace(ctx, "InnerServer.CountDownTimer.SendCmdMessage", func(ctx context.Context) {
  86. // 发踢下线的command指令
  87. uniqueId := in.GameId + "_" + in.PlayerId
  88. payload, _ := sonic.MarshalString(&model.CommandMessage{
  89. CmdInfo: "",
  90. })
  91. kMsg, _ := sonic.MarshalString(&model.KqMessage{
  92. Opt: model.CMD_CHAT_TIMEOUT,
  93. CreateTs: time.Now().Unix(),
  94. Payload: payload,
  95. SenderId: uniqueId,
  96. ReceiverId: uniqueId,
  97. GameId: in.GameId,
  98. Uid: in.PlayerId,
  99. Ext: "",
  100. })
  101. l.svcCtx.KqCmdBoxProducer.SendMessage(ctx, kMsg, uniqueId)
  102. })
  103. }
  104. })
  105. })
  106. return &pb.InnerCsConnectPlayerResp{}, nil
  107. }