addCards.js
1.64 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
const mysql=require('../../database/mysql')
const fs=require('fs')
let cardEnUS=[]
let cardKoKR=[]
//카드 덱 ID와 카드 정보를 받아와, 카드별로 dbfId랑 매칭 시킨 후, card 테이블에 추가
//return: 없음
exports.AddCards=(deckId,cards)=>{
return new Promise((resolve,reject)=>{
//전체 카드 읽어 오기
fs.readFile('cardsenUS2.json',(err,data)=>{
if (err) throw err
cardEnUS=JSON.parse(data)
//mysql 연결
mysql.getConnection((err,connection)=>{
if(err) throw err
//카드별로 dbfId 찾기
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) {
cardId = cardEnUS[i].dbfId
//single quote 처리
cardName=cardName.replace('\'','\'\'')
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()
})
})
})
}