Files
dflike/server/src/network/SocketServer.ts
T
2026-03-07 13:37:36 +00:00

115 lines
4.0 KiB
TypeScript

import { Server } from 'socket.io';
import type http from 'http';
import type { ClientEvents, ServerEvents, PlayerInput, Position, Movement, PlayerControlled, Appearance } from '@dflike/shared';
import { Direction, MAX_NPC_COUNT } from '@dflike/shared';
import { generateRandomAppearance } from '../spawner/appearanceGenerator.js';
import type { GameLoop } from '../game/GameLoop.js';
import { spawnNPC } from '../game/spawner.js';
import { serializeWorldState, serializeStateUpdate } from './stateSerializer.js';
export class SocketServer {
private io: Server<ClientEvents, ServerEvents>;
private gameLoop: GameLoop;
constructor(httpServer: http.Server, gameLoop: GameLoop) {
this.io = new Server(httpServer, {
cors: { origin: '*' },
});
this.gameLoop = gameLoop;
this.setupBroadcast();
this.setupConnections();
}
private setupBroadcast(): void {
this.gameLoop.setBroadcastHandler(() => {
const update = serializeStateUpdate(this.gameLoop.world, this.gameLoop.getTick());
this.io.emit('state-update', update);
});
}
private setupConnections(): void {
this.io.on('connection', (socket) => {
const playerId = socket.id;
console.log(`Player connected: ${playerId}`);
// Create player entity (starts in camera mode — no avatar)
const world = this.gameLoop.world;
const map = this.gameLoop.map;
const entity = world.createEntity();
const startPos = map.getRandomWalkable();
world.addComponent<Position>(entity, 'position', startPos);
world.addComponent<Movement>(entity, 'movement', {
state: 'idle', target: null, path: [], direction: Direction.DOWN, moveProgress: 0,
});
world.addComponent<PlayerControlled>(entity, 'playerControlled', {
playerId, mode: 'camera',
});
world.addComponent<Appearance>(entity, 'appearance', generateRandomAppearance());
// Send full world state
socket.emit('world-state', serializeWorldState(world, map));
socket.emit('player-joined', { playerId, entityId: entity });
// Notify others
socket.broadcast.emit('player-joined', { playerId, entityId: entity });
// Handle inputs
socket.on('player-input', (input: PlayerInput) => {
this.handleInput(playerId, entity, input);
});
// Handle NPC spawn requests
socket.on('spawn-npc', (data: { x: number; y: number }) => {
const npcCount = world.query('npcBrain').length;
if (npcCount >= MAX_NPC_COUNT) return;
const target = map.findNearestWalkable(data.x, data.y, 3)
?? map.getRandomWalkable();
spawnNPC(world, map, target);
});
// Handle disconnect
socket.on('disconnect', () => {
console.log(`Player disconnected: ${playerId}`);
world.removeEntity(entity);
this.io.emit('player-left', { playerId });
});
});
}
private handleInput(playerId: string, entityId: number, input: PlayerInput): void {
const world = this.gameLoop.world;
const pc = world.getComponent<PlayerControlled>(entityId, 'playerControlled');
if (!pc) return;
switch (input.type) {
case 'toggle-mode': {
pc.mode = pc.mode === 'avatar' ? 'camera' : 'avatar';
break;
}
case 'move': {
if (pc.mode !== 'avatar' || !input.direction) break;
const pos = world.getComponent<Position>(entityId, 'position');
const mov = world.getComponent<Movement>(entityId, 'movement');
if (!pos || !mov) break;
const newX = pos.x + input.direction.dx;
const newY = pos.y + input.direction.dy;
if (this.gameLoop.map.isWalkable(newX, newY)) {
pos.x = newX;
pos.y = newY;
const { dx, dy } = input.direction;
if (Math.abs(dx) > Math.abs(dy)) {
mov.direction = dx > 0 ? Direction.RIGHT : Direction.LEFT;
} else {
mov.direction = dy > 0 ? Direction.DOWN : Direction.UP;
}
}
break;
}
}
}
}