0

我有一个如下所示的条目:

{
  "__v": 10,
  "_id": "50f8050ed714645500000002",
  "sid": "DI7gZ0sprJ",
  "lines": [
    {
      "text": "Well that's what some people say anyway",
      "_id": "50f80526d714645500000003",
      "user": "Player456"
    },
    {
      "text": "They're people just like the rest of us",
      "_id": "50f805925543712caf000005",
      "user": "Player123"
    },
  ],
  "title": "Test",
  "maxlines": 0
}

并有一个如下所示的 socket.io 脚本:

socket.on('sendchat', function (data) {
    Story.findOne({ sid: socket.room }, function(err, story){
        if (err) {
            console.log(err);
        }
        else if(story == null) {
            console.log(err);
        }
        else {
            io.sockets.in(socket.room).emit('updatechat', socket.username, data);
            story.maxlines = story.maxlines - 1;

            if (story.maxlines == 0) {
                // End Game Script - Redirect to view page
                io.sockets.in(socket.room).emit('gameover', slugs(story.title), socket.room);
            }
            //Save Story to DB
            story.lines.push({
                user: socket.username,
                text: data
            });
            story.save(function(err, story){
                if (err) {
                    console.log(err);
                }
                else {
                    console.log("Story Updated");
                }
            });
        };
    });
});

有没有办法可以检查 socket.username 是否等于已输入的最新行上的用户?

例如上面的 Player123 是最后一个进入一行,所以如果 Player123 再次尝试它只会发出一个错误,但是如果 Player456 进入一行,那么 Player123 可以再次

我正在尝试创建一个简单的回合制游戏,该游戏会检查以确保一个人在轮不到他们时不能进入

4

1 回答 1

1

您可以直接检查对象中lines数组中的最后一个条目story。如:

if (story.lines.length &&
    story.lines[story.lines.length-1].user === socket.username) {
    // error, same user is trying to enter another line
}
于 2013-02-23T17:43:55.143 回答