Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create event broker #184

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib/game/baseGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<T>(event: string, arg?: T) {
this.brokerService.dispatch(event, arg)
}

#updateObjects() {
for (const object of this.children) {
object.animate()
Expand Down
42 changes: 42 additions & 0 deletions src/lib/game/services/broker/brokerService.ts
Original file line number Diff line number Diff line change
@@ -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<T>(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++
}
}
16 changes: 16 additions & 0 deletions src/lib/game/services/broker/interface.ts
Original file line number Diff line number Diff line change
@@ -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: <T>(event: string, arg?: T) => void
register: (event: string, callback: Function) => Registry
}
3 changes: 2 additions & 1 deletion src/routes/[lang]/(game)/play/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script lang='ts'>
import { onMount } from 'svelte'
import { onMount, setContext } from 'svelte'
import GameInterface from './GameInterface.svelte'
import { BaseGame } from '$lib/game/baseGame'
import { page } from '$app/stores'

const game = new BaseGame({ isSocketOn: true, profileJWT: $page.data.gameProfileJWT })
setContext('game', game)
let gameElement: HTMLElement

const handleVisibilityChange = () => {
Expand Down
Loading