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'
# list decks
'decks/list'
# add deck
'decks/add'
# modify deck
'decks/modify'
]

View file

@ -46,7 +46,8 @@ export default class {
let cardLines = str.match(cardLinesRE)
let cardCounts = {}
cardLines.forEach(cardLine => {
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)
@ -84,7 +85,8 @@ export default class {
cardCounts[serial] = 0
}
cardCounts[serial] += count
})
}
}
// push card data into deck
this.cards = []

View file

@ -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() {

View file

@ -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>

View file

@ -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)
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)
},
methods: {
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