Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JSON data #1026

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions actions/check_content_title_MOD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module.exports = {

Check failure on line 1 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`
name: 'Check if JSON Title Exists',

Check failure on line 2 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`
section: 'Json Data',

Check failure on line 3 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`
meta: {

Check failure on line 4 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`
version: '2.1.8',

Check failure on line 5 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`
author: 'DBM Mods',

Check failure on line 6 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`
},

Check failure on line 7 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`

Check failure on line 8 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`
subtitle(data) {

Check failure on line 9 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`
return `Check if JSON File Title "${data.title}" exists in File "${data.filePath}"`;

Check failure on line 10 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `␍`
},

fields: ['filePath', 'title', 'branch'],

html(isEvent, data) {

Check warning on line 15 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

'isEvent' is defined but never used

Check warning on line 15 in actions/check_content_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

'data' is defined but never used
return `
<div>
<span class="dbminputlabel">JSON File Path</span>
<input id="filePath" class="round" type="text">
</div>

<br><br><br>

<div>
<span class="dbminputlabel">Title to Check</span>
<input id="title" class="round" type="text">
</div>

<br><br><br><br>

<hr class="subtlebar">

<br>

<conditional-input id="branch" style="padding-top: 8px;"></conditional-input>`;
},

init() {},

action(cache) {
const data = cache.actions[cache.index];
const filePath = this.evalMessage(data.filePath, cache);
const titleToCheck = this.evalMessage(data.title, cache);

let jsonData;
try {
jsonData = require(filePath);
} catch (error) {
console.error(`Error reading JSON file: ${error.message}`);
}

let result = false;

if (jsonData && jsonData instanceof Object) {
result = jsonData.hasOwnProperty(titleToCheck);
}

this.executeResults(result, data.branch ?? data, cache);
},

modInit(data) {
this.prepareActions(data.branch?.iftrueActions);
this.prepareActions(data.branch?.iffalseActions);
},

mod() {},
};
61 changes: 61 additions & 0 deletions actions/check_title_MOD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module.exports = {
name: 'Check if JSON Title Exists',
section: 'Json Data',
meta: {
version: '2.1.8',
author: 'DBM Mods',
},

subtitle(data) {
return `Check if JSON Title "${data.title}" exists in File Path "${data.filePath}"`;
},

fields: ['filePath', 'title', 'branch'],

html(isEvent, data) {

Check warning on line 15 in actions/check_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

'isEvent' is defined but never used

Check warning on line 15 in actions/check_title_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

'data' is defined but never used
return `
<div>
<span class="dbminputlabel">File Path</span>
<input id="filePath" class="round" type="text">
</div>

<br><br><br>

<div>
<span class="dbminputlabel">Title to Check</span>
<input id="title" class="round" type="text">
</div>

<br><br><br>

<hr class="subtlebar">

<br>

<conditional-input id="branch" style="padding-top: 8px;"></conditional-input>`;
},

init() {},

action(cache) {
const data = cache.actions[cache.index];
const filePath = this.evalMessage(data.filePath, cache);
const titleToCheck = this.evalMessage(data.title, cache);
let result = false;

try {
const fileContent = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(fileContent);

if (jsonData && jsonData instanceof Object) {
result = jsonData.hasOwnProperty(titleToCheck);
}
} catch (error) {
console.error(`Error reading or parsing JSON file: ${error}`);
}

this.executeResults(result, data.branch ?? data, cache);
},

mod() {},
};
88 changes: 88 additions & 0 deletions actions/read_json_data_MOD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module.exports = {
name: 'Read JSON Data',
section: 'Json Data',
meta: {
version: '2.1.8',
author: 'Raikin',
},

subtitle(data) {
return `Read JSON Data from "${data.filename}"`;
},

variableStorage(data, varType) {
if (parseInt(data.storage, 10) !== varType) return;
return [data.varName, 'String'];
},

fields: ['filename', 'title', 'contentTitle', 'storage', 'varName'],

html() {
return `
<div>
<span class="dbminputlabel">File Path</span>
<input id="filename" class="round" type="text">
</div>
<br><br>

<div>
<span class="dbminputlabel">Title</span>
<input id="title" class="round" type="text">
</div>
<br>

<div>
<span class="dbminputlabel">Content Title</span>
<input id="contentTitle" class="round" type="text">
</div>
<br>

<div>
<store-in-variable dropdownLabel="Store In" selectId="storage" variableContainerId="varNameContainer" variableInputId="varName"></store-in-variable>
</div>
`;
},

async action(cache) {
const data = cache.actions[cache.index];
const { readFileSync, existsSync } = require('fs');
const path = this.evalMessage(data.filename, cache);
const title = this.evalMessage(data.title, cache);
const contentTitle = this.evalMessage(data.contentTitle, cache);
const varName = this.evalMessage(data.varName, cache);

try {
if (path) {
let jsonData = [];

if (existsSync(path)) {
const existingContent = readFileSync(path, 'utf8');

try {
jsonData = JSON.parse(existingContent);
} catch (err) {
console.error(`Error parsing existing JSON data: ${err}`);
}
}

const entry = jsonData.find((item) => item.Title === title);

if (entry && entry[contentTitle] !== undefined) {
const content = entry[contentTitle];
this.storeValue(content, parseInt(data.storage, 10), varName, cache);
} else {
console.warn(`Content not found for title "${title}" and content title "${contentTitle}"`);
this.storeValue('', parseInt(data.storage, 10), varName, cache);
}
} else {
console.log('File path is missing from Read JSON Data action!');
}
} catch (err) {
console.error(`ERROR! ${err.stack || err}`);
}

this.callNextAction(cache);
},

mod() {},
};
110 changes: 110 additions & 0 deletions actions/write_json_data_MOD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
module.exports = {
name: 'Write JSON Data',
section: 'Json Data',
meta: {
version: '2.1.8',
author: 'DBM Mods',
},

subtitle(data) {
return `Write JSON Data to "${data.filename}"`;
},

fields: ['filename', 'title', 'contentTitle', 'contentText'],

html() {
return `
<div>
<span class="dbminputlabel">File Path</span>
<input id="filename" class="round" type="text">
</div>
<br><br>

<div>
<span class="dbminputlabel">Title</span>
<input id="title" class="round" type="text">
</div>
<br>

<div>
<span class="dbminputlabel">Content Title</span>
<input id="contentTitle" class="round" type="text">
</div>
<br>

<div>
<span class="dbminputlabel">Content Text</span>
<textarea id="contentText" rows="3" style="width: 95%; white-space: nowrap; resize: none;"></textarea>
</div>
`;
},

async action(cache) {
const data = cache.actions[cache.index];
const { writeFileSync, readFileSync, existsSync } = require('fs');
const path = this.evalMessage(data.filename, cache);
const title = this.evalMessage(data.title, cache);
const contentTitle = this.evalMessage(data.contentTitle, cache);
const contentText = this.evalMessage(data.contentText, cache);

try {
if (path) {
let jsonData = [];

// Check if the file already exists
if (existsSync(path)) {
// Read existing content
const existingContent = readFileSync(path, 'utf8');

try {
// Parse existing JSON data
jsonData = JSON.parse(existingContent);
} catch (err) {
console.error(`Error parsing existing JSON data: ${err}`);
}
}

// Find the entry with the specified title
const entry = jsonData.find((item) => item.Title === title);

// If the contentTitle already exists, replace it
if (contentTitle.includes('/')) {
const nestedTitles = contentTitle.split('/');
let currentContent = entry || jsonData;

for (let i = 0; i < nestedTitles.length; i++) {
const nestedTitle = nestedTitles[i];

if (i === nestedTitles.length - 1) {
currentContent[nestedTitle] = contentText;
} else {
currentContent[nestedTitle] = currentContent[nestedTitle] || {};
currentContent = currentContent[nestedTitle];
}
}
} else {
// If the title doesn't exist, create a new entry
const newEntry = {
Title: title,
};

newEntry[contentTitle] = contentText;

// Push the new entry to JSON data
jsonData.push(newEntry);
}

// Write the updated JSON data back to the file
writeFileSync(path, JSON.stringify(jsonData, null, 2), 'utf8');
} else {
console.log('File path is missing from Write JSON Data action!');
}
} catch (err) {
console.error(`ERROR! ${err.stack || err}`);
}

this.callNextAction(cache);
},

mod() {},
};
Loading