getResultMine.js 3.2 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.GetResults=(req,res)=>{
    const mineClass = req.body.MineClass
    const name1 = req.body.value1
    const name2 = req.body.value2
    const name3 = req.body.value3
    const names = [name1, name2, name3]
    req.session.names = names
    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://www.heartharena.com/ko/tierlist`,{waitUntil: 'networkidle2'})
                    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 deckNames=$('li > dl')
            let decks=[0,0,0]
            for(let i=0; i < deckNames.length; i++){
                if($(deckNames[i]).attr('class') != 'empty'){
                    let temp = $(deckNames[i].children[0]).text().split('\n')
                    if(temp[0] == name1) decks[0] = $(deckNames[i].children[1]).text()
                    else if(temp[0] == name2) decks[1] = $(deckNames[i].children[1]).text()
                    else if(temp[0] == name3) decks[2] = $(deckNames[i].children[1]).text()
                }
            }
            req.session.decks = decks
            resolve(decks)
        })
    }
    GetOppContent()        
        .then(GetDeckInfo)
        .then((decks)=>{
            return new Promise((resolve,reject)=>{
                globalDecks=decks
                resolve()
            })
        })
        .then(()=>{
            return new Promise((resolve,reject)=>{
                let result={}
                result.decks=globalDecks
                resolve(result)
            })
        })
        .then((result)=>{
            fs.readFile('./views/ejs/score_result.ejs','utf-8',(err,data)=>{
                res.writeHead(200,{'Content-Type':'text/html'})
                res.end(ejs.render(data,{
                    decks:globalDecks || [],
                    MineClass:opponentClass || [],
                    Names:names || [],
                }))
            })
        })
        .catch((err)=>{
            console.log(err)
        })
}