main.js
2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module.exports = function (app) {
var request = require("request");
var urlenconde = require('urlencode');
var apikey = "RGAPI-899abd44-2f7c-4e2d-981e-7cad9e50d1cb"//api
app.get('/', function (req, res) {
res.render('main', { title: 'R U TROLL?' });
});
app.get('/search/:username/', function (req, res) {
// tft api url
const summonerName = req.params.username;
var nameUrl = `https://kr.api.riotgames.com/tft/summoner/v1/summoners/by-name/${summonerName}?api_key=${apikey}`
request(nameUrl, function (error, response, body) {
var info_summoner_json = JSON.parse(body);
console.log('summoner', body);
if (info_summoner_json.status?.status_code) {
return res.render('index', {
title: req.params.username,
error_message: info_summoner_json.status?.message
});
}
const summonerId = info_summoner_json["id"];
const summoner = info_summoner_json["name"];
const leagueUrl = `https://kr.api.riotgames.com/tft/league/v1/entries/by-summoner/${summonerId}?api_key=${apikey}`;
request(leagueUrl, function (error, response, body) {
const info_league_json = JSON.parse(body);
const matchIdUrl = `https://asia.api.riotgames.com/tft/match/v1/matches/by-puuid/${info_summoner_json.puuid}/ids?api_key=${apikey}`;
request(matchIdUrl, function (error, response, body) {
const matchIdList = JSON.parse(body);
console.log('matchIds', matchIdList);
const promiseList = [];
if (matchIdList.length > 0) {
matchIdList.forEach(matchId => {
promiseList.push(new Promise((resolve, reject) => {
const matchURL = `https://asia.api.riotgames.com/tft/match/v1/matches/${matchId}?api_key=${apikey}`;
request(matchURL, function (error, response, body) {
const match = JSON.parse(body);
resolve(match);
});
}));
});
Promise.all(promiseList).then(matches => {
console.log('matches', matches);
res.render('index', {
title: req.params.username,
error_message: false,
c_matchList: matches, // 게임 데이터, array
c_summoner: info_summoner_json, // 소환사, object
c_leagueList: info_league_json // leagues 데이터, array
});
})
} else {
res.render('index', {
title: req.params.username,
error_message: false,
c_matchList: [],
c_summoner: info_summoner_json,
c_leagueList: info_league_json
});
}
});
console.log('league', body);
});
});
});
};