This website is supposed to be a game where the players have some 'buildings' and these buildings produce resources. E.g. an iron mine may produce 30 pieces of iron ore per minute and automatically add it to the user's inventory. It doesn't matter whether the user is online or not, it should be running 24/7. So when the user does log in to their account, they will see their stack of iron ore has built up depending on how long it has been.

Some pointers in the right direction would be greatly appreciated :)

Need to save some state  
Put it in the database   
Retrieve on next login

Actually, when the user logs in again, what you'll need to do is figure out how many pieces of iron ore should have been mined while the user was gone, and update the database with that knowledge the next time the user logs in.

If other users interact with the "pieces of ore" information while the user is not logged in, you can update that information when it is requested by the other player.

The one thing that I don't think you want to do is create a "user agent" that runs in the background for every not logged-in person; I don't think you need that kind of complexity if you're interacting with players that are not even logged in, and if there are many players, it's not going to scale.

So update the information "just in time," when it is requested.

| improve this answer | |
  • it works fine if you have a simple case of just piling up resources. but if you want to implement upgrades that can improve input per time period (turn) or one player can conquer the resources of other players then a service that updates the state once per turn would make more sense and would make things way more simpler to manage. the guys at www.darkgalaxy.com did this for 3000 active players and it only took 1 minute per hour to update the whole thing. and this was years ago. – devnull Feb 11 '13 at 21:38
  • Perfect response! Thank you. I was thinking to do it how you said it but wasn't sure when I started reading about background processes. The "user agent" idea is a very bad idea especially when you have users that will never even play. I will confidently go with your first suggestion. – Deniz Feb 11 '13 at 21:40
  • @devnull What did you mean by implementing upgrades? In my case, the buildings will be 'upgraded' to produce more resources per interval of time. Not sure if that's what you meant. – Deniz Feb 11 '13 at 21:45
  • say you have a feature that boosts the output of a mine by 10% per turn, and i suppose that takes a couple of turns to develop/research. once that's complete the output will change. when the user logs in you will have to compute the ore mined using two rates: before and after the upgrade was finished. if you do it once per turn you only have to take into account the features active at that time. – devnull Feb 11 '13 at 21:49
  • @devnull I see, but wouldn't that put more pressure on the server to do it (like your e.g.) 1 minute per hour. Also what is the negative of making calculations using two rates? – Deniz Feb 11 '13 at 21:54

It should not be your {whatever technology} website that should be doing this processing at all.

A background process of some sort should be doing the processing, and persisting it to the database intermittently.

That way other users can interact with the data whenever it needs to.

When you login to your website, it will interact with both the background process and the database to figure out your current state.

| improve this answer | |
  • Agreed - a model of "service" that updates database and website that queries from database keeps everything nice and simple, the website can send requests to the service when something needs to change – Michael Jul 24 '13 at 14:18

Here are several approaches on top of my head:

  • Use a background process that runs continuously on the server, and checks the database of buildings every min/sec to see when a building last produced the items, and if it is time, do the production work.

  • Have buildings produce at certain times if the way above costs too much to run. Say every 15th min. of a hour. So your script will run 4 times in an hour, reducing the server workload.

  • Calculate the item values only when needed. You won't be running any continuos processes with this way.

| improve this answer | |
  • Thanks for your response, the reason why I don't want to update once an hour is because the rest of those 59 minutes, the user will have the wrong quantity of the resource, so when another player interacts with the resource, they are seeing the wrong amount. – Deniz Feb 11 '13 at 21:49
  • I meant 14 minutes not 59. – Deniz Feb 11 '13 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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