Notes
Notes - notes.io |
try {
console.log('Entering setupBinOrWin'); // Log 1
const roomID = req.body.roomID;
// Step 1: Find the game document and update necessary fields
const game = await Game.findOne({ roomID: roomID });
if (!game) {
return res.status(404).send('Game not found');
}
game.currentRound = 3;
game.gameState = 'BinOrWin';
// Filter and calculate MaximumJackpot
const numericBallValues = game.BinOrWinBalls
.filter(ball => ball !== 'Killer')
.map(ball => parseInt(ball, 10));
const maximumJackpot = numericBallValues.reduce((acc, curr) => acc + curr, 0);
game.MaximumJackpot = maximumJackpot;
// Then add a killer ball to the BinOrWinBalls array
game.BinOrWinBalls.push('Killer');
// Step 3: Remove eliminated players from both the game document and the database
const remainingPlayers = game.players.filter(player => !player.isEliminated);
for (const player of game.players) {
if (player.isEliminated) {
// Remove the player from the remainingPlayers array in the game document
game.remainingPlayers = game.remainingPlayers.filter(id => !id.equals(player._id));
// Remove the nested player document from the game document
const nestedPlayer = game.players.id(player._id);
if (nestedPlayer) {
nestedPlayer.deleteOne(); // Use remove() method to remove the nested document
}
// Find and remove the eliminated player from the database
await Player.deleteOne({ _id: player._id });
}
}
// Step 4: Update the playerNumbers for the remaining players
let newPlayerNumber = 1;
for (const player of remainingPlayers) {
player.playerNumber = newPlayerNumber++;
const playerDoc = await Player.findById(player._id);
if (playerDoc) {
playerDoc.playerNumber = player.playerNumber;
await playerDoc.save();
}
}
// Update the game.players to the remainingPlayers array
game.players = remainingPlayers;
// Save the updated game document
await game.save();
// Notify clients with the redirect URL directly
const wss = websocket.getWss();
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
// Find the matching player for this client
const player = remainingPlayers.find(p => p.name === client.playerName);
if (player) {
const redirectUrl = `/binorwin.html?roomID=${roomID}&playerName=${player.name}&hash=${player.hash}&playerNumber=${player.playerNumber}`;
client.send(JSON.stringify({
type: 'redirect',
url: redirectUrl
}));
} else {
console.log(`No matching player found for client ${client.playerName}`);
}
}
});
res.send('Setup for Bin or Win completed!');
} catch (error) {
console.error('Error setting up Bin or Win:', error);
res.status(500).send('Internal Server Error');
}
};
|
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team