Add new deck

This commit is contained in:
Jörn-Michael Miehe 2019-05-24 13:30:26 +02:00
parent 38b92829cd
commit 0475e45275
6 changed files with 123 additions and 48 deletions

View 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

View file

@ -22,6 +22,8 @@ fastify.route (require "./routes/#{route}") for route in [
'user/register' 'user/register'
# list decks # list decks
'decks/list' 'decks/list'
# add deck
'decks/add'
# modify deck # modify deck
'decks/modify' 'decks/modify'
] ]

View file

@ -46,45 +46,47 @@ export default class {
let cardLines = str.match(cardLinesRE) let cardLines = str.match(cardLinesRE)
let cardCounts = {} let cardCounts = {}
cardLines.forEach(cardLine => { if (cardLines) {
// extract serial (guaranteed to be in here!) for (let cardLine of cardLines) {
let serialRE = /\b(\d+)-0*(\d{1,3})[A-Z]?\b/i // extract serial (guaranteed to be in here!)
let serial = serialRE.exec(cardLine) let serialRE = /\b(\d+)-0*(\d{1,3})[A-Z]?\b/i
// force format 'x-xxx' let serial = serialRE.exec(cardLine)
serial = `${serial[1]}-${serial[2].padStart(3, '0')}` // force format 'x-xxx'
serial = `${serial[1]}-${serial[2].padStart(3, '0')}`
// strip out serial number // strip out serial number
cardLine = cardLine.replace(serialRE, '') cardLine = cardLine.replace(serialRE, '')
let countREs = [ let countREs = [
// prioritize a count with "times" symbol *, x, × // prioritize a count with "times" symbol *, x, ×
/\b([0-9]+)(?:[*×]|[x]\b)/, /\b([0-9]+)(?:[*×]|[x]\b)/,
/(?:[*×]|\b[x])([0-9]+)\b/, /(?:[*×]|\b[x])([0-9]+)\b/,
// next priority: count with whitespace // next priority: count with whitespace
/\s+([0-9]+)\s+/, /\s+([0-9]+)\s+/,
/\s+([0-9]+)\b/, /\s+([0-9]+)\b/,
/\b([0-9]+)\s+/, /\b([0-9]+)\s+/,
// least priority: any simple number // least priority: any simple number
/\b([0-9]+)\b/ /\b([0-9]+)\b/
] ]
// fallback value // fallback value
let count = 1 let count = 1
for (let countRE of countREs) { for (let countRE of countREs) {
let data = countRE.exec(cardLine) let data = countRE.exec(cardLine)
if (data) { if (data) {
count = Number(data[1]) count = Number(data[1])
break break
}
} }
}
// count copies // count copies
if (!cardCounts[serial]) { if (!cardCounts[serial]) {
cardCounts[serial] = 0 cardCounts[serial] = 0
}
cardCounts[serial] += count
} }
cardCounts[serial] += count }
})
// push card data into deck // push card data into deck
this.cards = [] this.cards = []

View file

@ -1,12 +1,17 @@
<template> <template>
<v-expansion-panel> <v-expansion-panel v-model="open">
<Deck <Deck
v-for="deck in linked" v-for="deck in linked"
:deck="deck" :deck="deck"
:key="deck.id" :key="deck.id"
@change="refresh_decks" @change="refresh_decks"
/> />
<NewDeck /> <NewDeck
@change="
open = null
refresh_decks()
"
/>
</v-expansion-panel> </v-expansion-panel>
</template> </template>
@ -26,6 +31,10 @@ export default {
NewDeck NewDeck
}, },
data: () => ({
open: null
}),
asyncComputed: { asyncComputed: {
decks: { decks: {
get() { get() {

View file

@ -6,13 +6,16 @@
<v-container> <v-container>
<v-card flat> <v-card flat>
<DeckEditor /> <DeckEditor ref="editor" @save="save_deck" />
</v-card> </v-card>
</v-container> </v-container>
</v-expansion-panel-content> </v-expansion-panel-content>
</template> </template>
<script> <script>
import * as Cookies from 'js-cookie'
import axios from '@/plugins/axios'
import DeckEditor from './forms/DeckEditor.vue' import DeckEditor from './forms/DeckEditor.vue'
export default { export default {
@ -20,6 +23,26 @@ export default {
components: { components: {
DeckEditor 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> </script>

View file

@ -49,23 +49,28 @@ export default {
}, },
data: () => ({ data: () => ({
check: { check: null,
count: 50, new_deck: null
maximum: 0,
checked: false
},
new_deck: {}
}), }),
created() { created() {
this.new_deck = new Deck(0) this.clear()
if (this.deck)
// this.deck should already be populated!
this.new_deck.from_object(this.deck)
}, },
methods: { 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() { validate() {
this.new_deck.from_deckList(this.$refs.deckList.lazyValue) this.new_deck.from_deckList(this.$refs.deckList.lazyValue)
this.new_deck.populate() this.new_deck.populate()
@ -75,9 +80,9 @@ export default {
// find most frequent card // find most frequent card
this.check.maximum = 0 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 if (card.count > this.check.maximum) this.check.maximum = card.count
}) }
// deck has now been checked // deck has now been checked
this.check.checked = true this.check.checked = true