Files
dflike/client/src/network/SocketClient.ts
T

83 lines
2.6 KiB
TypeScript

import { io, Socket } from 'socket.io-client';
import type { ServerEvents, ClientEvents, WorldState, StateUpdate, PlayerJoined, PlayerLeft, PlayerInput, SuperlativesData, NarrationEvent } from '@dflike/shared';
type TypedSocket = Socket<ServerEvents, ClientEvents>;
export class SocketClient {
private socket: TypedSocket;
private _playerId: string | null = null;
private _entityId: number | null = null;
// Event callbacks
onWorldState: ((data: WorldState) => void) | null = null;
onStateUpdate: ((data: StateUpdate) => void) | null = null;
onPlayerJoined: ((data: PlayerJoined) => void) | null = null;
onPlayerLeft: ((data: PlayerLeft) => void) | null = null;
onSuperlativesUpdate: ((data: SuperlativesData) => void) | null = null;
onNarrationEvent: ((data: NarrationEvent) => void) | null = null;
onNarrationUpdate: ((data: { id: number; narration: string }) => void) | null = null;
onNarrationHistory: ((data: NarrationEvent[]) => void) | null = null;
onNpcThought: ((data: { entityId: number; text: string; emoji: string }) => void) | null = null;
constructor(url: string) {
this.socket = io(url);
this.socket.on('world-state', (data) => {
this.onWorldState?.(data);
});
this.socket.on('state-update', (data) => {
this.onStateUpdate?.(data);
});
this.socket.on('player-joined', (data) => {
if (!this._playerId) {
this._playerId = data.playerId;
this._entityId = data.entityId;
}
this.onPlayerJoined?.(data);
});
this.socket.on('player-left', (data) => {
this.onPlayerLeft?.(data);
});
this.socket.on('superlatives-update', (data) => {
this.onSuperlativesUpdate?.(data);
});
this.socket.on('narration-event', (data) => this.onNarrationEvent?.(data));
this.socket.on('narration-update', (data) => this.onNarrationUpdate?.(data));
this.socket.on('narration-history', (data) => this.onNarrationHistory?.(data));
this.socket.on('npc-thought', (data) => this.onNpcThought?.(data));
}
get playerId(): string | null { return this._playerId; }
get entityId(): number | null { return this._entityId; }
sendInput(input: PlayerInput): void {
this.socket.emit('player-input', input);
}
spawnNpc(x: number, y: number): void {
this.socket.emit('spawn-npc', { x, y });
}
followNpc(entityId: number | null): void {
this.socket.emit('follow-npc', { entityId });
}
subscribeSuperlatives(): void {
this.socket.emit('superlatives-subscribe');
}
unsubscribeSuperlatives(): void {
this.socket.emit('superlatives-unsubscribe');
}
disconnect(): void {
this.socket.disconnect();
}
}