diff --git a/backend/routes/decks/add.coffee b/backend/routes/decks/add.coffee new file mode 100644 index 0000000..91e5c3e --- /dev/null +++ b/backend/routes/decks/add.coffee @@ -0,0 +1,34 @@ +logger = (require 'logging').default '/decks/add' + +# session storage (volatile data) +session = (require '../../session') +# fftcg.db (persistent data) +fftcgdb = (require '../../db') + +module.exports = + url: '/decks/add' + method: 'POST' + # schema: (require './modify.schema') + + handler: (request, reply) -> + session.check request.body.session ? "" + .then (userid) -> + # active session found, get associated user + fftcgdb.addDeck (userid), (request.body.deckCards) + .then (deckID) -> + logger.info "OK user '#{userid}' added deck '#{deckID}'" + reply.send + success: true + deck: deckID + + .catch (err) -> + # couldnt get user details + logger.warn "FAIL '#{err}' for user id '#{userid}'" + reply.send + success: false + + .catch -> + # no session found + logger.info "FAIL '#{request.body.session}' session not found" + reply.send + success: false diff --git a/backend/server.coffee b/backend/server.coffee index 021ec57..07e8367 100644 --- a/backend/server.coffee +++ b/backend/server.coffee @@ -22,6 +22,8 @@ fastify.route (require "./routes/#{route}") for route in [ 'user/register' # list decks 'decks/list' + # add deck + 'decks/add' # modify deck 'decks/modify' ] diff --git a/frontend/src/classes/Deck.js b/frontend/src/classes/Deck.js index 535ed1b..f7aaedc 100644 --- a/frontend/src/classes/Deck.js +++ b/frontend/src/classes/Deck.js @@ -46,45 +46,47 @@ export default class { let cardLines = str.match(cardLinesRE) let cardCounts = {} - cardLines.forEach(cardLine => { - // extract serial (guaranteed to be in here!) - let serialRE = /\b(\d+)-0*(\d{1,3})[A-Z]?\b/i - let serial = serialRE.exec(cardLine) - // force format 'x-xxx' - serial = `${serial[1]}-${serial[2].padStart(3, '0')}` + if (cardLines) { + for (let cardLine of cardLines) { + // extract serial (guaranteed to be in here!) + let serialRE = /\b(\d+)-0*(\d{1,3})[A-Z]?\b/i + let serial = serialRE.exec(cardLine) + // force format 'x-xxx' + serial = `${serial[1]}-${serial[2].padStart(3, '0')}` - // strip out serial number - cardLine = cardLine.replace(serialRE, '') + // strip out serial number + cardLine = cardLine.replace(serialRE, '') - let countREs = [ - // prioritize a count with "times" symbol *, x, × - /\b([0-9]+)(?:[*×]|[x]\b)/, - /(?:[*×]|\b[x])([0-9]+)\b/, - // next priority: count with whitespace - /\s+([0-9]+)\s+/, - /\s+([0-9]+)\b/, - /\b([0-9]+)\s+/, - // least priority: any simple number - /\b([0-9]+)\b/ - ] + let countREs = [ + // prioritize a count with "times" symbol *, x, × + /\b([0-9]+)(?:[*×]|[x]\b)/, + /(?:[*×]|\b[x])([0-9]+)\b/, + // next priority: count with whitespace + /\s+([0-9]+)\s+/, + /\s+([0-9]+)\b/, + /\b([0-9]+)\s+/, + // least priority: any simple number + /\b([0-9]+)\b/ + ] - // fallback value - let count = 1 - for (let countRE of countREs) { - let data = countRE.exec(cardLine) + // fallback value + let count = 1 + for (let countRE of countREs) { + let data = countRE.exec(cardLine) - if (data) { - count = Number(data[1]) - break + if (data) { + count = Number(data[1]) + break + } } - } - // count copies - if (!cardCounts[serial]) { - cardCounts[serial] = 0 + // count copies + if (!cardCounts[serial]) { + cardCounts[serial] = 0 + } + cardCounts[serial] += count } - cardCounts[serial] += count - }) + } // push card data into deck this.cards = [] diff --git a/frontend/src/components/DeckList.vue b/frontend/src/components/DeckList.vue index 32b56e1..4878047 100644 --- a/frontend/src/components/DeckList.vue +++ b/frontend/src/components/DeckList.vue @@ -1,12 +1,17 @@ @@ -26,6 +31,10 @@ export default { NewDeck }, + data: () => ({ + open: null + }), + asyncComputed: { decks: { get() { diff --git a/frontend/src/components/NewDeck.vue b/frontend/src/components/NewDeck.vue index 6a53a1a..42d6fc9 100644 --- a/frontend/src/components/NewDeck.vue +++ b/frontend/src/components/NewDeck.vue @@ -6,13 +6,16 @@ - + diff --git a/frontend/src/components/forms/DeckEditor.vue b/frontend/src/components/forms/DeckEditor.vue index 6fba9bb..611b6c2 100644 --- a/frontend/src/components/forms/DeckEditor.vue +++ b/frontend/src/components/forms/DeckEditor.vue @@ -49,23 +49,28 @@ export default { }, data: () => ({ - check: { - count: 50, - maximum: 0, - checked: false - }, - new_deck: {} + check: null, + new_deck: null }), created() { - this.new_deck = new Deck(0) - - if (this.deck) - // this.deck should already be populated! - this.new_deck.from_object(this.deck) + this.clear() }, methods: { + clear() { + this.check = { + count: 50, + maximum: 0, + checked: false + } + + this.new_deck = new Deck(0) + if (this.deck) + // this.deck should already be populated! + this.new_deck.from_object(this.deck) + }, + validate() { this.new_deck.from_deckList(this.$refs.deckList.lazyValue) this.new_deck.populate() @@ -75,9 +80,9 @@ export default { // find most frequent card this.check.maximum = 0 - this.new_deck.cards.forEach(card => { + for (let card of this.new_deck.cards) { if (card.count > this.check.maximum) this.check.maximum = card.count - }) + } // deck has now been checked this.check.checked = true