Skip to content

Commit

Permalink
feat(nexus-simplified-scripting-converter): ✨ add wait action to the …
Browse files Browse the repository at this point in the history
…list of supported actions

The wait action transforms into a timeout with all following actions inside the internal function
  • Loading branch information
keneanung committed Oct 24, 2023
1 parent 1d79ac0 commit 1f728ad
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,11 @@ exports[`Should be able to convert variable actions with valtype variable 1`] =
}"
`;
exports[`Should be able to convert wait actions 1`] = `
"// wait action (index 0)
setTimeout(() => {}, 20056);"
`;
exports[`Should be able to handle aliases 1`] = `
{
"description": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,76 @@ exports[`Numpad movement 1`] = `
"type": "group",
}
`;

exports[`timeout 1`] = `
{
"description": "",
"enabled": true,
"items": [
{
"code": "// Place any code here you'd like to autorun when the package is installed",
"enabled": true,
"name": "onInstall",
"type": "function",
},
{
"code": "// Place any code here you'd like to autorun when the package is uninstalled",
"enabled": true,
"name": "onUninstall",
"type": "function",
},
{
"actions": [
{
"action": "script",
"script": "const do_replace = (cmd, args) => {
const prefix = args.input.substr(0, args.index);
const posend = args.index + args[0].length;
const suffix = args.input.substr(posend);
const replace = {};
replace["match"] = args[0];
replace["line"] = args.input;
replace["prefix"] = prefix;
replace["suffix"] = suffix;
if (args.length > 1) {
for (let i = 1; i++; i < args.length) {
replace[i] = args[i];
}
}
if (args.groups) {
for (const group in args.groups) {
replace[group] = args.groups[group];
}
}
cmd = nexusclient.variables().expand(cmd, replace);
return cmd
}
// wait action (index 0)
setTimeout(() => {
// notify action (index 1)
{
let cmd = "Timeout Triggered";
if (args) {
cmd = do_replace(cmd, args);
// with empty replacement argument to do general variable replacement
cmd = nexusclient.variables().expand(cmd);
}
nexusclient.display_notice(cmd, "#ff0000", "#000000")
}
}, 2036);",
},
],
"case_sensitive": true,
"enabled": true,
"matching": "exact",
"name": "",
"prefix_suffix": true,
"text": "test timeout",
"type": "alias",
"whole_words": true,
},
],
"name": "timeouttestpackage",
"type": "group",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"group","name":"timeouttestpackage","enabled":true,"items":[{"type":"function","name":"onInstall","enabled":true,"code":"// Place any code here you'd like to autorun when the package is installed"},{"type":"function","name":"onUninstall","enabled":true,"code":"// Place any code here you'd like to autorun when the package is uninstalled"},{"type":"alias","name":"","enabled":true,"actions":[{"action":"wait","enabled":true,"seconds":2,"milliseconds":36},{"action":"notify","enabled":true,"notice":"Timeout Triggered","notice_fg":"#ff0000","notice_bg":"#000000"}],"text":"test timeout","matching":"exact","whole_words":true,"case_sensitive":true,"prefix_suffix":true}],"description":""}
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,17 @@ test('Should be able to convert button actions with action default', () => {

expect(result).toMatchSnapshot();
});

test('Should be able to convert wait actions', () => {
const actions: Action[] = [
{
action: 'wait',
milliseconds: '56',
seconds: '20'
},
];

const result = convertActions(actions, 'bar', 'trigger');

expect(result).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Package } from '@keneanung/iron-realms-nexus-typings';
import { convertPackage } from '..';
import numpadMovement from './examples/numpad_movement.json';
import timeout from './examples/test_timeout.json';

test('Numpad movement', () => {
const pkg = numpadMovement as Package;
convertPackage(pkg);

expect(pkg).toMatchSnapshot();
});

test('timeout', () => {
const pkg = timeout as Package;
convertPackage(pkg);

expect(pkg).toMatchSnapshot();
});
10 changes: 9 additions & 1 deletion packages/nexus-simplified-scripting-converter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import variableTemplate from './templates/variables.jsr';
import gagTemplate from './templates/gag.jsr';
import highlightTemplate from './templates/highlight.jsr';
import buttonAction from './templates/button.jsr';
import waitAction from './templates/wait.jsr';
import jsrender from 'jsrender';
import { Action, Package, Reflex } from '@keneanung/iron-realms-nexus-typings';
import beautify_js from 'js-beautify';
import { isBrowser } from 'browser-or-node';

// missing: WaitAction, WaitForAction, IfAction, RepeatAction, RewriteAction, LinkifyAction, LabelAction, GotoAction
// missing: WaitForAction, IfAction, RepeatAction, RewriteAction, LinkifyAction, LabelAction, GotoAction

const renderer = isBrowser ? jsrender() : jsrender;

Expand All @@ -36,6 +37,7 @@ const templates = renderer.templates({
gag: gagTemplate,
highlight: highlightTemplate,
button: buttonAction,
wait: waitAction,
});

const convertActions = (
Expand All @@ -45,6 +47,7 @@ const convertActions = (
) => {
const result = [];
let index = 0;
const stack = [];

if (
actions.some(
Expand All @@ -69,8 +72,13 @@ const convertActions = (
};
}
result.push(templates.templates[action.action](action));
if(action.action === 'wait'){
stack.push(`}, ${parseInt(action.seconds) * 1000 + parseInt(action.milliseconds)});`)
}
}

result.push(stack.reverse());

const resultingAction = result.join('\n');

return beautify_js(resultingAction, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setTimeout(() => {

0 comments on commit 1f728ad

Please sign in to comment.