Skip to content

Instantly share code, notes, and snippets.

@onEnterFrame
Created February 1, 2016 21:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onEnterFrame/fa204d3705dcd4bbb72b to your computer and use it in GitHub Desktop.
Save onEnterFrame/fa204d3705dcd4bbb72b to your computer and use it in GitHub Desktop.
//what is the URL of your google spreadsheet?
var sheetURL = 'YOUR_URL_HERE';
var scoreVarName = 'userScore'; //what is the name of the score varible in Captivate?
var userVarName = 'userName'; //what is the name of the user name varible in Captivate?
var topTenVarName = 'topTenMsg'; //what is the name of the top ten message varible in Captivate?
/*
*** Do not edit below this line unless you are confident about your JS skills. ***
*/
var userScore = window.cpAPIInterface.getVariableValue(scoreVarName); //Get the score from the player
var userTag = window.cpAPIInterface.getVariableValue(userVarName); //Get the user name from the player
window.cpAPIInterface.setVariableValue(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 (<br> creates a new line)
var userResults = 'You placed ' + topTenJson.user.rank + ' with a score of ' + topTenJson.user.score + '<br>';
//Then we introduce the top ten.
var topTenUsers = 'The top ' + topTenJson.users.length + ' gamers are: <br>';
//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 + '<br>';
}
//Push the message back into Captivate
window.cpAPIInterface.setVariableValue(topTenVarName, topTenMsg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment