adjustable settings

This commit is contained in:
Gavin McDonald
2025-04-16 15:20:06 -04:00
parent 3018cd7c10
commit 14dc1139fb
10 changed files with 158 additions and 63 deletions

View File

@@ -3,7 +3,8 @@ import { createServer } from 'http';
import { Server as SocketIOServer, type Socket } from 'socket.io';
import GameStore from '@/lib/GameStore';
import type { ClientUpdate } from '@/types';
import omit from '@/tools/omit';
import type { ClientUpdate, GameUpdate, Settings } from '@/types';
const dev = process.env.NODE_ENV !== 'production';
const hostname = '0.0.0.0';
@@ -19,6 +20,11 @@ app.prepare().then(() => {
const io = new SocketIOServer(httpServer);
const broadcast = (event: string, gameUpdate: GameUpdate) => {
io.to(gameUpdate.dmID).emit(event, gameUpdate);
io.to(gameUpdate.spectatorID).emit(event, omit(gameUpdate, 'dmID'));
};
io.on('connection', (socket: Socket) => {
console.log(`Client connected: ${socket.id}`);
@@ -39,8 +45,7 @@ app.prepare().then(() => {
socket.join(gameID);
if (gameID === gameUpdate.spectatorID) {
const { spectatorID, cards } = gameUpdate;
socket.emit('init', { spectatorID, cards });
socket.emit('init', omit(gameUpdate, 'dmID'));
} else {
socket.emit('init', gameUpdate);
}
@@ -58,7 +63,7 @@ app.prepare().then(() => {
const gameUpdate = gameStore.flipCard(gameID, cardIndex);
io.to(gameID).emit('card-flipped', gameUpdate);
broadcast('game-update', gameUpdate);
} catch (e) {
const error = e instanceof Error ? e.message : e;
@@ -67,6 +72,16 @@ app.prepare().then(() => {
}
});
socket.on('settings', ({ gameID, gameData }: { gameID: string; gameData: GameUpdate }) => {
try {
const gameUpdate = gameStore.updateSettings(gameID, gameData.settings);
broadcast('game-update', gameUpdate);
} catch (e) {
const error = e instanceof Error ? e.message : e;
console.error('Error', error);
}
});
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
});