Skip to content

Commit

Permalink
Updated JSON actions and added new json action. (#1039)
Browse files Browse the repository at this point in the history
* Updated with 2 things

Added create primitive mod

Added seperator mod

* Updated these with lint

* Updated these with lint

* Updated canvas_create_primitive_MOD.js

* Added json_read_MOD

added json_write_MOD.js

* Added json_read_MOD

added json_write_MOD.js

* Added json_read_MOD

added json_write_MOD.js

* Added json_read_MOD

added json_write_MOD.js

* Fixed lint issue I hope

* Fixed lint issue I hope

* Fixed prettier issues

Fixed linting issues inside json write

Fixed issue with canvas paint not using cases

* Added json random

* **Changelog**

* Fixed json_random_MOD.js meta data

* Fixed json_read_MOD.js meta data

* Added json_check_MOD.json

* **Changelog**

* Fixed json_random_MOD.js meta data

* Fixed json_read_MOD.js meta data

* Added json_check_MOD.json

* **Changelog**

* Fixed json_random_MOD.js meta data

* Fixed json_read_MOD.js meta data

* Added json_check_MOD.json

* **Changelog**

* Fixed json_random_MOD.js meta data

* Fixed json_read_MOD.js meta data

* Added json_check_MOD.json

* **Changelog**

* added nested check

* **Changelog**

* removed error handler

* **Changelog**

* removed error handler
  • Loading branch information
thekingofspace authored Jun 6, 2024
1 parent fbcd592 commit 54934ba
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
134 changes: 134 additions & 0 deletions actions/json_check_MOD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const fs = require('fs');
const path = require('path');

module.exports = {
name: 'Check JSON File',

section: 'File Stuff',

subtitle(data) {
return `Check JSON file "${data.filepath}"`;
},

meta: {
version: '2.1.7',
preciseCheck: false,
author: 'DBM Mods',
authorUrl: 'https://github.com/dbm-network/mods',
},

fields: ['filepath', 'checkType', 'title', 'contentTitle', 'branch'],

html() {
return `
<div style="padding: 10px;">
<span class="dbminputlabel">File Path</span>
<input id="filepath" class="round" type="text" placeholder="./data.json">
</div>
<div style="padding: 10px;">
<span class="dbminputlabel">Check Type</span><br>
<select id="checkType" class="round" onchange="glob.onCheckTypeChanged(this)">
<option value="0">Check if File is JSON Formatted</option>
<option value="1">Check if Title Exists</option>
<option value="2">Check if Content Exists Under Title</option>
</select>
</div>
<div id="titleSection" style="padding: 10px; display: none;">
<span class="dbminputlabel">Title</span>
<input id="title" class="round" type="text" placeholder="Title">
</div>
<div id="contentTitleSection" style="padding: 10px; display: none;">
<span class="dbminputlabel">Content Title</span>
<input id="contentTitle" class="round" type="text" placeholder="Content Title">
</div>
<conditional-input id="branch" style="padding-top: 8px;"></conditional-input>`;
},

init() {
const { glob, document } = this;

glob.onCheckTypeChanged = function handleCheckTypeChange(event) {
const titleSection = document.getElementById('titleSection');
const contentTitleSection = document.getElementById('contentTitleSection');
switch (event.value) {
case '0':
titleSection.style.display = 'none';
contentTitleSection.style.display = 'none';
break;
case '1':
titleSection.style.display = null;
contentTitleSection.style.display = 'none';
break;
case '2':
titleSection.style.display = null;
contentTitleSection.style.display = null;
break;
}
};

glob.onCheckTypeChanged(document.getElementById('checkType'));
},

async action(cache) {
const data = cache.actions[cache.index];
let filepath = this.evalMessage(data.filepath, cache);
const checkType = parseInt(data.checkType, 10);
const title = this.evalMessage(data.title, cache);
const contentTitle = this.evalMessage(data.contentTitle, cache);

if (filepath.startsWith('./')) {
filepath = path.join(__dirname, '..', filepath.substring(2));
}

let jsonData;

if (fs.existsSync(filepath)) {
try {
const fileData = fs.readFileSync(filepath);
if (fileData.length === 0) throw new Error('JSON file is empty.');
jsonData = JSON.parse(fileData);
} catch (error) {
console.error(`Error reading JSON file: ${error}`);
this.executeResults(false, data.branch, cache);
return;
}
} else {
this.executeResults(false, data.branch, cache);
return;
}

let result = false;

try {
switch (checkType) {
case 0:
result = true;
break;
case 1: {
result = jsonData.some((item) => item.Title === title);
break;
}
case 2: {
const titleData = jsonData.find((item) => item.Title === title);
if (!titleData) throw new Error('Title not found');
const nestedTitles = contentTitle.split('/');
let nestedData = titleData;
for (const nestedTitle of nestedTitles) {
if (!(nestedTitle in nestedData)) {
throw new Error('Content title not found');
}
nestedData = nestedData[nestedTitle];
}
result = true;
break;
}
}
} catch (error) {
console.error(`Error checking JSON data: ${error}`);
}

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

mod() {},
};
6 changes: 6 additions & 0 deletions actions/json_random_MOD.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ module.exports = {
name: 'Pick Random JSON Item',
section: 'File Stuff',
fields: ['filepath', 'title', 'storage', 'varName'],
meta: {
version: '2.1.7',
preciseCheck: false,
author: 'DBM Mods',
authorUrl: 'https://github.com/dbm-network/mods',
},

subtitle(data) {
return `Pick random item from JSON file "${data.filepath}"`;
Expand Down
6 changes: 6 additions & 0 deletions actions/json_read_MOD.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ module.exports = {
name: 'Read JSON File',
section: 'File Stuff',
fields: ['filepath', 'title', 'contentTitle', 'storage', 'varName'],
meta: {
version: '2.1.7',
preciseCheck: false,
author: 'DBM Mods',
authorUrl: 'https://github.com/dbm-network/mods',
},

subtitle(data) {
return `Read JSON file "${data.filepath}"`;
Expand Down

0 comments on commit 54934ba

Please sign in to comment.