mytest.js 18.5 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
module.exports = function(app){

    var request = require("request");
    var urlenconde = require('urlencode');
    var apikey = "RGAPI-b69c36b5-e5c6-4698-af93-4be17fc8a7e4"//api
    
    var profileIconId;  //아이콘 번호
    var revisionDate; //수정날짜
    var id; //소환사ID
    var accountId; //계정Id
    var name; //소환사 이름
    var summonerLevel;  //소환사
    var rotation_champ = new Array();
    var rotation_champ_newbie = new Array();
    
      app.get('/', function(req, res) {
            res.render('main', { title: 'LOL INFO' });
      });
    
      app.get('/search/:username/', function(req, res, next){
        //롤 api url
        name = req.params.username;
        var nameUrl = "https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/" + urlenconde(name)+"?api_key="+ apikey;
        request(nameUrl,function(error,response,body){
    
            // 요청에 대한 응답이 성공적으로 왔는지 검사.
            // status code가 200이 아니면 오류가 있었던 것으로 간주하고 함수 종료.
            console.log('response code ', response.statusCode);
            if (response.statusCode != 200) {
                console.log('Error with response code22 ', response.statusCode);
                res.end();
                return;
            }
    
          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"];
          console.log(info_summoner_json);
    
          var champUrl = "https://kr.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-summoner/" + urlenconde(id) + "?api_key=" + apikey;
          request(champUrl,function(error,response,body){
            var info_champ_json = JSON.parse(body);
            var champ_point = new Array();
            var champ_id = new Array();
            var champ_name = new Array();
            var rotation_name = new Array();
            var rotation_name_newbie = new Array();
            var champ_pic = new Array();
            var rotation_pic =new Array();
            var rotation_pic_newbie = new Array();
            var champions_length = Object.keys(info_champ_json).length;
    
            // status code가 200이 아니면 종료.
            if (info_champ_json["status"] != undefined) {
                if (info_champ_json["status"]["status_code"] != 200) {
                    console.log('Error with response code11 ', info_champ_json["status"]["status_code"]);
                    res.end();
                    return;
                }
            }
    
            for(var i=0; i < champions_length; i++){
              champ_point[i] = (info_champ_json[i]["championPoints"]);
              champ_id[i] = info_champ_json[i]["championId"];
    
            }
            var rotationUrl = "https://kr.api.riotgames.com/lol/platform/v3/champion-rotations?api_key="+apikey;
            request(rotationUrl,function(error,response,body){
              var info_rotation = JSON.parse(body);
              var keys = Object.keys(info_rotation);
              for(var k =0; k < info_rotation[keys[0]].length;k++)
              {
                rotation_champ[k] = info_rotation[keys[0]][k];
                console.log("rotation_champ:"+rotation_champ[k]);
                console.log("로테길이:"+rotation_champ.length);
              }
              for(var k =0; k < info_rotation[keys[1]].length;k++)
              {
                rotation_champ_newbie[k] = info_rotation[keys[1]][k];
                console.log("rotation_champ:"+rotation_champ_newbie[k]);
                console.log("로테길이:"+rotation_champ_newbie.length);
              }
    
    
    
    
    
            var staticUrl = "http://ddragon.leagueoflegends.com/cdn/10.11.1/data/en_US/champion.json";
            request(staticUrl,function(error,response,body) {
              var info_static_champ_json = JSON.parse(body);
              console.log(info_static_champ_json.data);
              var champion = info_static_champ_json["data"];
              var champ_count = 0;

              for(var name in champion) {
                if(champion.hasOwnProperty(name)) {
                  champ_count++;
                }
              }

              for(var i=0; i < champ_count; i++){
                    for(js in champion) {
                      for(j in champion[js]) {
                        if(champion[js]["key"] == champ_id[i]){
                          champ_name[i] = champion[js]["id"];
                          champ_pic[i] = "http://ddragon.leagueoflegends.com/cdn/10.11.1/img/champion/"+champ_name[i]+".png";
    
                        }
                      }
                    }
              }
              for(var i=0; i < champ_count; i++){
                    for(js in champion){
                      for(j in champion[js]){
                        if(champion[js]["key"] == rotation_champ[i]){
                          rotation_name[i] = champion[js]["id"];
                          rotation_pic[i] = "http://ddragon.leagueoflegends.com/cdn/10.11.1/img/champion/"+rotation_name[i]+".png";
                        }
                        if(champion[js]["key"] == rotation_champ_newbie[i]){
                          rotation_name_newbie[i] = champion[js]["id"]
                          rotation_pic_newbie[i] = "http://ddragon.leagueoflegends.com/cdn/10.11.1/img/champion/"+rotation_name_newbie[i]+".png";
                        }
                      }
                    }
              }
    
              var userLeagueUrl = "https://kr.api.riotgames.com/lol/league/v4/entries/by-summoner/"+ urlenconde(id)+"?api_key=" + apikey;
              request(userLeagueUrl,function(error,response,body){
                var info_user_league_json = JSON.parse(body);
                //console.log("userLeagueUrl:", userLeagueUrl);
                if(info_user_league_json[0] != null){
                var leagueId = info_user_league_json[0]["leagueId"];
                var wins = info_user_league_json[0]["wins"];
                var losses = info_user_league_json[0]["losses"];
                var leagueName = info_user_league_json[0]["leagueName"]
                var tier = info_user_league_json[0]["tier"];
                var rank = info_user_league_json[0]["rank"];
                var leaguePoints = info_user_league_json[0]["leaguePoints"];
                var img_tier;
                if(tier == "MASTER"){
                  img_tier = "https://i.imgur.com/nvQjonh.png";
                }else if(tier == "CHALLENGER"){
                  img_tier = "https://i.imgur.com/sbK1Edj.png";
                }else if(tier == "DIAMOND"){
                  img_tier = "https://i.imgur.com/5VBu8PF.png"
                }else if(tier == "PLATINUM"){
                  img_tier = "https://i.imgur.com/Eqi6858.png"
                }else if(tier == "GRANDMASTER"){
                  img_tier = "https://i.imgur.com/mcEhz1o.png"
                }else if(tier == "GOLD"){
                  img_tier = "https://i.imgur.com/Ec4hPuO.png"
                }else if(tier == "SILVER"){
                  img_tier = "https://i.imgur.com/GKnPu7s.png"
                }else if(tier == "BRONZE"){
                  img_tier ="https://i.imgur.com/TPZVXIr.png"
                }else{
                  img_tier = "https://i.imgur.com/kcdoC4r.png"
                }
            }
              champ_name[champ_name.length] = "total";
              var temp_id;
              var temp_name;
              var temp_point;
              var temp_pic;
              for(var i=0; i < champ_id.length-1; i++){
                for(var j=i+1;j <champ_id.length-1; j++)
                if(champ_point[i] > champ_point[j]){
                  temp_id = champ_id[i];
                  temp_name = champ_name[i];
                  temp_point = champ_point[i];
                  temp_pic = champ_pic[i];
                  champ_id[i] = champ_id[j]
                  champ_name[i] = champ_name[j];
                  champ_point[i] = champ_point[j];
                  champ_pic[i] = champ_pic[j];
                  champ_id[j] = temp_id
                  champ_name[j] = temp_name;
                  champ_point[j] = temp_point;
                  champ_pic[j] = temp_pic;
                }
              }
             const defaultMMR = [
               {"mmr" : 900, "tier" : "iron", "rank": 4},
               {"mmr" : 950, "tier" : "iron", "rank": 3},
               {"mmr" : 1000, "tier" : "iron", "rank": 2},
               {"mmr" : 1050, "tier" : "iron", "rank": 1},
               { "mmr" : 1100, "tier" : "bronze", "rank": 4 },
               { "mmr" : 1150, "tier" : "bronze", "rank": 3 },
               { "mmr" : 1200, "tier" : "bronze", "rank": 2 },
               { "mmr" : 1250, "tier" : "bronze", "rank": 1 },
               { "mmr" : 1300, "tier" : "silver", "rank": 4 },
               { "mmr" : 1350, "tier" : "silver", "rank": 3 },
               { "mmr" : 1400, "tier" : "silver", "rank": 2 },
               { "mmr" : 1450, "tier" : "silver", "rank": 1 },
               { "mmr" : 1500, "tier" : "gold", "rank": 4 },
               { "mmr" : 1550, "tier" : "gold", "rank": 3 },
               { "mmr" : 1600, "tier" : "gold", "rank": 2 },
               { "mmr" : 1650, "tier" : "gold", "rank": 1 },
               { "mmr" : 1700, "tier" : "platinum", "rank": 4 },
               { "mmr" : 1750, "tier" : "platinum", "rank": 3 },
               { "mmr" : 1800, "tier" : "platinum", "rank": 2 },
               { "mmr" : 1850, "tier" : "platinum", "rank": 1 },
               { "mmr" : 1900, "tier" : "diamond", "rank": 4 },
               { "mmr" : 1950, "tier" : "diamond", "rank": 3 },
               { "mmr" : 2000, "tier" : "diamond", "rank": 2 },
               { "mmr" : 2050, "tier" : "diamond", "rank": 1 },
               { "mmr" : 2100, "tier" : "master", "rank": 1 },
               { "mmr" : 2700, "tier" : "challenger", "rank": 1 },
             ]

            var spellUrl = "http://ddragon.leagueoflegends.com/cdn/10.12.1/data/en_US/summoner.json";
            request(spellUrl,function(error,response,body){
              var spell = (JSON.parse(body)).data;
              console.log(spell);


            var spectatorUrl = "https://kr.api.riotgames.com/lol/spectator/v4/active-games/by-summoner/" + urlenconde(id) + "?api_key=" + apikey;
            request(spectatorUrl,function(error,response,body) {
              var flag = true;

              if(response.statusCode != 200)
                flag = false;

              if(flag) {
                var spectator_json = JSON.parse(body);
                console.log(spectator_json);
                var spec_bannedChamp = spectator_json.bannedChampions;
                var spec_gameId = spectator_json.gameId;
                var spec_gameLength = spectator_json.gameLength;
                var spec_gameMode = spectator_json.gameMode;
                var spec_QueueId = spectator_json.gameQueueConfigId;
                var spec_mapId = spectator_json.mapId;
                var spec_participants = spectator_json.participants;

                var spec_bannedChamp_image = new Array();
                var spec_selectedChamp_image = new Array();
                var spec_summonerName = new Array();
                var spec_spellId = new Array();
                var spec_spell_image = new Array();
                for(var i=0; i<10; i++){
                  spec_summonerName.push(spec_participants[i].summonerName);
                  spec_spellId.push(spec_participants[i].spell1Id);
                  spec_spellId.push(spec_participants[i].spell2Id);
                }

                for(var i=0; i<10; i++) {
                  for(key in champion) {
                    if(champion.hasOwnProperty(key) && champion[key].key == spec_bannedChamp[i].championId) {
                      spec_bannedChamp_image.push("http://ddragon.leagueoflegends.com/cdn/10.11.1/img/champion/"+champion[key].id+".png");
                    }
                    if(champion.hasOwnProperty(key) && champion[key].key == spec_participants[i].championId) {
                      spec_selectedChamp_image.push("http://ddragon.leagueoflegends.com/cdn/10.11.1/img/champion/"+champion[key].id+".png");
                    }
                  }
                }

                for(var i=0; i<20; i++) {
                  for(key in spell){
                    if(spell.hasOwnProperty(key) && spell[key].key == spec_spellId[i]){
                      spec_spell_image.push("http://ddragon.leagueoflegends.com/cdn/10.12.1/img/spell/"+spell[key].id+".png");
                    }
                  }
                }
              }
                
              var matchUrl = "https://kr.api.riotgames.com/lol/match/v4/matchlists/by-account/" + accountId + "?api_key=" + apikey;
              request(matchUrl,function(error,response,body) {
                var match_json = JSON.parse(body).matches;
                console.log(match_json);

                var match_gameId = new Array();
                var match_mychamp = new Array();

                for(var i=0; i<5; i++){
                  match_gameId.push(match_json[i].gameId);
                  match_mychamp.push(match_json[i].champion);
                }

                // teamId: 100 = Blue , teamId:200 = Red
                var match1Url = "https://kr.api.riotgames.com/lol/match/v4/matches/" + match_gameId[0] + "?api_key=" + apikey;
                request(match1Url,function(error,response,body){
                  var match1_json = JSON.parse(body);
                  var match1_gameLength = match_json.gameDuration;
                  var match1_teams = match1_json.teams;
                  var match1_participants = match1_json.participants;
                  var match1_participantIdentities = match1_json.participantIdentities;

                  var match1_bannedChamp = new Array();
                  var match1_selectedChamp = new Array();
                  var match1_spellId = new Array();
                  var match1_summonerName = new Array();
                  var match1_kda = new Array();

                  var match1_bannedChamp_image = new Array();
                  var match1_selectedChamp_image = new Array();
                  var match1_spell_image = new Array();

                  for(var i=0; i<2; i++){
                    var temp = match1_teams[i].bans;
                    for(var j=0; j<5; j++){
                      match1_bannedChamp.push(temp[j].championId);
                    }
                  }

                  for(var i=0; i<10; i++){
                    match1_selectedChamp.push(match1_participants[i].championId);
                    match1_spellId.push(match1_participants[i].spell1Id);
                    match1_spellId.push(match1_participants[i].spell2Id);
                    match1_summonerName.push((match1_participantIdentities[i].player).summonerName);
                    var obj = {
                      'k' : (match1_participants[i].stats).kills,
                      'd' : (match1_participants[i].stats).deaths,
                      'a' : (match1_participants[i].stats).assists
                    };
                    match1_kda.push(obj);
                  }

                  for(var i=0; i<10; i++) {
                    for(key in champion) {
                      if(champion.hasOwnProperty(key) && champion[key].key == match1_bannedChamp[i]) {
                        match1_bannedChamp_image.push("http://ddragon.leagueoflegends.com/cdn/10.11.1/img/champion/"+champion[key].id+".png");
                      }
                      if(champion.hasOwnProperty(key) && champion[key].key == match1_selectedChamp[i]) {
                        match1_selectedChamp_image.push("http://ddragon.leagueoflegends.com/cdn/10.11.1/img/champion/"+champion[key].id+".png");
                      }
                    }
                  }
  
                  for(var i=0; i<20; i++) {
                    for(key in spell){
                      if(spell.hasOwnProperty(key) && spell[key].key == match1_spellId[i]){
                        match1_spell_image.push("http://ddragon.leagueoflegends.com/cdn/10.12.1/img/spell/"+spell[key].id+".png");
                      }
                    }
                  }
                  
                  var match1 = {
                    "summonerName" : match1_summonerName,
                    "selectedChamp_image" : match1_selectedChamp_image,
                    "bannedChamp_image" : match1_bannedChamp_image,
                    "spell_image" : match1_spell_image,
                    "kda" : match1_kda
                  };

                  console.log(match1);

                var match2Url = "https://kr.api.riotgames.com/lol/match/v4/matches/" + match_gameId[1] + "?api_key=" + apikey;
                request(match2Url,function(error,response,body){
                  var match2_json=JSON.parse(body);







                var match3Url = "https://kr.api.riotgames.com/lol/match/v4/matches/" + match_gameId[2] + "?api_key=" + apikey;
                request(match3Url,function(error,response,body){
                  var match3_json=JSON.parse(body);






                var match4Url = "https://kr.api.riotgames.com/lol/match/v4/matches/" + match_gameId[3] + "?api_key=" + apikey;
                request(match4Url,function(error,response,body){
                  var match4_json=JSON.parse(body);







                var match5Url = "https://kr.api.riotgames.com/lol/match/v4/matches/" + match_gameId[4] + "?api_key=" + apikey;
                request(match5Url,function(error,response,body){
                  var match5_json=JSON.parse(body);







                    



              
                 





                  res.render('index', { title: req.params.username ,
                  c_id: champ_id,
                  c_name: champ_name,
                  c_point: champ_point,
                  c_pic: champ_pic,
                  c_rotation : rotation_pic,
                  c_rotation_newbie : rotation_pic_newbie,
                  c_summoner: summoner,
                  c_wins: wins,
                  c_losses: losses,
                  c_tier: tier,
                  c_imgtier: img_tier,
                  c_rank: rank,
                  c_leaguePoint: leaguePoints
                  });

                });
                });
                });
                });
                });
              });
             });
            });
            });
          });
        });
      });
        });
      });
    };