game\index.html [code] I need you to remove the robot players from the game's logic and…
- User
game\index.html
code - Assistant
game\index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Card Bluff & Shoot</title>
<style>
body {
margin: 0;
padding: 20px;
background: #1a1a1a;
color: white;
font-family: Arial, sans-serif;
}
.game-container {
width: 100%;
min-height: 100vh;
display: grid;
grid-template-rows: auto 2fr 1fr;
gap: 20px;
}
.top-section, .bottom-section {
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
}
.player {
background: #333;
padding: 10px 20px;
border-radius: 8px;
text-align: center;
margin: 5px;
min-width: 120px;
position: relative;
}
.player.active {
border: 2px solid #4CAF50;
}
.player.eliminated {
background: #555;
opacity: 0.6;
text-decoration: line-through;
}
.player .bullets {
margin-top: 5px;
font-size: 14px;
}
.middle-section {
display: flex;
flex-direction: column;
align-items: center;
gap: 15px;
}
.game-info {
text-align: center;
font-size: 1.2em;
color: #ddd;
}
.player-hand {
display: flex;
gap: 10px;
flex-wrap: wrap;
justify-content: center;
}
.card {
width: 60px;
height: 90px;
background: white;
border-radius: 5px;
display: flex;
justify-content: center;
align-items: center;
color: black;
cursor: pointer;
user-select: none;
border: 2px solid transparent;
transition: border 0.3s;
position: relative;
font-size: 1em;
}
.card.selected {
border: 2px solid yellow;
}
.card.back {
background: #333;
color: #333;
cursor: default;
}
.actions {
display: flex;
gap: 10px;
margin-top: 10px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
background: #4CAF50;
color: white;
transition: background 0.3s;
}
button:hover {
background: #45a049;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.9);
display: none;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 20px;
z-index: 1000;
}
.overlay.active {
display: flex;
}
#debugControls {
position: fixed;
bottom: 10px;
right: 10px;
background: #333;
padding: 10px;
border-radius: 5px;
display: flex;
gap: 10px;
}
/* Modal Styles */
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.8);
display: none;
justify-content: center;
align-items: center;
z-index: 2000;
}
.modal.active {
display: flex;
}
.modal-content {
background: #222;
padding: 20px 30px;
border-radius: 10px;
text-align: center;
color: #ddd;
}
.modal-content h2 {
margin-bottom: 15px;
}
.modal-content input {
padding: 10px;
width: 80%;
margin-bottom: 15px;
border: none;
border-radius: 5px;
}
.modal-content button {
padding: 10px 20px;
background: #4CAF50;
border: none;
border-radius: 5px;
cursor: pointer;
color: white;
}
.modal-content button:hover {
background: #45a049;
}
@media (max-width: 600px) {
.player {
min-width: 100px;
padding: 8px 15px;
}
.card {
width: 50px;
height: 75px;
font-size: 0.9em;
}
}
/* Central Table Styles */
.central-table {
display: flex;
gap: 10px;
flex-wrap: wrap;
justify-content: center;
margin-top: 20px;
}
.central-table .card {
cursor: default;
}
/* Suit Color Styling */
.card.spades, .card.clubs {
color: black;
}
.card.hearts, .card.diamonds {
color: red;
}
</style>
</head>
<body>
<!-- Player Setup Modal (Hidden) -->
<div class="modal" id="setupModal">
<div class="modal-content">
<h2>Setup Players</h2>
<div id="playerInputs">
<input type="text" placeholder="Player 1 Name" required>
<input type="text" placeholder="Player 2 Name" required>
<input type="text" placeholder="Player 3 Name" required>
<input type="text" placeholder="Player 4 Name (Optional)">
<input type="text" placeholder="Player 5 Name (Optional)">
</div>
<button onclick="startGame()">Start Game</button>
</div>
</div>
<div class="game-container">
<div class="top-section" id="players"></div>
<div class="middle-section">
<div class="game-info">
<strong>Core Card:</strong> <span id="coreCard">-</span>
</div>
<div class="game-info">
<strong>Last Played:</strong> <span id="lastPlayed">None</span>
</div>
<!-- Central Card Table -->
<div class="central-table" id="centralTable"></div>
</div>
<div class="bottom-section">
<div class="player-hand" id="currentHand"></div>
<div class="actions">
<button id="playButton" onclick="game.playSelectedCards()">Play Selected Cards</button>
<button id="callBluffButton" onclick="game.callBluff()" style="background: #f44336">Call Bluff</button>
</div>
</div>
</div>
<div class="overlay" id="shootingOverlay">
<h2>🎲 Russian Roulette Time! 🔫</h2>
<button onclick="game.pullTrigger()" style="padding: 15px 30px; font-size: 1em;">Pull Trigger</button>
</div>
<div id="debugControls">
<button onclick="game.startNewGame()">New Game</button>
</div>
<script>
// Card Class
class Card {
constructor(value, suit) {
this.value = value;
this.suit = suit;
}
toString() {
return `${this.value}${this.suit}`;
}
getSuitClass() {
return this.suit === '♠' ? 'spades' :
this.suit === '♣' ? 'clubs' :
this.suit === '♥' ? 'hearts' :
'diamonds';
}
getColor() {
return (this.suit === '♥' || this.suit === '♦') ? 'red' : 'black';
}
}
// Player Class
class Player {
constructor(id, name, isHuman = false) {
this.id = id;
this.name = name;
this.hand = [];
this.bulletSlots = 6;
this.isEliminated = false;
this.isHuman = isHuman;
}
}
// Game Class
class Game {
constructor() {
this.players = [];
this.currentPlayerIndex = 0;
this.coreCard = null;
this.lastPlayedCards = [];
this.deck = [];
this.shootingPlayer = null;
this.isGameOver = false;
this.centralTableCards = []; // To keep track of displayed cards
this.initializeDeck();
this.initializePlayers();
this.startNewGame();
this.render();
}
// Initialize the deck of cards
initializeDeck() {
const values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
const suits = ['♠', '♥', '♦', '♣'];
this.deck = [];
for (let value of values) {
for (let suit of suits) {
this.deck.push(new Card(value, suit));
}
}
}
// Automatically initialize players: You + 3 Robots
initializePlayers() {
this.players = [];
this.players.push(new Player(0, 'You', true));
this.players.push(new Player(1, 'Robot 1'));
this.players.push(new Player(2, 'Robot 2'));
this.players.push(new Player(3, 'Robot 3'));
}
// Shuffle the deck using Fisher-Yates algorithm
shuffleDeck() {
for (let i = this.deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.deck[i], this.deck[j]] = [this.deck[j], this.deck[i]];
}
}
// Start a new game
startNewGame() {
this.initializeDeck();
this.shuffleDeck();
this.currentPlayerIndex = 0;
this.lastPlayedCards = [];
this.centralTableCards = [];
this.coreCard = null;
this.isGameOver = false;
this.players.forEach(player => {
player.hand = [];
player.bulletSlots = 6;
player.isEliminated = false;
});
this.dealNewRound();
this.render();
}
// Deal hands for a new round
dealNewRound() {
this.shuffleDeck();
// Ensure coreCard is set once per round and persists
if (!this.coreCard) {
// Assign the first card as the core card and remove it from the deck
this.coreCard = this.deck.shift();
}
// Ensure enough cards for all players
const requiredCards = this.players.filter(p => !p.isEliminated).length * 5;
if (this.deck.length < requiredCards) {
// Reinitialize and shuffle again if not enough cards
this.initializeDeck();
this.shuffleDeck();
this.coreCard = this.deck.shift();
}
let cardIndex = 0;
this.players.forEach(player => {
if (!player.isEliminated) {
player.hand = this.deck.slice(cardIndex, cardIndex + 5);
cardIndex += 5;
}
});
this.lastPlayedCards = [];
this.centralTableCards = [];
}
// Get the current active player
getCurrentPlayer() {
return this.players[this.currentPlayerIndex];
}
// Move to the next active player
nextPlayer() {
if (this.isGameOver) return;
let attempts = 0;
do {
this.currentPlayerIndex = (this.currentPlayerIndex + 1) % this.players.length;
attempts++;
if (attempts > this.players.length) break; // Prevent infinite loop
} while (this.getCurrentPlayer().isEliminated);
}
// Play selected cards
playSelectedCards() {
const selectedCardsElements = Array.from(document.querySelectorAll('.card.selected'));
const selectedIndices = selectedCardsElements.map(el => parseInt(el.dataset.index));
const selectedCards = selectedIndices.map(index => this.getCurrentPlayer().hand[index]);
if (selectedCards.length === 0) {
alert('Please select at least one card to play.');
return;
}
// Remove played cards from player's hand
selectedCards.sort((a, b) => this.getCurrentPlayer().hand.indexOf(b) - this.getCurrentPlayer().hand.indexOf(a));
selectedCards.forEach(card => {
const idx = this.getCurrentPlayer().hand.indexOf(card);
if (idx > -1) {
this.getCurrentPlayer().hand.splice(idx, 1);
}
});
this.lastPlayedCards = selectedCards;
// Add cards to central table as face-down
selectedCards.forEach(card => {
const cardObj = {
card: card,
revealed: false
};
this.centralTableCards.push(cardObj);
});
// Update last played description to show the declared value
this.updateLastPlayedDescription(false, selectedCards.length);
this.nextPlayer();
this.render();
// If the next player is a robot and the game is not over, make it act automatically
const nextPlayer = this.getCurrentPlayer();
if (!nextPlayer.isHuman && !this.isGameOver) {
setTimeout(() => {
this.robotDecide(nextPlayer);
}, 1000);
}
}
// Robot decision: play or call bluff
robotDecide(player) {
if (player.isEliminated || this.isGameOver) return;
// Decide randomly to play or call bluff based on previous actions
if (this.lastPlayedCards.length > 0) {
// 30% chance to call bluff
if (Math.random() < 0.3) {
this.callBluff();
return;
}
}
// Play cards
this.robotPlay(player);
}
// Robot player action: play cards
robotPlay(player) {
if (player.isEliminated || this.isGameOver) return;
const numCards = Math.min(3, player.hand.length);
if (numCards === 0) return;
// Select random cards to play
const selectedIndices = [];
while (selectedIndices.length < numCards) {
const randIndex = Math.floor(Math.random() * player.hand.length);
if (!selectedIndices.includes(randIndex)) {
selectedIndices.push(randIndex);
}
}
// Select the cards in the UI (simulate selection)
selectedIndices.forEach(index => {
const cardEl = document.querySelector(`.card[data-index="${index}"]`);
if (cardEl) {
cardEl.classList.add('selected');
}
});
setTimeout(() => {
this.playSelectedCards();
}, 1000);
}
// Call bluff on the previous player's last play
callBluff() {
if (this.lastPlayedCards.length === 0) {
alert('No cards have been played to call bluff!');
return;
}
const previousPlayerIndex = (this.currentPlayerIndex - 1 + this.players.length) % this.players.length;
const previousPlayer = this.players[previousPlayerIndex];
// Determine if the previous player was bluffing
const isBluff = !this.lastPlayedCards.every(card => card.value === this.coreCard.value);
this.shootingPlayer = isBluff ? previousPlayer : this.getCurrentPlayer();
// Reveal the last played cards if bluff was called
if (this.lastPlayedCards.length > 0) {
this.centralTableCards.forEach(cardObj => {
if (this.lastPlayedCards.includes(cardObj.card)) {
cardObj.revealed = true;
}
});
}
// Update last played description to indicate bluff
this.updateLastPlayedDescription(isBluff, this.lastPlayedCards.length);
// Show the shooting overlay
document.getElementById('shootingOverlay').classList.add('active');
this.render();
}
// Update the "Last Played" description based on bluff
updateLastPlayedDescription(isBluff, numCards) {
const lastPlayedSpan = document.getElementById('lastPlayed');
if (isBluff) {
lastPlayedSpan.textContent = 'Bluff Called!';
} else {
lastPlayedSpan.textContent = `${numCards} card(s) played as ${this.coreCard.value}${this.coreCard.suit}`;
}
}
// Handle the trigger pull
pullTrigger() {
document.getElementById('shootingOverlay').classList.remove('active');
const chance = 1 / this.shootingPlayer.bulletSlots;
const hitBullet = Math.random() < chance;
if (hitBullet) {
alert(`${this.shootingPlayer.name} has been shot and is eliminated!`);
this.shootingPlayer.isEliminated = true;
// Remove one bullet slot
this.shootingPlayer.bulletSlots--;
// Check if game is over
const remainingPlayers = this.players.filter(p => !p.isEliminated);
if (remainingPlayers.length === 1) {
alert(`Game Over! ${remainingPlayers[0].name} wins! 👑`);
this.isGameOver = true;
return;
}
// Reset coreCard for the next round
this.coreCard = null;
} else {
alert(`${this.shootingPlayer.name} survived the shot!`);
this.shootingPlayer.bulletSlots--;
}
// Start a new round
this.dealNewRound();
this.nextPlayer();
this.render();
// If the next player is a robot and the game is not over, make it act automatically
const nextPlayer = this.getCurrentPlayer();
if (!nextPlayer.isHuman && !this.isGameOver) {
setTimeout(() => {
this.robotDecide(nextPlayer);
}, 1000);
}
}
// Render the game state to the UI
render() {
// Render players
const playersContainer = document.getElementById('players');
playersContainer.innerHTML = '';
this.players.forEach(player => {
const playerEl = document.createElement('div');
playerEl.className = `player ${player === this.getCurrentPlayer() ? 'active' : ''} ${player.isEliminated ? 'eliminated' : ''}`;
playerEl.innerHTML = `
<strong>${player.name}${player.isHuman ? ' (You)' : ''}</strong>
<div class="bullets">Bullets: ${player.bulletSlots}/6</div>
`;
playersContainer.appendChild(playerEl);
});
// Render core card
const coreCardSpan = document.getElementById('coreCard');
if (this.coreCard) {
const coreCard = this.coreCard; // This is now a Card object
coreCardSpan.innerHTML = this.createCardHTML(coreCard);
} else {
coreCardSpan.textContent = '-';
}
// Render last played
const lastPlayedSpan = document.getElementById('lastPlayed');
if (this.lastPlayedCards.length > 0) {
// If "Bluff Called!", it's already updated in updateLastPlayedDescription
if (lastPlayedSpan.textContent !== 'Bluff Called!' && lastPlayedSpan.textContent !== 'Bluff Called!') {
lastPlayedSpan.textContent = `${this.lastPlayedCards.length} card(s) played as ${this.coreCard.value}${this.coreCard.suit}`;
}
} else {
lastPlayedSpan.textContent = 'None';
}
// Render central table
const centralTable = document.getElementById('centralTable');
centralTable.innerHTML = '';
this.centralTableCards.forEach((cardObj, index) => {
const cardEl = document.createElement('div');
cardEl.className = `card ${cardObj.revealed ? cardObj.card.getSuitClass() : 'back'}`;
if (cardObj.revealed) {
cardEl.innerHTML = `${cardObj.card.value}${cardObj.card.suit}`;
cardEl.style.color = cardObj.card.getColor();
} else {
cardEl.innerHTML = '🂠';
}
centralTable.appendChild(cardEl);
});
// Render current player's hand
const handContainer = document.getElementById('currentHand');
handContainer.innerHTML = '';
const currentPlayer = this.getCurrentPlayer();
if (!this.isGameOver && currentPlayer.isHuman) {
currentPlayer.hand.forEach((card, index) => {
const cardEl = document.createElement('div');
cardEl.className = `card ${card.getSuitClass()}`;
cardEl.style.color = card.getColor();
cardEl.dataset.index = index;
cardEl.innerHTML = `${card.value}${card.suit}`;
cardEl.onclick = () => {
cardEl.classList.toggle('selected');
};
handContainer.appendChild(cardEl);
});
}
// Enable/Disable buttons based on game state
const isHuman = currentPlayer.isHuman;
document.getElementById('playButton').disabled = (isHuman && currentPlayer.hand.length === 0) || this.isGameOver || !isHuman;
document.getElementById('callBluffButton').disabled = this.lastPlayedCards.length === 0 || this.isGameOver;
}
// Helper function to create card HTML with proper styling
createCardHTML(card) {
const color = card.getColor();
return `<span style="color: ${color}; font-size: 1em;">${card.value}${card.suit}</span>`;
}
}
// Initialize Game Instance
const game = new Game();
// Hide the setup modal since players are auto-added
document.getElementById('setupModal').style.display = 'none';
</script>
</body>
</html>
I need you to remove the robot players from the game's logic and instead set up an Express.js server. This server should manage a central data source, stored in a JSON file, that holds all the room information—such as player data, player hands, and the game state (e.g., what's on the table, turns, etc... ). The index.html file will then establish a WebSocket connection to interact with the server, enabling a real-time multiplayer experience. So, when you open the index.html for the first time you are going to be able to create or join to a room, if the user creates a room, returns an id that can be share with other players that they can use to join to the room... Once the creator of the room decide that all the players joined to the room, then starts the match, making impossible join to the room.
TLR:
Remove robot players from the game's logic. Set up an Express.js server to manage a central data source. Store all room information (player data, player hands, game state, etc.) in a JSON file. The index.html file should establish a WebSocket connection to the server for real-time interaction. On opening the index.html file, the user can either create or join a room. If the user creates a room, the server returns a unique room ID that can be shared with others. Players can use the room ID to join the created room. Waiting room it's created, showing in real time the players that joined to the match. Once the room creator confirms all players have joined, the match begins. After the match starts, joining the room will no longer be possible.
Goal: Start by setting up the server, and the client's side room's joining / creation. (Avoid the game's logic or game overall, let's focus on check the server and client connection works)
To transform your game into a real-time multiplayer experience, we'll set up an Express.js server with Socket.IO to handle WebSocket connections. The server will manage rooms, handle player connections, and maintain the game state using a JSON file. On the client side, we'll update your index.html to allow players to create or join rooms and communicate with the server in real-time.
server.jsrooms.jsonindex.html)
Here's how your project directory will be organized:
your-project/
│
├── server/
│ ├── server.js
│ ├── rooms.json
│ └── package.json
│
└── public/
└── index.html
package.json.index.html and any additional assets.First, create a new directory for your server and navigate into it:
mkdir server
cd server
Initialize a new Node.js project:
npm init -y
This command generates a package.json file with default settings.
Install the necessary dependencies: Express.js for the server, Socket.IO for WebSocket communication, uuid for unique room IDs, and fs for file operations.
npm install express socket.io uuid fs path
server.jsCreate a file named server.js inside the server directory and add the following code:
Explanation of server.js:
Express Setup: Serves static files from the public directory, which contains index.html.
Socket.IO Integration: Listens for WebSocket connections and handles events such as create_room, join_room, and start_game.
Room Management:
rooms.json.started.rooms.jsonCreate an empty rooms.json file inside the server directory to store room data:
touch server/rooms.json
Initialize it with an empty JSON object:
{}
index.html)We'll update your existing index.html to include the UI for creating and joining rooms and to handle real-time communication with the server.
Add a modal or a section where players can choose to create or join a room. We'll add this at the beginning of the body.
Explanation of Client-Side Updates:
Room Modal UI: A modal overlay (.room-modal) appears when the page loads, allowing players to enter their name and choose to create or join a room.
Create Room: When a player clicks "Create Room," their name is sent to the server via the create_room event. The server responds with a unique roomId, which is displayed to the player.
Join Room: Players can enter a roomId to join an existing room via the join_room event. The server validates the room and adds the player if possible.
Update Players List: The server emits update_players whenever a player joins or leaves a room. The client listens for this event to update the list of players in real-time.
Start Game: The room creator has the ability to start the game by emitting the start_game event. Once the game starts, the server notifies all players in the room via the game_started event.
Socket.IO Client Library: Included via <script src="/socket.io/socket.io.js"></script> to enable real-time communication between the client and server.
Ensure Directory Structure
Your project directory should look like this:
your-project/
│
├── server/
│ ├── server.js
│ ├── rooms.json
│ └── package.json
│
└── public/
└── index.html
Start the Server
Navigate to the server directory and start the server:
cd server
node server.js
You should see output indicating that the server is running:
Server is running on port 3000
Access the Client
Open your web browser and navigate to http://localhost:3000. You should see the room modal where you can create or join a room.
To ensure that the server and client are communicating correctly, perform the following steps:
Creating a Room
http://localhost:3000.Joining a Room
http://localhost:3000.Starting the Game
Handling Disconnections
Now that the server and client can handle room creation and joining with real-time updates, you can proceed with the following steps to complete your multiplayer game:
Integrate Game Logic with Server
Handle Real-Time Game Events
Manage Player Hands and Game State
Enhance UI/UX
Persist Game Data
Security Considerations
By following these steps, you'll lay a strong foundation for a real-time multiplayer experience in your "Card Bluff & Shoot" game. If you encounter any issues or need further assistance with specific functionalities, feel free to ask!
Not yet included in a published release.
// server/server.js
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const { v4: uuidv4 } = require('uuid');
const fs = require('fs');
const path = require('path');
// Initialize Express App
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, '../public')));
// Path to rooms JSON file
const ROOMS_FILE = path.join(__dirname, 'rooms.json');
// Helper function to read rooms data
function readRooms() {
try {
const data = fs.readFileSync(ROOMS_FILE);
return JSON.parse(data);
} catch (err) {
console.error('Error reading rooms.json:', err);
return {};
}
}
// Helper function to write rooms data
function writeRooms(rooms) {
try {
fs.writeFileSync(ROOMS_FILE, JSON.stringify(rooms, null, 2));
} catch (err) {
console.error('Error writing to rooms.json:', err);
}
}
// When a client connects
io.on('connection', (socket) => {
console.log(`New client connected: ${socket.id}`);
// Handle room creation
socket.on('create_room', (playerName, callback) => {
const rooms = readRooms();
const roomId = uuidv4();
rooms[roomId] = {
id: roomId,
players: [
{
id: socket.id,
name: playerName,
isCreator: true,
bulletSlots: 6,
isEliminated: false,
hand: []
}
],
state: 'waiting', // could be 'waiting' or 'started'
coreCard: null,
lastPlayedCards: [],
centralTableCards: []
};
writeRooms(rooms);
socket.join(roomId);
console.log(`Room created: ${roomId} by ${playerName}`);
callback({ success: true, roomId: roomId });
io.to(roomId).emit('update_players', rooms[roomId].players);
});
// Handle joining a room
socket.on('join_room', ({ roomId, playerName }, callback) => {
const rooms = readRooms();
if (rooms[roomId]) {
if (rooms[roomId].state === 'started') {
callback({ success: false, message: 'Game already started. Cannot join now.' });
return;
}
rooms[roomId].players.push({
id: socket.id,
name: playerName,
isCreator: false,
bulletSlots: 6,
isEliminated: false,
hand: []
});
writeRooms(rooms);
socket.join(roomId);
console.log(`${playerName} joined room: ${roomId}`);
callback({ success: true, roomId: roomId });
io.to(roomId).emit('update_players', rooms[roomId].players);
} else {
callback({ success: false, message: 'Room not found.' });
}
});
// Handle starting the game
socket.on('start_game', (roomId, callback) => {
const rooms = readRooms();
if (rooms[roomId]) {
const room = rooms[roomId];
const creator = room.players.find(p => p.isCreator);
if (creator.id !== socket.id) {
callback({ success: false, message: 'Only the room creator can start the game.' });
return;
}
room.state = 'started';
writeRooms(rooms);
io.to(roomId).emit('game_started', roomId);
console.log(`Game started in room: ${roomId}`);
callback({ success: true });
} else {
callback({ success: false, message: 'Room not found.' });
}
});
// Handle disconnect
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
const rooms = readRooms();
let updated = false;
// Iterate through rooms to find the disconnected player
for (const roomId in rooms) {
const room = rooms[roomId];
const playerIndex = room.players.findIndex(p => p.id === socket.id);
if (playerIndex !== -1) {
const player = room.players[playerIndex];
room.players.splice(playerIndex, 1);
updated = true;
console.log(`Player ${player.name} removed from room ${roomId}`);
// If the creator left, assign a new creator
if (player.isCreator && room.players.length > 0) {
room.players[0].isCreator = true;
console.log(`New creator for room ${roomId} is ${room.players[0].name}`);
}
// If no players left, delete the room
if (room.players.length === 0) {
delete rooms[roomId];
console.log(`Room ${roomId} deleted because no players left.`);
}
writeRooms(rooms);
io.to(roomId).emit('update_players', room.players);
break;
}
}
if (!updated) {
console.log('No room found for disconnected client.');
}
});
});
// Start the server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
<!-- public/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Card Bluff & Shoot</title>
<style>
/* Existing styles... */
/* Room Modal Styles */
.room-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.85);
display: flex;
justify-content: center;
align-items: center;
z-index: 3000;
flex-direction: column;
gap: 20px;
color: #fff;
}
.room-modal.active {
display: flex;
}
.room-modal.hidden {
display: none;
}
.room-modal input {
padding: 10px;
width: 300px;
margin-bottom: 10px;
border: none;
border-radius: 5px;
}
.room-modal button {
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
background: #4CAF50;
color: white;
transition: background 0.3s;
font-size: 1em;
}
.room-modal button:hover {
background: #45a049;
}
.room-info {
margin-top: 20px;
text-align: center;
}
.room-info p {
margin: 5px 0;
}
</style>
</head>
<body>
<!-- Room Modal -->
<div class="room-modal" id="roomModal">
<h2>Welcome to Card Bluff & Shoot</h2>
<input type="text" id="playerNameInput" placeholder="Enter your name" required>
<button id="createRoomBtn">Create Room</button>
<button id="joinRoomBtn">Join Room</button>
<!-- Join Room Section -->
<div id="joinRoomSection" class="hidden">
<input type="text" id="roomIdInput" placeholder="Enter Room ID" required>
<button id="confirmJoinRoomBtn">Join</button>
<button id="cancelJoinRoomBtn">Cancel</button>
</div>
<!-- Room Info Section -->
<div class="room-info" id="roomInfo" class="hidden">
<p id="currentRoomId"></p>
<p id="playersList"></p>
<button id="startGameBtn">Start Game</button>
</div>
</div>
<!-- Existing game-container -->
<div class="game-container" id="playersContainer">
<!-- Existing content... -->
</div>
<!-- Existing overlay, debugControls, etc... -->
<!-- Include Socket.IO Client Library -->
<script src="/socket.io/socket.io.js"></script>
<script>
// public/index.html (continued)
const socket = io();
const roomModal = document.getElementById('roomModal');
const createRoomBtn = document.getElementById('createRoomBtn');
const joinRoomBtn = document.getElementById('joinRoomBtn');
const joinRoomSection = document.getElementById('joinRoomSection');
const confirmJoinRoomBtn = document.getElementById('confirmJoinRoomBtn');
const cancelJoinRoomBtn = document.getElementById('cancelJoinRoomBtn');
const roomInfo = document.getElementById('roomInfo');
const currentRoomId = document.getElementById('currentRoomId');
const playersList = document.getElementById('playersList');
const startGameBtn = document.getElementById('startGameBtn');
let currentRoom = null;
let playerName = '';
// Show join room inputs
joinRoomBtn.addEventListener('click', () => {
joinRoomSection.classList.remove('hidden');
});
// Cancel join room
cancelJoinRoomBtn.addEventListener('click', () => {
joinRoomSection.classList.add('hidden');
});
// Create Room
createRoomBtn.addEventListener('click', () => {
const nameInput = document.getElementById('playerNameInput').value.trim();
if (nameInput === '') {
alert('Please enter your name.');
return;
}
playerName = nameInput;
socket.emit('create_room', playerName, (response) => {
if (response.success) {
currentRoom = response.roomId;
currentRoomId.textContent = `Room ID: ${currentRoom}`;
roomInfo.classList.remove('hidden');
roomModal.classList.add('hidden');
} else {
alert('Failed to create room.');
}
});
});
// Join Room
confirmJoinRoomBtn.addEventListener('click', () => {
const nameInput = document.getElementById('playerNameInput').value.trim();
const roomIdInput = document.getElementById('roomIdInput').value.trim();
if (nameInput === '' || roomIdInput === '') {
alert('Please enter your name and Room ID.');
return;
}
playerName = nameInput;
socket.emit('join_room', { roomId: roomIdInput, playerName: nameInput }, (response) => {
if (response.success) {
currentRoom = response.roomId;
currentRoomId.textContent = `Room ID: ${currentRoom}`;
roomInfo.classList.remove('hidden');
roomModal.classList.add('hidden');
} else {
alert(response.message);
}
});
});
// Start Game
startGameBtn.addEventListener('click', () => {
if (currentRoom) {
socket.emit('start_game', currentRoom, (response) => {
if (response.success) {
alert('Game has started!');
// Here you can redirect to the game screen or initialize the game logic
// For now, we'll just hide the room info
roomInfo.classList.add('hidden');
} else {
alert(response.message);
}
});
}
});
// Update Players List
socket.on('update_players', (players) => {
if (currentRoom) {
playersList.innerHTML = `<strong>Players in Room:</strong><br>`;
players.forEach(player => {
playersList.innerHTML += `${player.name}${player.isCreator ? ' (Host)' : ''}<br>`;
});
// Only show start button to the room creator
const creator = players.find(p => p.isCreator);
if (creator && creator.id === socket.id) {
startGameBtn.style.display = 'block';
} else {
startGameBtn.style.display = 'none';
}
}
});
// Handle Game Start
socket.on('game_started', (roomId) => {
if (roomId === currentRoom) {
alert('Game has started!');
// Initialize the game interface here
// For now, we'll just display a message
// You can hide the waiting room UI and show the game UI
}
});
</script>
</body>
</html>