I'm creating a rochambo game with Socket.io. Players add their bets to a waiting list, node checks in mondogDB sessions if the player has enough money to do so and then creates the bet before sending it to other players.
My code works but if I ask for creating multiple bets very fast, node doesn't have time to update the player balance in mongoDB before the next bet arrives, despite my waiting list. So a player can create bets even if he doesn't have enough money, and I'd like to avoid that.
I can't find a way to prevent this from happening. Certainly with callbacks or recursive function, but I didn't manage to make it work.
Sorry for my poor English, I'm french :)
Here is a simplified version of the code :
socket.on('placerunpari', function (screenName, amount, sign) {
newBetWaitingList.push({
screenName: screenName,
amount: amount,
sign: sign
});
for (var k in newBetWaitingList) {
screenName = newBetWaitingList[k].screenName;
amount = newBetWaitingList[k].amount;
sign = newBetWaitingList[k].sign;
playerModel.findOne({
screenName: screenName
}).exec(function (err, player) {
if (player != null) {
if (checkAmounts(amount) && (sign == "scissors" || sign == "rock" || sign == "paper")) {
if (amount <= player.balance) {
//DB writing
var newBet = new betModel({
screenName: screenName,
amount: amount,
sign: sign
});
newBet.save(function (err, res) {
io.sockets.socket(connected[screenName]).emit('mybet', newBet.id, amount, sign);
socket.broadcast.emit('newbet', newBet.id, screenName, amount);
creatorUpdateBalance(screenName, -amount);
})
} else {
io.sockets.socket(connected[screenName]).emit('showalert', 'You don\'t have enough money for this bet', 'red');
}
} else {
console.log('Unauthorized access');
}
}
});
newBetWaitingList.shift();
}});