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

feat: string/regex search commands #260

Merged
merged 2 commits into from
Apr 10, 2024
Merged
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
10 changes: 6 additions & 4 deletions docs/commands/all-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,12 @@ do_things element:

## Strings

| Command | Example | Description |
| -------------------------------- | --------------------------- | ---------------------------------------------------------------------------- |
| [concat](string-commands/concat) | `concat "Hello" "World"` | Concatenates two or more strings |
| [join](string-commands/join) | `join ", " "Hello" "World"` | Joins x strings, with the first character being the join string between them |
| Command | Example | Description |
| -------------------------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| [concat](string-commands/concat) | `concat "Hello" "World"` | Concatenates two or more strings |
| [join](string-commands/join) | `join ", " "Hello" "World"` | Joins x strings, with the first character being the join string between them |
| str_search | `var result (str_search "Hello world" "world")` | Searches for a substring in a string and returns the index of the first occurrence. Returns -1 if not found. |
| regex_search | `var result (regex_search "Hello world" "world")` | Searches for a regex pattern in a string and returns the index of the first occurrence. Returns -1 if not found. |

## Screen Objects

Expand Down
22 changes: 19 additions & 3 deletions packages/narrat/src/examples/default/scripts/default.narrat
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
dev_test:
run test_regex
run test_text_autoadvance
menu_return

test_regex:
var text "uwu i want to open a door mommy"
var res (str_search $text "open a door")
log $res
if (!= $res -1):
"Found the text"
else:
"Didn't find the text"

var text "0 1 2 a b c d e f g"
var regex_res (regex_search $text \w)
log $regex_res
if (!= $regex_res -1):
"Found the text with regex"
else:
"Didn't find the text with regex"

talk_shortcut text:
talk player idle $text

main:
- "Hello I'm the player"

test_text_autoadvance:
narrate "Hi!"
narrate "with delay " 2000
Expand All @@ -16,6 +31,7 @@ test_text_autoadvance:
narrate "auto advance waiting for text to print" 1 true
narrate "This is a normal line"


talk player idle "Hello!"
talk player idle "This will auto advance" 1 true
talk player idle "This will not auto advance but has a delay" 2000 false
Expand Down
10 changes: 8 additions & 2 deletions packages/narrat/src/vm/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ import {
randomFromArrayPlugin,
randomIntPlugin,
} from './random-commands';
import { stringConcatPlugin, stringJoinPlugin } from './string-commands';
import {
regexSearchPlugin,
stringConcatPlugin,
stringJoinPlugin,
stringSearchPlugin,
} from './string-commands';
import {
ceilPlugin,
clampPlugin,
Expand Down Expand Up @@ -304,7 +309,8 @@ export function registerBaseCommands(vm: VM) {
// Strings
vm.addCommand(stringConcatPlugin);
vm.addCommand(stringJoinPlugin);

vm.addCommand(stringSearchPlugin);
vm.addCommand(regexSearchPlugin);
// Maths
vm.addCommand(minPlugin);
vm.addCommand(maxPlugin);
Expand Down
31 changes: 31 additions & 0 deletions packages/narrat/src/vm/commands/string-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,34 @@ export const stringJoinPlugin = new CommandPlugin<{ a: number; b: number }>(
}, '');
},
);

export const stringSearchPlugin = CommandPlugin.FromOptions<{
str: string;
matcher: string;
}>({
keyword: 'str_search',
argTypes: [
{ name: 'str', type: 'string' },
{ name: 'matcher', type: 'string' },
],
runner: async (cmd) => {
const { str, matcher } = cmd.options;
return str.search(matcher);
},
});

export const regexSearchPlugin = CommandPlugin.FromOptions<{
str: string;
regex: string;
}>({
keyword: 'regex_search',
argTypes: [
{ name: 'str', type: 'string' },
{ name: 'regex', type: 'string' },
],
runner: async (cmd) => {
const { str, regex } = cmd.options;
const matcher = new RegExp(regex);
return str.search(matcher);
},
});
6 changes: 0 additions & 6 deletions packages/narrat/src/vm/commands/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@ export class BaseTextCommand<
Options extends BaseTextCommandArgs,
> extends CommandPlugin<Options> {
static async ManageAutoAdvance(cmd: Parser.Command<TalkArgs>) {
console.log('not interactive');
await useVM().waitForEndTextAnimation();
console.log('text animation ended');
await timeout(cmd.options.delay || 0);
console.log('delay ended');
if (cmd.options.autoAdvance) {
console.log('player answered');
playerAnswered(0);
} else {
console.log('make dialog interactive');
useDialogStore().makeLastDialogInteractive();
}
}
Expand Down Expand Up @@ -131,7 +126,6 @@ export const narrateCommand = BaseTextCommand.FromOptions<TalkArgs>({
export const textParser = (): CommandParserFunction<{}, { text: string }> => {
const parser = generateParser<{}, { text: string }>('text', []);
return (ctx, parsed) => {
console.log(parsed);
const result = parser(ctx, parsed);
parsed.command.staticOptions = {
text: parsed.code.substring(1, parsed.code.length - 1),
Expand Down
Loading