From 47cd3280bd7731b6f92b1b81bf4b5bde942b6a71 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 26 Jun 2024 22:46:02 +0500 Subject: [PATCH] feat: create event broker (#184) --- src/lib/game/baseGame.ts | 12 ++++++ src/lib/game/services/broker/brokerService.ts | 42 +++++++++++++++++++ src/lib/game/services/broker/interface.ts | 16 +++++++ src/routes/[lang]/(game)/play/+page.svelte | 3 +- 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/lib/game/services/broker/brokerService.ts create mode 100644 src/lib/game/services/broker/interface.ts diff --git a/src/lib/game/baseGame.ts b/src/lib/game/baseGame.ts index 8901bce7..7af35e9f 100644 --- a/src/lib/game/baseGame.ts +++ b/src/lib/game/baseGame.ts @@ -33,6 +33,8 @@ import { PlayerService } from '$lib/game/services/player/playerService' import { Raider } from '$lib/game/objects/units/raider' import { QuestService } from '$lib/game/services/quest/questService' import type { Wagon } from '$lib/game/services/wagon/interface' +import type { Broker } from '$lib/game/services/broker/interface' +import { BrokerService } from '$lib/game/services/broker/brokerService' interface BaseGameOptions { isSocketOn?: boolean @@ -51,6 +53,7 @@ export class BaseGame extends Container implements Game { tick: Game['tick'] = 0 group: Group + brokerService: Broker actionService: ActionService eventService: EventService tradeService: TradeService @@ -80,6 +83,7 @@ export class BaseGame extends Container implements Game { this.bg = new BackgroundGenerator(this.app) this.group = new Group() + this.brokerService = new BrokerService() this.actionService = new ActionService(this) this.eventService = new EventService(this) this.tradeService = new TradeService(this) @@ -233,6 +237,14 @@ export class BaseGame extends Container implements Game { this.removeChild(...this.children) } + subscribe(event: string, callback: Function) { + return this.brokerService.register(event, callback) + } + + dispatch(event: string, arg?: T) { + this.brokerService.dispatch(event, arg) + } + #updateObjects() { for (const object of this.children) { object.animate() diff --git a/src/lib/game/services/broker/brokerService.ts b/src/lib/game/services/broker/brokerService.ts new file mode 100644 index 00000000..fe5e2bb8 --- /dev/null +++ b/src/lib/game/services/broker/brokerService.ts @@ -0,0 +1,42 @@ +import type { Broker, Registry, Subscriber } from './interface' + +export class BrokerService implements Broker { + private subscribers: Subscriber + private static nextId = 0 + + constructor() { + this.subscribers = {} + } + + public dispatch(event: string, arg?: T): void { + const subscriber = this.subscribers[event] + + if (subscriber === undefined) { + return + } + + Object.keys(subscriber).forEach((key) => subscriber[key](arg)) + } + + public register(event: string, callback: Function): Registry { + const id = this.getNextId() + if (!this.subscribers[event]) { + this.subscribers[event] = {} + } + + this.subscribers[event][id] = callback + + return { + unregister: () => { + delete this.subscribers[event][id] + if (Object.keys(this.subscribers[event]).length === 0) { + delete this.subscribers[event] + } + }, + } + } + + private getNextId(): number { + return BrokerService.nextId++ + } +} diff --git a/src/lib/game/services/broker/interface.ts b/src/lib/game/services/broker/interface.ts new file mode 100644 index 00000000..429a5e87 --- /dev/null +++ b/src/lib/game/services/broker/interface.ts @@ -0,0 +1,16 @@ +export interface Registry { + unregister: () => void +} + +export interface Callable { + [key: string]: Function +} + +export interface Subscriber { + [key: string]: Callable +} + +export interface Broker { + dispatch: (event: string, arg?: T) => void + register: (event: string, callback: Function) => Registry +} diff --git a/src/routes/[lang]/(game)/play/+page.svelte b/src/routes/[lang]/(game)/play/+page.svelte index 7b421644..225762c6 100644 --- a/src/routes/[lang]/(game)/play/+page.svelte +++ b/src/routes/[lang]/(game)/play/+page.svelte @@ -1,10 +1,11 @@