Cloud Code JavaScript スクリプトによるリーダーボードへのアクセス
Refer to code samples that access Leaderboards with the Cloud Code JavaScript Software Development Kit.
読み終わるまでの所要時間 1 分最終更新 4日前
Cloud Code JavaScript Leaderboards SDK を使用してリーダーボードにアクセスできます。Cloud Code SDK ドキュメントポータル では、利用可能なすべてのメソッドが記載されているドキュメントを提供しています。 プレイヤーが Cloud Code スクリプトを呼び出す例について考えてみましょう。以下のスクリプトは、プレイヤーのスコアを送信し、そのエントリー (ランクなど) を取得し、さらにリーダーボードの上位スコアを取得します。 JavaScript:
プレイヤースコープのエンドポイントへの管理レベルのアクセスも Cloud Code を介して利用可能です。これにより、単一の Cloud Code スクリプトから、複数のプレイヤーのスコアや、Cloud Code スクリプトを呼び出したプレイヤー以外のプレイヤーのスコアを更新できます。 JavaScript:const { LeaderboardsApi } = require("@unity-services/leaderboards-1.1");module.exports = async ({ params, context, logger }) => { // Retrieve the playerId and accessToken from the context // allows you to ensure requests are scoped to the player // who invoked the Cloud Code script. const { projectId, playerId, accessToken } = context; const { leaderboardId, score } = params; // Initializing the LeaderboardsApi using the accessToken // ensures the requests are made as the player who invoked the // Cloud Code script. const leaderboardsApi = new LeaderboardsApi({ accessToken}); const addScoreResult = await leaderboardsApi.addLeaderboardPlayerScore(projectId, leaderboardId, playerId, { score: score }); const getScoreResult = await leaderboardsApi.getLeaderboardPlayerScore(projectId, leaderboardId, playerId); const getScoresResult = await leaderboardsApi.getLeaderboardScores(projectId, leaderboardId); return { addScoreResultStatus: addScoreResult.status, getScoreResult: getScoreResult.data, getScoresResult: getScoresResult.data };};
const { LeaderboardsApi } = require("@unity-services/leaderboards-1.1");module.exports = async ({ params, context, logger }) => { const { projectId } = context; // The below playerId comes from your input parameters. This can // be any player that you wish. const { leaderboardId, playerId, score } = params; // Initialize the LeaderboardsApi using the context allows // for admin-level access to Leaderboards endpoints. const leaderboardsApi = new LeaderboardsApi(context); const addScoreResult = await leaderboardsApi.addLeaderboardPlayerScore(projectId, leaderboardId, playerId, { score: score }); const getScoreResult = await leaderboardsApi.getLeaderboardPlayerScore(projectId, leaderboardId, playerId); const getScoresResult = await leaderboardsApi.getLeaderboardScores(projectId, leaderboardId); return { addScoreResultStatus: addScoreResult.status, getScoreResult: getScoreResult.data, getScoresResult: getScoresResult.data };};