54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = setupSocket;
|
|
const index_js_1 = require("../index.js");
|
|
function setupSocket(io) {
|
|
io.on('connection', (socket) => {
|
|
console.log('Client connected:', socket.id);
|
|
socket.on('join-game', async (gameId) => {
|
|
socket.join(`game:${gameId}`);
|
|
console.log(`Socket ${socket.id} joined game:${gameId}`);
|
|
});
|
|
socket.on('leave-game', (gameId) => {
|
|
socket.leave(`game:${gameId}`);
|
|
});
|
|
socket.on('team-location', async (data) => {
|
|
await index_js_1.prisma.team.update({
|
|
where: { id: data.teamId },
|
|
data: { lat: data.lat, lng: data.lng }
|
|
});
|
|
io.to(`game:${data.gameId}`).emit('team-location', {
|
|
teamId: data.teamId,
|
|
lat: data.lat,
|
|
lng: data.lng
|
|
});
|
|
});
|
|
socket.on('chat-message', async (data) => {
|
|
const chatMessage = await index_js_1.prisma.chatMessage.create({
|
|
data: {
|
|
gameId: data.gameId,
|
|
teamId: data.teamId,
|
|
userId: data.userId,
|
|
message: data.message
|
|
}
|
|
});
|
|
io.to(`game:${data.gameId}`).emit('chat-message', {
|
|
id: chatMessage.id,
|
|
teamId: data.teamId,
|
|
userId: data.userId,
|
|
userName: data.userName,
|
|
message: data.message,
|
|
sentAt: chatMessage.sentAt
|
|
});
|
|
});
|
|
socket.on('team-advanced', async (data) => {
|
|
io.to(`game:${data.gameId}`).emit('team-advanced', {
|
|
teamId: data.teamId
|
|
});
|
|
});
|
|
socket.on('disconnect', () => {
|
|
console.log('Client disconnected:', socket.id);
|
|
});
|
|
});
|
|
return io;
|
|
}
|