From 2fcf3557b43af99a425d2fc4bb7065d8e1362158 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 23 Oct 2017 00:10:38 -0400 Subject: [PATCH] [add] lokijs in-memory database --- chaotic-portal.sublime-project | 11 +-- package.json | 22 +++--- src/Base.js | 3 +- src/components/ExampleComponent.js | 3 +- src/components/ExampleTwoDeepComponent.js | 3 +- src/components/PageNotFound.js | 3 +- src/components/SpreadsheetData.js | 79 +++++++++------------ src/components/UnderConstruction.js | 3 +- src/components/database/cards.js | 35 ++++++++++ src/components/database/portal.js | 85 +++++++++++++++++++++++ webpack.config.babel.js | 4 ++ 11 files changed, 187 insertions(+), 64 deletions(-) create mode 100644 src/components/database/cards.js create mode 100644 src/components/database/portal.js diff --git a/chaotic-portal.sublime-project b/chaotic-portal.sublime-project index bc56dfd..6c34aae 100644 --- a/chaotic-portal.sublime-project +++ b/chaotic-portal.sublime-project @@ -1,10 +1,11 @@ { "folders": - [ - { - "path": "." - } - ], + [ + { + "path": ".", + "folder_exclude_patterns": ["build", "node_modules"] + } + ], "syntax_override": { "\\.js$": diff --git a/package.json b/package.json index e9d27a5..9cbaffe 100644 --- a/package.json +++ b/package.json @@ -13,24 +13,28 @@ "author": "Danude Sandstorm", "license": "MIT", "dependencies": { - "react": "^15.4.1", - "react-dom": "^15.4.1", - "react-interactive": "^0.5.1", - "react-router": "^3.0.0", + "mobx": "^3.3.1", + "mobx-react": "^4.3.3", + "prop-types": "^15.6.0", + "react": "^16.0.0", + "react-dom": "^16.0.0", + "react-interactive": "^0.8.1", + "react-router": "^3.2.0", + "react-router-dom": "^4.2.2", "whatwg-fetch": "^2.0.3" }, "devDependencies": { "babel-core": "^6.21.0", - "babel-eslint": "^7.1.1", + "babel-eslint": "^8.0.0", "babel-loader": "^6.2.10", "babel-preset-es2015": "^6.18.0", "babel-preset-react": "^6.16.0", "babel-preset-stage-1": "^6.16.0", - "eslint": "^3.12.2", - "eslint-config-airbnb": "^13.0.0", + "eslint": "^4.0.0", + "eslint-config-airbnb": "^16.0.0", "eslint-plugin-import": "^2.2.0", - "eslint-plugin-jsx-a11y": "^2.2.3", - "eslint-plugin-react": "^6.8.0", + "eslint-plugin-jsx-a11y": "^6.0.0", + "eslint-plugin-react": "^7.0.0", "webpack": "^1.14.0", "webpack-dev-server": "^1.16.2" } diff --git a/src/Base.js b/src/Base.js index 4bafd28..2e06e36 100644 --- a/src/Base.js +++ b/src/Base.js @@ -1,4 +1,5 @@ -import React, { PropTypes } from 'react'; +import React from 'react'; +import PropTypes from 'prop-types'; import Interactive from 'react-interactive'; import { Link } from 'react-router'; import s from './styles/app.style'; diff --git a/src/components/ExampleComponent.js b/src/components/ExampleComponent.js index f08ddda..042119a 100644 --- a/src/components/ExampleComponent.js +++ b/src/components/ExampleComponent.js @@ -1,4 +1,5 @@ -import React, { PropTypes } from 'react'; +import React from 'react'; +import PropTypes from 'prop-types'; import Interactive from 'react-interactive'; import { Link } from 'react-router'; import s from '../styles/exampleComponent.style'; diff --git a/src/components/ExampleTwoDeepComponent.js b/src/components/ExampleTwoDeepComponent.js index 0646d5c..379cf17 100644 --- a/src/components/ExampleTwoDeepComponent.js +++ b/src/components/ExampleTwoDeepComponent.js @@ -1,4 +1,5 @@ -import React, { PropTypes } from 'react'; +import React from 'react'; +import PropTypes from 'prop-types'; import Interactive from 'react-interactive'; import { Link } from 'react-router'; import s from '../styles/exampleTwoDeepComponent.style'; diff --git a/src/components/PageNotFound.js b/src/components/PageNotFound.js index 8a4d3a2..5d13f85 100644 --- a/src/components/PageNotFound.js +++ b/src/components/PageNotFound.js @@ -1,4 +1,5 @@ -import React, { PropTypes } from 'react'; +import React from 'react'; +import PropTypes from 'prop-types'; import s from '../styles/pageNotFound.style'; const propTypes = { diff --git a/src/components/SpreadsheetData.js b/src/components/SpreadsheetData.js index 0fa569d..1688776 100644 --- a/src/components/SpreadsheetData.js +++ b/src/components/SpreadsheetData.js @@ -1,39 +1,44 @@ -import React from 'react'; import 'whatwg-fetch'; +import CardsDB from './database/cards'; +import PortalDB from './database/portal'; -class URLS { +class API { static base_url = "https://spreadsheets.google.com/feeds/list/"; static data_format = "/od6/public/values?alt=json"; // + "/od6/public/basic?alt=json"; // Alternate data format static base_spreadsheet = "1cUNmwV693zl2zqbH_IG4Wz8o9Va_sOHe7pAZF6M59Es"; get base_image() { return "https://drive.google.com/uc?id="; } - static path(spreadsheetID) { - return URLS.base_url + spreadsheetID + URLS.data_format; - } - - constructor() { - // This sets up urls - this.urls = {}; - var self = this; - this.getSpreadsheet(URLS.path(URLS.base_spreadsheet), function(data) { - data.forEach(function(d) { - if (!self.urls[d.gsx$type.$t]) self.urls[d.gsx$type.$t] = {}; - self.urls[d.gsx$type.$t][d.gsx$subtype.$t] = d.gsx$url.$t; - }); - }); - } - // Singleton instance = null; static getInstance() { - if (!URLS.instance) { URLS.instance = new URLS(); } - return URLS.instance; + if (!API.instance) { API.instance = new API(); } + return API.instance; + } + + static path(spreadsheetID) { + return API.base_url + spreadsheetID + API.data_format; + } + + constructor() { + var self = this; + + // This sets up urls and kicks off db + this.urls = {}; + this.getSpreadsheet(API.path(API.base_spreadsheet), function(data) { + if (data == null) return; + data.forEach(function(d) { + if (!self.urls[d.gsx$type.$t]) self.urls[d.gsx$type.$t] = {}; + self.urls[d.gsx$type.$t][d.gsx$subtype.$t] = API.path(d.gsx$url.$t); + }); + self.setupDB(); + }); } getSpreadsheet(spreadsheet, callback) { fetch(spreadsheet) .then(function(response) { + // console.log(response); return response.json(); }).then(function(json) { return callback(json.feed.entry); @@ -43,31 +48,15 @@ class URLS { }); } - /* Creatures */ - /* TODO remove legacy getters when introducing state management*/ - get Creatures_Card_Data() { - return URLS.path("1fUFYhG1NLLkSTzrdbevm6ZMKNP6xLiKUZvM1sY10pVI"); + setupDB() { + try { + this.portal = new PortalDB(this); + this.cards = new CardsDB(this); + } + catch (err) { + console.log('setting up database failed', err); + } } - - get Creatures() { - return { - 'Overworld': URLS.path("1Z4_MmlV7uE34nLzrcxslqQKRwL4OBXNA15s7G8eteXU"), - 'Underworld': URLS.path("1c_XAxQsDIWVdzUxWl5t-K_mQumNjX-yrg0X-0NsZVts"), - 'Mipedian': URLS.path("1P4FKASfnhR46j2bqm89T9xhI1Yyyy-jiZ1CkRglSy2k"), - 'Danian': URLS.path("1-Lz-itwOobEvqr8HSLFFwg3JFkT64NbwyGFEPfj9rxU") - }; - } - - get Mugic() { - return { - 'Overworld': URLS.path("1KsVX5SkygwPP6I8yd6xgcN8gB746o_FTh6SK1TvAcbU"), - 'Underworld': URLS.path("1F7FHlob52cb_7J65cddM3The7w-kFiStBRd5wm4JRlA"), - 'Mipedian': URLS.path("1QDsiSUuBV_4Jn6mvl96EGU06XEoVqILGN4suYKvh9CA"), - 'Danian': URLS.path("1tEuwPGixJH2A03YtYL6Ar-MSFvtfrlaveT98GwJhw1g"), - 'Generic': URLS.path("1M9iAbpYAHq_ppwm0PZKUvZGfu-zbrK1CZMY16m4Plf8") - }; - } - } -export default URLS.getInstance(); +export default API.getInstance(); diff --git a/src/components/UnderConstruction.js b/src/components/UnderConstruction.js index f774de7..e154f1f 100644 --- a/src/components/UnderConstruction.js +++ b/src/components/UnderConstruction.js @@ -1,4 +1,5 @@ -import React, { PropTypes } from 'react'; +import React from 'react'; +import PropTypes from 'prop-types'; import s from '../styles/pageNotFound.style'; const propTypes = { diff --git a/src/components/database/cards.js b/src/components/database/cards.js new file mode 100644 index 0000000..42425ae --- /dev/null +++ b/src/components/database/cards.js @@ -0,0 +1,35 @@ +export default class CardsDB { + constructor(API) { + // this.api = API; + // this.attacks = db.addCollection('attacks'); + // this.battlegear = db.addCollection('battlegear'); + // this.creatures = db.addCollection('creatures'); + // this.locations = db.addCollection('locations'); + // this.mugic = db.addCollection('mugic'); + // this.db = db; + } + + setupCreatures(spreadsheet) { + this.getSpreadsheet(spreadsheet, function(json) { + + }); + } + + setupMugic(spreadsheet) { + this.getSpreadsheet(spreadsheet, function(json) { + + }); + } + + setupBattleGear(spreadsheet) { + this.getSpreadsheet(spreadsheet, function(json) { + + }); + } + + setupLocations(spreadsheet) { + this.getSpreadsheet(spreadsheet, function(json) { + + }); + } +} diff --git a/src/components/database/portal.js b/src/components/database/portal.js new file mode 100644 index 0000000..e6405aa --- /dev/null +++ b/src/components/database/portal.js @@ -0,0 +1,85 @@ +import loki from 'lokijs'; + +export default class PortalDB { + constructor(API) { + this.api = API; + this.setupDB(); + this.setupCreatures("Overworld"); + } + + setupDB() { + var self = this; + // let db = new loki("chaotic_portal.db", { autosave: true, autoload: true, autoloadCallback: databaseInitialize, autosaveInterval: 4000, persistenceMethod: 'localStorage' }); + let db = new loki("chaotic_portal.db"); // ignoring persistence for now + this.attacks = db.addCollection('attacks'); + this.battlegear = db.addCollection('battlegear'); + this.creatures = db.addCollection('creatures'); + this.locations = db.addCollection('locations'); + this.mugic = db.addCollection('mugic'); + this.db = db; + + // function databaseInitialize() { + // var entries; + // if ((entries = db.getCollection("attacks")) === null) + // entries = db.addCollection("attacks"); + // self.attacks = entries; + + // if ((entries = db.getCollection("battlegear")) === null) + // entries = db.addCollection("battlegear"); + // self.battlegear = entries; + + // console.log(db.getCollection("creatures")); + // if ((entries = db.getCollection("creatures")) === null) + // entries = db.addCollection("creatures"); + // self.creatures = db.addCollection('creatures'); + + // if ((entries = db.getCollection("locations")) === null) + // entries = db.addCollection("locations"); + // self.locations = entries + + // if ((entries = db.getCollection("mugic")) === null) + // entries = db.addCollection("mugic"); + // self.mugic = entries; + // } + } + + setup(spreadsheet, callback) { + this.api.getSpreadsheet(spreadsheet, function(data) { + data.map(function(item) { + // delete item.category; + // delete item.id; + // delete item.updated; + // delete item.content; + // delete item.link; + // delete item.title; + item.name = item.gsx$name.$t; + return item; + }); + callback(data); + }); + } + + setupAttacks() { + + } + + setupBattleGear() { + + } + + setupCreatures(tribe) { + // console.log(this); + var self = this; + this.setup(this.api.urls.Creatures[tribe], function(data) { + self.creatures.insert(data); + }); + } + + setupLocations() { + + } + + setupMugic(tribe) { + + } +} diff --git a/webpack.config.babel.js b/webpack.config.babel.js index 578dfe0..0b45362 100644 --- a/webpack.config.babel.js +++ b/webpack.config.babel.js @@ -19,6 +19,10 @@ export default { extensions: ['', '.js', '.jsx'], }, + node: { + fs: 'empty' + }, + plugins: process.argv.indexOf('-p') === -1 ? null : [ new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production'),