implement addDeck.js,addCard.js, and match deckId,dbfId to eachcards
Showing
7 changed files
with
137 additions
and
14 deletions
api/card/addCards.js
0 → 100644
1 | +const mysql=require('../../database/mysql') | ||
2 | +const fs=require('fs') | ||
3 | + | ||
4 | +let cardEnUS=[] | ||
5 | +let cardKoKR=[] | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | +exports.AddCards=(deckId,cards)=>{ | ||
10 | + | ||
11 | + fs.readFile('cardskoKR.json',(err,data)=>{ | ||
12 | + if (err) throw err | ||
13 | + cardKoKR=JSON.parse(data) | ||
14 | + }) | ||
15 | + | ||
16 | + return new Promise((resolve,reject)=>{ | ||
17 | + fs.readFile('cardsenUS.json',(err,data)=>{ | ||
18 | + if (err) throw err | ||
19 | + cardEnUS=JSON.parse(data) | ||
20 | + mysql.getConnection((err,connection)=>{ | ||
21 | + if(err) throw err | ||
22 | + for (let i=0;i<cards.length;i++) { | ||
23 | + let cardCost = cards[i].cardCost | ||
24 | + let cardName = cards[i].cardName | ||
25 | + let cardNum = cards[i].cardNum | ||
26 | + let cardId = 0 | ||
27 | + for (let i = 0; i < cardEnUS.length; i++) { | ||
28 | + if (cardEnUS[i].name === cardName) { | ||
29 | + console.log(cardEnUS[i].dbfId) | ||
30 | + cardId = cardEnUS[i].dbfId | ||
31 | + break | ||
32 | + } | ||
33 | + } | ||
34 | + | ||
35 | + connection.query(`insert into card (deckId,cardId,cardCost,cardName,cardNum) values (\'${deckId}\',\'${cardId}\',\'${cardCost}\',\'${cardName}\',\'${cardNum}\')`, (err, results, fields) => { | ||
36 | + // | ||
37 | + if (err) throw err | ||
38 | + }) | ||
39 | + } | ||
40 | + connection.release() | ||
41 | + resolve() | ||
42 | + }) | ||
43 | + }) | ||
44 | + }) | ||
45 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -2,12 +2,14 @@ const rp=require('request-promise') | ... | @@ -2,12 +2,14 @@ const rp=require('request-promise') |
2 | const mysql=require('../../database/mysql') | 2 | const mysql=require('../../database/mysql') |
3 | const crawler=require('./crawler') | 3 | const crawler=require('./crawler') |
4 | const cheerio=require('cheerio') | 4 | const cheerio=require('cheerio') |
5 | - | 5 | +const addCards=require('../card/addCards') |
6 | +const addDeck=require('../../database/deck/addDeck') | ||
7 | +const getDeckId=require('../../database/deck/getDeckId') | ||
8 | +let deckId=-1 | ||
6 | 9 | ||
7 | exports.NewDeck=(req,res)=>{ | 10 | exports.NewDeck=(req,res)=>{ |
8 | - const deckOwner=req.session.sid | 11 | + const deckOwner=req.session.sid || 'test' |
9 | - const deckTitle=req.body.deckTitle | 12 | + const deckTitle=req.body.deckTitle || new Date() |
10 | - const deckClass='paladin' | ||
11 | let deckCode=req.body.deckCode | 13 | let deckCode=req.body.deckCode |
12 | let cards=[] | 14 | let cards=[] |
13 | 15 | ||
... | @@ -15,7 +17,7 @@ exports.NewDeck=(req,res)=>{ | ... | @@ -15,7 +17,7 @@ exports.NewDeck=(req,res)=>{ |
15 | 17 | ||
16 | const DataCheck=()=>{ | 18 | const DataCheck=()=>{ |
17 | return new Promise((resolve,reject)=>{ | 19 | return new Promise((resolve,reject)=>{ |
18 | - if(!deckTitle || !deckCode){ | 20 | + if(!deckCode){ |
19 | return reject({ | 21 | return reject({ |
20 | code: 'request_body_error', | 22 | code: 'request_body_error', |
21 | message: 'request body is not defined' | 23 | message: 'request body is not defined' |
... | @@ -28,7 +30,6 @@ exports.NewDeck=(req,res)=>{ | ... | @@ -28,7 +30,6 @@ exports.NewDeck=(req,res)=>{ |
28 | return crawler.Crawl(deckCode) | 30 | return crawler.Crawl(deckCode) |
29 | } | 31 | } |
30 | const DeckCrawl=(result)=>{ | 32 | const DeckCrawl=(result)=>{ |
31 | - //console.log(result) | ||
32 | const $=cheerio.load(result) | 33 | const $=cheerio.load(result) |
33 | let cardCosts=$('div.hs-tile-info').children('.hs-tile-info-left.mdc-list-item__start-detail') | 34 | let cardCosts=$('div.hs-tile-info').children('.hs-tile-info-left.mdc-list-item__start-detail') |
34 | let cardNames=$('div.hs-tile-info').find('span').find('span') | 35 | let cardNames=$('div.hs-tile-info').find('span').find('span') |
... | @@ -44,23 +45,45 @@ exports.NewDeck=(req,res)=>{ | ... | @@ -44,23 +45,45 @@ exports.NewDeck=(req,res)=>{ |
44 | cardNum=cardNum.trim() | 45 | cardNum=cardNum.trim() |
45 | cards.push({cardCost: cardCost, cardName: cardName, cardNum: cardNum}) | 46 | cards.push({cardCost: cardCost, cardName: cardName, cardNum: cardNum}) |
46 | } | 47 | } |
47 | - console.log(cards) | 48 | + //console.log(cards) |
48 | } | 49 | } |
49 | 50 | ||
50 | const AddDeck=()=>{ | 51 | const AddDeck=()=>{ |
51 | - mysql.getConnection((err,connection)=>{ | 52 | + const asyncAddDeck=async ()=>{ |
52 | - if (err) throw err | 53 | + try{ |
53 | - connection.query(`insert into deck (deckOwner,deckTitle,deckClass,deckCode) values (\'${deckOwner}\',\'${deckTitle}\',\'${deckClass}\',\'${deckCode}\');`,(err,results,fields)=>{ | 54 | + const results=await addDeck.AddDeck(deckOwner,deckTitle,deckCode) |
54 | - if (err) throw err | 55 | + } |
55 | - connection.release() | 56 | + catch (err) { |
56 | - }) | 57 | + throw err |
57 | - }) | 58 | + } |
59 | + } | ||
60 | + return asyncAddDeck() | ||
61 | + } | ||
62 | + | ||
63 | + const GetId=()=>{ | ||
64 | + const asyncGetDeckId=async ()=>{ | ||
65 | + try{ | ||
66 | + const results=await getDeckId.GetDeckId(deckOwner,deckTitle) | ||
67 | + console.log(results) | ||
68 | + return results | ||
69 | + } | ||
70 | + catch (err){ | ||
71 | + throw err | ||
72 | + } | ||
73 | + } | ||
74 | + return asyncGetDeckId() | ||
75 | + } | ||
76 | + const AddCards=(deckId)=>{ | ||
77 | + console.log(`deckid in AddCards : ${deckId}`) | ||
78 | + return addCards.AddCards(deckId,cards) | ||
58 | } | 79 | } |
59 | 80 | ||
60 | DataCheck() | 81 | DataCheck() |
61 | .then(CrawlPage) | 82 | .then(CrawlPage) |
62 | .then(DeckCrawl) | 83 | .then(DeckCrawl) |
63 | .then(AddDeck) | 84 | .then(AddDeck) |
85 | + .then(GetId) | ||
86 | + .then(AddCards) | ||
64 | .then(()=>{ | 87 | .then(()=>{ |
65 | res.status(200).json({message:'Complete Adding Deck'}) | 88 | res.status(200).json({message:'Complete Adding Deck'}) |
66 | }) | 89 | }) | ... | ... |
... | @@ -7,6 +7,7 @@ const bcrypt=require('bcrypt-nodejs') | ... | @@ -7,6 +7,7 @@ const bcrypt=require('bcrypt-nodejs') |
7 | const rp=require('request-promise') | 7 | const rp=require('request-promise') |
8 | const morgan=require('morgan') | 8 | const morgan=require('morgan') |
9 | const cheerio=require('cheerio') | 9 | const cheerio=require('cheerio') |
10 | +const mysqlApostrophe=require('mysql-apostrophe') | ||
10 | require('dotenv').config() | 11 | require('dotenv').config() |
11 | const app=express() | 12 | const app=express() |
12 | 13 | ||
... | @@ -27,6 +28,8 @@ app.use(session({ | ... | @@ -27,6 +28,8 @@ app.use(session({ |
27 | saveUninitialized:true | 28 | saveUninitialized:true |
28 | })) | 29 | })) |
29 | 30 | ||
31 | +app.use(mysqlApostrophe) | ||
32 | + | ||
30 | app.use('/api',require('./api')) | 33 | app.use('/api',require('./api')) |
31 | 34 | ||
32 | let allCards=[] | 35 | let allCards=[] |
... | @@ -77,24 +80,39 @@ app.post('/logout',(req,res)=>{ | ... | @@ -77,24 +80,39 @@ app.post('/logout',(req,res)=>{ |
77 | }) | 80 | }) |
78 | 81 | ||
79 | app.get('/decklist',(req,res)=>{ | 82 | app.get('/decklist',(req,res)=>{ |
83 | + if(!req.session.sid) | ||
84 | + res.redirect('/login') | ||
85 | + else{ | ||
80 | fs.readFile('./views/html/decklist.html',(err,data)=>{ | 86 | fs.readFile('./views/html/decklist.html',(err,data)=>{ |
81 | res.writeHead(200,{'Content-Type':'text/html'}) | 87 | res.writeHead(200,{'Content-Type':'text/html'}) |
82 | res.end(data) | 88 | res.end(data) |
83 | }) | 89 | }) |
90 | + } | ||
91 | + | ||
84 | }) | 92 | }) |
85 | 93 | ||
86 | app.get('/ingame',(req,res)=>{ | 94 | app.get('/ingame',(req,res)=>{ |
95 | + if(!req.session.sid) | ||
96 | + res.redirect('/login') | ||
97 | + else{ | ||
87 | fs.readFile('./views/html/ingame.html',(err,data)=>{ | 98 | fs.readFile('./views/html/ingame.html',(err,data)=>{ |
88 | res.writeHead(200,{'Content-Type':'text/html'}) | 99 | res.writeHead(200,{'Content-Type':'text/html'}) |
89 | res.end(data) | 100 | res.end(data) |
90 | }) | 101 | }) |
102 | + } | ||
103 | + | ||
91 | }) | 104 | }) |
92 | 105 | ||
93 | app.get('/newdeck',(req,res)=>{ | 106 | app.get('/newdeck',(req,res)=>{ |
107 | + if(!req.session.sid) | ||
108 | + res.redirect('/login') | ||
109 | + else{ | ||
94 | fs.readFile('./views/html/newdeck.html',(err,data)=>{ | 110 | fs.readFile('./views/html/newdeck.html',(err,data)=>{ |
95 | res.writeHead(200,{'Content-Type':'text/html'}) | 111 | res.writeHead(200,{'Content-Type':'text/html'}) |
96 | res.end(data) | 112 | res.end(data) |
97 | }) | 113 | }) |
114 | + } | ||
115 | + | ||
98 | }) | 116 | }) |
99 | 117 | ||
100 | app.listen(process.env.SERVER_PORT || 3000,()=>{ | 118 | app.listen(process.env.SERVER_PORT || 3000,()=>{ | ... | ... |
database/deck/addDeck.js
0 → 100644
1 | +const mysql=require('../mysql') | ||
2 | + | ||
3 | +let deckId | ||
4 | + | ||
5 | +exports.AddDeck=(deckOwner,deckTitle,deckCode)=>{ | ||
6 | + return new Promise((resolve,reject)=> { | ||
7 | + mysql.getConnection((err, connection) => { | ||
8 | + if (err) throw err | ||
9 | + connection.query(`insert into deck (deckOwner,deckTitle,deckCode) values (\'${deckOwner}\',\'${deckTitle}\',\'${deckCode}\');`, (err, results, fields) => { | ||
10 | + if (err) throw err | ||
11 | + connection.release() | ||
12 | + resolve(results) | ||
13 | + }) | ||
14 | + }) | ||
15 | + }) | ||
16 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
database/deck/getDeckId.js
0 → 100644
1 | +const mysql=require('../mysql') | ||
2 | + | ||
3 | +exports.GetDeckId=(deckOwner,deckTitle)=>{ | ||
4 | + return new Promise((resolve,reject)=>{ | ||
5 | + mysql.getConnection((err,connection)=>{ | ||
6 | + if (err) throw err | ||
7 | + connection.query(`select * from deck where deckOwner=\'${deckOwner}\' and deckTitle=\'${deckTitle}\'`,(err,results,field)=>{ | ||
8 | + if (err) throw err | ||
9 | + console.log('result in getDeckId'+results[0]) | ||
10 | + connection.release() | ||
11 | + resolve(results[0].id) | ||
12 | + }) | ||
13 | + }) | ||
14 | + }) | ||
15 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -655,6 +655,11 @@ | ... | @@ -655,6 +655,11 @@ |
655 | } | 655 | } |
656 | } | 656 | } |
657 | }, | 657 | }, |
658 | + "mysql-apostrophe": { | ||
659 | + "version": "1.0.8", | ||
660 | + "resolved": "https://registry.npmjs.org/mysql-apostrophe/-/mysql-apostrophe-1.0.8.tgz", | ||
661 | + "integrity": "sha512-uZZJdCg8rQTNhKz9oudGZGIbO+BVxTytoWgM9xZtbUkDcDdGG9sfFNbLjC01j/oGnyRR56Oa/Xr0X6nV9rq3vw==" | ||
662 | + }, | ||
658 | "negotiator": { | 663 | "negotiator": { |
659 | "version": "0.6.1", | 664 | "version": "0.6.1", |
660 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz", | 665 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz", | ... | ... |
... | @@ -24,6 +24,7 @@ | ... | @@ -24,6 +24,7 @@ |
24 | "iconv-lite": "^0.4.24", | 24 | "iconv-lite": "^0.4.24", |
25 | "morgan": "^1.9.1", | 25 | "morgan": "^1.9.1", |
26 | "mysql": "^2.16.0", | 26 | "mysql": "^2.16.0", |
27 | + "mysql-apostrophe": "^1.0.8", | ||
27 | "path": "^0.12.7", | 28 | "path": "^0.12.7", |
28 | "request": "^2.88.0", | 29 | "request": "^2.88.0", |
29 | "request-promise": "^4.2.2" | 30 | "request-promise": "^4.2.2" | ... | ... |
-
Please register or login to post a comment