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.