서주원

implement addDeck.js,addCard.js, and match deckId,dbfId to eachcards

const mysql=require('../../database/mysql')
const fs=require('fs')
let cardEnUS=[]
let cardKoKR=[]
exports.AddCards=(deckId,cards)=>{
fs.readFile('cardskoKR.json',(err,data)=>{
if (err) throw err
cardKoKR=JSON.parse(data)
})
return new Promise((resolve,reject)=>{
fs.readFile('cardsenUS.json',(err,data)=>{
if (err) throw err
cardEnUS=JSON.parse(data)
mysql.getConnection((err,connection)=>{
if(err) throw err
for (let i=0;i<cards.length;i++) {
let cardCost = cards[i].cardCost
let cardName = cards[i].cardName
let cardNum = cards[i].cardNum
let cardId = 0
for (let i = 0; i < cardEnUS.length; i++) {
if (cardEnUS[i].name === cardName) {
console.log(cardEnUS[i].dbfId)
cardId = cardEnUS[i].dbfId
break
}
}
connection.query(`insert into card (deckId,cardId,cardCost,cardName,cardNum) values (\'${deckId}\',\'${cardId}\',\'${cardCost}\',\'${cardName}\',\'${cardNum}\')`, (err, results, fields) => {
//
if (err) throw err
})
}
connection.release()
resolve()
})
})
})
}
\ No newline at end of file
......@@ -2,12 +2,14 @@ const rp=require('request-promise')
const mysql=require('../../database/mysql')
const crawler=require('./crawler')
const cheerio=require('cheerio')
const addCards=require('../card/addCards')
const addDeck=require('../../database/deck/addDeck')
const getDeckId=require('../../database/deck/getDeckId')
let deckId=-1
exports.NewDeck=(req,res)=>{
const deckOwner=req.session.sid
const deckTitle=req.body.deckTitle
const deckClass='paladin'
const deckOwner=req.session.sid || 'test'
const deckTitle=req.body.deckTitle || new Date()
let deckCode=req.body.deckCode
let cards=[]
......@@ -15,7 +17,7 @@ exports.NewDeck=(req,res)=>{
const DataCheck=()=>{
return new Promise((resolve,reject)=>{
if(!deckTitle || !deckCode){
if(!deckCode){
return reject({
code: 'request_body_error',
message: 'request body is not defined'
......@@ -28,7 +30,6 @@ exports.NewDeck=(req,res)=>{
return crawler.Crawl(deckCode)
}
const DeckCrawl=(result)=>{
//console.log(result)
const $=cheerio.load(result)
let cardCosts=$('div.hs-tile-info').children('.hs-tile-info-left.mdc-list-item__start-detail')
let cardNames=$('div.hs-tile-info').find('span').find('span')
......@@ -44,23 +45,45 @@ exports.NewDeck=(req,res)=>{
cardNum=cardNum.trim()
cards.push({cardCost: cardCost, cardName: cardName, cardNum: cardNum})
}
console.log(cards)
//console.log(cards)
}
const AddDeck=()=>{
mysql.getConnection((err,connection)=>{
if (err) throw err
connection.query(`insert into deck (deckOwner,deckTitle,deckClass,deckCode) values (\'${deckOwner}\',\'${deckTitle}\',\'${deckClass}\',\'${deckCode}\');`,(err,results,fields)=>{
if (err) throw err
connection.release()
})
})
const asyncAddDeck=async ()=>{
try{
const results=await addDeck.AddDeck(deckOwner,deckTitle,deckCode)
}
catch (err) {
throw err
}
}
return asyncAddDeck()
}
const GetId=()=>{
const asyncGetDeckId=async ()=>{
try{
const results=await getDeckId.GetDeckId(deckOwner,deckTitle)
console.log(results)
return results
}
catch (err){
throw err
}
}
return asyncGetDeckId()
}
const AddCards=(deckId)=>{
console.log(`deckid in AddCards : ${deckId}`)
return addCards.AddCards(deckId,cards)
}
DataCheck()
.then(CrawlPage)
.then(DeckCrawl)
.then(AddDeck)
.then(GetId)
.then(AddCards)
.then(()=>{
res.status(200).json({message:'Complete Adding Deck'})
})
......
......@@ -7,6 +7,7 @@ const bcrypt=require('bcrypt-nodejs')
const rp=require('request-promise')
const morgan=require('morgan')
const cheerio=require('cheerio')
const mysqlApostrophe=require('mysql-apostrophe')
require('dotenv').config()
const app=express()
......@@ -27,6 +28,8 @@ app.use(session({
saveUninitialized:true
}))
app.use(mysqlApostrophe)
app.use('/api',require('./api'))
let allCards=[]
......@@ -77,24 +80,39 @@ app.post('/logout',(req,res)=>{
})
app.get('/decklist',(req,res)=>{
if(!req.session.sid)
res.redirect('/login')
else{
fs.readFile('./views/html/decklist.html',(err,data)=>{
res.writeHead(200,{'Content-Type':'text/html'})
res.end(data)
})
}
})
app.get('/ingame',(req,res)=>{
if(!req.session.sid)
res.redirect('/login')
else{
fs.readFile('./views/html/ingame.html',(err,data)=>{
res.writeHead(200,{'Content-Type':'text/html'})
res.end(data)
})
}
})
app.get('/newdeck',(req,res)=>{
if(!req.session.sid)
res.redirect('/login')
else{
fs.readFile('./views/html/newdeck.html',(err,data)=>{
res.writeHead(200,{'Content-Type':'text/html'})
res.end(data)
})
}
})
app.listen(process.env.SERVER_PORT || 3000,()=>{
......
const mysql=require('../mysql')
let deckId
exports.AddDeck=(deckOwner,deckTitle,deckCode)=>{
return new Promise((resolve,reject)=> {
mysql.getConnection((err, connection) => {
if (err) throw err
connection.query(`insert into deck (deckOwner,deckTitle,deckCode) values (\'${deckOwner}\',\'${deckTitle}\',\'${deckCode}\');`, (err, results, fields) => {
if (err) throw err
connection.release()
resolve(results)
})
})
})
}
\ No newline at end of file
const mysql=require('../mysql')
exports.GetDeckId=(deckOwner,deckTitle)=>{
return new Promise((resolve,reject)=>{
mysql.getConnection((err,connection)=>{
if (err) throw err
connection.query(`select * from deck where deckOwner=\'${deckOwner}\' and deckTitle=\'${deckTitle}\'`,(err,results,field)=>{
if (err) throw err
console.log('result in getDeckId'+results[0])
connection.release()
resolve(results[0].id)
})
})
})
}
\ No newline at end of file
......@@ -655,6 +655,11 @@
}
}
},
"mysql-apostrophe": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/mysql-apostrophe/-/mysql-apostrophe-1.0.8.tgz",
"integrity": "sha512-uZZJdCg8rQTNhKz9oudGZGIbO+BVxTytoWgM9xZtbUkDcDdGG9sfFNbLjC01j/oGnyRR56Oa/Xr0X6nV9rq3vw=="
},
"negotiator": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz",
......
......@@ -24,6 +24,7 @@
"iconv-lite": "^0.4.24",
"morgan": "^1.9.1",
"mysql": "^2.16.0",
"mysql-apostrophe": "^1.0.8",
"path": "^0.12.7",
"request": "^2.88.0",
"request-promise": "^4.2.2"
......