I’m going to create a tic tac toe multiplayer game with sockets and cookies, where it can include two players who can choose their own name and color, the game has a play again button that restarts the game board but here comes my problem, everything works fine until you have to press the play again button, then the name and color are changed to the same as player 2. So if Player 1 is called p1 and has the color red and player 2 is called p2 with the color blue, then the color and name of player 1 are changed to p2 and the color blue so both players have the same name and color but the game is still distinguishing the players. Here is part of my code where I think I need to change but don’t know how, I have more code in other files that might affect too but please tell me what you think and I can fill in with more of the code in that case.
app.get(‘/’, (req, res) => {
if (req.cookies.nickName && req.cookies.color) {
res.sendFile(__dirname + '/static/html/index.html');
}else {
res.sendFile(__dirname + '/static/html/loggain.html');
}
});
app.get(‘/reset’, (req, res) => {
if(req.cookies.nickName) {
res.clearCookie('nickName');
}
if(req.cookies.color) {
res.clearCookie('color');
}
globalObject.resetGameArea();
res.redirect('/');
});
let clientCount = 0;
io.on(‘connection’, (socket) => {
console.log(‘En användare anslöt med socket’);
clientCount++;
socket.on('disconnect', () => {
clientCount--;
console.log('Antal anslutna disconnect: ' + clientCount);
});
let cookies = globalObject.parseCookies(socket.handshake.headers.cookie);
if (cookies.nickName && cookies.color) {
if (clientCount > 2) {
console.log('Mer än 2 klienter anslutna, kopplar från klient: ' + socket.id);
console.log('Redan två spelare anslutna!');
socket.disconnect('Redan två spelare anslutna!' + socket.id);
}
if (clientCount === 1) {
console.log(socket.id);
globalObject.playerOneNick = cookies.nickName;
globalObject.playerOneColor = cookies.color;
globalObject.playerOneSocketId = socket.id;
console.log('p1 nick: ' + globalObject.playerOneNick);
}
if (clientCount === 2) {
console.log(socket.id);
globalObject.playerTwoNick = cookies.nickName;
globalObject.playerTwoColor = cookies.color;
globalObject.playerTwoSocketId = socket.id;
console.log('p2 nick: ' + globalObject.playerTwoNick);
globalObject.resetGameArea();
let playerOneData = {
opponentNick: globalObject.playerTwoNick,
opponentColor: globalObject.playerTwoColor,
myColor: globalObject.playerOneColor
};
let playerTwoData = {
opponentNick: globalObject.playerOneNick,
opponentColor: globalObject.playerOneColor,
myColor: globalObject.playerTwoColor
};
console.log('emit newgame');
io.to(globalObject.playerOneSocketId).emit('newGame', playerOneData);
io.to(globalObject.playerTwoSocketId).emit('newGame', playerTwoData);
globalObject.currentPlayer = 1;
io.to(globalObject.playerOneSocketId).emit('yourMove', globalObject.cellId);
}
} else {
console.log('Kakorna saknas!');
socket.disconnect('Kakor saknas!');
};
socket.on('newMove', (data) => {
globalObject.gameArea[data.cellId] = globalObject.currentPlayer;
if (globalObject.currentPlayer === 1) {
globalObject.currentPlayer = 2;
io.to(globalObject.playerTwoSocketId).emit('yourMove', data);
} else if (globalObject.currentPlayer === 2) {
globalObject.currentPlayer = 1;
io.to(globalObject.playerOneSocketId).emit('yourMove', data);
}
let winner = globalObject.checkForWinner();
if (winner === 1) {
io.emit('gameover', 'Spelare 1 är vinnare!');
} else if (winner === 2) {
io.emit('gameover', 'Spelare 2 är vinnare!');
} else if (winner === 3) {
io.emit('gameover', 'Oavgjort!');
}
});
});
Usage of globalObject: The code utilizes a global object named globalObject to store player information such as nicknames and colors. The expectation is that this global object maintains its state across different parts of the application, ensuring that player data is not overwritten inadvertently.
Control of client count: The clientCount variable is used to track the number of connected players. By checking this count, the code aims to assign player information only when necessary, preventing accidental overwriting of existing data.
Conditional assignment of player information: Before assigning player information to globalObject, the code checks if it’s already set. This conditional check is expected to ensure that player data is only assigned if it’s not already present, thereby avoiding unintended overwriting.
The expected outcome of these strategies is to prevent the issue where player data gets overwritten when the game is replayed.
Lunisen123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.