csfetchplayerqueuelogic.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package logic
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "google.golang.org/protobuf/types/known/structpb"
  6. "time"
  7. "ylink/comm/model"
  8. "ylink/comm/result"
  9. "ylink/core/inner/rpc/internal/ext"
  10. "ylink/core/inner/rpc/internal/svc"
  11. "ylink/core/inner/rpc/pb"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type CsFetchPlayerQueueLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewCsFetchPlayerQueueLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CsFetchPlayerQueueLogic {
  20. return &CsFetchPlayerQueueLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. func (l *CsFetchPlayerQueueLogic) CsFetchPlayerQueue(in *pb.InnerCsFetchPlayerQueueReq) (*pb.InnerCsFetchPlayerQueueResp, error) {
  27. queueSize := int32(ext.WaitingQueue.Size())
  28. if queueSize == 0 {
  29. // 等待队列为空直接返回
  30. return &pb.InnerCsFetchPlayerQueueResp{
  31. List: nil,
  32. }, nil
  33. }
  34. var index int32 = 0
  35. if in.Limit != 0 && in.Limit < queueSize {
  36. queueSize = in.Limit
  37. }
  38. queue := make([]interface{}, queueSize)
  39. for iter := ext.WaitingQueue.Begin(); iter.IsValid(); iter.Next() {
  40. if index >= queueSize {
  41. break
  42. }
  43. info := iter.Value().(*model.PlayerInfo)
  44. queue[index] = map[string]interface{}{
  45. "game_id": info.GameId,
  46. "player_id": info.PlayerId,
  47. "wait_time": time.Now().Unix() - info.EnqueueTs,
  48. }
  49. index += 1
  50. }
  51. //for node := ext.WaitingQueue.FrontNode(); node != nil && index < queueLen; node = node.Next() {
  52. // info := node.Value.(*model.PlayerInfo)
  53. // queue[index] = map[string]interface{}{
  54. // "game_id": info.GameId,
  55. // "player_id": info.PlayerId,
  56. // "wait_time": time.Now().Unix() - info.EnqueueTs,
  57. // }
  58. // index += 1
  59. //}
  60. list, err := structpb.NewList(queue)
  61. if err != nil {
  62. return nil, errors.Wrap(result.NewErrMsg("fetch player wait queue error"), "")
  63. }
  64. return &pb.InnerCsFetchPlayerQueueResp{
  65. List: list,
  66. }, nil
  67. }