Created
April 23, 2018 20:59
-
-
Save FeraruSilviuMarian/f7a392951d42e181f36b2061541766bb to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* @OnlyCurrentDoc | |
*/ | |
// setup | |
var usernameP1 = 'first username goes here'; | |
var usernameP2 = 'second username goes here'; | |
var sheetMode = 'duo'; // available modes: 'solo', 'duo', 'all' or 'squads' | |
// get an api key by direct messaging @Fortnite Stats on discord, with the message !getapikey visit https://fortnite.y3n.co/ for more details | |
var key = 'your key goes here'; | |
// title | |
var titleCellRange = 'B2:D2'; | |
// columns | |
var entryColumn = 'M'; | |
var winsP1Column = 'N'; | |
var winsP2Column = 'O'; | |
var winrateP1Column = 'P'; | |
var winrateP2Column = 'Q'; | |
var killsP1Column = 'R'; | |
var killsP2Column = 'S'; | |
var matchesP1Column = 'T'; | |
var matchesP2Column = 'U'; | |
var kpdP1Column = 'V'; | |
var kpdP2Column = 'W'; | |
// column title ranges | |
var entryTitleRange = 'M3'; | |
var winsP1TitleRange = 'N3'; | |
var winsP2TitleRange = 'O3'; | |
var winrateP1TitleRange = 'P3'; | |
var winrateP2TitleRange = 'Q3'; | |
var killsP1TitleRange = 'R3'; | |
var killsP2TitleRange = 'S3'; | |
var matchesP1TitleRange = 'T3'; | |
var matchesP2TitleRange = 'U3'; | |
var kpdP1TitleRange = 'V3'; | |
var kpdP2TitleRange = 'W3'; | |
// datestamp and timestamp columns | |
var timestampColumn = 'Y'; // datestamp | |
var currentTImeColumn = 'X'; // timestamp | |
// server status | |
var serverStatusCell = 'E2:F2'; | |
// server message | |
var serverMessageCell = 'G2:I2'; | |
// data cells | |
var currentRowDataCell = 'Z1'; | |
// populate a row with stats, called on a time based interval, edit the time interval from edit>current project trigger | |
function populate(){ | |
var gameStatus = getGamestatusJson(); // API call | |
updateServerMessages(gameStatus); // server status and server message displays | |
updateCellNames(); | |
initializeVariables(); | |
var crow = SpreadsheetApp.getActiveSheet().getRange(currentRowDataCell).getValue(); // current row to populate | |
var jsonp1 = getResponse(usernameP1); // API call | |
var jsonp2 = getResponse(usernameP2); // API call | |
// populate rows IFF at least one player played new matches | |
if ((playedNewMatch(crow,jsonp1, 'p1')) || (playedNewMatch(crow,jsonp2, 'p2'))) { | |
populateRow(crow, jsonp1, 'p1'); | |
populateRow(crow, jsonp2, 'p2'); | |
timeStamp(crow); // timestamp the current row | |
// increment current row to prepare for next call | |
var temp = parseInt(crow) + 1; | |
var set = temp.toString(); | |
SpreadsheetApp.getActiveSheet().getRange(currentRowDataCell).setValue(set); | |
} | |
} | |
function soloStats(crow, json, player){ | |
// prepare data | |
var wins = json.br.stats.pc.solo.wins | |
var winrate = json.br.stats.pc.solo.winRate | |
var kills = json.br.stats.pc.solo.kills | |
var matches = json.br.stats.pc.solo.matchesPlayed | |
var kpd = json.br.stats.pc.solo.kpd | |
insertDataIntoTable(crow, wins, winrate, kills, matches, kpd, player); | |
} | |
function duoStats(crow, json, player){ | |
// prepare data | |
var wins = json.br.stats.pc.duo.wins | |
var winrate = json.br.stats.pc.duo.winRate | |
var kills = json.br.stats.pc.duo.kills | |
var matches = json.br.stats.pc.duo.matchesPlayed | |
var kpd = json.br.stats.pc.duo.kpd | |
insertDataIntoTable(crow, wins, winrate, kills, matches, kpd, player); | |
} | |
function squadStats(crow, json, player){ | |
// prepare data | |
var wins = json.br.stats.pc.squad.wins | |
var winrate = json.br.stats.pc.squad.winRate | |
var kills = json.br.stats.pc.squad.kills | |
var matches = json.br.stats.pc.squad.matchesPlayed | |
var kpd = json.br.stats.pc.squad.kpd | |
insertDataIntoTable(crow, wins, winrate, kills, matches, kpd, player); | |
} | |
function allStats(crow, json, player){ | |
// prepare data | |
var wins = json.br.stats.pc.all.wins | |
var winrate = json.br.stats.pc.all.winRate | |
var kills = json.br.stats.pc.all.kills | |
var matches = json.br.stats.pc.all.matchesPlayed | |
var kpd = json.br.stats.pc.all.kpd | |
insertDataIntoTable(crow, wins, winrate, kills, matches, kpd, player); | |
} | |
function insertDataIntoTable(crow, wins, winrate, kills, matches, kpd, player){ | |
SpreadsheetApp.getActiveSheet().getRange(entryColumn + crow).setValue(crow - 4); | |
if(player == 'p1'){ | |
SpreadsheetApp.getActiveSheet().getRange(winsP1Column + crow).setValue(wins); | |
SpreadsheetApp.getActiveSheet().getRange(winrateP1Column + crow).setValue(winrate); | |
SpreadsheetApp.getActiveSheet().getRange(killsP1Column + crow).setValue(kills); | |
SpreadsheetApp.getActiveSheet().getRange(matchesP1Column + crow).setValue(matches); | |
SpreadsheetApp.getActiveSheet().getRange(kpdP1Column + crow).setValue(kpd); | |
}else if(player == 'p2'){ | |
SpreadsheetApp.getActiveSheet().getRange(winsP2Column + crow).setValue(wins); | |
SpreadsheetApp.getActiveSheet().getRange(winrateP2Column + crow).setValue(winrate); | |
SpreadsheetApp.getActiveSheet().getRange(killsP2Column + crow).setValue(kills); | |
SpreadsheetApp.getActiveSheet().getRange(matchesP2Column + crow).setValue(matches); | |
SpreadsheetApp.getActiveSheet().getRange(kpdP2Column + crow).setValue(kpd); | |
} | |
} | |
function populateRow(crow, json, player){ | |
if(sheetMode == 'solo'){ | |
soloStats(crow, json, player); | |
} | |
else if(sheetMode == 'duo'){ | |
duoStats(crow, json, player); | |
} | |
else if(sheetMode == 'squad'){ | |
squadStats(crow, json, player); | |
} | |
else if(sheetMode == 'all'){ | |
allStats(crow, json, player); | |
} | |
} | |
function playedNewMatch(crow, json, player){ | |
var lastRow = parseInt(crow) - 1; | |
var lastMatches = 0; | |
if(crow == 4){ | |
lastMatches = 0; | |
}else{ | |
if(player == 'p1'){ | |
lastMatches = parseInt(SpreadsheetApp.getActiveSheet().getRange(matchesP1Column + lastRow.toString()).getValue()); | |
}else if (player == 'p2'){ | |
lastMatches = parseInt(SpreadsheetApp.getActiveSheet().getRange(matchesP2Column + lastRow.toString()).getValue()); | |
} | |
} | |
var newMatches = getMatches(json); | |
return (newMatches > lastMatches); | |
} | |
function getMatches(json){ | |
if(sheetMode == 'solo'){ | |
return parseInt(json.br.stats.pc.solo.matchesPlayed); | |
} | |
else if(sheetMode == 'duo'){ | |
return parseInt(json.br.stats.pc.duo.matchesPlayed); | |
} | |
else if(sheetMode == 'squad'){ | |
return parseInt(json.br.stats.pc.squad.matchesPlayed); | |
} | |
else if(sheetMode == 'all'){ | |
return parseInt(json.br.stats.pc.all.matchesPlayed); | |
} | |
} | |
// helper functions | |
function timeStamp(crow){ | |
var date = Utilities.formatDate(new Date(), "GMT+2", "dd/MM/yyyy"); // formated date | |
SpreadsheetApp.getActiveSheet().getRange(timestampColumn + crow).setValue(date); // Set the value of a cell to a date. | |
var d = new Date(); | |
var currentTime = d.toLocaleTimeString().substring(0,11); | |
SpreadsheetApp.getActiveSheet().getRange(currentTImeColumn + crow).setValue(currentTime); | |
} | |
function getResponse(ftname){ | |
var url = "https://fortnite.y3n.co/v2/player/" + ftname; | |
var options = { | |
'headers': {'X-Key': key} | |
}; | |
var response = UrlFetchApp.fetch(url, options); | |
//var response = UrlFetchApp.fetch('https://ide502-silvermirai.cs50.io:8080/fortnitestats?username=' + username); // using my server as relay | |
var text = response.getContentText(); | |
var json = JSON.parse(text); | |
return json; | |
} | |
function updateServerMessages(gameStatus){ | |
SpreadsheetApp.getActiveSheet().getRange(serverStatusCell).setValue('servers: ' + gameStatus.status); | |
SpreadsheetApp.getActiveSheet().getRange(serverMessageCell).setValue(gameStatus.message); | |
} | |
function isServerUp(gameStatus){ | |
return (gameStatus.status == 'UP'); | |
} | |
function getGamestatusJson(){ | |
var url = "https://fortnite.y3n.co/v2/gamestatus"; | |
var options = { | |
'headers': {'X-Key': key} | |
}; | |
var response = UrlFetchApp.fetch(url, options); | |
var text = response.getContentText(); | |
var json = JSON.parse(text); | |
return(json); | |
} | |
// TODO | |
function updateCellNames(){ | |
SpreadsheetApp.getActiveSheet().getRange(titleCellRange).setValue('Fortnite stats ' + sheetMode); | |
SpreadsheetApp.getActiveSheet().getRange(entryTitleRange).setValue('entry'); | |
SpreadsheetApp.getActiveSheet().getRange(winsP1TitleRange).setValue('wins ' + usernameP1); | |
SpreadsheetApp.getActiveSheet().getRange(winsP2TitleRange).setValue('wins ' + usernameP2); | |
SpreadsheetApp.getActiveSheet().getRange(winrateP1TitleRange).setValue('winrate ' + usernameP1); | |
SpreadsheetApp.getActiveSheet().getRange(winrateP2TitleRange).setValue('winrate ' + usernameP2); | |
SpreadsheetApp.getActiveSheet().getRange(killsP1TitleRange).setValue('kills ' + usernameP1); | |
SpreadsheetApp.getActiveSheet().getRange(killsP2TitleRange).setValue('kills ' + usernameP2); | |
SpreadsheetApp.getActiveSheet().getRange(matchesP1TitleRange).setValue('matches ' + usernameP1); | |
SpreadsheetApp.getActiveSheet().getRange(matchesP2TitleRange).setValue('matches ' + usernameP2); | |
SpreadsheetApp.getActiveSheet().getRange(kpdP1TitleRange).setValue('kpd ' + usernameP1); | |
SpreadsheetApp.getActiveSheet().getRange(kpdP2TitleRange).setValue('kpd ' + usernameP2); | |
} | |
function initializeVariables(){ | |
var cellCrowVal = SpreadsheetApp.getActiveSheet().getRange(currentRowDataCell).getValue(); | |
if(cellCrowVal == ''){ | |
SpreadsheetApp.getActiveSheet().getRange(currentRowDataCell).setValue(4); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment