LeagueStats/server.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-03-24 19:25:45 +00:00
require('dotenv').config()
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');
2019-03-24 19:25:45 +00:00
const Promise = require("bluebird");
2018-12-01 16:51:26 +00:00
const app = express()
2019-03-24 19:25:45 +00:00
/* Global Variables */
const key = process.env.API_KEY;
let summonerID;
let accountID;
let username;
let JSONMatches;
2019-03-25 20:32:32 +00:00
let finalJSON = [];
2019-03-24 19:25:45 +00:00
/* Set Port */
app.set('port', (process.env.PORT || 5000))
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'));
});
2019-03-24 19:25:45 +00:00
/* Summoners pages */
2019-03-18 19:55:33 +00:00
app.get('/summoners', function (request, response) {
2019-03-23 20:28:10 +00:00
response.sendFile(path.join(__dirname, 'summoner.html'));
2018-12-01 16:51:26 +00:00
});
/* Public assets folder */
app.use('/public', express.static(__dirname + '/public'));
/* Launch app */
2019-03-25 20:32:32 +00:00
app.listen(app.get('port'), () => console.log(`RiotAPI app listening on port ${app.get('port')}!`))
2018-12-01 16:51:26 +00:00
2019-03-25 20:32:32 +00:00
// Send data of a summoner
2018-12-05 21:04:16 +00:00
app.post('/api', function (req, res) {
//console.log(req.body.playerName);
2019-03-24 18:47:13 +00:00
console.time('all')
2019-03-24 19:25:45 +00:00
username = req.body.playerName;
2019-03-25 20:32:32 +00:00
finalJSON = [];
getAccountInfos(res);
2018-12-03 21:11:52 +00:00
});
2018-12-01 16:51:26 +00:00
// Get account infos of an username
2019-03-25 20:32:32 +00:00
const getAccountInfos = function (res) {
2019-03-24 19:25:45 +00:00
request('https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + encodeURIComponent(username) + '?api_key=' + key, function (error, response, body) {
2018-12-01 16:51:26 +00:00
if (!error && response.statusCode == 200) {
2019-03-24 19:25:45 +00:00
let JSONBody = JSON.parse(body);
2018-12-01 16:51:26 +00:00
summonerID = JSONBody.id;
accountID = JSONBody.accountId;
2019-03-25 20:32:32 +00:00
finalJSON.push(JSONBody)
getRanked(res);
2018-12-01 16:51:26 +00:00
}
2018-12-11 22:17:33 +00:00
else {
2019-03-24 18:47:13 +00:00
console.log(response.statusCode);
2018-12-11 22:17:33 +00:00
console.log('username not found');
2019-03-25 20:32:32 +00:00
res.send(null);
2018-12-11 22:17:33 +00:00
}
2018-12-01 16:51:26 +00:00
});
}
2019-03-25 20:32:32 +00:00
// Get data of rankeds stats
const getRanked = function (res) {
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) {
let JSONBody = JSON.parse(body);
if (JSONBody.length > 0) {
finalJSON.push(...JSONBody);
} else {
console.log('empty rank stats')
finalJSON.push(null, null);
}
getMatches(res);
}
})
}
2018-12-01 16:51:26 +00:00
2019-03-25 20:32:32 +00:00
// Get 10 matches of an accountID
const getMatches = function (res) {
2019-03-18 19:55:33 +00:00
console.time('getMatches');
2019-03-25 20:32:32 +00:00
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);
2019-03-25 20:32:32 +00:00
const matchsId = JSONMatches.matches.map(x => x.gameId)
2018-12-11 22:17:33 +00:00
2019-03-25 20:32:32 +00:00
Promise.map(matchsId, function (id) {
return getMatch('match/v4/matches/' + id);
2018-12-05 21:04:16 +00:00
}).then(() => {
2019-03-18 19:55:33 +00:00
console.timeEnd('getMatches');
2019-03-25 20:32:32 +00:00
console.log('Finished - Data sent to front');
finalJSON.push(JSONMatches)
res.send(finalJSON);
console.timeEnd('all')
2018-12-05 21:04:16 +00:00
}).catch(err => {
2019-03-25 20:32:32 +00:00
console.log('Error Promise');
console.log(err);
2018-12-05 21:04:16 +00:00
});
2018-12-01 16:51:26 +00:00
}
});
2018-12-05 21:04:16 +00:00
}
2019-03-25 20:32:32 +00:00
// Get data of one match
const getMatch = async function (urlApi) {
//console.log(urlApi);
return rp({ url: 'https://euw1.api.riotgames.com/lol/' + urlApi + '?api_key=' + key, json: true }).then(function (obj) {
return addMatchToJSON(obj);
});
}
2019-03-24 19:25:45 +00:00
// Add match to global JSON
2019-03-25 20:32:32 +00:00
const addMatchToJSON = function (obj) {
JSONMatches.matches = JSONMatches.matches.map((match) => match.gameId === obj.gameId ? obj : match);
}