Compare commits
2 Commits
c8bfab7dbc
...
5a3095aa73
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5a3095aa73 | ||
![]() |
7f519d42cb |
@ -343,6 +343,7 @@
|
|||||||
//first we draw the spaces
|
//first we draw the spaces
|
||||||
let puzzle = message.puzzle.puzzle;
|
let puzzle = message.puzzle.puzzle;
|
||||||
let puzzleBoard = document.querySelector('.puzzleboard');
|
let puzzleBoard = document.querySelector('.puzzleboard');
|
||||||
|
|
||||||
let resultingPuzzleBoard = [];
|
let resultingPuzzleBoard = [];
|
||||||
//if the first entry of the puzzle is an array and not a string display the puzzle differently
|
//if the first entry of the puzzle is an array and not a string display the puzzle differently
|
||||||
if (Array.isArray(message.puzzle.puzzle[0])) {
|
if (Array.isArray(message.puzzle.puzzle[0])) {
|
||||||
@ -366,6 +367,7 @@
|
|||||||
resultingPuzzleBoard.push(Array(12).fill(null));
|
resultingPuzzleBoard.push(Array(12).fill(null));
|
||||||
console.log(resultingPuzzleBoard);
|
console.log(resultingPuzzleBoard);
|
||||||
//after we've drawn our data for our board,now its time to visualize it to the user
|
//after we've drawn our data for our board,now its time to visualize it to the user
|
||||||
|
puzzleBoard.innerHTML = ``;
|
||||||
for(let i of resultingPuzzleBoard) {
|
for(let i of resultingPuzzleBoard) {
|
||||||
for (let j in i) {
|
for (let j in i) {
|
||||||
const letter = i[j];
|
const letter = i[j];
|
||||||
@ -438,7 +440,7 @@
|
|||||||
|
|
||||||
document.getElementById("guess-button").onclick = () => {
|
document.getElementById("guess-button").onclick = () => {
|
||||||
const letter = document.getElementById("guess-letter").value;
|
const letter = document.getElementById("guess-letter").value;
|
||||||
socket.send(JSON.stringify({ type: "guess_letter", letter }));
|
socket.send(JSON.stringify({ type: "guess_letter", letter}));
|
||||||
document.getElementById("guess-letter").value = ""; // Clear input
|
document.getElementById("guess-letter").value = ""; // Clear input
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -521,6 +523,11 @@
|
|||||||
if (message.type === "guess_result") {
|
if (message.type === "guess_result") {
|
||||||
const outcome = message.correct ? "correct" : "incorrect";
|
const outcome = message.correct ? "correct" : "incorrect";
|
||||||
document.getElementById("status").innerText = `Guess '${message.letter}' was ${outcome} (${message.player})`;
|
document.getElementById("status").innerText = `Guess '${message.letter}' was ${outcome} (${message.player})`;
|
||||||
|
console.log(message);
|
||||||
|
if (message.puzzle) {
|
||||||
|
drawPuzzle(message);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.type === "new_leader") {
|
if (message.type === "new_leader") {
|
||||||
|
147
server.js
147
server.js
@ -11,7 +11,27 @@ const wheel = [
|
|||||||
300,600,300,500,800,550,400,300,900,500,'spin again',
|
300,600,300,500,800,550,400,300,900,500,'spin again',
|
||||||
900,'Bankrupt',600,400,300
|
900,'Bankrupt',600,400,300
|
||||||
] //represents wheel in wheel of fortune game.
|
] //represents wheel in wheel of fortune game.
|
||||||
|
function removeProp(obj, prop) {
|
||||||
|
obj = JSON.parse(JSON.stringify(obj))
|
||||||
|
if(!Array.isArray(prop)){
|
||||||
|
if (!Object.hasOwn(obj,prop)) {
|
||||||
|
console.error(`the property '${prop}' you are trying to remove doesn't exist on this object!`)
|
||||||
|
} else {
|
||||||
|
delete obj[prop]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (let i = 0; i < prop.length; i++) {
|
||||||
|
const key = prop[i]
|
||||||
|
if (!Object.hasOwn(obj, key)) {
|
||||||
|
console.error(`The property '${key}' you are trying to remove doesn't exist on this object!`)
|
||||||
|
} else {
|
||||||
|
delete obj[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj
|
||||||
|
}
|
||||||
//make sure this is run so puzzles are easier to contruct
|
//make sure this is run so puzzles are easier to contruct
|
||||||
function checkPuzzleData(puzzles) {
|
function checkPuzzleData(puzzles) {
|
||||||
for(let puzzle in puzzles) {
|
for(let puzzle in puzzles) {
|
||||||
@ -99,11 +119,10 @@ function loadCurrentPuzzle(gameStateObject) {
|
|||||||
formattedPuzzle.push(processPuzzle(letterArray,given))
|
formattedPuzzle.push(processPuzzle(letterArray,given))
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
'puzzleLevel':gameStateObject.puzzleLevel,
|
'answer':gameStateObject.puzzles[gameStateObject.puzzleLevel].answer,
|
||||||
'given':gameStateObject.puzzles[gameStateObject.puzzleLevel].given,
|
'given':gameStateObject.puzzles[gameStateObject.puzzleLevel].given,
|
||||||
'category':gameStateObject.puzzles[gameStateObject.puzzleLevel].category,
|
'category':gameStateObject.puzzles[gameStateObject.puzzleLevel].category,
|
||||||
'puzzle':formattedPuzzle,
|
'puzzle':formattedPuzzle,
|
||||||
'levelsRemaining':(gameStateObject.puzzles.length - 1 - gameStateObject.puzzleLevel)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -113,11 +132,10 @@ function loadCurrentPuzzle(gameStateObject) {
|
|||||||
//console.log(gameStateObject.puzzles[gameStateObject.puzzleLevel].puzzle);
|
//console.log(gameStateObject.puzzles[gameStateObject.puzzleLevel].puzzle);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'puzzleLevel':gameStateObject.puzzleLevel,
|
'answer':gameStateObject.puzzles[gameStateObject.puzzleLevel].answer,
|
||||||
'given':gameStateObject.puzzles[gameStateObject.puzzleLevel].given,
|
'given':gameStateObject.puzzles[gameStateObject.puzzleLevel].given,
|
||||||
'category':gameStateObject.puzzles[gameStateObject.puzzleLevel].category,
|
'category':gameStateObject.puzzles[gameStateObject.puzzleLevel].category,
|
||||||
'puzzle':gameStateObject.puzzles[gameStateObject.puzzleLevel].puzzle,
|
'puzzle':gameStateObject.puzzles[gameStateObject.puzzleLevel].puzzle,
|
||||||
'levelsRemaining':(gameStateObject.puzzles.length - 1 - gameStateObject.puzzleLevel)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -127,12 +145,48 @@ function checkGuess(letter,gameStateObject) {
|
|||||||
const currentPuzzle = gameStateObject.puzzles[gameStateObject.puzzleLevel]
|
const currentPuzzle = gameStateObject.puzzles[gameStateObject.puzzleLevel]
|
||||||
|
|
||||||
console.log(currentPuzzle)
|
console.log(currentPuzzle)
|
||||||
if (typeof currentPuzzle.puzzle[0] == string) {
|
if (typeof currentPuzzle.answer == 'string') {
|
||||||
|
//first create an indexed hashMap
|
||||||
|
|
||||||
|
let charArray = currentPuzzle.answer.split('')
|
||||||
|
let matches = []
|
||||||
|
for(const c in charArray) {
|
||||||
|
const char = charArray[c]
|
||||||
|
if (letter.toUpperCase() == char.toUpperCase()) {
|
||||||
|
matches.push({id:c,letter:char})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//if there are any matches write them to the gameState Object
|
||||||
|
if (!matches.length) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for(const m of matches) {
|
||||||
|
gameStateObject.puzzles[gameStateObject.puzzleLevel].puzzle[m.id] = m.letter
|
||||||
|
}
|
||||||
|
console.log('matches found!!!', gameStateObject.puzzles[gameStateObject.puzzleLevel].puzzle)
|
||||||
|
return [true, matches.length]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else if (Array.isArray(currentPuzzle.answer)) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
else console.error('invalid input to checkGuess() function',currentPuzzle.puzzle[0])
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// function checkIfClientExistsInRoom(ws,room) {
|
||||||
|
// console.log(room,ws)
|
||||||
|
// //assumes ws message is sent and room code exists
|
||||||
|
// for(const c in room.clients) {
|
||||||
|
// const client = room.clients[c]
|
||||||
|
// if (ws.identifierToken == client.identifierToken && ws.roomCode == client.roomCode) {
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
checkPuzzleData(puzzles)
|
checkPuzzleData(puzzles)
|
||||||
|
|
||||||
// server.js
|
// server.js
|
||||||
@ -191,8 +245,12 @@ wss.on('connection', (ws) => {
|
|||||||
started:false,
|
started:false,
|
||||||
puzzles:shuffleArray(puzzles),
|
puzzles:shuffleArray(puzzles),
|
||||||
puzzleLevel:0,
|
puzzleLevel:0,
|
||||||
|
turn:0,
|
||||||
|
turnState:null,
|
||||||
|
players:[]
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
console.log(rooms[roomCode].clients[0].name)
|
||||||
ws.roomCode = roomCode
|
ws.roomCode = roomCode
|
||||||
ws.send(JSON.stringify({ type: 'room_created', roomCode,
|
ws.send(JSON.stringify({ type: 'room_created', roomCode,
|
||||||
isLeader: true,
|
isLeader: true,
|
||||||
@ -240,11 +298,16 @@ wss.on('connection', (ws) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data.type === 'start_game') {
|
if (data.type === 'start_game') {
|
||||||
|
|
||||||
const room = rooms[ws.roomCode]
|
const room = rooms[ws.roomCode]
|
||||||
|
const currentPuzzle = loadCurrentPuzzle(room.gameState)
|
||||||
|
// const clientPuzzle = Object.entries(currentPuzzle).filter(([key])=> key != 'answer')
|
||||||
|
|
||||||
room.gameState.started = true
|
room.gameState.started = true
|
||||||
room.gameState.turn = ws.name
|
|
||||||
room.gameState.turnState = 'spin'
|
room.gameState.turnState = 'spin'
|
||||||
room.gameState.players = []
|
room.gameState.puzzles[room.gameState.puzzleLevel] = currentPuzzle
|
||||||
|
room.gameState.levelsRemaining = (room.gameState.puzzles.length - 1 - room.gameState.puzzleLevel)
|
||||||
room.clients.forEach((client)=>{
|
room.clients.forEach((client)=>{
|
||||||
room.gameState.players.push({
|
room.gameState.players.push({
|
||||||
id:client.identifierToken,
|
id:client.identifierToken,
|
||||||
@ -260,7 +323,8 @@ wss.on('connection', (ws) => {
|
|||||||
client.send(JSON.stringify({
|
client.send(JSON.stringify({
|
||||||
type: 'game_started',
|
type: 'game_started',
|
||||||
roomCode: ws.roomCode,
|
roomCode: ws.roomCode,
|
||||||
puzzle:loadCurrentPuzzle(room.gameState),
|
// eslint-disable-next-line no-undef
|
||||||
|
puzzle: removeProp(currentPuzzle,'answer'),
|
||||||
turn:room.gameState.turn,
|
turn:room.gameState.turn,
|
||||||
turnState:room.gameState.turnState
|
turnState:room.gameState.turnState
|
||||||
}))
|
}))
|
||||||
@ -274,11 +338,28 @@ wss.on('connection', (ws) => {
|
|||||||
const room = rooms[ws.roomCode]
|
const room = rooms[ws.roomCode]
|
||||||
if (room && room.clients.includes(ws)) {
|
if (room && room.clients.includes(ws)) {
|
||||||
// Handle spin and guess events
|
// Handle spin and guess events
|
||||||
|
|
||||||
if (data.type === 'spin_wheel') {
|
if (data.type === 'spin_wheel') {
|
||||||
|
if (room.gameState.turnState === null) {
|
||||||
|
ws.send(JSON.stringify({ type: 'error', message: 'the game hasn\'t started yet!' }))
|
||||||
|
return
|
||||||
|
}else if (ws.identifierToken !== room.gameState.players[room.gameState.turn].id) {
|
||||||
|
ws.send(JSON.stringify({ type: 'error', message: 'its not your turn to spin!' }))
|
||||||
|
return
|
||||||
|
}
|
||||||
// Simulate a wheel spin result and update room state
|
// Simulate a wheel spin result and update room state
|
||||||
const spinResult = getRandomValue(wheel)
|
let spinResult = getRandomValue(wheel)
|
||||||
|
if (spinResult == 'Bankrupt') {
|
||||||
|
spinResult = 0
|
||||||
|
}
|
||||||
|
if (spinResult == 'spin again') {
|
||||||
|
spinResult = 0
|
||||||
|
room.gameState.players = room.gameState.players.map((player) => {
|
||||||
|
return player.id == ws.identifierToken ? {...player, points:player.points + spinResult, condition:'spin again'} : player
|
||||||
|
})
|
||||||
|
}
|
||||||
room.gameState.players = room.gameState.players.map((player) => {
|
room.gameState.players = room.gameState.players.map((player) => {
|
||||||
return player.id == ws.identifierToken ? {...player, points:spinResult} : player
|
return player.id == ws.identifierToken ? {...player, points:player.points + spinResult} : player
|
||||||
})
|
})
|
||||||
console.log('players', room.gameState.players)
|
console.log('players', room.gameState.players)
|
||||||
room.gameState.spinResult = spinResult
|
room.gameState.spinResult = spinResult
|
||||||
@ -292,16 +373,48 @@ wss.on('connection', (ws) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data.type === 'guess_letter') {
|
if (data.type === 'guess_letter') {
|
||||||
|
if (room.gameState.turnState === null) {
|
||||||
|
ws.send(JSON.stringify({ type: 'error', message: 'the game hasn\'t started yet!' }))
|
||||||
|
return
|
||||||
|
}else if (ws.identifierToken !== room.gameState.players[room.gameState.turn].id) {
|
||||||
|
ws.send(JSON.stringify({ type: 'error', message: 'its not your turn to guess!' }))
|
||||||
|
return
|
||||||
|
}
|
||||||
room.gameState.turnState = 'spin'
|
room.gameState.turnState = 'spin'
|
||||||
const { letter } = data
|
const { letter } = data
|
||||||
// Handle guess logic (e.g., check if the letter is in the puzzle)
|
// Handle guess logic (e.g., check if the letter is in the puzzle)
|
||||||
const correctGuess = Math.random() > 0.5 // Random outcome for simplicity
|
|
||||||
|
|
||||||
checkGuess(letter,room.gameState)
|
const guessResult = checkGuess(letter,room.gameState)
|
||||||
room.gameState.lastGuess = { letter, correct: correctGuess }
|
|
||||||
room.clients.forEach((client) =>
|
if (!guessResult) {
|
||||||
client.send(JSON.stringify({ type: 'guess_result', letter, correct: correctGuess, player: ws === room.leader ? 'Leader' : 'Player' }))
|
//if the player guesses incorrectly, and theres more than one player,
|
||||||
)
|
//its the next players turn
|
||||||
|
room.clients.forEach((client) => {
|
||||||
|
client.send(JSON.stringify(
|
||||||
|
{ type: 'guess_result', letter,
|
||||||
|
correct: guessResult,
|
||||||
|
player: ws === room.leader ? 'Leader' : 'Player' }))
|
||||||
|
})
|
||||||
|
|
||||||
|
if (room.gameState.turn == room.gameState.players.length - 1) {
|
||||||
|
room.gameState.turn = 0
|
||||||
|
}
|
||||||
|
else room.gameState.turn++
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//the player guessed correctly and its still their turn
|
||||||
|
room.clients.forEach((client) =>
|
||||||
|
client.send(JSON.stringify(
|
||||||
|
{ type: 'guess_result', letter,
|
||||||
|
correct: guessResult,
|
||||||
|
player: ws === room.leader ? 'Leader' : 'Player',
|
||||||
|
puzzle: room.gameState.puzzles[room.gameState.puzzleLevel],
|
||||||
|
turn:room.gameState.turn,
|
||||||
|
turnState:room.gameState.turnState
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ws.send(JSON.stringify({ type: 'error', message: 'You are not in this room' }))
|
ws.send(JSON.stringify({ type: 'error', message: 'You are not in this room' }))
|
||||||
|
Loading…
Reference in New Issue
Block a user