main.js 2.44 KB
module.exports = function(app) {

	var request = require("request");
	var urlenconde = require('urlencode');
	var apikey = "RGAPI-8547dfd4-a79e-4392-b60d-f89b0fd1347c" //api key

	var profileIconId; //프로필 아이콘
	var revisionDate; //수정 날짜
	var id; //소환사 id
	var accountId; //계정 id
	var name; //소환사 이름
    var summonerLevel; //소환사 레벨
  
	app.get('/', function(req, res) {
		res.render('main', { title: 'LOL Helper' });
	});

	app.get('/search/:username/', function(req, res) {
		//리그오브레전드 api url
		name = req.params.username;
		var nameUrl = "https://kr.api.riotgames.com/lol/summoner/v3/summoners/by-name/" + urlenconde(name) + "?api_key=" + apikey;
		request(nameUrl, function(error, response, body) {
			var info_summoner_json = JSON.parse(body);

			accountId = info_summoner_json["accountId"];
			id = info_summoner_json["id"];
			summoner = info_summoner_json["name"];
			profileIconId = info_summoner_json["profileIconId"];
			summonerLevel = info_summoner_json["summonerLevel"];
			revisionDate = info_summoner_json["revisionDate"];

			var champUrl = "https://kr.api.pvp.net/api/lol/kr/v1.3/stats/by-summoner/" + urlenconde(id) + "/ranked?api_key=" + apikey;
			request(champUrl, function(error, response, body) {
				var info_champ_json = JSON.parse(body);
				var champions = info_champ_json["champions"];
				var champ_id = new Array();
				var champ_name = new Array();
				var champ_pic = new Array();
				var champions_length = Object.keys(champions).length;

				for (var i = 0; i < champions_length; i++) {
					champ_id[i] = champions[i]["id"];
				}

				var staticUrl = "https://global.api.pvp.net/api/lol/static-data/kr/v1.2/champion/?api_key=" + apikey;
				request(staticUrl, function(error, response, body) {
					var info_static_champ_json = JSON.parse(body);
					var champion = info_static_champ_json["data"];
					for (var i = 0; i < champ_id.length; i++) {
						for (js in champion) {
							for (j in champion[js]) {
								if (champion[js]["id"] == champ_id[i]) {
									champ_name[i] = champion[js]["key"];
									champ_pic[i] = "https://opgg-static.akamaized.net/images/lol/champion/" + champ_name[i] + ".png?image=c_scale,w_46";
								}
							}
						}
					}
					champ_name[champ_name.length] = "total";

					res.render('index', { title: req.params.username,
					c_id : champ_id,
					c_name : champ_name,
					c_pic : champ_pic,
					c_summoner : summoner
					});
				});
			});
		});
	});
};