import { TargetDrainedVigorCondition } from '@/game/combat/conditions' import { Creature } from '@/game/creature' import { LogEntry, LogLines, nilLog } from "@/game/interface" import { Container } from '@/game/vore' import { Action } from './combat' // eslint-disable-next-line @typescript-eslint/no-explicit-any abstract class Relay { private subscriptions: { [K in keyof EventMap]: Array<(sender: Sender, args: EventMap[K]) => LogEntry> } constructor (private eventNames: Array) { const partialSubscriptions: Partial<{ [K in keyof EventMap]?: Array<(sender: Sender, args: EventMap[K]) => LogEntry>}> = {} this.eventNames.forEach(name => { partialSubscriptions[name] = [] }) this.subscriptions = partialSubscriptions as Required<{ [K in keyof EventMap]: Array<(sender: Sender, args: EventMap[K]) => LogEntry> }> } subscribe (name: K, callback: (sender: Sender, args: EventMap[K]) => LogEntry): void { if (this.subscriptions[name] === undefined) { this.subscriptions[name] = [] } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.subscriptions[name]!.push(callback) } dispatch (name: K, sender: Sender, args: EventMap[K]): LogEntry { const subscriptionList = this.subscriptions[name] if (subscriptionList !== undefined) { return new LogLines(...subscriptionList.map(sub => sub(sender, args))) } else { return nilLog } } connect (parent: Relay): void { this.eventNames.forEach(name => { parent.subscribe(name, (sender, args) => this.dispatch(name, sender, args)) }) } } type VoreMap = { "onEaten": { prey: Creature }; "onReleased": { prey: Creature }; "onEntered": { prey: Creature }; "onExited": { prey: Creature }; "onDigested": { prey: Creature }; "onAbsorbed": { prey: Creature }; } export class VoreRelay extends Relay { constructor () { super(["onEaten", "onReleased", "onEntered", "onExited", "onDigested", "onAbsorbed"]) } } type ActionMap = { "onActionAttempt": { user: Creature; target: Creature; action: Action }; "onActionFail": { user: Creature; target: Creature; action: Action }; "onActionSuccess": { user: Creature; target: Creature; action: Action }; } export class ActionRelay extends Relay { constructor () { super([ "onActionAttempt", "onActionFail", "onActionSuccess" ]) } }