getResultMine.js
3.2 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
//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)
})
}