getdeckname.js 5.32 KB
const puppeteer=require('puppeteer')
const cheerio=require('cheerio')
const fs=require('fs')
const ejs=require('ejs')

exports.getDeckname=(req,res)=>{
    const deckname=req.session.deckname
    const name1 = req.body.value1
    const name2 = req.body.value2
    const name3 = req.body.value3
    const name4 = req.body.value4
    const names=[name1, name2, name3, name4]
    req.session.mulName = names
    let mulRates
    let WinRate=req.session.winRate
    const info={'비밀 사냥꾼':'Secret Hunter', '기계 사냥꾼':'Mech Hunter', '멀록 주술사':'Murloc Shaman', '토큰 드루이드':'Token Druid', '컨트롤 전사':'Control Warrior',
'폭탄 전사':'Bomb Warrior', '기계 성기사':'Mech Paladin',  '위니 흑마':'Zoo Warlock', '미드레인지 사냥꾼':'Midrange Hunter', '용 마법사':'Dragon Mage',
 '죽음의 메아리 사냥꾼':'Deathrattle Hunter', 'Cyclone Mage':'Cyclone Mage', '빅 주술사':'Big Shaman', '부활 사제':'Resurrect Priest','컨트롤 주술사':'Control Shaman',
  '빅 성기사':'Big Paladin', '신의 격노 성기사':'Holy Wrath Paladin', '템포 도적':'Tempo Rogue', '빙결 마법사':'Freeze Mage', '주문 마법사':'Spell Mage',
   '회복 드루이드':'Heal Druid', '침묵 사제':'Silence Priest', '노미 사제':'Nomi Priest', '거인 흑마법사':'Handlock', '첩보 도적':'Thief Rogue'}
    const GetMulContent=()=>{
        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`,{waitUntil: 'networkidle2'})
                    const content=await page.content()
                    browser.close()
                    return content
                }
                catch(err)
                {
                    console.log(err)
                    browser.close()
                }
            }
            resolve(asyncFunc())
        })
    }
    const GetDeckInfo=(content)=>{
        return new Promise((resolve,reject)=>{
            const $=cheerio.load(content)
            let i = 0
            let deckNames=$('.archetype-info')
            for(; i < deckNames.length; i++){
                if($(deckNames[i].children[0]).text() == info[deckname]){
                    WinRate = $(deckNames[i].children[1]).text()
                    req.session.winRate = WinRate
                    break
                }
            }
            let deckaddress=$($('a.btn')[i+1]).attr('href')
            resolve(deckaddress)
        })
    }
    const GetMulRate=(deckaddress)=>{
        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` + deckaddress,{waitUntil: 'networkidle2'})
                    const content=await page.content()
                    browser.close()
                    return content
                }
                catch(err)
                {
                    console.log(err)
                    browser.close()
                }
            }
            resolve(asyncFunc())
        })
    }
    const GetMulInfo=(content)=>{
        return new Promise((resolve,reject)=>{
            const $=cheerio.load(content)
            let MulNames=$('span.card-name')
            let MulRates=$('#mulligan-guide > div > div:nth-child(1) > div:nth-child(4) > div > div > div')
            mulRates=['', '' ,'' ,'']
            let USname = []
            let KRname = []
            USname = JSON.parse(fs.readFileSync('./cardsenUS.json'))
            KRname = JSON.parse(fs.readFileSync('./cardskoKR.json'))
            for(let i = 0  ;i < MulNames.length; i++){
                let j =0
                for(; j < USname.length; j++){
                    if(USname[j].name == $(MulNames[i].children[0]).text())
                        break
                }
                if(j != USname.length){
                    if(name1 == KRname[j].name) mulRates[0] = $(MulRates[6*i]).text().substring(1,6)
                    else if(name2 == KRname[j].name) mulRates[1] = $(MulRates[6*i]).text().substring(1,6)
                    else if(name3 == KRname[j].name) mulRates[2] = $(MulRates[6*i]).text().substring(1,6)
                    else if(name4 == KRname[j].name) mulRates[3] = $(MulRates[6*i]).text().substring(1,6)   
                }
            }
            req.session.mulwinRate = mulRates
            resolve(mulRates)
        })
    }
    GetMulContent()
        .then(GetDeckInfo)
        .then(GetMulRate)
        .then(GetMulInfo)
        .then(()=>{
            fs.readFile('./views/ejs/prob_result.ejs','utf-8',(err,data)=>{
                res.writeHead(200,{'Content-Type':'text/html'})
                res.end(ejs.render(data,{
                    deckName : deckname || [],
                    winRate:WinRate || [],
                    mulNames:names || ["", "", "", ""],
                    mulwinRate:mulRates || [0, 0, 0, 0]
                }))
            })
        })
        .catch((err)=>{
            console.log(err)
        })
}