1
0
Fork 0

Merge pull request #18 from sharpshark28/enhancement/spellids

[FIX]: Spells now identified by IDs, fixes #17
This commit is contained in:
Joe L Wroten 2017-06-24 14:09:41 -05:00 committed by GitHub
commit 17f669f2d2
15 changed files with 65 additions and 32 deletions

View file

@ -1,3 +1,4 @@
build/*.js build/*.js
config/*.js config/*.js
dist/*.js dist/*.js
tmp/*.js

2
.gitignore vendored
View file

@ -1,6 +1,8 @@
.DS_Store .DS_Store
tmp/
node_modules/ node_modules/
dist/ dist/
src/statics/spells.json
npm-debug.log npm-debug.log
npm-debug.log.* npm-debug.log.*
selenium-debug.log selenium-debug.log

38
build/generate_index.js Normal file
View file

@ -0,0 +1,38 @@
'use strict'
const fs = require('fs')
const spells = require('../src/spells_original.json')
const hashCode = function (str) {
let hash = 0, i, chr
if (str.length === 0) return hash
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i)
hash = ((hash << 5) - hash) + chr
hash |= 0
}
return hash
}
let spellsWithIDs = spells.map(spell => {
spell.id = hashCode(spell.name).toString()
return spell
})
let indexedSpells = spellsWithIDs.map(spell => {
return {
id: spell.id,
name: spell.name,
classes: spell.classes,
level: spell.level
}
})
let dirs = ['tmp']
dirs.forEach(dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
})
fs.writeFileSync('./src/statics/spells.json', JSON.stringify(spellsWithIDs))
fs.writeFileSync('./tmp/spells_index.js', `export default ${JSON.stringify(indexedSpells)};`)

View file

@ -2,5 +2,5 @@ var
shell = require('shelljs'), shell = require('shelljs'),
path = require('path') path = require('path')
shell.rm('-rf', path.resolve(__dirname, '../dist')) shell.rm('-rf', path.resolve(__dirname, '../dist ../tmp'))
console.log(' Cleaned build artifacts.\n') console.log(' Cleaned build artifacts.\n')

View file

@ -1,5 +1,6 @@
process.env.NODE_ENV = 'development' process.env.NODE_ENV = 'development'
require('./generate_index')
require('colors') require('colors')
var var

View file

@ -47,6 +47,7 @@
"extract-text-webpack-plugin": "^2.0.0-beta.4", "extract-text-webpack-plugin": "^2.0.0-beta.4",
"file-loader": "^0.11.1", "file-loader": "^0.11.1",
"friendly-errors-webpack-plugin": "^1.1.3", "friendly-errors-webpack-plugin": "^1.1.3",
"fs": "0.0.1-security",
"html-webpack-plugin": "^2.8.1", "html-webpack-plugin": "^2.8.1",
"http-proxy-middleware": "^0.17.0", "http-proxy-middleware": "^0.17.0",
"json-loader": "^0.5.4", "json-loader": "^0.5.4",

View file

@ -37,10 +37,6 @@ function fetchSuccess (data) {
loaded: true loaded: true
} }
}) })
dispatch({
type: 'SPELLS_CREATE_INDEX',
data
})
} }
function fetchFailure (reason) { function fetchFailure (reason) {
@ -54,7 +50,7 @@ function fetchFailure (reason) {
} }
function fetchSpells () { function fetchSpells () {
fetch('./statics/dnd5e.json') fetch('./statics/spells.json')
.then(response => response.json()) .then(response => response.json())
.then(fetchSuccess) .then(fetchSuccess)
.catch(fetchFailure) .catch(fetchFailure)
@ -71,8 +67,6 @@ export default {
} }
if (!this.state.spells.loaded) { if (!this.state.spells.loaded) {
Loading.show()
fetchSpells() fetchSpells()
} }
} }

View file

@ -35,7 +35,7 @@ export default {
return this.state.chosen.map(chosen => { return this.state.chosen.map(chosen => {
return this.state.indexedSpells.find(spell => { return this.state.indexedSpells.find(spell => {
return spell.name === chosen return spell.id === chosen
}) })
}) })
} }

View file

@ -82,6 +82,7 @@
</template> </template>
<script> <script>
import { Loading } from 'quasar'
import { dispatch, state } from '../store' import { dispatch, state } from '../store'
import { capitalize } from '../utils' import { capitalize } from '../utils'
import Marked from 'marked' import Marked from 'marked'
@ -91,14 +92,17 @@ export default {
return { state } return { state }
}, },
mounted () { mounted () {
this.state.lastSpell = this.spell.name if (this.state.spells.loaded === false) {
Loading.show()
}
this.state.lastSpell = this.spell.id
}, },
computed: { computed: {
checked () { checked () {
return this.state.chosen.indexOf(this.spell.name) >= 0 return this.state.chosen.indexOf(this.spell.id) >= 0
}, },
spell () { spell () {
return this.state.spells.data.find(spell => spell.name === this.$route.params.name) return this.state.spells.data.find(spell => spell.id === this.$route.params.id)
}, },
level () { level () {
return this.spell.level.toLowerCase() === 'cantrip' ? 'C' : this.spell.level return this.spell.level.toLowerCase() === 'cantrip' ? 'C' : this.spell.level
@ -124,7 +128,7 @@ export default {
type: 'CHANGE_CHOSEN', type: 'CHANGE_CHOSEN',
data: { data: {
want, want,
name: this.spell.name id: this.spell.id
} }
}) })
} }

View file

@ -40,7 +40,7 @@ export default {
return this.spell.classes.map(cla => capitalize(cla)).join(', ') return this.spell.classes.map(cla => capitalize(cla)).join(', ')
}, },
checked () { checked () {
return this.state.chosen.indexOf(this.spell.name) >= 0 return this.state.chosen.indexOf(this.spell.id) >= 0
} }
}, },
data () { data () {
@ -51,14 +51,14 @@ export default {
], ],
methods: { methods: {
openSpell (event) { openSpell (event) {
this.$router.push('/spell/' + this.spell.name) this.$router.push('/spell/' + this.spell.id)
}, },
toggle () { toggle () {
dispatch({ dispatch({
type: 'CHANGE_CHOSEN', type: 'CHANGE_CHOSEN',
data: { data: {
want: !this.checked, want: !this.checked,
name: this.spell.name id: this.spell.id
} }
}) })
} }

View file

@ -9,8 +9,8 @@
is="spell-item" is="spell-item"
class="item two-lines item-link" class="item two-lines item-link"
v-for="spell in filteredSpells" v-for="spell in filteredSpells"
:key="spell.name" :key="spell.id"
:ref="spell.name" :ref="spell.id"
:spell="spell" :spell="spell"
> >
</label> </label>

View file

@ -23,7 +23,7 @@ export default new VueRouter({
routes: [ routes: [
{ path: '/', component: load('Index') }, // Default { path: '/', component: load('Index') }, // Default
{ path: '/my', component: load('Myspells') }, { path: '/my', component: load('Myspells') },
{ path: '/spell/:name', component: load('Spell') }, { path: '/spell/:id', component: load('Spell') },
{ path: '/about', component: load('About') }, { path: '/about', component: load('About') },
{ path: '*', component: load('Error404') } // Not found { path: '*', component: load('Error404') } // Not found
] ]

View file

@ -1,11 +1,12 @@
import { LocalStorage } from 'quasar' import { LocalStorage } from 'quasar'
import indexedSpells from '../tmp/spells_index'
export let state = { export let state = {
indexedSpells,
spells: { spells: {
loaded: false, loaded: false,
data: [] data: []
}, },
indexedSpells: [],
chosen: [], chosen: [],
search: '', search: '',
sortBy: 'name', sortBy: 'name',
@ -18,15 +19,6 @@ export function dispatch (action) {
case 'SPELLS_RESOLVED': case 'SPELLS_RESOLVED':
state.spells = action.data state.spells = action.data
break break
case 'SPELLS_CREATE_INDEX':
state.indexedSpells = action.data.map(spell => {
return {
name: spell.name,
classes: spell.classes,
level: spell.level
}
})
break
case 'LOAD_LOCAL_CHOSEN' : case 'LOAD_LOCAL_CHOSEN' :
state.chosen = LocalStorage.get.item('chosen').split(',') state.chosen = LocalStorage.get.item('chosen').split(',')
break break
@ -36,10 +28,10 @@ export function dispatch (action) {
break break
case 'CHANGE_CHOSEN': case 'CHANGE_CHOSEN':
if (action.data.want) { if (action.data.want) {
state.chosen.push(action.data.name) state.chosen.push(action.data.id)
} }
else { else {
let index = state.chosen.indexOf(action.data.name) let index = state.chosen.indexOf(action.data.id)
if (index >= 0) state.chosen.splice(index, 1) if (index >= 0) state.chosen.splice(index, 1)
} }

View file

@ -1 +1 @@
export let capitalize = str => str.charAt(0).toUpperCase() + str.slice(1) export const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)