getResult.js 3.69 KB
const getCardId=require('./getCardId')
const puppeteer=require('puppeteer')
const request = require('request')
const cheerio=require('cheerio')
const fs=require('fs')
const ejs=require('ejs')
let globalMulligan
let globalDecks

//result.ejs 렌더링 전 수행됨. 현재 덱 ID와 상대 직업을 가지고 상대의 점유율 상위 3개 덱과, 내 덱의 추천 멀리건 출력
//return : 렌더링 된 result.ejs
exports.GetResult=(req,res)=>{
    //const deckId=req.session.deckId
    const opponentClass=req.session.opponentClass
    
    //상대 직업을 가지고 hsreplay.net에서 검색, html로 반환
    const GetOppContent=()=>{
        return new Promise((resolve,reject)=>{
            const asyncFunc=async ()=>{
                const browser=await puppeteer.launch()
                try{
                    const page=await browser.newPage()
                    await page.setViewport({width:1366,height:768})
                    await page.goto('https://hsreplay.net/meta/#tab=archetypes',{waitUntil: 'networkidle2'})
                    await page.click('#table1-column0')
                    const content=await page.content()
                    browser.close()
                    return content
                }
                catch(err)
                {
                    console.log(err)
                    browser.close()
                }
            }
            resolve(asyncFunc())
        })
    }
    //받아온 html로부터 덱의 이름, 게임 횟수 추출
    const GetDeckInfo=(content)=>{
        return new Promise((resolve,reject)=>{
            const $=cheerio.load(content)
            let index = 0
            if(opponentClass == "DRUID"){ index = 1 }
            else if(opponentClass == "HUNTER"){ index = 2 }
            else if(opponentClass == "MAGE"){ index = 3 }
            else if(opponentClass == "PALADIN"){ index = 4 }
            else if(opponentClass == "PRIEST"){ index = 5 }
            else if(opponentClass == "ROGUE"){ index = 6 }
            else if(opponentClass == "SHAMAN"){ index = 7 }
            else if(opponentClass == "WARLOCK"){ index = 8 }
            else if(opponentClass == "WARRIOR"){ index = 9 }
            let deckNames=$('#archetypes > div > div:nth-child(' + index + ') > div.box-content > div > div:nth-child(1) > div:nth-child(3) > div > div > a')
            let deckRates=$('#archetypes > div > div:nth-child(' + index + ') > div.box-content > div > div:nth-child(1) > div:nth-child(4) > div > div > a')
            let len = deckNames.length - 1
            let decks=[]
            for(let i=0; i < len; i++) {
                if(i == 3) break
                let deckName = $($('#table' + index + '-row' + i)[1]).text()
                let deckRate = $(deckRates[3 * i]).text()
                decks.push({deckTitle:deckName,deckWinRate:deckRate})
            }
            resolve(decks)
        })
    }
    GetOppContent()
        .then(GetDeckInfo)
        .then((decks)=>{
            return new Promise((resolve,reject)=>{
                globalDecks=decks
                resolve()
            })
        })
        .then(()=>{
            return new Promise((resolve,reject)=>{
                let result={}
                result.cards=globalMulligan
                result.decks=globalDecks
                resolve(result)
            })
        })
        .then((result)=>{
            fs.readFile('./views/ejs/deck_result.ejs','utf-8',(err,data)=>{
                res.writeHead(200,{'Content-Type':'text/html'})
                res.end(ejs.render(data,{
                    cards:globalMulligan || [],
                    decks:globalDecks || [],
                }))
            })
        })
        .catch((err)=>{
            console.log(err)
        })
}