diff --git a/b3-dev/api/api-td-node/team-lol/.env b/b3-dev/api/api-td-node/team-lol/.env new file mode 100644 index 0000000..85ac6f4 --- /dev/null +++ b/b3-dev/api/api-td-node/team-lol/.env @@ -0,0 +1,3 @@ +lol_match=https://europe.api.riotgames.com +lol_account=https://euw1.api.riotgames.com +keyLol=RGAPI-b1198975-16a9-4767-a81c-e3fae2a6df1f \ No newline at end of file diff --git a/b3-dev/api/api-td-node/team-lol/Manager.js b/b3-dev/api/api-td-node/team-lol/Manager.js new file mode 100644 index 0000000..76f48d2 --- /dev/null +++ b/b3-dev/api/api-td-node/team-lol/Manager.js @@ -0,0 +1,74 @@ +import axios from 'axios'; +import dotenv from 'dotenv'; +dotenv.config(); + +const lolaccount = process.env.lol_account; +const lolmatch = process.env.lol_match; +const header = { headers: { 'X-Riot-Token': process.env.keyLol }}; + +export default class Manager { + + async getInfo(name) { + const user0 = await this.getAccountByName(name); + if (!user0?.puuid) { + return {error: 'Pseudo invalide'}; + } + const listMatch = await this.getMatchByUuid(user0.puuid); + const lastMatchInfo = await this.getInfoByMatchId(listMatch[0]); + let allUser = lastMatchInfo.info.participants; + allUser = allUser.map(x => ({ + puuid: x.puuid, + champion: x.championName, + kda: `${x.kills}/${x.deaths}/${x.assists}`, + resultat: x.win ? 'Victoire' : 'Defaite' + })); + let results = []; + for (const user of allUser) { + let nameLevel = await this.getNameLevelByUuid(user.puuid); + delete user.puuid; + results.push({...nameLevel, ...user}); + } + return results; + } + + async getAccountByName(name) { + let response = {}; + try { + response = await axios.get(`${lolaccount}/lol/summoner/v4/summoners/by-name/${encodeURIComponent(name)}`, header); + } catch(e) { + console.log(e); + } + return response.data; + } + + async getMatchByUuid(uuid) { + let response = {}; + try { + response = await axios.get(`${lolmatch}/lol/match/v5/matches/by-puuid/${encodeURIComponent(uuid)}/ids`, header); + } catch(e) { + console.log(e); + } + return response.data; + } + + async getInfoByMatchId(id) { + let response = {}; + try { + response = await axios.get(`${lolmatch}/lol/match/v5/matches/${encodeURIComponent(id)}`, header); + } catch(e) { + console.log(e); + } + return response.data; + } + + async getNameLevelByUuid(uuid) { + let response = {}; + try { + response = await axios.get(`${lolaccount}/lol/summoner/v4/summoners/by-puuid/${encodeURIComponent(uuid)}`, header); + } catch(e) { + console.log(e); + } + return {name: response.data.name, level: response.data.summonerLevel}; + } +} + diff --git a/b3-dev/api/api-td-node/team-lol/index.js b/b3-dev/api/api-td-node/team-lol/index.js new file mode 100644 index 0000000..53c13e9 --- /dev/null +++ b/b3-dev/api/api-td-node/team-lol/index.js @@ -0,0 +1,23 @@ +import express from 'express'; +import Manager from './Manager.js'; + +const app = express(); +const appManager = new Manager(); + +app.get('/api/lastmatch', async function(req, res) { + if (!req.query.name) { + return res.status(400).send(); + } + + try { + const results = await appManager.getInfo(req.query.name); + res.send(results); + } catch (e) { + console.error(e); + return res.status(503).send(); + } +}); + +app.listen('80', async function() { + console.log(`server listening on 80`); +}); diff --git a/b3-dev/api/api-td-node/team-lol/package.json b/b3-dev/api/api-td-node/team-lol/package.json new file mode 100644 index 0000000..abf9086 --- /dev/null +++ b/b3-dev/api/api-td-node/team-lol/package.json @@ -0,0 +1,17 @@ +{ + "name": "node-cours", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "axios": "^0.24.0", + "dotenv": "^10.0.0", + "express": "^4.17.1" + }, + "type": "module" +}