oop DeckEditor (codebase join for /decks/add)
This commit is contained in:
parent
db4677d751
commit
f363ead51a
2 changed files with 103 additions and 104 deletions
96
frontend/src/classes/Deck.js
Normal file
96
frontend/src/classes/Deck.js
Normal file
|
@ -0,0 +1,96 @@
|
|||
'use strict'
|
||||
|
||||
export default class {
|
||||
constructor(str) {
|
||||
// select all lines containing card serial numbers
|
||||
let cardLinesRE = /^.*\b\d-\d{3}[A-Z]?\b.*$/gm
|
||||
let cardLines = str.match(cardLinesRE)
|
||||
let cardCounts = {}
|
||||
|
||||
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([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 i = 0; i < countREs.length; i++) {
|
||||
let data = countREs[i].exec(cardLine)
|
||||
|
||||
if (data) {
|
||||
count = Number(data[1])
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// count copies
|
||||
if (!cardCounts[serial]) {
|
||||
cardCounts[serial] = 0
|
||||
}
|
||||
cardCounts[serial] += count
|
||||
})
|
||||
|
||||
// push card data into deck
|
||||
this.cards = []
|
||||
Object.keys(cardCounts).forEach(serial => {
|
||||
this.cards.push({
|
||||
serial: serial,
|
||||
count: cardCounts[serial]
|
||||
})
|
||||
})
|
||||
|
||||
// strip out lines with serial numbers
|
||||
str = str.replace(cardLinesRE, '')
|
||||
// then strip out anything after the first empty line
|
||||
str = str.replace(/^[\s]*$[^]*/m, '')
|
||||
|
||||
// select the line containing 'deck name:'
|
||||
// and its successor (ffdecks format)
|
||||
let metaRE = /^Deck Name: (.+)$[\s]*?^(.+)$/m
|
||||
let metaData = metaRE.exec(str)
|
||||
|
||||
// fallback
|
||||
this.name = 'Unnamed Deck'
|
||||
this.note = ''
|
||||
|
||||
if (!metaData) {
|
||||
// use lax format: <anything>:[deck name][newline][note]
|
||||
metaRE = /[^]*?:(.+)$[\s]*?^([^]*)/m
|
||||
metaData = metaRE.exec(str)
|
||||
}
|
||||
|
||||
// look again, I am not an else!
|
||||
if (metaData) {
|
||||
// extract matches
|
||||
this.name = metaData[1].trim()
|
||||
this.note = metaData[2].trim()
|
||||
}
|
||||
}
|
||||
|
||||
count() {
|
||||
return this.cards.reduce((total, card) => total + card.count, 0)
|
||||
}
|
||||
|
||||
plainObject() {
|
||||
return {
|
||||
name: this.name,
|
||||
note: this.note,
|
||||
cards: this.cards
|
||||
}
|
||||
}
|
||||
}
|
|
@ -49,6 +49,7 @@
|
|||
<script>
|
||||
import * as Cookies from 'js-cookie'
|
||||
import axios from '@/plugins/axios'
|
||||
import Deck from '@/classes/Deck'
|
||||
|
||||
export default {
|
||||
name: 'DeckEditor',
|
||||
|
@ -72,123 +73,25 @@ export default {
|
|||
session: () => Cookies.get('session'),
|
||||
|
||||
new_deck() {
|
||||
let deck
|
||||
try {
|
||||
return this.parse_deck(this.new_decklist)
|
||||
deck = new Deck(this.new_decklist)
|
||||
} catch (e) {
|
||||
return this.parse_deck(this.value)
|
||||
deck = new Deck(this.value)
|
||||
}
|
||||
return deck
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
parse_deck(deck_string) {
|
||||
let retval = {
|
||||
name: '',
|
||||
note: '',
|
||||
cards: [],
|
||||
count: 0
|
||||
}
|
||||
|
||||
// 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!)
|
||||
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([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 i = 0; i < countREs.length; i++) {
|
||||
let data = countREs[i].exec(cardLine)
|
||||
|
||||
if (data) {
|
||||
count = Number(data[1])
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 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: cardCounts[serial]
|
||||
})
|
||||
})
|
||||
|
||||
// strip out lines with serial numbers
|
||||
deck_string = deck_string.replace(cardLinesRE, '')
|
||||
|
||||
// select the line containing 'deck name:'
|
||||
// and its successor (ffdecks format)
|
||||
let metaRE = /^Deck Name: (.+)$[\s]*?^(.+)$/m
|
||||
let metaData = metaRE.exec(deck_string)
|
||||
|
||||
// fallback
|
||||
retval.name = 'Unnamed Deck'
|
||||
retval.note = ''
|
||||
|
||||
if (!metaData) {
|
||||
// no ffdecks format found: strip out anything after the first empty line
|
||||
deck_string = deck_string.replace(/^[\s]*$[^]*/m, '')
|
||||
|
||||
// use lax format: <anything>:[deck name][newline][note]
|
||||
metaRE = /[^]*?:(.+)$[\s]*?^([^]*)/m
|
||||
metaData = metaRE.exec(deck_string)
|
||||
}
|
||||
|
||||
// look again, I am not an else!
|
||||
if (metaData) {
|
||||
// extract matches
|
||||
retval.name = metaData[1].trim()
|
||||
retval.note = metaData[2].trim()
|
||||
}
|
||||
|
||||
return retval
|
||||
},
|
||||
|
||||
close() {
|
||||
this.checked = false
|
||||
this.$emit('close')
|
||||
},
|
||||
|
||||
update_deck(text) {
|
||||
try {
|
||||
this.new_deck = this.parse_deck(text)
|
||||
} catch (e) {
|
||||
this.new_deck = this.parse_deck(this.value)
|
||||
}
|
||||
},
|
||||
|
||||
check_deck() {
|
||||
// count number of cards
|
||||
this.check.count = this.new_deck.count
|
||||
this.check.count = this.new_deck.count()
|
||||
|
||||
// find most frequent card
|
||||
this.check.maximum = 0
|
||||
|
@ -205,7 +108,7 @@ export default {
|
|||
.post('/decks/modify', {
|
||||
session: this.session,
|
||||
deckID: this.id,
|
||||
deckCards: this.new_deck
|
||||
deckCards: this.new_deck.plainObject()
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data.success) {
|
||||
|
|
Reference in a new issue