2024-11-19 12:16:26 +00:00
|
|
|
const WebSocket = require("ws");
|
|
|
|
const { v4: uuidv4 } = require("uuid");
|
|
|
|
const puzzles = require('./puzzles.json');
|
2024-11-28 15:08:38 +00:00
|
|
|
const names = require('./assignedNames.json');
|
2024-11-19 12:16:26 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
const wss = new WebSocket.Server({ port: 8080 });
|
|
|
|
const rooms = {}; // Stores rooms and their clients
|
|
|
|
const wheel = [
|
|
|
|
'lose a turn',800,350,450,700,300,600,5000,
|
|
|
|
300,600,300,500,800,550,400,300,900,500,'spin again',
|
|
|
|
900,'Bankrupt',600,400,300
|
|
|
|
] //represents wheel in wheel of fortune game.
|
|
|
|
function getRandomValue(array) {
|
|
|
|
const randomIndex = crypto.randomInt(0, array.length);
|
|
|
|
return array[randomIndex];
|
|
|
|
}
|
2024-11-28 15:08:38 +00:00
|
|
|
function shuffleArray(array) {
|
|
|
|
for (let i = array.length - 1; i > 0; i--) {
|
|
|
|
// Generate a cryptographically secure random index
|
|
|
|
const j = crypto.randomInt(0, i + 1);
|
|
|
|
// Swap elements
|
|
|
|
[array[i], array[j]] = [array[j], array[i]];
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultNames = shuffleArray(names);
|
|
|
|
let NameIndex = 0;
|
|
|
|
function getRandomName() {
|
|
|
|
if (NameIndex >= DefaultNames.length) {
|
|
|
|
NameIndex = 0;
|
|
|
|
}else{
|
|
|
|
NameIndex++;
|
|
|
|
}
|
|
|
|
//since we've incremented the index but not returned the value,
|
|
|
|
//calculate the index again
|
|
|
|
return NameIndex === 0 ? DefaultNames[NameIndex] : DefaultNames[NameIndex-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadCurrentPuzzle(gameStateObject) {
|
|
|
|
console.log(gameStateObject);
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:16:26 +00:00
|
|
|
|
|
|
|
// server.js
|
|
|
|
wss.on("connection", (ws) => {
|
|
|
|
ws.on("message", (message) => {
|
|
|
|
const data = JSON.parse(message);
|
|
|
|
|
|
|
|
if (data.type === "create_room") {
|
|
|
|
const roomCode = uuidv4().slice(0, 5);
|
2024-11-28 15:08:38 +00:00
|
|
|
ws.name = DefaultNames
|
|
|
|
rooms[roomCode] = {
|
|
|
|
clients: [ws],
|
|
|
|
leader: ws,
|
|
|
|
gameState: {
|
|
|
|
started:false,
|
|
|
|
puzzles:shuffleArray(puzzles),
|
|
|
|
puzzleLevel:0,
|
|
|
|
},
|
|
|
|
};
|
2024-11-19 12:16:26 +00:00
|
|
|
ws.roomCode = roomCode;
|
|
|
|
ws.send(JSON.stringify({ type: "room_created", roomCode, isLeader: true }));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.type === "join_room") {
|
|
|
|
const { roomCode } = data;
|
2024-11-28 15:08:38 +00:00
|
|
|
console.log(rooms);
|
|
|
|
console.log(rooms[roomCode],roomCode);
|
|
|
|
|
|
|
|
if (!rooms[roomCode]) {
|
|
|
|
ws.send(JSON.stringify({ type: "error", message: "Room not found" }));
|
|
|
|
}
|
|
|
|
else if (rooms[roomCode].gameState.started) {
|
|
|
|
ws.send(JSON.stringify({ type: "error", message: "Game has already Started!!!" }));
|
|
|
|
}
|
|
|
|
else {
|
2024-11-19 12:16:26 +00:00
|
|
|
rooms[roomCode].clients.push(ws);
|
|
|
|
ws.roomCode = roomCode;
|
2024-11-28 15:08:38 +00:00
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: "joined_room", roomCode,
|
|
|
|
isLeader: rooms[roomCode].leader === ws
|
|
|
|
}));
|
2024-11-19 12:16:26 +00:00
|
|
|
}
|
2024-11-28 15:08:38 +00:00
|
|
|
console.log('clients: ',rooms[roomCode].clients);
|
2024-11-19 12:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data.type === "start_game") {
|
|
|
|
const room = rooms[ws.roomCode];
|
2024-11-28 15:08:38 +00:00
|
|
|
room.gameState.started = true;
|
2024-11-19 12:16:26 +00:00
|
|
|
console.log('game started for:',room);
|
|
|
|
if (room && room.leader === ws) {
|
|
|
|
room.clients.forEach((client) => {
|
2024-11-28 15:08:38 +00:00
|
|
|
client.send(JSON.stringify({
|
|
|
|
type: "game_started",
|
|
|
|
roomCode: ws.roomCode,
|
|
|
|
puzzle:loadCurrentPuzzle(room.gameState)
|
|
|
|
}));
|
2024-11-19 12:16:26 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ws.send(JSON.stringify({ type: "error", message: "Only the leader can start the game" }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.type === "spin_wheel" || data.type === "guess_letter") {
|
|
|
|
const room = rooms[ws.roomCode];
|
|
|
|
if (room && room.clients.includes(ws)) {
|
|
|
|
// Handle spin and guess events
|
|
|
|
if (data.type === "spin_wheel") {
|
|
|
|
// Simulate a wheel spin result and update room state
|
|
|
|
const spinResult = getRandomValue(wheel);
|
|
|
|
room.gameState.spinResult = spinResult;
|
|
|
|
room.clients.forEach((client) =>
|
2024-11-28 15:08:38 +00:00
|
|
|
client.send(JSON.stringify({
|
|
|
|
type: "spin_result", spinResult,
|
|
|
|
player: ws === room.leader ? "Leader" : "Player"
|
|
|
|
}))
|
2024-11-19 12:16:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.type === "guess_letter") {
|
|
|
|
const { letter } = data;
|
|
|
|
// Handle guess logic (e.g., check if the letter is in the puzzle)
|
|
|
|
const correctGuess = Math.random() > 0.5; // Random outcome for simplicity
|
|
|
|
room.gameState.lastGuess = { letter, correct: correctGuess };
|
|
|
|
room.clients.forEach((client) =>
|
|
|
|
client.send(JSON.stringify({ type: "guess_result", letter, correct: correctGuess, player: ws === room.leader ? "Leader" : "Player" }))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ws.send(JSON.stringify({ type: "error", message: "You are not in this room" }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.on("close", () => {
|
|
|
|
if (ws.roomCode && rooms[ws.roomCode]) {
|
|
|
|
const room = rooms[ws.roomCode];
|
|
|
|
room.clients = room.clients.filter((client) => client !== ws);
|
|
|
|
if (room.leader === ws && room.clients.length > 0) {
|
|
|
|
room.leader = room.clients[0];
|
|
|
|
room.leader.send(JSON.stringify({ type: "new_leader" }));
|
|
|
|
}
|
|
|
|
if (room.clients.length === 0) delete rooms[ws.roomCode];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
console.log("WebSocket server is running on ws://localhost:8080");
|
2024-11-28 15:08:38 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
DefaultNames,NameIndex,
|
|
|
|
getRandomName,getRandomValue,shuffleArray
|
|
|
|
}
|