Skip to content

Commit

Permalink
feat: When idle trigger
Browse files Browse the repository at this point in the history
Closes #33
  • Loading branch information
Alorel committed Jan 16, 2023
1 parent a071f4e commit ca15cd5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
8 changes: 3 additions & 5 deletions src/lib/data/trigger-definition-context.mts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ export class TriggerDefinitionContext<T extends object = {}>
const sub = listenInput$
? from(listenInput$)
.pipe(logError('[TriggerDefinitionCtx.listen]'))
.subscribe({
complete: () => {
listener.notify();
},
.subscribe(() => {
listener.notify();
})
: undefined;

return () => {
debugLog(`Stopping trigger listener ${this.id} with`, data);
sub?.unsubscribe();
listeners.delete(listener);
sub?.unsubscribe();
};
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/util/next-tick.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,14 @@ export const tickEnd$ = new LazyValue<Observable<void>>(() => {

return out;
});
export const tickStart$ = new LazyValue<Observable<void>>(() => {
const out = new Subject<void>();
ctx.patch(Game, 'tick').before(() => {
out.next();
});

return out;
});

export const nextTickEnd$ = new LazyValue<Observable<void>>(() => tickEnd$.value.pipe(take(1)));
export const nextTickStart$ = new LazyValue<Observable<void>>(() => tickStart$.value.pipe(take(1)));
2 changes: 1 addition & 1 deletion src/public_api/trigger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface TriggerNodeDefinition<T extends object = {}> extends NodeDefini
init?(): void;

/**
* Override the default trigger listener logic & make the trigger fire when the returned observable completes
* Override the default trigger listener logic & make the trigger fire when the returned observable emits
*/
listen?(data: T): ObservableInput<any>;
}
11 changes: 3 additions & 8 deletions src/triggers/combination/and-or-trigger.mts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,9 @@ defineLocalTrigger<Data>({
check: ({triggers}) => triggers.some(triggerPasses),
label: 'Or',
listen({triggers}) {
if (!triggers.length) {
return NEVER; // EMPTY won't work as it we're listening for completion and it completes immediately
}

const src$ = triggers.map(trigger => trigger.listen());

// As soon as one of them emits we're good to go
return merge(...src$).pipe(take(1));
return triggers.length
? merge(...triggers.map(trigger => trigger.listen()))
: NEVER;
},
localID: 'or',
media: 'https://raw.githubusercontent.com/Alorel/melvor-action-workflows/0.8.0/src/ui/assets/or.png',
Expand Down
20 changes: 20 additions & 0 deletions src/triggers/core/idle-trigger.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {EMPTY, filter, switchMap} from 'rxjs';
import {InternalCategory} from '../../lib/registries/action-registry.mjs';
import {defineLocalTrigger} from '../../lib/util/define-local.mjs';
import {nextTickStart$, tickEnd$} from '../../lib/util/next-tick.mjs';

defineLocalTrigger({
category: InternalCategory.CORE,
check: () => !game.activeAction,
label: 'Idle',
listen() {
const ifHasAction$ = nextTickStart$.value.pipe(filter(() => !game.activeAction));

return tickEnd$.value
.pipe(
switchMap(() => game.activeAction ? EMPTY : ifHasAction$)
);
},
localID: 'idle',
media: 'https://raw.githubusercontent.com/Alorel/melvor-action-workflows/0.12.0/src/ui/assets/lazy-peon.png',
});
1 change: 1 addition & 0 deletions src/triggers/core/index.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './equipped-food-count';
import './equipped-item-count';
import './idle-trigger.mjs';
import './item-charges-trigger';
import './item-quantity-trigger';
import './level-gained-trigger';
Expand Down
Binary file added src/ui/assets/lazy-peon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ca15cd5

Please sign in to comment.