2

I'm in the process of analysis for a browser-based game I'm making, and I have question about programming the economy system. I'll use a simplified system to ask my question.

Each user as GameState object, holding everything there is to know about his game. The structures, each level, the army and whatnot. More importantly :

  • His current amount of money
  • His current income of money for each cycle (a cycle is yet to define, lets say 1 second for the sake of the example)
  • His current money cap, the maximum amount of money he can reach.

My problem lies here :

How do I cleanly and nicely update everyone's money every second with as little resources as possible ?

My knowledge in databases and queries is quite limited, as I don't really know how much time / resources it takes for a query of that size to occur. But I am afraid that very fast, with a small amount of players, the database will start to slow down because of the amount of requests I'll do every second added to all the requests those players will actually make.

Right now, I have some possible ideas that don't work :

Every second, loop on every single player and update his money by his current income.

This seems rather insane, as the loop might not even be finished after a second, which will make players start to fall behind in terms of money cycles.

Load the complete database in memory, update all the players money, rewrite complete database.

That seems even more crazy, with the same drawbacks of the first solution + more possible issues due to loading the complete database in memory, and many things could happen while the update is occuring. That's a no-no.

Not updating the money. Only calculate it when someone needs it (an enemy player or the player itself). I would save the timestamp of latest update, compare with server-time now, and multiply by income and I should be good. Right?

Every time the user wants to see his money count, he would have to refresh the page to make sure he is somewhat up to date. That's fine by me, really.

That solution feels like my best shot but I have a really bad feeling about this. I can't put my finger on it but it feels so error prone.

Anyway, I'm here to see if any of you has already done something like this, or has an idea to share. I'm not sure how to approach this, even though this looks rather simple on a small scale.

Note : I'll be using C# for all my logic, the website will be a .NET based website (still working onto this), but you probably know what I mean. The persistence of data will probably be a MySQL database or something similar, maybe Entity Framework.

2
  • 1
    cross site duplicate: gamedev.stackexchange.com/q/3880
    – null
    Commented Sep 22, 2016 at 14:40
  • 1
    Indeed it's pretty much the same question, and I'm happy to see they ended up with a very similar solution. :) Thanks for the link !
    – Gil Sand
    Commented Sep 22, 2016 at 14:55

2 Answers 2

1

My experience is somewhat limited, but I think I'd call a function. As Caleth said, it's better if you update the actually value only when it's actually needed. Something like this could work nicely:

checkMoney(){
   return money + (timeSinceLastCheck * income);
}

Including player IDs and verifiers as necessary.

It's not even necessary to update the database at this point. You can increase performance by skipping the database write until they actually try to change/interact with their money. If they're only viewing it, there's no need to change the content of the database.

1

Your third solution is simple in the case where all changes of income are a result of player action. Each time the player requests the game state, and each time they submit an action, do an update.

You can still do the same if there are ways to passively increase income (e.g. "build 100 gold mines"), as the money update is Interval * (StartIncome + EndIncome) / 2. You can be fairly complex here, so long as you can integrate the income function, you can have an exact solution to the money function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.