这是一场私人比赛,游戏有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')
}
但是,只要我没有得到答复,名称和密码都将是不确定的。
最佳答案
您可以为此目的使用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()
})