I'm creating an action based rpg (not really realtime), where player can build it's own city and move his hero on a tile map.
Background:
I have such collections:
- map tiles (each tile has it's own document),
- hero (contains stats + map tile reference - to place it on map)
- city (each player has it's city, where we store player gold).
Scenario:
To move a hero, I should:
- get destination map tile from collection, to check if it's occupied
- get hero from collection and check if he's already at destination map tile
- get city from collection and check if player has enough gold to move
Then:
- subtract gold for movement and save city document
- update hero with new map tile and save hero document
Problem:
My main concern about whole process, is that city after finding and before subtracting gold can be changed by another action (i.e claim gold action which just adds gold in city by atomic $add on city document).
Of course some request blocking is done at frontend, but game server is just api so everyone can fire 20 same requests at the same time and cause desynchronization of data. Is there anything which can be done or am I supersensitive?
Ps. Everywhere where we work on one document we use update or findOneAndUpdate (with $set,$add etc.), to omit finding, changing record in callback and then saving.