csconnectplayerlogic.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. payload, _ := sonic.MarshalString(&model.CommandMessage{
  54. CmdInfo: map[string]interface{}{
  55. "queue_size": ext.WaitingQueue.Size(),
  56. },
  57. })
  58. kMsg, _ := sonic.MarshalString(&model.KqMessage{
  59. Opt: model.CMD_UPDATE_WAITING_QUEUE,
  60. CreateTs: time.Now().Unix(),
  61. Payload: payload,
  62. SenderId: uniqueId,
  63. ReceiverId: globalkey.AllNormalPlayer,
  64. GameId: in.GameId,
  65. Uid: in.PlayerId,
  66. Ext: "",
  67. })
  68. l.svcCtx.KqCmdBoxProducer.SendMessage(l.ctx, kMsg, globalkey.AllNormalPlayer)
  69. }
  70. traceId := ctxdata.GetTraceIdFromCtx(l.ctx)
  71. trace.RunOnTracing(traceId, func(ctx context.Context) {
  72. var entryId cron.EntryID
  73. entryId, _ = l.svcCtx.TimeoutCron.AddFunc("@every 1m", func() {
  74. var timeoutTs int64
  75. if playerInfo.LastChatTs == 0 {
  76. timeoutTs = time.Now().Unix() - playerInfo.ConnectTs
  77. } else {
  78. timeoutTs = time.Now().Unix() - playerInfo.LastChatTs
  79. }
  80. if timeoutTs >= 3600 {
  81. // 释放计时器任务
  82. _ = event.MustFire(globalkey.EventRemoveTimeoutJob, event.M{"entry_id": entryId})
  83. l.Logger.Infof("trigger timeout event, remove cron job, entry id: %d", entryId)
  84. trace.StartTrace(ctx, "InnerServer.CountDownTimer.SendCmdMessage", func(ctx context.Context) {
  85. // 发踢下线的command指令
  86. uniqueId := in.GameId + "_" + in.PlayerId
  87. payload, _ := sonic.MarshalString(&model.CommandMessage{
  88. CmdInfo: "",
  89. })
  90. kMsg, _ := sonic.MarshalString(&model.KqMessage{
  91. Opt: model.CMD_CHAT_TIMEOUT,
  92. CreateTs: time.Now().Unix(),
  93. Payload: payload,
  94. SenderId: uniqueId,
  95. ReceiverId: uniqueId,
  96. GameId: in.GameId,
  97. Uid: in.PlayerId,
  98. Ext: "",
  99. })
  100. l.svcCtx.KqCmdBoxProducer.SendMessage(ctx, kMsg, uniqueId)
  101. })
  102. }
  103. })
  104. })
  105. return &pb.InnerCsConnectPlayerResp{}, nil
  106. }