From 04105d6c73054276cb9b5251d8197e9bf3ea16f6 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 12 Oct 2017 03:41:16 -0400 Subject: [PATCH] Further fix server deck JSON generation --- server.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/server.js b/server.js index e95c8d0..0499a9c 100644 --- a/server.js +++ b/server.js @@ -170,17 +170,14 @@ function handleUpload(res, req) { const json = JSON.parse(body); const deckJSON = json.json; const cardTemplate = fs.readFileSync('template/card.json'); + const template = JSON.parse(fs.readFileSync(`template/${deckJSON.type}/input.json`)); + const cardCount = Object.entries(template.cardTypes) + .map(ct => deckJSON[ct[0]].length * (ct[1].back ? 2 : 1)) + .reduce((sum, current) => sum + current, 0); let deckOut = JSON.parse(fs.readFileSync('template/deck.json')); - deckOut.ObjectStates[0].Nickname = deckJSON.name; - let template = JSON.parse(fs.readFileSync(`template/${deckJSON.type}/input.json`)); - - let cardCount = Object.entries(template.cardTypes) - .map(ct => deckJSON[ct[0]].length * (ct[1].back ? 2 : 1)) - .reduce((sum, current) => sum + current, 0); - Object.assign(deckOut.ObjectStates[0].CustomDeck['1'], {NumWidth: Math.ceil(Math.sqrt(cardCount)), NumHeight: Math.ceil(Math.sqrt(cardCount)), @@ -195,6 +192,12 @@ function handleUpload(res, req) { Object.assign(cardOut, {Nickname: cardIn.name, Description: cardIn.keywords, CardID: index}); + + for (let ii=0; ii<(cardIn.count || 1); ii++) { + deckOut.ObjectStates[0].DeckIDs.push(index); + } + index++; + if(cardType[1].back) { let cardBack = JSON.parse(cardTemplate); Object.assign(cardBack, {Nickname: cardIn.back.name, @@ -203,12 +206,9 @@ function handleUpload(res, req) { cardOut.States = {"2": cardBack}; index++; } - for (let ii=0; ii<(cardIn.count || 1); ii++) { - deckOut.ObjectStates[0].DeckIDs.push(index); - } - index++; return cardOut; - })); + })) + .reduce((sum, cur) => sum.concat(cur), []); fs.writeFileSync(`decks/${deckJSON.name}.json`, JSON.stringify(deckOut)); fs.writeFileSync(`decks/${deckJSON.name}.input.json`, JSON.stringify(deckJSON));