Last active
April 3, 2021 04:18
-
-
Save saamerm/08cce5ff8039854fd2fa14af1eab391a to your computer and use it in GitHub Desktop.
Free and universal leaderboard/scoreboard for Games using Google Sheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: For this to work, you just need to put your spreadsheet ID here in lines 26 and 43 and follow this tutorial https://medium.com/@prototypemakers/simplest-universal-free-game-leaderboard-with-google-sheets-5ab548db009f | |
// to see the steps for implementing this | |
// POST and GET API Entry points | |
// ------------ | |
function doPost(request){ | |
var requestObject = JSON.parse(request.postData.contents); | |
var result = processPostRequest(requestObject); | |
return ContentService | |
.createTextOutput(JSON.stringify(result)) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
function doGet(){ | |
var result = processGetRequest(); | |
return ContentService | |
.createTextOutput(JSON.stringify(result)) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
// Private Functions | |
// ------------ | |
function processGetRequest() | |
{ | |
var sheet = SpreadsheetApp.openById("PUT YOUR Google Sheet ID HERE"); | |
var x = JSON.parse('[]'); | |
var i; | |
for (i=0; i < 10; i++) | |
{ | |
var a = SpreadsheetApp.getActiveSheet().getRange(i+2,2).getValue(); | |
var b = SpreadsheetApp.getActiveSheet().getRange(i+2,3).getValue(); | |
var c = SpreadsheetApp.getActiveSheet().getRange(i+2,4).getValue(); | |
x[i] =JSON.parse('{"Rank": ' + a + ',"Name": "' + b + '","Score": ' + c + '}'); | |
} | |
console.log(x) | |
return x; | |
} | |
function processPostRequest(requestObject) | |
{ | |
// Open Google Sheet using ID | |
var sheet = SpreadsheetApp.openById("PUT YOUR Google Sheet ID HERE"); | |
var result = {"Status": "LOSE", "Message": "Score was not enough"}; | |
try{ | |
var today = new Date(); | |
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate(); | |
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); | |
var dateTime = date+' '+time; | |
// Update data on Google Sheet | |
var i; | |
var startSwapping = false; | |
var previousValue =JSON.parse('{"Timestamp": "0","Rank": 0,"Name": "0","Score": "0"}'); | |
var swapHelper =JSON.parse('{"Timestamp": "0","Rank": 0,"Name": "0","Score": "0"}'); | |
for (i = 1; i < 11; i ++) | |
{ | |
if (startSwapping) | |
{ | |
swapHelper.TimeStamp = previousValue.TimeStamp; | |
swapHelper.Name = previousValue.Name; | |
swapHelper.Score = previousValue.Score; | |
previousValue.TimeStamp = SpreadsheetApp.getActiveSheet().getRange(i+1,1).getValue(); | |
previousValue.Name = SpreadsheetApp.getActiveSheet().getRange(i+1,3).getValue(); | |
previousValue.Score = SpreadsheetApp.getActiveSheet().getRange(i+1,4).getValue(); | |
SpreadsheetApp.getActiveSheet().getRange(i+1,1).setValue(swapHelper.TimeStamp); | |
SpreadsheetApp.getActiveSheet().getRange(i+1,3).setValue(swapHelper.Name); | |
SpreadsheetApp.getActiveSheet().getRange(i+1,4).setValue(swapHelper.Score); | |
} | |
// Change the ">" to "<" if a lower score is a higher rank in your game | |
else if (requestObject.Score > SpreadsheetApp.getActiveSheet().getRange(i+1,4).getValue()) | |
{ | |
previousValue.TimeStamp = SpreadsheetApp.getActiveSheet().getRange(i+1,1).getValue(); | |
previousValue.Name = SpreadsheetApp.getActiveSheet().getRange(i+1,3).getValue(); | |
previousValue.Score = SpreadsheetApp.getActiveSheet().getRange(i+1,4).getValue(); | |
console.log(SpreadsheetApp.getActiveSheet().getRange(i+1,4).getValue()); | |
console.log(previousValue.Score); | |
SpreadsheetApp.getActiveSheet().getRange(i+1,4).setValue(requestObject.Score); | |
SpreadsheetApp.getActiveSheet().getRange(i+1,3).setValue(requestObject.Name); | |
// We don't want to copy over rank, because the rank column should be static | |
SpreadsheetApp.getActiveSheet().getRange(i+1,1).setValue(dateTime); | |
var result = {"Status": "WIN", "Message": "Score added to the Leaderboard"}; | |
startSwapping = true; | |
} | |
} | |
} | |
catch(exc){ | |
// If error occurs, throw exception | |
result = {"Status": "FAILED", "Message": exc}; | |
} | |
// Return result | |
console.log(result) | |
return result; | |
} | |
// Unit Tests | |
// ------------ | |
function testGet(){ | |
var x = processGetRequest() | |
console.log(x) | |
} | |
function testPost(){ | |
var Name = "RAD" | |
//var Score = "0" //Test Case 1 [x] (No score added) | |
// var Score = "1" //Test Case 2 [x] (Bottom of list) | |
// var Score = "2" //Test Case 3 [x] (Between the values) | |
// var myJSObject='{"Name": "' + Name + '","Score": "' + Score + '"}' | |
// var x = processPostRequest(JSON.parse(myJSObject)) | |
var Score = "3" //Test Case 4 [] (Ensuring more than 1 score) | |
var myJSObject='{"Name": "' + Name + '","Score": "' + Score + '"}' | |
var x = processPostRequest(JSON.parse(myJSObject)) | |
var y = processPostRequest(JSON.parse(myJSObject)) | |
console.log(x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment