· 6 years ago · Apr 14, 2020, 03:20 PM
1function main() {
2
3 // Match and API Key
4 var matchID = " xxxx ";
5 var riotAPIKey = " xxx ";
6
7 // Get data from the server
8 var response = getMatchData(matchID, riotAPIKey);
9
10 // Convert response to JSON
11 var matchData = JSON.parse(response.getContentText());
12
13 //Fill MatchPlayers sheet with details from each player
14 fillMatchPlayersDetails(matchData)
15
16 //Fill Match sheet with details of the match (currently only matchId, duration, creation and which side won)
17 fillMatchDetails(matchData);
18}
19
20function getMatchData(id, key) {
21
22 // Return the response from the GET Request
23 return UrlFetchApp.fetch("https://br1.api.riotgames.com/lol/match/v4/matches/" + id.toString() + "?api_key=" + key);
24
25}
26
27
28function fillMatchPlayersDetails(data) {
29 // This is getting champion data: Name <> ID
30 var responseChampionData = UrlFetchApp.fetch("https://www.bangingheads.net/cdn/champion_en_us.json");
31
32 //Set sheet for usage
33 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
34 var Match = spreadsheet.getSheets()[0];
35 var sheet = SpreadsheetApp.setActiveSheet(Match);
36
37 // Long block getting each field from JSON
38 for (var i = 0; i < 10; i++) {
39 var playerName = ("Jogador " + (i + 1));
40 var championName = getChampionNameById(data["participants"][i].championId, responseChampionData);
41 var kills = data["participants"][i].stats["kills"];
42 var deaths = data["participants"][i].stats["deaths"];
43 var assists = data["participants"][i].stats["assists"];
44 var doubleKills = data["participants"][i].stats["doubleKills"];
45 var tripleKills = data["participants"][i].stats["tripleKills"];
46 var quadraKills = data["participants"][i].stats["quadraKills"];
47 var pentakills = data["participants"][i].stats["pentaKills"];
48 var totalDamageToChampions = data["participants"][i].stats["totalDamageDealtToChampions"];
49 var cs = data["participants"][i].stats["totalMinionsKilled"] + data["participants"][i].stats["neutralMinionsKilled"];
50 var gold = data["participants"][i].stats["goldEarned"];
51
52 if (data["participants"][i].stats["win"]) {
53 var win = 1;
54 } else {
55 var win = 0;
56 }
57
58
59 var matchId = data["gameId"];
60
61 // The roles consist of both lane and role, so we need to filter (sometimes the API brings wrong results, pay attention to that, mainly when there are strange picks like garen mid or heimerdinger bottom lane
62 var role;
63
64 switch (data["participants"][i].timeline["lane"]) {
65 case "MIDDLE":
66 role = ("Mid");
67 break;
68 case "JUNGLE":
69 role = ("Jungle");
70 break;
71 case "TOP":
72 role = ("Top");
73 break;
74 case "BOTTOM":
75 if (data["participants"][i].timeline["role"] == "DUO_CARRY") {
76 role = ("ADC");
77 } else {
78 role = ("Support");
79 }
80 break;
81 }
82
83 var visionScore = data["participants"][i].stats["visionScore"];
84
85
86 var playerDataInThisMatch = [[playerName, championName, kills, deaths, assists, totalDamageToChampions, cs, gold, win, matchId, role, visionScore, doubleKills, tripleKills, quadraKills, pentakills]];
87
88 var lastLine = sheet.getRange(sheet.getLastRow() + 1, 1, 1, 16);
89
90 //Writes on last line
91 lastLine.setValues(playerDataInThisMatch)
92 }
93
94
95
96
97}
98
99function fillMatchDetails(gameData) {
100 var responseChampionData = UrlFetchApp.fetch("https://www.bangingheads.net/cdn/champion_en_us.json");
101
102 var data = gameData;
103
104 // Get game details sheet
105 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
106 var GameDetails = spreadsheet.getSheets()[3];
107 var sheet = SpreadsheetApp.setActiveSheet(GameDetails);
108
109 //Pega dados do Jogo atual
110 var duration = (data["gameDuration"]);
111 var gameDate = new Date(data["gameCreation"]).toLocaleString(undefined, { day: 'numeric', month: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit', });
112 var win;
113 if (data["teams"][0].win == "TRUE") {
114 win = "Blue";
115 } else {
116 win = "Red";
117 }
118 var matchId = data["gameId"];
119
120 //This is divided by 86400 to convert to duration for google sheets
121 var matchData = [[matchId, duration / 86400, gameDate, win]];
122
123 // Writes it down ()
124 sheet.getRange(sheet.getLastRow() + 1, 1, 1, 4).setValues(matchData);
125
126
127 //Get bans sheeets
128 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
129 var Bans = spreadsheet.getSheets()[5];
130
131 //interact with both teams bans
132 for (var j = 0; j < 2; j++) {
133 var bans = data["teams"][j].bans;
134 for (var k = 0; k < bans.length; k++) {
135 var nome = getChampionNameById(bans[k].championId,responseChampionData);
136 sheet = spreadsheet.setActiveSheet(Bans);
137 sheet.getRange(sheet.getLastRow() + 1, 1).setValue(nome);
138 }
139 }
140
141
142}
143
144
145// Get champion name by ID
146function getChampionNameById(id, response) {
147
148 var json = JSON.parse(response.getContentText());
149
150 // not sure why this is this way and I got no time to test it yet
151 return json["" + id + ""];
152}