From 2a12f70c911af56a82a7c765db9efde3b5521ab3 Mon Sep 17 00:00:00 2001 From: BinarySandia04 Date: Mon, 21 Oct 2024 17:27:23 +0200 Subject: [PATCH] Datagen is generated inside convinient mongodb schemas --- backend/models/DatagenCollection.js | 12 +++++++++ backend/models/DatagenEntry.js | 11 ++++++++ backend/services/datagen.js | 25 ++++++++++++++++--- .../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 | 18 ++++++++++++- .../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 ++- 15 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 backend/models/DatagenCollection.js create mode 100644 backend/models/DatagenEntry.js diff --git a/backend/models/DatagenCollection.js b/backend/models/DatagenCollection.js new file mode 100644 index 00000000..34e5ede1 --- /dev/null +++ b/backend/models/DatagenCollection.js @@ -0,0 +1,12 @@ +const mongoose = require("mongoose"); +const Schema = mongoose.Schema; + +const DatagenCollectionSchema = new Schema({ + name: {type: String, required: true}, + id: { type: String, required: true }, + package: { type: String, required: true}, + desc: { type: String }, + entries: [ {type: mongoose.Types.ObjectId, ref: "DatagenEntry"} ], +}); + +module.exports = mongoose.model('DatagenCollection', DatagenCollectionSchema); \ No newline at end of file diff --git a/backend/models/DatagenEntry.js b/backend/models/DatagenEntry.js new file mode 100644 index 00000000..be6c7822 --- /dev/null +++ b/backend/models/DatagenEntry.js @@ -0,0 +1,11 @@ +const mongoose = require("mongoose"); +const Schema = mongoose.Schema; + +const DatagenEntrySchema = new Schema({ + id: {type: String, required: true}, + schema: { type: String, required: true}, + data: { type: Object }, + datagen_collection: {type: mongoose.Types.ObjectId, ref: "DatagenCollection"}, +}); + +module.exports = mongoose.model('DatagenEntry', DatagenEntrySchema); \ No newline at end of file diff --git a/backend/services/datagen.js b/backend/services/datagen.js index 4cb4c590..0b1ce4b1 100644 --- a/backend/services/datagen.js +++ b/backend/services/datagen.js @@ -4,14 +4,19 @@ const path = require('path'); const basePath = path.resolve(__dirname, '../') +const DatagenCollection = require("../models/DatagenCollection"); +const DatagenEntry = require('../models/DatagenEntry'); + async function datagenTask(models) { console.log("Inicializing datagen task"); + await DatagenCollection.deleteMany({}); + await DatagenEntry.deleteMany({}); + 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") @@ -26,6 +31,13 @@ async function datagenTask(models) { async function resolveDatagen(models, path, info){ // Do locale translation with info somewhere...? + const datagenCollection = await DatagenCollection.create({ + name: info.name, + id: info.id, + desc: info.desc, + package: info.package + }); + const modelNames = models[info.package].modelNames; modelNames.forEach(modelName => { if(Object.keys(mongoose.models).includes(modelName)){ @@ -36,7 +48,7 @@ async function resolveDatagen(models, path, info){ let modelPath = path + "/data/" + modelLastName; jsonFiles.forEach(file => { if(fs.lstatSync(modelPath + "/" + file).isFile()){ - appendDatagen(modelPath + "/" + file, modelName, info); + appendDatagen(modelPath + "/" + file, modelName, info, datagenCollection); } }) } @@ -44,8 +56,15 @@ async function resolveDatagen(models, path, info){ }) } -async function appendDatagen(file, modelName, info){ +async function appendDatagen(file, modelName, info, datagenCollection){ // "Appending " + file + " to model " + modelName + " from " + info.id + " for package " + info.package + let fileData = JSON.parse(fs.readFileSync(file)); + await DatagenEntry.create({ + id: fileData.id, + data: fileData.value, + schema: modelName, + datagen_collection: datagenCollection + }); } module.exports = { 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 index 544b7b4d..a0fc6c4a 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "effects/test_effect", + "value": {} } \ 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 index 544b7b4d..a6ed6885 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "consumables/test_consumable", + "value": {} } \ 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 index 544b7b4d..11ddc8d8 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "container/test_container", + "value": {} } \ 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 index 544b7b4d..361a07c6 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "equipment/test_equipment", + "value": {} } \ 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 index 544b7b4d..aafc4739 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "feature/test_feature", + "value": {} } \ 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 index 544b7b4d..4f091bc4 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "spells/test_spell", + "value": {} } \ 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 index 544b7b4d..e48370fc 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "tools/test_tool", + "value": {} } \ 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 index 544b7b4d..d58f1184 100644 --- 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 @@ -1,3 +1,19 @@ { - + "id": "weapon/test_weapon", + "value": { + "name": "Test datagen weapon", + "type": "Weapon", + "data": {}, + "info": { + "icon": "icons/weapons/ammunition/shot-round-blue.webp", + "weapon_type": "Natural", + "rarity": "Rare", + "quantity": "1", + "weight": "10", + "price": "10", + "properties": [ + "Finesse" + ] + } + } } \ 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 index 544b7b4d..818a5698 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "monsters/test_monster", + "value": {} } \ 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 index 544b7b4d..f6b44d86 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "classes/test_class", + "value": {} } \ 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 index 544b7b4d..e486d974 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "races/test_race", + "value": {} } \ 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 index 544b7b4d..c03fadce 100644 --- 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 @@ -1,3 +1,4 @@ { - + "id": "table/test_table", + "value": {} } \ No newline at end of file