From 1e4072dae6d3aafe2c766a1260f8e2d156aa2fd0 Mon Sep 17 00:00:00 2001 From: BinarySandia04 Date: Sat, 19 Oct 2024 14:20:21 +0200 Subject: [PATCH] Datagen loads files, remains register to custom models to mongodb --- .gitignore | 1 + backend/services/api.js | 12 ++++- backend/services/datagen.js | 53 +++++++++++++++++++ backend/services/plugins.js | 10 ++-- client/src/services/Api.js | 5 ++ plugins/dnd-5e/backend/main.js | 7 +++ .../dnd-5e-base/data/effect/test_effect.json | 3 ++ .../item/consumables/test_consumable.json | 3 ++ .../data/item/containers/test_container.json | 3 ++ .../data/item/equipment/test_equipment.json | 3 ++ .../data/item/features/test_feature.json | 3 ++ .../data/item/spells/test_spell.json | 3 ++ .../data/item/tools/test_tool.json | 3 ++ .../data/item/weapons/test_weapon.json | 3 ++ .../data/monster/test_monster.json | 3 ++ .../data/progressable/classes/test_class.json | 3 ++ .../data/progressable/races/test_race.json | 3 ++ .../dnd-5e-base/data/table/test_table.json | 3 ++ .../datagen/{ => dnd-5e-base}/datagen.json | 1 + .../{ => dnd-5e-base}/locales/en-US.json | 0 prebuild.js | 11 ++++ 21 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 backend/services/datagen.js create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/effect/test_effect.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/item/consumables/test_consumable.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/item/containers/test_container.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/item/equipment/test_equipment.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/item/features/test_feature.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/item/spells/test_spell.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/item/tools/test_tool.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/item/weapons/test_weapon.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/monster/test_monster.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/progressable/classes/test_class.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/progressable/races/test_race.json create mode 100644 plugins/dnd-5e/datagen/dnd-5e-base/data/table/test_table.json rename plugins/dnd-5e/datagen/{ => dnd-5e-base}/datagen.json (77%) rename plugins/dnd-5e/datagen/{ => dnd-5e-base}/locales/en-US.json (100%) diff --git a/.gitignore b/.gitignore index c5fdbdfc..a242b8c9 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ client/public/plugins/ # Backend backend/plugins/ +backend/datagen/ # Docs documentation/docs/.vuepress/.cache diff --git a/backend/services/api.js b/backend/services/api.js index 4a783fbb..698aae4b 100644 --- a/backend/services/api.js +++ b/backend/services/api.js @@ -13,18 +13,20 @@ class BackendApi { #_router; #_expressRouter; #_internalSocket; + #_datagenRegisrty; #_socket; /** * This object is already created for you * @param {plugin} Plugin instance */ - constructor(plugin, router, internalSocket){ + constructor(plugin, router, internalSocket, datagenRegistry){ this.#_plugin = plugin; this.#_expressRouter = router; this.#_internalSocket = internalSocket; this.#_socket = new BackendSocket(`plugins/${plugin.package}`, internalSocket); this.#_router = new BackendRouter(`${plugin.package}`, this.#_expressRouter); + this.#_datagenRegisrty = datagenRegistry; } /** @@ -63,6 +65,10 @@ class BackendApi { createModule(id){ return new BackendModule(this.#_plugin, id, this.#_expressRouter, this.#_internalSocket); } + + registerDatagen(model){ + this.#_datagenRegisrty.modelNames.push(model.mongoName); + } }; /** @@ -289,6 +295,10 @@ class BackendModel { get mongoSchema() { return this.#_mongoSchema; } + + get mongoName(){ + return `${this.#_prefix}/${this.#_name}` + } }; class BackendSocket { diff --git a/backend/services/datagen.js b/backend/services/datagen.js new file mode 100644 index 00000000..4cb4c590 --- /dev/null +++ b/backend/services/datagen.js @@ -0,0 +1,53 @@ +const fs = require('fs'); +const { default: mongoose } = require('mongoose'); +const path = require('path'); + +const basePath = path.resolve(__dirname, '../') + +async function datagenTask(models) { + console.log("Inicializing datagen task"); + + const datagenPluginFolders = fs.readdirSync(path.resolve(basePath + '/datagen')); + datagenPluginFolders.forEach(datagenPluginFolder => { + const datagenFolders = fs.readdirSync(path.resolve(basePath + '/datagen/' + datagenPluginFolder)); + datagenFolders.forEach(datagenFolder => { + console.log(datagenFolder); + let folder = basePath + '/datagen/' + datagenPluginFolder + "/" + datagenFolder; + const datagenInfo = JSON.parse(fs.readFileSync( + path.resolve(folder + "/datagen.json") + )); + + resolveDatagen(models, folder, datagenInfo); + }) + }); + + console.log("Ended datagen task"); +} + +async function resolveDatagen(models, path, info){ + // Do locale translation with info somewhere...? + const modelNames = models[info.package].modelNames; + modelNames.forEach(modelName => { + if(Object.keys(mongoose.models).includes(modelName)){ + let modelLastName = modelName.split('/').pop(); + let modelDataPath = path + "/data/" + modelLastName; + if(fs.existsSync(modelDataPath)){ + const jsonFiles = fs.readdirSync(path + "/data/" + modelLastName, {recursive: true}); + let modelPath = path + "/data/" + modelLastName; + jsonFiles.forEach(file => { + if(fs.lstatSync(modelPath + "/" + file).isFile()){ + appendDatagen(modelPath + "/" + file, modelName, info); + } + }) + } + } + }) +} + +async function appendDatagen(file, modelName, info){ + // "Appending " + file + " to model " + modelName + " from " + info.id + " for package " + info.package +} + +module.exports = { + datagenTask +} \ No newline at end of file diff --git a/backend/services/plugins.js b/backend/services/plugins.js index 3b9c7cf2..a479ba9e 100644 --- a/backend/services/plugins.js +++ b/backend/services/plugins.js @@ -1,18 +1,19 @@ const fs = require('fs'); -const path = require('path') +const path = require('path'); const BackendApi = require('./api').BackendApi const express = require('express'); const { getIo } = require('../io/socket'); +const { datagenTask } = require('./datagen'); const router = express.Router({ mergeParams: true }); const basePath = path.resolve(__dirname, '../') -console.log(basePath) let pluginsInfo = []; let plugins = {}; let internalSocket = {}; +let datagenRegistry = {}; function init(){ console.log("Initializing plugins"); @@ -31,10 +32,13 @@ function init(){ // Execute main Object.keys(plugins).forEach(k => { - let pluginApi = new BackendApi(plugins[k].info, router, internalSocket); + datagenRegistry[k] = {modelNames: []}; + let pluginApi = new BackendApi(plugins[k].info, router, internalSocket, datagenRegistry[k]); plugins[k].payload.Main(pluginApi); }); + datagenTask(datagenRegistry); + console.log(internalSocket); getIo().on('connect', (socket) => { Object.keys(internalSocket).forEach(k => { diff --git a/client/src/services/Api.js b/client/src/services/Api.js index cda5cf39..710b2bca 100644 --- a/client/src/services/Api.js +++ b/client/src/services/Api.js @@ -8,6 +8,7 @@ import * as _Tooltip from "@/services/Tooltip" import * as _Windows from "@/services/Windows" import Server from '@/services/Server'; import { socket } from '@/services/Socket'; +const { t } = useI18n() /** * Class for managing the client api @@ -75,6 +76,10 @@ class ClientApi { _Windows.ClearWindow(id); } + t(key){ + return t(`plugins.${plugin.package}.${key}`); + } + /** * Returns the client router * @type {ClientRouter} diff --git a/plugins/dnd-5e/backend/main.js b/plugins/dnd-5e/backend/main.js index 348fb9ef..c6fb5f4a 100644 --- a/plugins/dnd-5e/backend/main.js +++ b/plugins/dnd-5e/backend/main.js @@ -84,6 +84,13 @@ function Main(api){ dndModule.createModelRoutes(effectsModel); dndModule.createModelRoutes(entityModel); + Api.registerDatagen(itemModel); + Api.registerDatagen(monsterModel); + Api.registerDatagen(actorModel); + Api.registerDatagen(tableModel); + Api.registerDatagen(progressableModel); + Api.registerDatagen(effectsModel); + // Api.socket.on("test", () => console.log("test")); // Api.router.createModelRoutes(itemModel, 'item'); } diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/effect/test_effect.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/effect/test_effect.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/effect/test_effect.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/item/consumables/test_consumable.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/consumables/test_consumable.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/consumables/test_consumable.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/item/containers/test_container.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/containers/test_container.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/containers/test_container.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/item/equipment/test_equipment.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/equipment/test_equipment.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/equipment/test_equipment.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/item/features/test_feature.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/features/test_feature.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/features/test_feature.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/item/spells/test_spell.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/spells/test_spell.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/spells/test_spell.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/item/tools/test_tool.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/tools/test_tool.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/tools/test_tool.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/item/weapons/test_weapon.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/weapons/test_weapon.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/item/weapons/test_weapon.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/monster/test_monster.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/monster/test_monster.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/monster/test_monster.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/progressable/classes/test_class.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/progressable/classes/test_class.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/progressable/classes/test_class.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/progressable/races/test_race.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/progressable/races/test_race.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/progressable/races/test_race.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/dnd-5e-base/data/table/test_table.json b/plugins/dnd-5e/datagen/dnd-5e-base/data/table/test_table.json new file mode 100644 index 00000000..544b7b4d --- /dev/null +++ b/plugins/dnd-5e/datagen/dnd-5e-base/data/table/test_table.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/datagen.json b/plugins/dnd-5e/datagen/dnd-5e-base/datagen.json similarity index 77% rename from plugins/dnd-5e/datagen/datagen.json rename to plugins/dnd-5e/datagen/dnd-5e-base/datagen.json index 6eadf575..df33d593 100644 --- a/plugins/dnd-5e/datagen/datagen.json +++ b/plugins/dnd-5e/datagen/dnd-5e-base/datagen.json @@ -1,5 +1,6 @@ { "package": "dnd-5e", + "id": "dnd-5e-base", "name": "info.name", "desc": "info.description" } \ No newline at end of file diff --git a/plugins/dnd-5e/datagen/locales/en-US.json b/plugins/dnd-5e/datagen/dnd-5e-base/locales/en-US.json similarity index 100% rename from plugins/dnd-5e/datagen/locales/en-US.json rename to plugins/dnd-5e/datagen/dnd-5e-base/locales/en-US.json diff --git a/prebuild.js b/prebuild.js index 999799b2..6feccef4 100755 --- a/prebuild.js +++ b/prebuild.js @@ -88,23 +88,34 @@ for(let i = 0; i < locales.length; i++){ console.log("Updated Locales") + // #endregion // #region plugins for(let j = 0; j < plugins.length; j++){ + // Cient scripts and views if(fs.existsSync(`./plugins/${plugins[j]}/client/`)){ fs.cpSync(`./plugins/${plugins[j]}/client/`, `./client/plugins/${plugins[j]}`, {recursive: true}); fs.copyFileSync(`./plugins/${plugins[j]}/plugin.json`, `./client/plugins/${plugins[j]}/plugin.json`); } + // Backend scripts if(fs.existsSync(`./plugins/${plugins[j]}/backend/`)){ fs.cpSync(`./plugins/${plugins[j]}/backend/`, `./backend/plugins/${plugins[j]}`, {recursive: true}); fs.copyFileSync(`./plugins/${plugins[j]}/plugin.json`, `./backend/plugins/${plugins[j]}/plugin.json`); } + // Public folder if(fs.existsSync(`./plugins/${plugins[j]}/public/`)){ fs.cpSync(`./plugins/${plugins[j]}/public/`, `./client/public/plugins/${plugins[j]}`, {recursive: true}); } + + // Datagen + if(fs.existsSync(`./plugins/${plugins[j]}/datagen`)){ + fs.readdirSync(`./plugins/${plugins[j]}/datagen`).forEach(d => { + fs.cpSync(`./plugins/${plugins[j]}/datagen/${d}`, `./backend/datagen/${plugins[j]}/${d}`, {recursive: true}); + }) + } } fs.writeFileSync(outputPath, JSON.stringify({