Following situation:
Users have balances:
user:{
balance: 100
id: xxx
}
Games have users and bet amount:
game: {
bet: 1000,
p1: yyy,
p2: null
}
When a player joins a game, I need to check if his current balance is sufficient to match the games bet (set by the creator of the game: p1).
I could easily just do something like this:
const user = users.find({id: xxx})
const game = games.find({p2: null})
if(user.balance >= game.bet)
games.updateOne({...}, {$set: {p2: user.id}})
BUT, this would only work if I only ever had one server instance running as otherwise I would run into a race condition between finding the game and deciding on the result.
MongoDB 4.0 comes with multi-document transactions however I cant seem to find a way to put them to use here. It seems like I need something else. I feel like what I need is a server side (db side) script in place that will run this logic on data whilst completely locking the users and games collection.
Am I right that you cant implement this without serverside scripts?