csfetchplayerqueuelogic.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. queueLen := int64(ext.WaitingQueue.Len())
  28. if queueLen == 0 {
  29. // 等待队列为空直接返回
  30. return &pb.InnerCsFetchPlayerQueueResp{
  31. List: nil,
  32. }, nil
  33. }
  34. var index int64 = 0
  35. if in.Limit != 0 && in.Limit < queueLen {
  36. queueLen = in.Limit
  37. }
  38. queue := make([]interface{}, queueLen)
  39. for node := ext.WaitingQueue.FrontNode(); node != nil && index < queueLen; node = node.Next() {
  40. info := node.Value.(*model.PlayerWaitingInfo)
  41. queue[index] = map[string]interface{}{
  42. "player_id": info.PlayerId,
  43. "game_id": info.GameId,
  44. "wait_time": time.Now().Unix() - info.EnqueueTime,
  45. }
  46. index += 1
  47. }
  48. list, err := structpb.NewList(queue)
  49. if err != nil {
  50. return nil, errors.Wrap(result.NewErrMsg("fetch player wait queue error"), "")
  51. }
  52. return &pb.InnerCsFetchPlayerQueueResp{
  53. List: list,
  54. }, nil
  55. }