Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question Leaderboard Reset via Cloud Code not Archiving

Discussion in 'Leaderboards' started by traviswinegar, May 24, 2023.

  1. traviswinegar

    traviswinegar

    Joined:
    Nov 29, 2022
    Posts:
    15
    Hi Folks,

    So I have written a Cloud Code script to reset and, optionally, archive leaderboard scores. The scores are reset correctly but fails to archive. Could someone take a look and point out what I'm doing wrong?

    Code (JavaScript):
    1. const _ = require('lodash-4.17');
    2. const axios = require("axios-0.21");
    3.  
    4. module.exports = async ({ params, context, logger }) => {
    5.   const { projectId, playerId, environmentId } = context;
    6.   const { groupId, archive } = params;
    7.  
    8.   const serviceCredentials = 'XXX';
    9.   const resetLeaderboardConfig = {
    10.     headers: {
    11.       Authorization: `Basic ${serviceCredentials}`,
    12.     },
    13.     data: {
    14.       archive: archive
    15.     }
    16.   };
    17.  
    18.    var apiUrl = `https://services.api.unity.com/leaderboards/v1/projects/${projectId}/environments/${environmentId}/leaderboards/${groupId}_Count/scores`;
    19.     await axios.delete(apiUrl, resetLeaderboardConfig);
    20.  
    21.   return resetLeaderboardConfig; //just seeing what's in the request
    22. };
    Thanks!
    Travis
     
    Last edited by a moderator: May 24, 2023
  2. devingunity

    devingunity

    Unity Technologies

    Joined:
    May 26, 2021
    Posts:
    20
    Hi Travis,

    Just wanted to start by noting that you may want to delete your service account key and create a new one - we've edited your message to remove the credential but it was temporarily exposed, and so the safest option would be to remove it and generate a new one.

    In terms of your question, the
    archive
    parameter is a query parameter, not part of the request body! So the correct approach would be to update your URL as such:
    Code (JavaScript):
    1.  
    2. var apiUrl = `https://services.api.unity.com/leaderboards/v1/projects/${projectId}/environments/${environmentId}/leaderboards/${groupId}_Count/scores?archive=${archive}`;
    3.  
    I hope this helps!
    Devin
     
    traviswinegar likes this.
  3. traviswinegar

    traviswinegar

    Joined:
    Nov 29, 2022
    Posts:
    15
    Hi Devin,

    Thank you for anonymizing our key. That was a bonehead move. :)

    And thank you for your fix as well. I also changed my "data" element to "params", which worked as well.