Add new deck
This commit is contained in:
parent
38b92829cd
commit
0475e45275
6 changed files with 123 additions and 48 deletions
34
backend/routes/decks/add.coffee
Normal file
34
backend/routes/decks/add.coffee
Normal file
|
@ -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
|
|
@ -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'
|
||||
]
|
||||
|
|
|
@ -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 = []
|
||||
|
|
|
@ -1,12 +1,17 @@
|
|||
<template>
|
||||
<v-expansion-panel>
|
||||
<v-expansion-panel v-model="open">
|
||||
<Deck
|
||||
v-for="deck in linked"
|
||||
:deck="deck"
|
||||
:key="deck.id"
|
||||
@change="refresh_decks"
|
||||
/>
|
||||
<NewDeck />
|
||||
<NewDeck
|
||||
@change="
|
||||
open = null
|
||||
refresh_decks()
|
||||
"
|
||||
/>
|
||||
</v-expansion-panel>
|
||||
</template>
|
||||
|
||||
|
@ -26,6 +31,10 @@ export default {
|
|||
NewDeck
|
||||
},
|
||||
|
||||
data: () => ({
|
||||
open: null
|
||||
}),
|
||||
|
||||
asyncComputed: {
|
||||
decks: {
|
||||
get() {
|
||||
|
|
|
@ -6,13 +6,16 @@
|
|||
|
||||
<v-container>
|
||||
<v-card flat>
|
||||
<DeckEditor />
|
||||
<DeckEditor ref="editor" @save="save_deck" />
|
||||
</v-card>
|
||||
</v-container>
|
||||
</v-expansion-panel-content>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import * as Cookies from 'js-cookie'
|
||||
import axios from '@/plugins/axios'
|
||||
|
||||
import DeckEditor from './forms/DeckEditor.vue'
|
||||
|
||||
export default {
|
||||
|
@ -20,6 +23,26 @@ export default {
|
|||
|
||||
components: {
|
||||
DeckEditor
|
||||
},
|
||||
|
||||
computed: {
|
||||
session: () => Cookies.get('session')
|
||||
},
|
||||
|
||||
methods: {
|
||||
save_deck(new_deck) {
|
||||
axios
|
||||
.post('/decks/add', {
|
||||
session: this.session,
|
||||
deckCards: new_deck.plainObject()
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data.success) {
|
||||
this.$emit('change')
|
||||
this.$refs.editor.clear()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in a new issue