LeagueStats/server.js

136 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-12-01 16:51:26 +00:00
const express = require('express')
const request = require('request');
const path = require('path');
2018-12-03 21:11:52 +00:00
const bodyParser = require('body-parser');
2018-12-05 21:04:16 +00:00
const rp = require('request-promise');
var Promise = require("bluebird");
2018-12-01 16:51:26 +00:00
const app = express()
2018-12-01 16:56:41 +00:00
app.set('port', (process.env.PORT || 5000))
2018-12-01 16:51:26 +00:00
2019-02-03 14:35:42 +00:00
const key = 'RGAPI-a9f85905-6287-4c29-bf01-8d8c56e9f75f';
2018-12-01 16:51:26 +00:00
var summonerID = 'HMOiIUvzYtfgPk5X53zWTeOZo52T-HYJQhwvhkPNh0BWxZ0';
var accountID = 'V1xNS14bjVeP54hg03JeMxkXJB29K4TfUMvijDB85nxbD4Y';
var pseudo = 'Chil';
2018-12-05 21:04:16 +00:00
var JSONMatches;
2018-12-01 16:51:26 +00:00
2018-12-03 21:11:52 +00:00
/* To retrieve data of post request */
2018-12-05 21:04:16 +00:00
app.use(bodyParser.json()); // to support JSON-encoded bodies
2018-12-03 21:11:52 +00:00
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
2018-12-05 21:04:16 +00:00
}));
2018-12-03 21:11:52 +00:00
2018-12-01 16:51:26 +00:00
/* Homepage */
app.get('/', function (request, response) {
2019-03-18 19:55:33 +00:00
response.sendFile(path.join(__dirname, 'home.html'));
});
app.get('/summoners', function (request, response) {
2018-12-01 16:51:26 +00:00
response.sendFile(path.join(__dirname, 'index.html'));
});
/* Public assets folder */
app.use('/public', express.static(__dirname + '/public'));
/* Launch app */
2018-12-01 16:56:41 +00:00
app.listen(app.get('port'), () => console.log(`RiotAPI test app listening on port ${app.get('port')}!`))
2018-12-01 16:51:26 +00:00
2018-12-05 21:04:16 +00:00
2019-03-18 19:55:33 +00:00
async function apicall(urlApi) {
2018-12-05 21:04:16 +00:00
//console.log(urlApi);
return rp({ url: 'https://euw1.api.riotgames.com/lol/match/v4/matches/' + urlApi + '?api_key=' + key, json: true }).then(function (obj) {
return addMatchToJSON(obj);
});
}
2018-12-01 16:51:26 +00:00
// Get data of rankeds
function getRanked(callback) {
request('https://euw1.api.riotgames.com/lol/league/v4/positions/by-summoner/' + summonerID + '?api_key=' + key, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(body);
}
})
}
// send data of rankeds and of username
2018-12-05 21:04:16 +00:00
app.post('/api', function (req, res) {
//console.log(req.body.playerName);
2018-12-03 21:11:52 +00:00
pseudo = req.body.playerName;
getAccountInfos(function (account) {
2018-12-11 22:17:33 +00:00
if (!account)
res.send(null)
2018-12-03 21:11:52 +00:00
getRanked(function (ranked) {
2018-12-05 21:04:16 +00:00
getMatches(function (matches) {
2018-12-03 21:11:52 +00:00
var finalJSON = new Array();
finalJSON.push(account);
finalJSON.push(ranked);
finalJSON.push(matches);
2019-03-18 19:55:33 +00:00
console.log('Data sent to front');
2018-12-03 21:11:52 +00:00
res.send(finalJSON);
});
});
});
});
2018-12-01 16:51:26 +00:00
// Get account infos of an username
function getAccountInfos(callback) {
request('https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + pseudo + '?api_key=' + key, function (error, response, body) {
if (!error && response.statusCode == 200) {
var JSONBody = JSON.parse(body);
2018-12-05 21:04:16 +00:00
//console.log(JSONBody);
2018-12-01 16:51:26 +00:00
summonerID = JSONBody.id;
accountID = JSONBody.accountId;
callback(JSONBody);
}
2018-12-11 22:17:33 +00:00
else {
console.log('username not found');
callback(null);
}
2018-12-01 16:51:26 +00:00
});
}
// Get matches of an accountID
function getMatches(callback) {
2019-03-18 19:55:33 +00:00
console.time('getMatches');
2018-12-05 21:04:16 +00:00
request('https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/' + accountID + '?endIndex=10&api_key=' + key, function (error, response, body) {
2018-12-01 16:51:26 +00:00
if (!error && response.statusCode == 200) {
2018-12-05 21:04:16 +00:00
JSONMatches = JSON.parse(body);
2018-12-11 22:17:33 +00:00
2018-12-10 20:03:04 +00:00
var matchsId = new Array();
for (var i = 0; i < JSONMatches.matches.length; i++) {
2018-12-05 21:04:16 +00:00
matchsId[i] = JSONMatches.matches[i].gameId;
}
Promise.mapSeries(matchsId, function (item) {
return apicall(item);
}).then(() => {
2019-03-18 19:55:33 +00:00
console.timeEnd('getMatches');
2018-12-05 21:04:16 +00:00
console.log('Finished');
callback(JSONMatches);
}).catch(err => {
console.log('Error');
});
2018-12-01 16:51:26 +00:00
}
});
2018-12-05 21:04:16 +00:00
}
function addMatchToJSON(obj) {
//console.log(obj.gameId);
2018-12-10 20:03:04 +00:00
for (var i = 0; i < JSONMatches.matches.length; i++) {
2018-12-11 22:17:33 +00:00
if (JSONMatches.matches[i].gameId == obj.gameId) {
2018-12-05 21:04:16 +00:00
//console.log('yes');
JSONMatches.matches[i] = obj;
}
}
2018-12-03 21:11:52 +00:00
}