From f0a80e3beb84795574ecb4b5b8ea2da5141c9511 Mon Sep 17 00:00:00 2001 From: sharpshark28 Date: Sat, 24 Jun 2017 14:08:35 -0500 Subject: [PATCH] [FIX]: Spells now identified by IDs, fixes #17 --- .eslintignore | 1 + .gitignore | 2 + build/generate_index.js | 38 +++++++++++++++++++ build/script.clean.js | 2 +- build/script.dev.js | 1 + package.json | 1 + src/App.vue | 8 +--- src/components/Myspells.vue | 2 +- src/components/Spell.vue | 12 ++++-- src/components/Spellitem.vue | 6 +-- src/components/Spelllist.vue | 4 +- src/router.js | 2 +- .../dnd5e.json => spells_original.json} | 0 src/store.js | 16 ++------ src/utils.js | 2 +- 15 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 build/generate_index.js rename src/{statics/dnd5e.json => spells_original.json} (100%) diff --git a/.eslintignore b/.eslintignore index 2fece73..f1c92da 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,4 @@ build/*.js config/*.js dist/*.js +tmp/*.js diff --git a/.gitignore b/.gitignore index 4a44985..23e3789 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .DS_Store +tmp/ node_modules/ dist/ +src/statics/spells.json npm-debug.log npm-debug.log.* selenium-debug.log diff --git a/build/generate_index.js b/build/generate_index.js new file mode 100644 index 0000000..52be9ab --- /dev/null +++ b/build/generate_index.js @@ -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)};`) diff --git a/build/script.clean.js b/build/script.clean.js index c8158b3..d45b1e9 100644 --- a/build/script.clean.js +++ b/build/script.clean.js @@ -2,5 +2,5 @@ var shell = require('shelljs'), path = require('path') -shell.rm('-rf', path.resolve(__dirname, '../dist')) +shell.rm('-rf', path.resolve(__dirname, '../dist ../tmp')) console.log(' Cleaned build artifacts.\n') diff --git a/build/script.dev.js b/build/script.dev.js index 058288f..69f0398 100644 --- a/build/script.dev.js +++ b/build/script.dev.js @@ -1,5 +1,6 @@ process.env.NODE_ENV = 'development' +require('./generate_index') require('colors') var diff --git a/package.json b/package.json index 96013da..3841688 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "extract-text-webpack-plugin": "^2.0.0-beta.4", "file-loader": "^0.11.1", "friendly-errors-webpack-plugin": "^1.1.3", + "fs": "0.0.1-security", "html-webpack-plugin": "^2.8.1", "http-proxy-middleware": "^0.17.0", "json-loader": "^0.5.4", diff --git a/src/App.vue b/src/App.vue index 294c10c..d65ad8a 100644 --- a/src/App.vue +++ b/src/App.vue @@ -37,10 +37,6 @@ function fetchSuccess (data) { loaded: true } }) - dispatch({ - type: 'SPELLS_CREATE_INDEX', - data - }) } function fetchFailure (reason) { @@ -54,7 +50,7 @@ function fetchFailure (reason) { } function fetchSpells () { - fetch('./statics/dnd5e.json') + fetch('./statics/spells.json') .then(response => response.json()) .then(fetchSuccess) .catch(fetchFailure) @@ -71,8 +67,6 @@ export default { } if (!this.state.spells.loaded) { - Loading.show() - fetchSpells() } } diff --git a/src/components/Myspells.vue b/src/components/Myspells.vue index fe2a0f0..626f9a7 100644 --- a/src/components/Myspells.vue +++ b/src/components/Myspells.vue @@ -35,7 +35,7 @@ export default { return this.state.chosen.map(chosen => { return this.state.indexedSpells.find(spell => { - return spell.name === chosen + return spell.id === chosen }) }) } diff --git a/src/components/Spell.vue b/src/components/Spell.vue index 0adbb04..cfd034f 100644 --- a/src/components/Spell.vue +++ b/src/components/Spell.vue @@ -82,6 +82,7 @@