|
| 1 | +import uuidv4 from 'uuid/v4'; |
| 2 | +import { lastValueFrom, Subscription, timer } from 'rxjs'; |
| 3 | +import { Actor } from '@engine/world/actor/actor'; |
| 4 | +import { ActionHook } from '@engine/world/action/hooks/index'; |
| 5 | +import { World } from '@engine/world'; |
| 6 | +import { logger } from '@runejs/core'; |
| 7 | +import { Player } from '@engine/world/actor/player/player'; |
| 8 | +import { Npc } from '@engine/world/actor/npc/npc'; |
| 9 | +import { ActionStrength } from '@engine/world/action'; |
| 10 | + |
| 11 | + |
| 12 | +export type TaskSessionData = { [key: string]: any }; |
| 13 | + |
| 14 | + |
| 15 | +export interface HookTask<T = any> { |
| 16 | + canActivate?: <Q = T>(task: TaskExecutor<Q>, iteration?: number) => boolean | Promise<boolean>; |
| 17 | + activate: <Q = T>(task: TaskExecutor<Q>, iteration?: number) => void | undefined | boolean | Promise<void | undefined | boolean>; |
| 18 | + onComplete?: <Q = T>(task: TaskExecutor<Q>, iteration?: number) => void | Promise<void>; |
| 19 | + delay?: number; // # of ticks before execution |
| 20 | + delayMs?: number; // # of milliseconds before execution |
| 21 | + interval?: number; // # of ticks between loop intervals (defaults to single run task) |
| 22 | + intervalMs?: number; // # of milliseconds between loop intervals (defaults to single run task) |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +// T = current action info (ButtonAction, MoveItemAction, etc) |
| 27 | +export class TaskExecutor<T> { |
| 28 | + |
| 29 | + public readonly taskId = uuidv4(); |
| 30 | + public readonly strength: ActionStrength; |
| 31 | + public running: boolean = false; |
| 32 | + public session: TaskSessionData = {}; // a session store to use for the lifetime of the task |
| 33 | + |
| 34 | + private iteration: number = 0; |
| 35 | + private intervalSubscription: Subscription; |
| 36 | + |
| 37 | + public constructor(public readonly actor: Actor, |
| 38 | + public readonly task: HookTask<T>, |
| 39 | + public readonly hook: ActionHook, |
| 40 | + public readonly actionData: T) { |
| 41 | + this.strength = this.hook.strength || 'normal'; |
| 42 | + } |
| 43 | + |
| 44 | + public async run(): Promise<void> { |
| 45 | + this.running = true; |
| 46 | + |
| 47 | + if(!!this.task.delay || !!this.task.delayMs) { |
| 48 | + await lastValueFrom(timer(this.task.delayMs !== undefined ? this.task.delayMs : |
| 49 | + (this.task.delay * World.TICK_LENGTH))); |
| 50 | + } |
| 51 | + |
| 52 | + if(!!this.task.interval || !!this.task.intervalMs) { |
| 53 | + // Looping execution task |
| 54 | + const intervalMs = this.task.intervalMs !== undefined ? this.task.intervalMs : |
| 55 | + (this.task.interval * World.TICK_LENGTH); |
| 56 | + |
| 57 | + await new Promise<void>(resolve => { |
| 58 | + this.intervalSubscription = timer(0, intervalMs).subscribe( |
| 59 | + async() => { |
| 60 | + if(!await this.execute()) { |
| 61 | + this.intervalSubscription?.unsubscribe(); |
| 62 | + resolve(); |
| 63 | + } |
| 64 | + }, |
| 65 | + error => { |
| 66 | + logger.error(error); |
| 67 | + resolve(); |
| 68 | + }, |
| 69 | + () => resolve()); |
| 70 | + }); |
| 71 | + } else { |
| 72 | + // Single execution task |
| 73 | + await this.execute(); |
| 74 | + } |
| 75 | + |
| 76 | + if(this.running) { |
| 77 | + await this.stop(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public async execute(): Promise<boolean> { |
| 82 | + if(!this.actor) { |
| 83 | + // Actor destroyed, cancel the task |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + if(!await this.canActivate()) { |
| 88 | + // Unable to activate the task, cancel |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + if(this.actor.actionPipeline.paused) { |
| 93 | + // Action paused, continue loop if applicable |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + if(!this.running) { |
| 98 | + // Task no longer running, cancel execution |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + try { |
| 103 | + const response = await this.task.activate(this, this.iteration++); |
| 104 | + return typeof response === 'boolean' ? response : true; |
| 105 | + } catch(error) { |
| 106 | + logger.error(`Error executing action task`); |
| 107 | + logger.error(error); |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public async canActivate(): Promise<boolean> { |
| 113 | + if(!this.valid) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + if(!this.task.canActivate) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + try { |
| 122 | + return this.task.canActivate(this, this.iteration); |
| 123 | + } catch(error) { |
| 124 | + logger.error(`Error calling action canActivate`, this.task); |
| 125 | + logger.error(error); |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public async stop(): Promise<void> { |
| 131 | + this.running = false; |
| 132 | + this.intervalSubscription?.unsubscribe(); |
| 133 | + |
| 134 | + await this.task?.onComplete(this, this.iteration); |
| 135 | + |
| 136 | + this.session = null; |
| 137 | + } |
| 138 | + |
| 139 | + public getDetails(): { |
| 140 | + actor: Actor; |
| 141 | + player: Player | undefined; |
| 142 | + npc: Npc | undefined; |
| 143 | + actionData: T; |
| 144 | + session: TaskSessionData; } { |
| 145 | + const { |
| 146 | + type: { |
| 147 | + player, |
| 148 | + npc |
| 149 | + } |
| 150 | + } = this.actor; |
| 151 | + |
| 152 | + return { |
| 153 | + actor: this.actor, |
| 154 | + player, |
| 155 | + npc, |
| 156 | + actionData: this.actionData, |
| 157 | + session: this.session |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + public get valid(): boolean { |
| 162 | + return !!this.task?.activate && !!this.actionData; |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments