如何将基于回合的逻辑实现到Socket.io中

时间:2018-01-14 07:49:19

标签: javascript node.js socket.io

我试图使用Node.js和Socket.IO制作回合制游戏

目前,当玩家使用移动时,服务器认为下一个玩家转向。这是一个视觉:

player that's supposed to go
           v   player that's not supposed to go.        
                      v
["Bob","Janet",    "Mark", "Jane"]

服务器事件:

socket.on('start_game', function(data) {
    let groupIndex = games.findIndex(function(item) {
        return item.name === data.group.name;
    });
    if (groupIndex > -1) {
        let playerIndex =  games[groupIndex].players.findIndex(function(item) {
            return item.id === data.player.id;
        });
        if (playerIndex > -1) {
            if (games[groupIndex].players[playerIndex].leader) {
                games[groupIndex].started = true;
                console.log("============== Started Game ==============");
                gameNSP.to(games[groupIndex].name).emit("started_game");
            }
        }
    }
});

socket.on('pass_turn', function(data) {
    let groupIndex = games.findIndex(function(item) {
        return item.name === data.group.name;
    });
    if (groupIndex > -1) {
        if (games[groupIndex].started) {
            console.log(games[groupIndex].players[games[groupIndex]._turn], data.player);
            if (games[groupIndex].players[games[groupIndex]._turn].id == data.player.id) {
                console.log("Passing Turn", data);
                games[groupIndex].resetTimeOut();
                games[groupIndex].next_turn(data.move);
            }
        }
    }
});

Game.js:https://gist.github.com/Hydrosaur/54b8bbe25aeccaa376adefc5fd375089

0 个答案:

没有答案