index.js 15.7 KB
module.exports = function (app) {
    var express = require('express');
    var request = require("request");
    var urlencode = require('urlencode');
    var apikey = "RGAPI-3aeb21da-5555-4fa8-906a-2497942cd52e"; //매일 API 바꿔주기!!

    var id; //소환사ID
    var accountId; //계정ID
    var puuid; //특정ID
    var name; //소환사이름
    var profileIconId; //소환사아이콘ID
    var summonerLevel; //소환사 레벨
    var revisionDate; //수정날짜
    var winning_rate; //승률


    var leagueId = 0; //리그ID
    var tier = ""; //티어
    var rank = "Unranked"; //랭크
    var wins = 0; //이긴 수
    var losses = 0; //진 수
    var leaguePoints = 0; //티어 포인트


    var temp_t = new Array();
    var temp_traits = new Array();
    var temp_traits_num = new Array();
    var temp_traits_png = new Array();
    var temp_units = new Array();
    var temp_units_tier = new Array();
    var temp_units_items = new Array();
    var temp_units_png = new Array();
    var temp_all = new Array();
    var temp_all_traits = new Array();
    var temp_all_units = new Array();
    var temp_all_units_items = new Array();

    var img_tier;

    var participants; //참가자
    var placement; //참가자 순위
    var time_eliminated; //참가자 참여시간
    var traits = new Array(); //시너지
    var traits_num = new Array(); //시너지갯수
    var traits_png = new Array(); //시너지 PNG                  
    var units = new Array(); //챔피언
    var units_tier = new Array(); //챔피언 레벨
    var units_items = new Array(); //챔피언 아이템
    var units_png = new Array(); //챔피언 PNG

    app.get('/', function (req, res) {
        res.render('main', { title: 'Chess' });
    });

    app.post('/', function(req,res){
        console.log('post');
        name =req.body.name;
        res.redirect('/search');
    })
    app.get('/search', function (req, res) {

        name = req.query.summoner;
        console.log('get');
        if (res.statusCode == 200) {
            console.log(name);

            var summonerUrl = "https://kr.api.riotgames.com/tft/summoner/v1/summoners/by-name/" + urlencode(name) + '?api_key=' + apikey;
            request(summonerUrl, function (err1, res1, body1) {
                //console.log(res.statusCode);
                if (res.statusCode == 200) {


                    var summoner_json = JSON.parse(body1);

                    id = summoner_json['id'];
                    //console.log(id);
                    accountId = summoner_json['accountId'];
                    puuid = summoner_json['puuid'];
                    profileIconId = summoner_json['profileIconId'];
                    summonerLevel = summoner_json['summonerLevel'];
                    revisionDate = summoner_json['revisionDate'];

                    profileIconId = "http://ddragon.leagueoflegends.com/cdn/10.11.1/img/profileicon/" + urlencode(profileIconId) + '.png'

                    var leagueUrl = "https://kr.api.riotgames.com/tft/league/v1/entries/by-summoner/" + urlencode(id) + '?api_key=' + apikey;
                    request(leagueUrl, function (err2, res2, body2) {
                        //console.log(res.statusCode);
                        // if (res.statusCode != 200) {
                        //     console.log("Error ", res.statusCode);
                        //     res.setEncoding();
                        //     return;
                        // }
                        var league_json = JSON.parse(body2);

                        if (league_json[0] != null) {
                            leagueId = league_json[0]['leagueId'];
                            tier = league_json[0]['tier'];
                            rank = league_json[0]['rank'];
                            wins = league_json[0]['wins'];
                            losses = league_json[0]['losses'];
                            leaguePoints = league_json[0]['leaguePoints'];

                            winning_rate = Math.round((wins / (wins + losses)) * 100);
                            if (tier == "CHALLENGER") {
                                img_tier = 'images/Emblem/Challenger_Emblem.png';
                            } else if (tier == "GRANDMASTER") {
                                img_tier = 'images/Emblem/Grandmaster_Emblem.png';
                            } else if (tier == "MASTER") {
                                img_tier = 'images/Emblem/Master_Emblem.png';
                            } else if (tier == "DIAMOND") {
                                img_tier = 'images/Emblem/Diamond_Emblem.png';
                            } else if (tier == "PLATINUM") {
                                img_tier = 'images/Emblem/Platinum_Emblem.png';
                            } else if (tier == "GLOD") {
                                img_tier = 'images/Emblem/Glod_Emblem.png';
                            } else if (tier == "SILVER") {
                                img_tier = 'images/Emblem/Silver_Emblem.png';
                            } else if (tier == "BRONZE") {
                                img_tier = 'images/Emblem/Bronze_Emblem.png';
                            } else if (tier == "IRON") {
                                img_tier = 'images/Emblem/Iron_Emblem.png';
                            } else {
                                img_tier = 'images/Emblem/Unranked.png';
                            }
                        }

                        //console.log(img_tier);

                        var matchUrl = "https://asia.api.riotgames.com/tft/match/v1/matches/by-puuid/" + urlencode(puuid) + "/ids?count=10&api_key=" + apikey;
                        request(matchUrl, function (err3, res3, body3) {
                            //console.log(res.statusCode);
                            if (res3.statusCode == 200) {

                                var match_json = String(body3).replace('[', '').replace(/"/gi, '').replace(']', '').split(',');;
                                //console.log(match_json);

                                var match_info_Url = "https://asia.api.riotgames.com/tft/match/v1/matches/" + urlencode(match_json[0]) + "?api_key=" + apikey;
                                request(match_info_Url, function (err4, res4, body4) {
                                    //console.log(res.statusCode);
                                    if (res.statusCode == 200) {
                                        //     console.log("Error ", res.statusCode);
                                        //     res.setEncoding();
                                        //     return;
                                        // }
                                        var match_info_json = JSON.parse(body4);

                                        for (var i = 0; i < match_info_json['metadata']['participants'].length; i++) {
                                            if (match_info_json['info']['participants'][i]['puuid'] == puuid) {
                                                //participants = match_info_json['metadata']['participants'][i];
                                                placement = match_info_json['info']['participants'][i]['placement'];

                                                //time_eliminated = Math.round(match_info_json['info']['participants'][i]['time_eliminated']);
                                            }
                                        }
                                        for (var i = 0; i < match_info_json['metadata']['participants'].length; i++) {
                                            if (match_info_json['info']['participants'][i]['puuid'] == puuid) {
                                                var traits_array = match_info_json['info']['participants'][i]['traits'];
                                                var count = 0;
                                                for (var j = 0; j < traits_array.length; j++) {

                                                    if (traits_array[j]['style'] != 0) {
                                                        temp_t[count] = traits_array[j]; //trait_array에는 잘 들어감
                                                        count++;
                                                    }
                                                }

                                                //console.log(temp_t); 

                                                for (var j = 0; j < temp_t.length; j++) {
                                                    temp_traits.push(temp_t[j]['name']);
                                                    temp_traits_num.push(temp_t[j]['num_units']);
                                                    traits_png[j] = ("/images/traits/" + urlencode(temp_t[j]['name']) + ".png");
                                                }

                                                traits.push(temp_traits);
                                                traits_num.push(temp_traits_num)
                                                //traits_png.push(temp_traits_png);

                                                var units_array = match_info_json['info']['participants'][i]['units'];

                                                for (var j = 0; j < units_array.length; j++) {
                                                    temp_units.push(match_info_json['info']['participants'][i]['units'][j]['character_id']);
                                                    temp_units_tier.push(match_info_json['info']['participants'][i]['units'][j]['tier']);
                                                    temp_units_items.push(String(match_info_json['info']['participants'][i]['units'][j]['items']).replace('[', '').replace(/"/gi, '').replace(']', '').split(','));
                                                    units_png[j] = ("/images/champions/" + urlencode(match_info_json['info']['participants'][i]['units'][j]['character_id']) + ".png");
                                                }

                                                units.push(temp_units);
                                                units_tier.push(temp_units_tier);
                                                units_items.push(temp_units_items);
                                                //units_png.push(temp_units_png);
                                            }

                                            // console.log(participants);
                                            // console.log('--------------------------------------');
                                            // console.log(placement);
                                            // console.log('--------------------------------------');
                                            // console.log(time_eliminated);
                                            // console.log('--------------------------------------');
                                            // console.log(traits);
                                            // console.log('--------------------------------------');
                                            // console.log(traits_num);
                                            // console.log('--------------------------------------');
                                            // console.log(traits_png);
                                            // console.log('--------------------------------------');
                                            // console.log(units);
                                            // console.log('--------------------------------------');
                                            // console.log(units_tier);
                                            // console.log('--------------------------------------');
                                            // console.log(units_items);
                                            // console.log('--------------------------------------');
                                            // console.log(units_png);


                                            //temp_all.push(participants);
                                            //temp_all.push(placement);
                                            //temp_all.push(time_eliminated);
                                            // temp_all_traits.push(traits);
                                            // temp_all_traits.push(traits_num);
                                            temp_all_traits.push(traits_png);
                                            // temp_all_units.push(units);
                                            // temp_all_units.push(units_tier);
                                            // temp_all_units_items.push(units_items);
                                            temp_all_units.push(units_png);
                                        }




                                        // console.log(id);
                                        // console.log(accountId);
                                        // console.log(puuid);
                                        // console.log(name);
                                        // console.log(profileIconId);
                                        // console.log(summonerLevel);
                                        // console.log(revisionDate);
                                        // console.log(winning_rate);
                                        // console.log(leagueId);
                                        // console.log(tier);
                                        // console.log(img_tier);
                                        // console.log(rank);
                                        // console.log(wins);
                                        // console.log(losses);
                                        // console.log(leaguePoints);
                                        //console.log(placement);
                                        //console.log(traits_png);
                                        // console.log(temp_all_units);
                                        //console.log(units_png);
                                        res.render('index', {
                                            title: req.params.username,
                                            c_id: id,
                                            c_accountId: accountId,
                                            c_puuid: puuid,
                                            c_name: name,
                                            c_profileIconId: profileIconId,
                                            c_summonerLevel: summonerLevel,
                                            c_revisionDate: revisionDate,
                                            c_winning_rate: winning_rate,
                                            c_leagueId: leagueId,
                                            c_tier: tier,
                                            c_imgtier: img_tier,
                                            c_rank: rank,
                                            c_wins: wins,
                                            c_losses: losses,
                                            c_leaguePoints: leaguePoints,
                                            c_temp_all: placement,
                                            c_temp_all_traits: traits_png,
                                            c_temp_all_units: units_png
                                        });
                                    }
                                });

                            }

                            // console.log(participants);
                            // console.log(placement);
                            // console.log(time_eliminated);
                            // console.log(traits_png);
                            // console.log(units_png);

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