Skip to content

Commit

Permalink
fix: Stop running from combat on action execution
Browse files Browse the repository at this point in the history
Closes #152
  • Loading branch information
Alorel committed Jan 16, 2023
1 parent f1d6825 commit cb4c2ea
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 69 deletions.
51 changes: 21 additions & 30 deletions src/lib/execution/workflow-execution.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,7 @@ import {Memoise} from '@aloreljs/memoise-decorator';
import {nextComplete} from '@aloreljs/rxutils';
import {logError} from '@aloreljs/rxutils/operators';
import type {MonoTypeOperatorFunction, Observer, Subscription, TeardownLogic} from 'rxjs';
import {
BehaviorSubject,
concat,
defer,
EMPTY,
filter,
from,
last,
map,
Observable,
of,
startWith,
takeUntil,
tap
} from 'rxjs';
import {BehaviorSubject, concat, EMPTY, filter, from, Observable, of, startWith, takeUntil, tap} from 'rxjs';
import {switchMap, take} from 'rxjs/operators';
import type {WorkflowExecutionCtx} from '../../public_api';
import type ActionConfigItem from '../data/action-config-item.mjs';
Expand All @@ -27,9 +13,9 @@ import AutoIncrement from '../decorators/auto-increment.mjs';
import PersistClassName from '../decorators/PersistClassName.mjs';
import WorkflowRegistry from '../registries/workflow-registry.mjs';
import {debugLog, errorLog} from '../util/log.mjs';
import {nextTickEnd$} from '../util/next-tick.mjs';
import prependErrorWith from '../util/rxjs/prepend-error-with.mjs';
import ShareReplayLike from '../util/share-replay-like-observable.mjs';
import {stopAction} from '../util/stop-action.mjs';
import type {
ActionExecutionEvent,
StepCompleteEvent,
Expand Down Expand Up @@ -200,26 +186,32 @@ export class WorkflowExecution extends ShareReplayLike<Out> {
*/
private executeAction(step: WorkflowStep, stepIdx: number, actionIdx: number): Observable<ActionExecutionEvent> {
const action = step.actions[actionIdx];
const logBase = `${step.actions[actionIdx]?.action.id}[${actionIdx}]@${this.workflow.name}[${stepIdx}]`;

const src$ = new Observable<ActionExecutionEvent>(subscriber => {
debugLog('[ExecAction] START', logBase);

const src$ = defer((): Observable<ActionExecutionEvent> => {
const def = action.action.def;
const result = def
.execute(action.opts, def.execContext ? this.makeExecCtx(stepIdx) : undefined);

const successEvent = this.makeSuccessEvent(action, step);
return result == null
? of(successEvent)
: from(result).pipe(
last(null, null),
map(() => successEvent)
);
if (result == null) {
return nextComplete(subscriber, successEvent);
}

return from(result)
.subscribe({
complete() {
debugLog('[ExecAction] OK', logBase);
nextComplete(subscriber, successEvent);
},
error: subscriber.error.bind(subscriber),
});
});

return src$.pipe(
tap(() => {
debugLog('Executed action', actionIdx, 'in workflow', this.workflow.name, 'step', stepIdx);
}),
logError(`[Exec action ${step.actions[actionIdx]?.action.id}[${actionIdx}]@${this.workflow.name}[${stepIdx}]]`),
logError(`[ExecAction] ERR ${logBase}`),
prependErrorWith(e => of<ActionExecutionEvent>({
...this.makeSuccessEvent(action, step),
err: e.message,
Expand All @@ -239,9 +231,7 @@ export class WorkflowExecution extends ShareReplayLike<Out> {
return EMPTY;
}

return stopAction().pipe(
switchMap(() => concat(...step.actions.map((_, i) => this.executeAction(step, stepIdx, i))))
);
return concat(...step.actions.map((_, i) => this.executeAction(step, stepIdx, i)));
}

/**
Expand All @@ -258,6 +248,7 @@ export class WorkflowExecution extends ShareReplayLike<Out> {
const exec$: Observable<Out> = step.trigger.listen()
.pipe(
take(1),
switchMap(() => nextTickEnd$.value),
switchMap(() => this.executeActions(step, stepIdx)),
logError(`Error executing step ${stepIdx} in workflow ${this.workflow.name}:`),
prependErrorWith(e => of<StepCompleteEvent>({
Expand Down
15 changes: 15 additions & 0 deletions src/lib/util/next-tick.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type {Observable} from 'rxjs';
import {Subject} from 'rxjs';
import {take} from 'rxjs/operators';
import LazyValue from './lazy-value.mjs';

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

return out;
});

export const nextTickEnd$ = new LazyValue<Observable<void>>(() => tickEnd$.value.pipe(take(1)));
39 changes: 0 additions & 39 deletions src/lib/util/stop-action.mts

This file was deleted.

2 changes: 2 additions & 0 deletions types/melvor/game/skilling.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export class Skill extends NamespacedObject {
* @param newLevel
*/
onLevelUp(oldLevel: number, newLevel: number): void;

stop(): void;
}

export class SkillWithMastery<T> extends Skill {
Expand Down

0 comments on commit cb4c2ea

Please sign in to comment.