1

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.

spamec
  • 301
  • 1
  • 5
  • 15
  • Versioning is a common way and it comes built into mongoose it seems: http://aaronheckmann.blogspot.co.uk/2012/06/mongoose-v3-part-1-versioning.html – Sammaye Jul 19 '14 at 12:54
  • Thx Sammaye, I know about versioning in mongoose. In fact getting Document version error was the first moment when I realized the problem of not having up to date document.. – spamec Jul 19 '14 at 16:58
  • That is what I would do, and when you are told by mongoose your document is out of date I would reload that document – Sammaye Jul 19 '14 at 20:33
  • Alright, almost got it.. but what I have is multistep validation on 3 document. That would mean that after reloading document I should run validation once again. Am I overcomplicating things? As far as I see on various projects nobody do such things :) – spamec Jul 20 '14 at 10:10
  • I would personally only do validation on your main hero document. If your main hero document has changed them obviously he has done something worth updating for. So all actions should save two documents, one hero and one the actual document (or however many documents that action touches), that way your only doing this quite slow operation once and just sending the rest of the data to the server blindly – Sammaye Jul 20 '14 at 11:03
  • Of course, I am unsure how mongoose implements versioning, but another not so slow way is to work the version number into the queries for updating etc so that it only updates if the version is the same, then there is no speed loss – Sammaye Jul 20 '14 at 11:04

0 Answers0