This commit is contained in:
Gavin McDonald
2025-04-12 15:17:02 -04:00
parent 1734eec436
commit 6508d40b2d
19 changed files with 1415 additions and 1344 deletions

View File

@@ -6,51 +6,50 @@ import GameStore from './lib/GameStore';
import type { ClientUpdate } from './types';
const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const hostname = '0.0.0.0';
const port = 3000;
// when using middleware `hostname` and `port` must be provided
const app = next({ dev, hostname, port });
const handler = app.getRequestHandler();
const gameStore = new GameStore();
app.prepare().then(() => {
const httpServer = createServer(handler);
const httpServer = createServer(handler);
const io = new SocketIOServer(httpServer);
const io = new SocketIOServer(httpServer);
io.on('connection', (socket: Socket) => {
console.log(`Client connected: ${socket.id}`);
io.on('connection', (socket: Socket) => {
console.log(`Client connected: ${socket.id}`);
socket.on('join', (gameID) => {
socket.join(gameID);
const gameUpdate = gameStore.joinGame(gameID, socket.id);
socket.on('join', (gameID) => {
socket.join(gameID);
const gameUpdate = gameStore.joinGame(gameID, socket.id);
console.log(`Socket ${socket.id} joined game ${gameID}`)
console.log(`Socket ${socket.id} joined game ${gameID}`);
socket.emit('init', gameUpdate);
})
socket.emit('init', gameUpdate);
});
socket.on('flip-card', ({ gameID, cardIndex }: ClientUpdate) => {
console.log('Card flipped:', { gameID, cardIndex });
socket.on('flip-card', ({ gameID, cardIndex }: ClientUpdate) => {
console.log('Card flipped:', { gameID, cardIndex });
const gameUpdate = gameStore.flipCard(gameID, cardIndex);
const gameUpdate = gameStore.flipCard(gameID, cardIndex);
io.to(gameID).emit('card-flipped', gameUpdate);
});
io.to(gameID).emit('card-flipped', gameUpdate);
});
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
});
});
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
});
});
httpServer
.once('error', (err) => {
console.error('Server error:', err);
process.exit(1);
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
httpServer
.once('error', (err) => {
console.error('Server error:', err);
process.exit(1);
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
});