reactivity DeckEditor

This commit is contained in:
Jörn-Michael Miehe 2019-05-23 18:11:39 +02:00
parent f363ead51a
commit 077806411c
5 changed files with 163 additions and 117 deletions

View file

@ -1,7 +1,23 @@
'use strict' 'use strict'
import CardsDB from '@/plugins/ffdecks'
export default class { export default class {
constructor(str) { constructor() {
this.id = null
this.name = ''
this.note = ''
this.cards = []
}
from_plainObject(obj) {
this.id = obj.id
this.name = obj.content.name
this.note = obj.content.note
this.cards = obj.content.cards
}
from_deckList(str) {
// select all lines containing card serial numbers // select all lines containing card serial numbers
let cardLinesRE = /^.*\b\d-\d{3}[A-Z]?\b.*$/gm let cardLinesRE = /^.*\b\d-\d{3}[A-Z]?\b.*$/gm
let cardLines = str.match(cardLinesRE) let cardLines = str.match(cardLinesRE)
@ -29,8 +45,8 @@ export default class {
// fallback value // fallback value
let count = 1 let count = 1
for (let i = 0; i < countREs.length; i++) { for (let countRE of countREs) {
let data = countREs[i].exec(cardLine) let data = countRE.exec(cardLine)
if (data) { if (data) {
count = Number(data[1]) count = Number(data[1])
@ -47,12 +63,12 @@ export default class {
// push card data into deck // push card data into deck
this.cards = [] this.cards = []
Object.keys(cardCounts).forEach(serial => { for(let serial in cardCounts) {
this.cards.push({ this.cards.push({
serial: serial, serial: serial,
count: cardCounts[serial] count: cardCounts[serial]
}) })
}) }
// strip out lines with serial numbers // strip out lines with serial numbers
str = str.replace(cardLinesRE, '') str = str.replace(cardLinesRE, '')
@ -82,10 +98,6 @@ export default class {
} }
} }
count() {
return this.cards.reduce((total, card) => total + card.count, 0)
}
plainObject() { plainObject() {
return { return {
name: this.name, name: this.name,
@ -93,4 +105,70 @@ export default class {
cards: this.cards cards: this.cards
} }
} }
parts() {
let retval = ['Forwards', 'Backups', 'Summons, Monsters & more'].map(
item => ({
heading: item,
cards: [],
count: 0
})
)
for (let card of this.cards) {
let target
switch (card.dbentry.type) {
case 'Forward':
target = 0
break
case 'Backup':
target = 1
break
default:
target = 2
break
}
retval[target].cards.push(card)
retval[target].count += card.count
}
for (let part of retval) {
part.cards.sort(
(card_l, card_r) => card_l.dbentry.cost - card_r.dbentry.cost
)
}
return retval
}
deckList() {
let lines = []
// begin with deck name and note
lines.push('Deck Name: ' + this.name)
lines.push(this.note)
lines.push('')
// list each deck part
for(let part of this.parts()) {
lines.push(`${part.heading} (${part.count}):`)
for(let card of part.cards)
lines.push(`${card.count}x ${card.serial} "${card.dbentry.name}"`)
lines.push('')
}
return lines.join('\n')
}
count() {
return this.cards.reduce((total, card) => total + card.count, 0)
}
populate() {
for (let card of this.cards) {
card.dbentry = CardsDB[card.serial]
}
}
} }

View file

@ -10,7 +10,7 @@
<v-container v-if="!editing" style="position: relative" grid-list-md fluid> <v-container v-if="!editing" style="position: relative" grid-list-md fluid>
<v-layout row wrap> <v-layout row wrap>
<v-flex v-for="part in deck_parts" :key="part.heading" xs12 sm6 md4> <v-flex v-for="part in deck.parts()" :key="part.heading" xs12 sm6 md4>
<v-card> <v-card>
<v-card-title>{{ part.count }} {{ part.heading }}</v-card-title> <v-card-title>{{ part.count }} {{ part.heading }}</v-card-title>
<v-list dense subheader> <v-list dense subheader>
@ -30,17 +30,20 @@
</v-btn> </v-btn>
</v-layout> </v-layout>
</v-container> </v-container>
<DeckEditor <DeckEditor
:visible="editing" :visible="editing"
:id="deck.id" :deck="deck"
:value="deck_list"
@close="editing = false" @close="editing = false"
@change="$emit('change')" @change="change_deck"
/> />
</v-expansion-panel-content> </v-expansion-panel-content>
</template> </template>
<script> <script>
import * as Cookies from 'js-cookie'
import axios from '@/plugins/axios'
import Card from './Card.vue' import Card from './Card.vue'
import DeckEditor from './forms/DeckEditor.vue' import DeckEditor from './forms/DeckEditor.vue'
@ -61,60 +64,24 @@ export default {
}), }),
computed: { computed: {
deck_parts() { session: () => Cookies.get('session')
let retval = ['Forwards', 'Backups', 'Summons, Monsters & more'].map( },
item => ({
heading: item, methods: {
cards: [], change_deck(new_deck) {
count: 0 axios
.post('/decks/modify', {
session: this.session,
deckID: this.deck.id,
deckCards: new_deck
})
.then(response => {
if (response.data.success) {
this.$emit('change')
} else {
this.editing = true
}
}) })
)
this.deck.cards.forEach(card => {
let target
switch (card.dbentry.type) {
case 'Forward':
target = 0
break
case 'Backup':
target = 1
break
default:
target = 2
break
}
retval[target].cards.push(card)
retval[target].count += card.count
})
retval.forEach(part =>
part.cards.sort(
(card_l, card_r) => card_l.dbentry.cost - card_r.dbentry.cost
)
)
return retval
},
deck_list() {
let lines = []
// begin with deck name and note
lines.push('Deck Name: ' + this.deck.name)
lines.push(this.deck.note)
lines.push('')
// list each deck part
this.deck_parts.forEach(part => {
lines.push(`${part.heading} (${part.count}):`)
part.cards.forEach(card =>
lines.push(`${card.count}x ${card.serial} "${card.dbentry.name}"`)
)
lines.push('')
})
return lines.join('\n')
} }
} }
} }

View file

@ -1,26 +1,29 @@
<template> <template>
<v-expansion-panel v-if="linked_decks"> <v-expansion-panel>
<Deck <Deck
v-for="deck in linked_decks" v-for="deck in linked"
:deck="deck" :deck="deck"
:key="deck.id" :key="deck.id"
@change="refresh_decks" @change="refresh_decks"
/> />
<NewDeck />
</v-expansion-panel> </v-expansion-panel>
</template> </template>
<script> <script>
import * as Cookies from 'js-cookie' import * as Cookies from 'js-cookie'
import axios from '@/plugins/axios' import axios from '@/plugins/axios'
import CardsDB from '@/plugins/ffdecks' import DeckJS from '@/classes/Deck'
import Deck from './Deck.vue' import Deck from './Deck.vue'
import NewDeck from './NewDeck.vue'
export default { export default {
name: 'DeckList', name: 'DeckList',
components: { components: {
Deck Deck,
NewDeck
}, },
asyncComputed: { asyncComputed: {
@ -43,29 +46,15 @@ export default {
computed: { computed: {
session: () => Cookies.get('session'), session: () => Cookies.get('session'),
linked_decks() { linked() {
let result = [] let result = []
for (let i = 0; i < this.decks.length; i++) { for (let plainDeck of this.decks) {
let elem = this.decks[i] let deck = new DeckJS()
let cards = [] deck.from_plainObject(plainDeck)
deck.populate()
for (let j = 0; j < elem.content.cards.length; j++) { result.push(deck)
let card = elem.content.cards[j]
cards.push({
count: card.count,
serial: card.serial,
dbentry: CardsDB[card.serial]
})
}
result.push({
id: elem.id,
name: elem.content.name,
note: elem.content.note,
cards: cards
})
} }
return result return result

View file

@ -0,0 +1,25 @@
<template>
<v-expansion-panel-content>
<template v-slot:header>
<span class="subheading">New Deck</span>
</template>
<v-container>
<v-card flat>
<!-- <DeckEditor :visible="true" value="" @close="" @change="" /> -->
</v-card>
</v-container>
</v-expansion-panel-content>
</template>
<script>
import DeckEditor from './forms/DeckEditor.vue'
export default {
name: 'NewDeck',
components: {
DeckEditor
}
}
</script>

View file

@ -11,12 +11,11 @@
<v-textarea <v-textarea
label="Edit Deck" label="Edit Deck"
:value="value" v-model="new_decklist"
rows="35" rows="35"
hint="Change card counts and/or serial numbers. Names will be updated accordingly!" hint="Change card counts and/or serial numbers. Names will be updated accordingly!"
style="font-family: monospace" style="font-family: monospace"
@input="check.checked = false" @input="check.checked = false"
@change="new_decklist = $event"
> >
</v-textarea> </v-textarea>
@ -47,16 +46,13 @@
</template> </template>
<script> <script>
import * as Cookies from 'js-cookie'
import axios from '@/plugins/axios'
import Deck from '@/classes/Deck' import Deck from '@/classes/Deck'
export default { export default {
name: 'DeckEditor', name: 'DeckEditor',
props: { props: {
id: Number, deck: Object,
value: String,
visible: Boolean visible: Boolean
}, },
@ -69,23 +65,24 @@ export default {
new_decklist: null new_decklist: null
}), }),
computed: { created() {
session: () => Cookies.get('session'), this.new_decklist = this.deck.deckList()
},
computed: {
new_deck() { new_deck() {
let deck let deck = new Deck()
try { deck.from_deckList(this.new_decklist)
deck = new Deck(this.new_decklist) deck.populate()
} catch (e) {
deck = new Deck(this.value) this.new_decklist = deck.deckList()
}
return deck return deck
} }
}, },
methods: { methods: {
close() { close() {
this.checked = false this.check.checked = false
this.$emit('close') this.$emit('close')
}, },
@ -104,18 +101,8 @@ export default {
}, },
save_deck() { save_deck() {
axios this.$emit('change', this.new_deck.plainObject())
.post('/decks/modify', { this.close()
session: this.session,
deckID: this.id,
deckCards: this.new_deck.plainObject()
})
.then(response => {
if (response.data.success) {
this.$emit('change')
this.close()
}
})
} }
} }
} }