diff options
| author | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-09-07 20:20:07 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-09-12 17:23:30 -0700 |
| commit | 8ec7f5368232d59f344e1067e1bad5e48dbcb7ae (patch) | |
| tree | 1ad2df4dc00773f2307d1525cc80ac7410ea8fba /static/src/network.ts | |
| parent | e4e31978bae7e45be57b376415a4b925ac8cbc03 (diff) | |
| download | kennel.hatecomputers.club-8ec7f5368232d59f344e1067e1bad5e48dbcb7ae.tar.gz kennel.hatecomputers.club-8ec7f5368232d59f344e1067e1bad5e48dbcb7ae.zip | |
get "cats" up there
Diffstat (limited to 'static/src/network.ts')
| -rw-r--r-- | static/src/network.ts | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/static/src/network.ts b/static/src/network.ts deleted file mode 100644 index fac96df..0000000 --- a/static/src/network.ts +++ /dev/null @@ -1,92 +0,0 @@ -export enum EventType { - INITIAL_STATE = "INITIAL_STATE", - SET_CONTROLLABLE = "SET_CONTROLLABLE", - ENTITY_BORN = "ENTITY_BORN", - ENTITY_DEATH = "ENTITY_DEATH", - ENTITY_POSITION_UPDATE = "ENTITY_POSITION_UPDATE", -} - -export interface Event { - event_type: EventType; - data: any; -} - -export interface EntityPositionUpdateEvent extends Event { - event_type: EventType.ENTITY_POSITION_UPDATE; - data: { - id: string; - position: { - x: number; - y: number; - }; - }; -} - -export interface InitialStateEvent extends Event { - event_type: EventType.INITIAL_STATE; - data: { - world: { width: number; height: number }; - entities: any[]; - }; -} - -export interface SetControllableEvent extends Event { - event_type: EventType.SET_CONTROLLABLE; - data: { - id: string; - client_id: string; - }; -} - -export interface EventQueue { - peek(): Event[]; - clear(): void; -} - -export interface EventPublisher { - add(event: Event): void; - publish(): void; -} - -export class WebSocketEventQueue implements EventQueue { - private queue: Event[]; - - constructor(websocket: WebSocket) { - this.queue = []; - this.listen_to(websocket); - } - - public peek() { - return this.queue; - } - - public clear() { - this.queue = []; - } - - private listen_to(websocket: WebSocket) { - websocket.onmessage = ({ data }) => { - this.queue = this.queue.concat(JSON.parse(data)); - }; - } -} - -export class WebsocketEventPublisher implements EventPublisher { - private queue: Event[]; - - constructor(private readonly websocket: WebSocket) { - this.queue = []; - } - - public add(event: Event) { - this.queue.push(event); - } - - public publish() { - if (this.queue.length === 0) { - return; - } - this.websocket.send(JSON.stringify(this.queue)); - this.queue = []; - } -} |
