I'm making a web game RTS, it's mostly create units, manage resources, attack with army, and level up factories. I have created the basics of the game using Javascript, PHP, and MySQL databases. I use the database to store player credentials and stats, and PHP as a server-side logic (like retrieve the right player and attack with X units). On the client side I use HTML/CSS and javascript.

My problem is that I need to keep adding resources based on the factories level (ex. 10 gold every min, when mine-factory = lvl.1). The only answers that I have found, is to modify the PHP file to run an infinite loop and keep connecting to the database to add those resources 24/7. I think the server also needs to handle attack travel (when units arrive at the enemy castle and the fight is over) and some other logic, but I don't have physical access to the server, and keep connecting to the database to add resources doesn't seem a good idea. I'm willing to make a server using other languages, or use other way to store data that keeps changing, so I can avoid multiple connections to the database. If you know a better way to do it, or something that can put me on tracks I will appreciate it.


One way to do this is not to change the actual values in the database in realtime. When the player logs on, you see how long it's been, and how much stuff he should have if he were earning during the time he was logged off, then update the value accordingly.

One benefit of this is that if a player never logs on again, you're not spending server time and resources updating his information.

If these values are needed at times when the user is logged off (say when he gets attacked) you just check the time since it was last updated, and add the accumulated value for that time frame.

  • 1
    \$\begingroup\$ But please note that if you want attackers stealing his resources, then you need to update his resources before calculating the loot. \$\endgroup\$ – akaltar Dec 12 '15 at 17:11
  • \$\begingroup\$ Thanks for the response, but how am I going to store all the values when the player is afk?, I don't know how to keep the server running yet :( \$\endgroup\$ – Alex Parra Dec 12 '15 at 17:14
  • \$\begingroup\$ If you're asking how to keep values while they're afk, you either don't understand my answer, or need to read up more on PHP and mySQL. This is not intended to be an insult. :) \$\endgroup\$ – Almo Dec 12 '15 at 17:31
  • \$\begingroup\$ Sorry, I meant offline. The main difficulty is to keep the server thread alive to handle the logic and add resources. PHP or website hosting services don'f offer any services that could help me. \$\endgroup\$ – Alex Parra Dec 12 '15 at 18:55
  • 3
    \$\begingroup\$ Ok, so you didn't understand my answer. There's no need to update the players inventory until someone needs to know how much he has. \$\endgroup\$ – Almo Dec 12 '15 at 19:07

I think you are facing the wrong direction, if you want to build a RTS you have to build a serverside application that handle the game's processes and a clientside app to be shown to the users.

If you have a good PHP level maybe you should read about sockets and websockets, that would make it really easy, i've been building a MORTS based on websockets nodejs/javascript using socket.io, you can have a peek here, it's not even finished, but it could give you an idea.

The idea is that your server is running constantly and the players connect to it when they log in, so the information is updated in realtime.

Of course, you could build your own services and a daemon that updates the game status every 0.3/1/2/5/10 secs but IMHO that's a waste of time.

About your "hosting services doesnt provide....", there are lot of hosting services that provides you with node.js or lets you run daemons, even shell access.

  • \$\begingroup\$ Yes, I was looking some like this. I've been learning about node.js, if I download a javascript library that lets it connect to SQL database, do I need php? or based in your experience, are limitations with php that javascript + SQL library cant do?. Thanks for your response. \$\endgroup\$ – Alex Parra Dec 16 '15 at 16:56

Just like @Almo's answer, you only need to calculate the resources once someone needs to know those resources, and not incrementally. For instance, if I gain 5 coin every second that I have a coin factory up and running, then someone comes along to try to steal that coin, then all I need to do is calculate how many seconds have passed since it was last updated and multiply that by the coin amount to find out how much coin they have at that moment.

If you absolutely need to update every user at a certain frequency, then spawn a thread and update the users in a separate thread.

However, you also asked another question, which is how to keep the service running while you are away. I presume you are running this while connected directly to the machine over SSH(since you don't go into detail on how you are launching the process, I can only make assumptions). Doing so means that signing out of SSH will kill the process. You can look into using nohup or other forms of keeping a process alive after you sign out(screen, tmux, etc) but there are a number of security concerns there as you will be running the process under your user account, which presumably also has sudo access. Personally, I've always gone the route of setting up a daemon, complete with limited system user to restrict access to any hackers. It is a bit fiddly to get set up the first time, but once running, will run forever.


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.