这是一场私人比赛,游戏有2个玩家,姓名和密码,如果您想加入,则必须与他们匹配。
问题是:当比赛或名字不比赛或比赛已经满了时,我该如何控制,以便我可以向客户端发送不同的消息? 如果有人可以给我看另一个有用的帖子或某些文档链接可以为我提供帮助,我将非常高兴。
谢谢
exports.joinPrivateMatch = (req, res) =>{
let gamePass = req.body.password;
let gameName = req.body.name;
let playerid = req.body.playerid;
let socketPID = req.body.socketid;
creatematch.findOneAndUpdate({player2: null, name: gameName, password: gamePass}, {
player2: playerid,
socketID2: socketPID
}, {new:true}, function(err, result){
if(result){
res.send(result);
ioConnection.to(`${result.socketID1}`).emit('addplayer2', result);
ioConnection.to(`${result.socketID1}`).emit('matchfull', /*{ScreenReady: true} */);
ioConnection.to(`${result.socketID2}`).emit('matchfull', /*{ScreenReady: false} */);
}
if(err){
return res.send('something went wrong', err)
}
else{
console.log('Contraseña incorrecta o partida llena')
ioConnection.to(`${socketPID}`).emit('sadsmiley')
}
})
我写了这样的东西:
if(name != gameName || password != gamePass){
console.log("name or password doesnt match")
ioConnection.to(`${socketPID}`).emit('findError1')
}
但是只要我没有得到答复,用户名和密码都将是不确定的。
答案 0 :(得分:1)
您可以为此目的使用Express中间件... 参见Using Express Middleware
var app = express()
var router = express.Router()
// a middleware function with no mount path. This code is executed for every request to the router
router.use(function (req, res, next) {
//here implement code for matching gameName & Password
//if not match, return authentication error response
//else pass the control to the next function
next()
})