Tic Tack Toe Multiplayer

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.

New contributor

Lunisen123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật