Please or Register to create posts and topics.

LeaderBoard JavaScript Original Version

//what is the URL of your google spreadsheet?
var sheetURL = "https://script.google.com/macros/s/AKfycbzbERPJjI0RYBAijhYrJVoGn9NVbYcC5tx97LkvtyZQP_rkJCQ/exec";
var scoreVarName = "userScore"; //what is the name of the score varible in SL?
var userVarName = 'userName'; //what is the name of the user name varible in SL?
var topTenVarName = 'topTen'; //what is the name of the top ten message varible in SL?

/*
 *** Do not edit below this line. ***
 */
var player = GetPlayer(); //Get the SL player
var userScore = player.GetVar(scoreVarName); //Get the score from the player
var userTag = player.GetVar(userVarName); //Get the user name from the player
player.SetVar(topTenVarName, "Loading..."); //Temporarily set the top ten message 
var topTenMsg; //We will build the top ten message in this var. 
//Set up our AJAX 
var xhttp;
if (window.XMLHttpRequest) {
 xhttp = new XMLHttpRequest();
} else {
 xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", sheetURL + "?id=" + userTag + "&score=" + userScore, true);
xhttp.send();
xhttp.onreadystatechange = function () {
 //If we get a successful reply from our spreadsheet:
 if (xhttp.readyState == 4 && xhttp.status == 200) {
 var topTenJson = JSON.parse(xhttp.responseText);
 //This is the begining of the top ten message. Edit carefully
 //First we create the results for this learner (\n creates a new line)
 var userResults = "You placed " + topTenJson.user.rank + " with a score of " + topTenJson.user.score + "\n";
 //Then we introduce the top ten.
 var topTenUsers = "The top " + topTenJson.users.length + " gamers are: \n";
 //We append the second line of text to the message
 topTenMsg = userResults + topTenUsers;
 //Now we loop through each of the top 10
 for (var i = 0; i < topTenJson.users.length; i++) {
 //convert the date to something friendly 
 var recordDate = new Date(topTenJson.users[i].date);
 var recordDateString = recordDate.getDate() + "/" + (recordDate.getMonth() + 1) + "/" + recordDate.getFullYear() + " " + recordDate.getHours() + ":" + recordDate.getMinutes();
 //Append the users info (rank, name, date) to the message. 
 topTenMsg += (i + 1) + " - User: " + topTenJson.users[i].id + " Score: " + topTenJson.users[i].score + " When: " + recordDateString + "\n";
 }
 //Push the message back into Storyline
 player.SetVar(topTenVarName, topTenMsg);
 }
}

GAS Script:

https://gist.github.com/onEnterFrame/464ea79d400b839c92a2


// Usage
// 1. Save your GAS Project
//
// 2. Click: Run > setup
//
// 3. Click: Publish > Deploy as web app 
// - enter Project Version Description (optional) 
// - set security level and enable service ( execute as 'me' and access 'anyone, even anonymously) 
//
// 4. Copy the 'Current web app URL' and paste it into the Leaderboard JavaScript file (first line)
//

/*
** Do Not Edit Below This Line **
*/

// Create a new property service to maintain variables accross instances.
var SCRIPT_PROP = PropertiesService.getScriptProperties();

// What to do when a when we recieve an HTTP GET request
function doGet(e) {
 // For this simple app we only need to do one thing.
 return addUser(e.parameter['id'], e.parameter['score'])
}

// Add our user record and return the list of top ten. 
function addUser(id, score) {
 //Open the spreadsheet we set up by it's ID
 var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
 //Get the first sheet
 var sheet = doc.getSheets()[0];
 // we want a public lock, one that locks for all invocations
 var lock = LockService.getPublicLock();
 lock.waitLock(30000); // wait 30 seconds before conceding defeat.
 //wrap everything in a try/catch to handle errors
 try {
 var timeNow = new Date() //create a timestamp
 var nextRow = sheet.getLastRow() + 1; // get next empty row
 //create an array of data to put into the row
 var row = [
 [id, score, timeNow]
 ];
 //put the data into the row
 sheet.getRange(nextRow, 1, 1, 3).setValues(row);
 //sort the sheet with our custom function
 sortByScore(sheet)
 // get the top ten with our custom function
 var top10 = getTop10(sheet)
 // get this user's rank from our custom function
 var userRank = findUserRank(id, score, timeNow, sheet);
 //retun a JSON with the top ten and our user
 return ContentService.createTextOutput(JSON.stringify({
 "result": "success",
 "users": top10,
 "user": userRank
 })).setMimeType(ContentService.MimeType.JSON);
 } catch (e) {//something went wrong. Return an error
 return ContentService.createTextOutput(JSON.stringify({
 "result": "error",
 "error": e
 })).setMimeType(ContentService.MimeType.JSON);
 } finally { //release lock
 lock.releaseLock();
 }
}

//sort the sheet by column two (score)
function sortByScore(sheet) {
 //find the last row with data
 var lastRow = sheet.getLastRow()
 // get the range of data
 var range = sheet.getRange(2, 1, lastRow, 3)
 //sort by score
 range.sort({
 column: 2,
 ascending: false
 });
}

// gets the top ten users
function getTop10(sheet) {
 //get the first ten rows 
 var range = sheet.getRange(2, 1, 10, 3)
 var users = range.getValues()
 var top10 = []; //we'll store those ten users in this array
 //loop thru all ten and create a user object for each
 for (row = 0, len = users.length; row < len; row++) {
 if (users[row][0] != '') {//if the row is not empty
 var user = {}; //our user object
 //add data for each user
 user.id = users[row][0]
 user.score = +users[row][1]
 user.date = +users[row][2]
 //push this user into the array of users
 top10.push(user)
 }
 }
 //return the top ten
 return top10
}

//find this users rank. In case they have multiple records we use the timestamp to get this record.
function findUserRank(id, score, timeNow, sheet) {
 //find the last row with data
 var lastRow = sheet.getLastRow()
 //get the data from the range
 var range = sheet.getRange(2, 1, lastRow, 3)
 var users = range.getValues()
 var user = {}; //create a user object
 //loop thru all rows to find our user
 for (row = 0, len = users.length; row < len; row++) {
 //if this row is our user
 if (users[row][0] == id && users[row][1] == score && users[row][2] == timeNow.toString()) {
 //add data to the user object
 user.id = users[row][0]
 user.score = +users[row][1]
 user.date = +users[row][2]
 //add the users rank (what row were they?)
 user.rank = row + 1
 break //exit our loop
 }
 }
 //return this user's data
 return user
}


//Setup our spreadsheet
function setup() {
 //get the spreadsheet
 var doc = SpreadsheetApp.getActiveSpreadsheet();
 //save the spreadsheet's id for later 
 SCRIPT_PROP.setProperty("key", doc.getId());
 //get the first sheet
 var sheet = doc.getSheets()[0];
 //create an array of labels
 var row = [
 ["ID", "score", "Timestamp"]
 ];
 //set the first row of the sheet to our labels
 sheet.getRange(1, 1, 1, 3).setValues(row);
}

SL files (both original and the 360 upgraded version.

If there’s one place where you can see my regular updates, it’s this one. Welcome to my idea dumping ground. This is my warehouse for all the ideas that I get while I do some other important stuffs. Then I revisit this place at my leisure time to work on those sparks.
Found something useful and want to discuss?
Sure! Feel free to connect.
Top