From 4f5522ff77bff1942386f7c9bbf11fd220ac84d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Fri, 17 May 2019 01:16:22 +0200 Subject: [PATCH] better decklist parser + validator --- frontend/src/components/Deck.vue | 2 +- frontend/src/components/forms/DeckEditor.vue | 33 +++++-- frontend/src/views/UserCP.vue | 91 -------------------- 3 files changed, 29 insertions(+), 97 deletions(-) diff --git a/frontend/src/components/Deck.vue b/frontend/src/components/Deck.vue index 3fbed89..ce2ff7b 100644 --- a/frontend/src/components/Deck.vue +++ b/frontend/src/components/Deck.vue @@ -98,7 +98,7 @@ export default { lines.push(this.deck.note) lines.push('') - // list forwards + // list each deck part this.deck_parts.forEach(part => { lines.push(`${part.heading} (${part.count}):`) diff --git a/frontend/src/components/forms/DeckEditor.vue b/frontend/src/components/forms/DeckEditor.vue index c4a9c92..77dd323 100644 --- a/frontend/src/components/forms/DeckEditor.vue +++ b/frontend/src/components/forms/DeckEditor.vue @@ -3,12 +3,15 @@ {{ deck.count }} cards detected! (Decks should have 50) + + Card with {{ deck.maximum }} copies detected! (Cards should not have more than 3 copies) + @@ -31,7 +34,8 @@ export default { data: () => ({ deck: { - count: 50 + count: 50, + maximum: 0 } }), @@ -47,6 +51,7 @@ export default { // select all lines containing card serial numbers let cardLinesRE = /^.*\b\d-\d{3}[A-Z]?\b.*$/gm let cardLines = deck_string.match(cardLinesRE) + let cardCounts = {} cardLines.forEach(cardLine => { // extract serial (guaranteed to be in here!) @@ -79,12 +84,21 @@ export default { } } - // push card data into deck + // count copies + if (!cardCounts[serial]) { + cardCounts[serial] = 0 + } + cardCounts[serial] += count + + retval.count += count + }) + + // push card data into deck + Object.keys(cardCounts).forEach(serial => { retval.cards.push({ serial: serial, - count: count + count: cardCounts[serial] }) - retval.count += count }) // strip out lines with serial numbers @@ -120,7 +134,16 @@ export default { check() { let new_deck = this.parse_deck(this.$refs.textarea.lazyValue) + + // count number of cards this.deck.count = new_deck.count + + // find most frequent card + this.deck.maximum = 0 + new_deck.cards.forEach(card => { + if(card.count > this.deck.maximum) + this.deck.maximum = card.count + }) } } } diff --git a/frontend/src/views/UserCP.vue b/frontend/src/views/UserCP.vue index 1a1e4c4..3391c38 100644 --- a/frontend/src/views/UserCP.vue +++ b/frontend/src/views/UserCP.vue @@ -77,97 +77,6 @@ export default { } }) } - }, - - mounted() { - let fileContent = `--Generated By FF Decks (www.ffdecks.com)-- -Deck Name: Ashe turbo -Created by: hawks1997 - - -Forward(26): -3 Garland (6-002) -3 Rain (8-134) -3 Garnet (3-129) -2 Zidane (8-115) -3 Ashe (2-121) -3 Basch (2-014) -3 Steiner (4-129) -3 Vivi (3-017) -3 Cecil (2-129) - -Summon(8): -2 Cuchulainn, the Impure (2-133) -2 Bahamut (4-016) -1 Phoenix (5-019) -3 Leviathan (6-125) - -Backup(16): -2 Sage (2-005) -2 Vermilion Bird l'Cie Caetuna (6-010) -2 Yotsuyu (8-020) -2 Rasler (5-166) -2 Red Mage (1-003) -3 Hilda (6-122) -3 Astrologian (2-130) - -Monster(0):` - - // select the line containing 'deck name:' - // and its successor - let metaRE = /^deck name: (.+)$[\s]*?^(.+)$/im - let metaData = metaRE.exec(fileContent) - - // extract matches - let deckData = { - name: metaData[1], - note: metaData[2], - cards: [] - } - - // select all lines containing card serial numbers - let cardLinesRE = /^.*\b\d-\d{3}[A-Z]?\b.*$/gm - let cardLines = fileContent.match(cardLinesRE) - - cardLines.forEach(cardLine => { - // extract serial (guaranteed to be in here!) - let serialRE = /\b(\d-\d{3})[A-Z]?\b/i - let serial = serialRE.exec(cardLine)[1] - - // strip out serial number - cardLine = cardLine.replace(serialRE, '') - - let countREs = [ - // prioritize a count with "times" symbol *, x, × - /\b([1-3])(?:[*×]|[x]\b)/, - /(?:[*×]|\b[x])([1-3])\b/, - // next priority: with whitespace - /\s+([1-3])\s+/, - /\s+([1-3])\b/, - /\b([1-3])\s+/, - // least priority: any single digit - /\b([1-3])\b/ - ] - - // fallback value - let count = '0' - for (let i = 0; i < countREs.length; i++) { - let data = countREs[i].exec(cardLine) - - if (data) { - count = data[1] - break - } - } - - // push card data into deck - deckData.cards.push({ - serial: serial, - count: count - }) - }) - - console.log(deckData) } }