LeagueStats/server.js
Valentin Kaelin 6289229573 Refactor some code 🤟
2019-03-25 21:32:32 +01:00

122 lines
3.6 KiB
JavaScript

require('dotenv').config()
const express = require('express')
const request = require('request');
const path = require('path');
const bodyParser = require('body-parser');
const rp = require('request-promise');
const Promise = require("bluebird");
const app = express()
/* Global Variables */
const key = process.env.API_KEY;
let summonerID;
let accountID;
let username;
let JSONMatches;
let finalJSON = [];
/* Set Port */
app.set('port', (process.env.PORT || 5000))
/* To retrieve data of post request */
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
/* Homepage */
app.get('/', function (request, response) {
response.sendFile(path.join(__dirname, 'home.html'));
});
/* Summoners pages */
app.get('/summoners', function (request, response) {
response.sendFile(path.join(__dirname, 'summoner.html'));
});
/* Public assets folder */
app.use('/public', express.static(__dirname + '/public'));
/* Launch app */
app.listen(app.get('port'), () => console.log(`RiotAPI app listening on port ${app.get('port')}!`))
// Send data of a summoner
app.post('/api', function (req, res) {
//console.log(req.body.playerName);
console.time('all')
username = req.body.playerName;
finalJSON = [];
getAccountInfos(res);
});
// Get account infos of an username
const getAccountInfos = function (res) {
request('https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + encodeURIComponent(username) + '?api_key=' + key, function (error, response, body) {
if (!error && response.statusCode == 200) {
let JSONBody = JSON.parse(body);
summonerID = JSONBody.id;
accountID = JSONBody.accountId;
finalJSON.push(JSONBody)
getRanked(res);
}
else {
console.log(response.statusCode);
console.log('username not found');
res.send(null);
}
});
}
// 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);
}
})
}
// Get 10 matches of an accountID
const getMatches = function (res) {
console.time('getMatches');
request('https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/' + accountID + '?endIndex=10&api_key=' + key, function (error, response, body) {
if (!error && response.statusCode == 200) {
JSONMatches = JSON.parse(body);
const matchsId = JSONMatches.matches.map(x => x.gameId)
Promise.map(matchsId, function (id) {
return getMatch('match/v4/matches/' + id);
}).then(() => {
console.timeEnd('getMatches');
console.log('Finished - Data sent to front');
finalJSON.push(JSONMatches)
res.send(finalJSON);
console.timeEnd('all')
}).catch(err => {
console.log('Error Promise');
console.log(err);
});
}
});
}
// 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);
});
}
// Add match to global JSON
const addMatchToJSON = function (obj) {
JSONMatches.matches = JSONMatches.matches.map((match) => match.gameId === obj.gameId ? obj : match);
}