Bảng xếp hạng

Bạn có thể hiển thị bảng xếp hạng tùy chỉnh theo cá nhân trên trang trò chơi hiển thị kết quả của những người chơi hàng đầu và vị trí của người dùng trong bảng xếp hạng.

Để các yêu cầu bên dưới hoạt động, hãy đảm bảo rằng:

Chú ý

Khi ứng dụng của bạn yêu cầu bảng xếp hạng, hãy đảm bảo rằng tên bảng xếp hạng trong yêu cầu khớp với tên bảng xếp hạng trong Technical leaderboard name trong Bảng điều khiển trò chơi. Nếu không, yêu cầu sẽ trả về lỗi 404.

Mô tả bảng xếp hạng

Để nhận mô tả về bảng xếp hạng theo tên, hãy sử dụng phương thức ysdk.leaderboards.get_description():

ysdk.leaderboards.get_description(
    leaderboard_name: string,
    callback: function
)

callback: function — bộ xử lý của phương thức được gọi. Có dạng:

function(self, description: table|nil): nil
  • description: table — mô tả bảng xếp hạng. Bao gồm các thuộc tính:
description: {
  app_id: string,
  default: boolean,
  invert_sort_order: boolean,
  decimal_offset: integer,
  type: string,
  name: string,
  title: {
    en: string,
    ru: string
  }
}

Ví dụ

function display_leaderboard()
  ysdk.leaderboards.get_description("highscores",
    function (self, description)
      if description then
        print(
          description.name,
          description.title.en
        )
    end
  end)
end

Điểm số mới

Chú ý

Yêu cầu chỉ dành cho người dùng được cho phép. Nếu cần, hãy yêu cầu sự cho phép.

Để thiết lập điểm số mới cho người chơi, hãy sử dụng phương thức ysdk.leaderboards.set_score():

ysdk.leaderboards.set_score(
    leaderboard_name: string,
    score: integer,
    extraData: string|nil
)

Ghi chú

Giới hạn số lượng yêu cầu là 60 lần mỗi phút. Nếu không, yêu cầu sẽ bị từ chối cùng với lỗi.

Nhận xếp hạng

Chú ý

Yêu cầu chỉ dành cho người dùng được cho phép. Nếu cần, hãy yêu cầu sự cho phép.

Để nhận xếp hạng của người dùng, hãy sử dụng phương thức ysdk.leaderboards.get_player_entry():

ysdk.leaderboards.get_player_entry(
    leaderboard_name: string,
    callback: function
)

callback: function — trình xử lý của phương thức được gọi. Có dạng:

function(self, player_entry: table|nil): nil
  • player_entry: table — xếp hạng của người dùng. Bao gồm các thuộc tính:
player_entry: {
  score: integer,
  extraData: string,
  rank: integer,
  avatar_src: {
    small: string,
    medium: string,
    large: string,
  },
  avatar_srcset: {
    small: string,
    medium: string,
    large: string,
  },
  lang: string,
  public_name: string,
  unique_id: string,
  formatted_score: string
}

Ví dụ

function display_high_score()
  ysdk.leaderboards.get_player_entry("highscores",
    function (self, player_entry)
      if player_entry then
        print("highscore: " .. player_entry.score)
      end
    end)
end

Mục nhập bảng xếp hạn

Để hiển thị xếp hạng của người dùng, hãy sử dụng phương thức ysdk.leaderboards.get_entries():

ysdk.leaderboards.get_entries(
    leaderboardName: string,
    callback: function,
    options: {
        include_user: boolean|nil,
        quantity_around: number|nil,
        quantity_top: number|nil
    }
)

callback: function— trình xử lý phương thức được gọi. Có dạng:

function(self, entries: table|nil): nil

entries: table — bảng xếp hạng người dùng. Bao gồm các thuộc tính:

entries: {
  leaderboard: {
    ...
  },
  ranges: [
    {
      start: integer,
      size: integer
    }
  ],
  userRank: integer,
  entries: [
    {
      score: integer,
      extraData: string,
      rank: integer,
      avatar_src: {
        small: string,
        medium: string,
        large: string,
      },
      avatar_srcset: {
        small: string,
        medium: string,
        large: string,
      },
      lang: string,
      public_name: string,
      unique_id: string,
      formatted_score: string
    },
    ...
  ]
}

Kho lưu trữ