|
- import { TargetDrainedVigorCondition } from '@/game/combat/conditions'
- import { Creature } from '@/game/creature'
- import { LogEntry, LogLines, nilLog } from "@/game/interface"
- import { VoreContainer } from '@/game/vore'
-
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- abstract class Relay<Sender, EventMap extends { [name: string]: any }> {
- private subscriptions: { [K in keyof EventMap]: Array<(sender: Sender, args: EventMap[K]) => LogEntry> }
-
- constructor (private eventNames: Array<keyof EventMap>) {
- 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<K extends keyof EventMap> (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<K extends keyof EventMap> (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<ParentEventMap extends EventMap> (parent: Relay<Sender, ParentEventMap>): void {
- this.eventNames.forEach(name => {
- parent.subscribe(name, (sender, args) => this.dispatch(name, sender, args))
- })
- }
- }
-
- type VoreMap = {
- "onEaten": { prey: Creature };
- "onReleased": { prey: Creature };
- "onDigested": { prey: Creature };
- "onAbsorbed": { prey: Creature };
- }
-
- export class VoreRelay extends Relay<VoreContainer, VoreMap> {
- constructor () {
- super(["onEaten", "onReleased", "onDigested", "onAbsorbed"])
- }
- }
|