Skip to content

Commit

Permalink
WIP. Fixed match list retrieval and implemented match details and tim…
Browse files Browse the repository at this point in the history
…eline retrieval. Needs testing
  • Loading branch information
hcaseyal committed Nov 20, 2017
1 parent 7d1125b commit 9db56dd
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 48 deletions.
Binary file added img/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<input type="button" class="enter-btn" value="Enter" onclick="searchSummoner()"></input>
</div>
<div class="filters"></div>
<div class="vis-area"></div>
<div class="vis-area">
<div class="summmoner-name-display"></div>
</div>
<div class="analysis-area"></div>
</body>

Expand Down
95 changes: 89 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Example URL for requesting from RIOT API:
// https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/RiotSchmick?api_key=RGAPI-0c3f4dea-5a44-40ce-b29d-88e00f218389
// Requests the summoner object for RiotSchmick

var mapWidth = 750;
var mapHeight = 750;
var analysisArea = d3.select('.analysis-area');
var visArea = d3.select('.vis-area');

// Node server url
var baseURL = "http://localhost:8080/";

// Add an SVG element to the DOM
var svg = d3.select('body').select('.vis-area')
.append('svg')
.attr('width', mapWidth)
.attr('height', mapHeight);

// Add SVG map at correct size, assuming map is saved in a subdirectory called `data`
svg.append('image')
.attr('width', mapWidth)
.attr('height', mapHeight)
.attr('xlink:href', 'img/map.png');

function searchSummoner() {
var summoner = d3.select('.summoner-name').node().value;
visArea.text(summoner);
Expand All @@ -19,8 +29,81 @@ function searchForSummoner(summoner) {
var url = `${baseURL}summoner/?name=${summoner}`;
xhttp.open("GET", url);
xhttp.onload = () => {
analysisArea.text(xhttp.responseText);
var data = JSON.parse(xhttp.responseText);
dispalySummonerInfo(data);
renderMapKda(receiveKdaData(data));
displayAnalysis(data);
}
xhttp.onerror = () => analysisArea.text("ERROR " + xhttp.statusText);
xhttp.onerror = () => analysisArea.text("Couldn't retrieve summoner. " + xhttp.statusText);
xhttp.send();
}

function displaySummonerInfo(data) {
d3.select('.summoner-name-display').text(data.summoner.name);
}

function receiveKdaData(data) {
var timelines = data.timelines;
var kills = [];
var deaths = [];
var assists = [];

for (var i = 0; i < timelines.length; i++) {
var match = timelines[i];
for (var j in match) {
var matchInfoAtMinute = match[j];
for (var eventIndex in matchInfoAtMinute.events) {
var event = matchInfoAtMinute.events[eventIndex];
event.id = i;
if (isKill(event, data.summoner)) {
kills.push(event);
}
else if (isDeath(event, data.summoner)) {
deaths.push(event);
}
else if (isAssist(event, data.summoner)){
assists.push(event);
}
}
}
}

return {kills, deaths, assists};
}

function renderMapKda(kda) {
var kills = svg.selectAll('.kills')
.data(kda.kills, d => d.id)
.enter()
.append('circle')
.attr('class', 'kills');

var circles = svg.selectAll('.kills')
.data(kda.kills, d => d.id)
.enter()
.append('circle')
.attr('class', 'kills');

var circles = svg.selectAll('.kills')
.data(kda.kills, d => d.id)
.enter()
.append('circle')
.attr('class', 'kills');

}

function isKill(event, summonerInfo) {
return (event.killer === summonerInfo.accountId);
}

function isDeath(event, summonerInfo) {
return (event.victim === summonerInfo.accountId);
}

function isAssist(event, summonerInfo) {
return (event.assists.contains(champion => champion === summonerInfo.accountId));
}

function displayAnalysis(data) {

}
173 changes: 132 additions & 41 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Example URL for requesting from RIOT API:
// https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/RiotSchmick?api_key=RGAPI-0c3f4dea-5a44-40ce-b29d-88e00f218389
// Requests the summoner object for RiotSchmick

var http = require('http');
const axios = require('axios');
var express = require('express');
Expand Down Expand Up @@ -33,61 +37,154 @@ app.get('/matchlist', function (req, res) {

app.listen(port, function () {
//TODO: remove prefetchData
//prefetchData();
prefetchMasterLeagueMatchData();
//prefetchFavoritesMatchData();
prefetchMasterLeagueMatchListData();
prefetchTimelinesData();
console.log(`Server running at http://localhost:${port}/`)
});

function onMatchlistFileContent(filename, content, requestContext, gameIdCache) {
var matches = JSON.parse(content);
for (var i = 0; i < matches.length; i++) {
var gameId = matches[i].gameId;

if (!gameIdCache[gameId]) {
// For each game that hasn't already been cached,
// get the timeline and details from Riot and write it to a file
setTimeout(() => {
fetchAndWriteTimelineAndMatchDetails(gameId);
}, 3000 * requestContext.requestId);
requestContext.requestId++;
gameIdCache[gameId] = true;
}
}
}

function onError(err) {
console.log(err);
}

function prefetchData() {
function prefetchTimelinesData() {
var requestContext = {requestId: 0};
var gameIdCache = {};
readFiles("./matchlist_data", function(filename, content) {
onMatchlistFileContent(filename, content, requestContext, gameIdCache)
},
onError);
}

function readFiles(dirname, onFileContent, onError) {
fs.readdir(dirname, function(err, filenames) {
if (err) {
onError(err);
return;
}
filenames.forEach(function(filename) {
fs.readFile(dirname + filename, 'utf-8', function(err, content) {
if (err) {
onError(err);
return;
}
onFileContent(filename, content);
});
});
});
}
function prefetchFavoritesMatchData() {
var summonerList =
["Trick2g", "Hikashikun", "Sirhcez", "Amrasarfeiniel",
"FlowerKitten", "Reignover", "Meteos", "IWDominate", "Voyboy", "Doublelift", "Bjergsen",
"FlowerKitten", "Reignover", "Meteos", "Voyboy", "Doublelift", "Bjergsen",
"Svenskeren", "HotGuySixPack", "Xmithie", "ILovePotatoChips", "xNaotox"];

var requestId = 0;
for (var i = 0; i < summonerList.length; i++) {
let name = summonerList[i];
fetchSummonerInfo(name).then((info) => {
console.log(info);
for (let index = 0; index < 1000; index += 100) {
setTimeout(() => fetchMatchlist(info.accountId, name, index), 1500 * requestId);
requestId++;
}
});
}
prefetchMatchListData(summonerList);
}

function prefetchMasterLeagueMatchData() {
fetchMasterLeague().then((master) => {
var requestId = 0;
for (let i = 0; i < master.entries.length; i++) {
function prefetchMatchListData(summonerNames) {
console.log(summonerNames);
var requestContext = {requestId: 0};
for (let i = 0; i < summonerNames.length; i++) {
let name = summonerNames[i];
setTimeout(() => {
let name = master.entries[i].playerOrTeamName;
fetchSummonerInfo(name).then((info) => {
for (let index = 0; index < 1000; index += 100) {
setTimeout(() => fetchMatchlist(info.accountId, name, index), 1500 * requestId);
requestId++;
}
fetchAndWriteMatchList(info, name, requestContext);
});
}, 1500 * requestId);
requestId++;
}, 1500 * requestContext.requestId);
requestContext.requestId++;
}
}

function fetchAndWriteMatchList(info, name, requestContext) {
let promises = [];
for (let index = 0; index < 1000; index += 100) {
promises.push(new Promise((resolve, reject) => {
setTimeout(() => fetchMatchlist(info.accountId, name, index, resolve), 1500 * requestContext.requestId);
}));
requestContext.requestId++;
}

//matchlists is an array of the 100Matchlists
Promise.all(promises).then(matchLists => {
let matches = matchLists.reduce((prev, next) => prev.concat(next.matches), []);
fs.writeFile("matchlist_data/matchlist_" + info.accountId + "_" + name, JSON.stringify(matches), function(err) {
if(err) {
console.log(err);
}
else {
console.log("File saved");
}
});
});
}

function fetchAndWriteTimelineAndMatchDetails(gameId) {
var timelineUrl = `${baseURL}/lol/match/v3/timelines/by-match/${gameId}?api_key=${apiKey}`;
axios.get(timelineURl)
.then(response => {
fs.writeFile("timeline_data/timeline_" + gameId, JSON.stringify(response.data), function(err) {
if(err) {
console.log(err);
}
else {
console.log("File saved");
}
});
});

var matchDetailsUrl = `${baseURL}/lol/match/v3/matches/${matchId}?api_key=${apiKey}`;

axios.get(matchDetailsUrl);
.then(response => {
fs.writeFile("matchdetails_data/matchdetails_" + gameId, JSON.stringify(response.data), function(err) {
if(err) {
console.log(err);
}
else {
console.log("File saved");
}
});
});
}

function prefetchMasterLeagueMatchListData() {
fetchMasterLeague().then((master) => {
var names = master.entries.map(entry => entry.playerOrTeamName);
prefetchMatchListData(names);
});
}

function fetchMasterLeague() {
var url = `${baseURL}league/v3/masterleagues/by-queue/RANKED_SOLO_5x5?api_key=${apiKey}`;
return axios.get(url)
.then(response => {
return response.data;
})
.catch(error => {
console.log(error);
});
.then(response => {
return response.data;
})
.catch(error => {
console.log(error);
});
}

function fetchSummonerInfo(name) {
console.log("Fetching summoner info for " + name);
var url = encodeURI(`${baseURL}summoner/v3/summoners/by-name/${name}?api_key=${apiKey}`);
return axios.get(url)
.then(response => {
Expand All @@ -98,19 +195,13 @@ function fetchSummonerInfo(name) {
});
}

function fetchMatchlist(accountId, name, beginIndex) {
function fetchMatchlist(accountId, name, beginIndex, resolve) {
console.log("Fetching match list for " + name);
var url = `${baseURL}match/v3/matchlists/by-account/${accountId}?api_key=${apiKey}&beginIndex=${beginIndex}`;

axios.get(url)
.then(response => {
fs.appendFile("data/matchlist_" + accountId + "_" + name, JSON.stringify(response.data), function(err) {
if(err) {
console.log(err);
}
else {
console.log("File saved");
}
});
resolve(response.data);
})
.catch(error => {
console.log(error);
Expand Down

0 comments on commit 9db56dd

Please sign in to comment.