fixed the backend server dying when someone leaves... kind of shipping anyway
This commit is contained in:
parent
e2f2b0c713
commit
396b1ea5bf
12
index.html
12
index.html
@ -90,6 +90,9 @@
|
|||||||
background-color: #efefef;
|
background-color: #efefef;
|
||||||
color: gray;
|
color: gray;
|
||||||
}
|
}
|
||||||
|
.footerMsg {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@ -149,7 +152,15 @@
|
|||||||
<canvas id="wheelCanvas" width="500" height="500"></canvas>
|
<canvas id="wheelCanvas" width="500" height="500"></canvas>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
<footer class="flx(column) middle center is-full mt-5" style="">
|
||||||
|
<span class="footerMsg mb-3"></span>
|
||||||
|
<a href='https://ko-fi.com/I3I0G1CQM' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi5.png?v=6' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
|
||||||
|
</footer>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
let currentYear = new Date().getFullYear();
|
||||||
|
console.log(currentYear);
|
||||||
|
document.querySelector('.footerMsg').innerText = `Made by Meleeman 🦊 © ${currentYear}`;
|
||||||
//================general Purpose functions=========================================//
|
//================general Purpose functions=========================================//
|
||||||
function cryptoSecureShuffle(array) {
|
function cryptoSecureShuffle(array) {
|
||||||
const shuffledArray = [...array]; // Create a copy to avoid mutating the original array
|
const shuffledArray = [...array]; // Create a copy to avoid mutating the original array
|
||||||
@ -801,6 +812,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (message.type === "new_leader") {
|
if (message.type === "new_leader") {
|
||||||
|
console.log('you are the new leader!');
|
||||||
drawPlayers(message);
|
drawPlayers(message);
|
||||||
document.getElementById("status").innerText = "You are now the party leader!";
|
document.getElementById("status").innerText = "You are now the party leader!";
|
||||||
}
|
}
|
||||||
|
56
server.js
56
server.js
@ -1,6 +1,6 @@
|
|||||||
const WebSocket = require('ws')
|
const WebSocket = require('ws')
|
||||||
const { v4: uuidv4 } = require('uuid')
|
const { v4: uuidv4 } = require('uuid')
|
||||||
const puzzles = require('./puzzles2.json')
|
const puzzles = require('./puzzles.json')
|
||||||
const names = require('./assignedNames.json')
|
const names = require('./assignedNames.json')
|
||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
|
|
||||||
@ -330,7 +330,7 @@ function changeTurn(gameStateObject,ws) {
|
|||||||
console.log('change turn hit')
|
console.log('change turn hit')
|
||||||
const room = rooms[ws.roomCode]
|
const room = rooms[ws.roomCode]
|
||||||
|
|
||||||
if (gameStateObject.turn === gameStateObject.players.length - 1) {
|
if (gameStateObject.turn >= gameStateObject.players.length - 1) {
|
||||||
gameStateObject.turn = 0
|
gameStateObject.turn = 0
|
||||||
}
|
}
|
||||||
else gameStateObject.turn++
|
else gameStateObject.turn++
|
||||||
@ -681,27 +681,71 @@ wss.on('connection', (ws) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
function moveToNextPlayer(room,playerLeaving) {
|
||||||
|
console.log(room)
|
||||||
|
let gameStateObject = room.gameState
|
||||||
|
console.log('moveToNextPlayer', gameStateObject)
|
||||||
|
//we need to take note of where teh players are before the leaving happens.
|
||||||
|
for (let p in gameStateObject.players) {
|
||||||
|
if (gameStateObject.players[p].id == playerLeaving.id) {
|
||||||
|
|
||||||
|
if (gameStateObject.players[p+1] === undefined) {
|
||||||
|
gameStateObject.turn = 0
|
||||||
|
}
|
||||||
|
else gameStateObject.turn = p
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gameStateObject.players = gameStateObject.players.filter((client)=> client.id != ws.identifierToken)
|
||||||
|
|
||||||
|
//alert next user of their turn
|
||||||
|
room.clients.forEach((client) => {
|
||||||
|
client.send(JSON.stringify(
|
||||||
|
{ type: 'next_turn',
|
||||||
|
user:room.gameState.players[room.gameState.turn].name,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
//set the game index and initiate a turn;
|
||||||
|
}
|
||||||
ws.on('close', () => {
|
ws.on('close', () => {
|
||||||
if (ws.roomCode && rooms[ws.roomCode]) {
|
if (ws.roomCode && rooms[ws.roomCode]) {
|
||||||
|
|
||||||
console.log('closing')
|
console.log('closing')
|
||||||
const room = rooms[ws.roomCode]
|
const room = rooms[ws.roomCode]
|
||||||
const roomCode = ws.roomCode
|
const roomCode = ws.roomCode
|
||||||
|
|
||||||
|
const playerLeaving = room.gameState.players.filter((client) => client.id == ws.identifierToken)[0]
|
||||||
|
|
||||||
|
console.log(playerLeaving)
|
||||||
room.clients = room.clients.filter((client) => client !== ws)
|
room.clients = room.clients.filter((client) => client !== ws)
|
||||||
room.gameState.players = room.gameState.players.filter((client)=> client.id != ws.identifierToken)
|
|
||||||
|
room.gameState.turnState = 'spin'
|
||||||
|
//if the client who's leaving isn't taking their turn, use the changeTurnFunction
|
||||||
|
moveToNextPlayer(room,playerLeaving)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
console.log('in close',room.gameState.players)
|
||||||
//decide who's turn it is based on who leaves...
|
//decide who's turn it is based on who leaves...
|
||||||
if (room.leader === ws && room.clients.length > 0) {
|
if (room.leader === ws && room.clients.length > 0) {
|
||||||
console.log('closing within room leader')
|
console.log('closing within room leader')
|
||||||
room.leader = room.clients[0]
|
room.leader = room.clients[0]
|
||||||
room.leader.send(JSON.stringify({ type: 'new_leader' }))
|
room.leader.send(JSON.stringify({
|
||||||
|
type: 'new_leader',
|
||||||
|
leaderName:rooms[roomCode].leader.name,
|
||||||
|
playerStats:room.gameState.players.map(({id,...rest}) => rest),
|
||||||
|
turn:room.gameState.turn,
|
||||||
|
turnState:room.gameState.turnState
|
||||||
|
}))
|
||||||
|
|
||||||
}
|
}
|
||||||
room.clients.forEach((client) =>
|
room.clients.forEach((client) =>
|
||||||
client.send(JSON.stringify({
|
client.send(JSON.stringify({
|
||||||
type: 'joined_room', roomCode,
|
type: 'joined_room', roomCode,
|
||||||
isLeader: rooms[roomCode].leader === ws ,
|
isLeader: rooms[roomCode].leader === ws ,
|
||||||
clients: rooms[roomCode].clients.map((i)=> i.name),
|
playerStats:room.gameState.players.map(({id,...rest}) => rest),
|
||||||
leaderName:rooms[roomCode].leader.name
|
leaderName:rooms[roomCode].leader.name,
|
||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
if (room.clients.length === 0) delete rooms[ws.roomCode]
|
if (room.clients.length === 0) delete rooms[ws.roomCode]
|
||||||
|
Loading…
Reference in New Issue
Block a user