4 Replies - 1474 Views - Last Post: 27 February 2010 - 08:50 AM Rate Topic: -----

#1 Arbaces  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 25-February 10

online game - server updates

Posted 25 February 2010 - 04:50 AM

Hello,

<offtopic>This community looks great and its my first topic so I should at least say I entered here because I've recently started to become more and more into web programming and decided to create an online game. Right now I'm digging all around the web to find things to start with. I have basic PHP & MySQL knowledge (and good Java and C), but that's why I want to learn along the way as I'm making something..

So, what I'm trying to make is a game similar to Cybernations, Travian and Hattrick, with ideas and concepts from many other games but brought online, in-browser.</offtopic>


The question that bugs me lately is: how will I make the server update itself (scheduled updates and scheduled script running, say from 10 to 10 hrs or something like it).

For example, as an exercise - say I'm making a page which I want from 5 to 5 minutes to update itself with some text (automatically, by the server). for ex:

i upload the page on a free hosting service with php and mysql - like FreeHostia
now the page is blank ..
//after ~12 minutes it shows up:
5 minutes passed! update [1]
5 minutes passed!

I hope you get it, I want to know how can I configure a server / a hosting service / everything , to run scripts on a scheduled time (scripts to update a db, process some algorithms etc). I need something I can start studying on.


Thanks in advance
-Arbaces

Is This A Good Question/Topic? 1
  • +

Replies To: online game - server updates

#2 myork  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 129
  • Joined: 04-March 09

Re: online game - server updates

Posted 25 February 2010 - 02:33 PM

You can achieve this on *nix systems with cron
Was This Post Helpful? 0
  • +
  • -

#3 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: online game - server updates

Posted 26 February 2010 - 05:46 AM

There are three basic approaches to this sort of thing:

The first is to use cron or the Windows equivalent. This has the advantage of being quick and simple to set up, but can have issues with resource consumption (if your program has a lot of overhead on startup, using cron to run it every minute may not be a good idea). cron also only checks once a minute for jobs to run, so this is not suited to anything that needs to run more frequently or requires greater accuracy in its timing.

The second is to write a daemon which runs constantly, monitors the database for things that need to be done, and does them. It's absolutely essential to have the daemon sleep for some period of time (or make a blocking "select" on a socket which will alert it when there's something to do) after each check or else it will hog all available CPU time and really piss the sysadmin off! (Trust me on this. I'm a former sysadmin and have been on both sides of that exchange.) This is the approach with the most potential to cause problems, but it also gives you the most power and flexibility if you need to-the-second (or even sub-second) timing or need to dig through large, complex data sets to do your periodic processing. It's also vulnerable to problems if the daemon dies unexpectedly, since the world will not update until it's restarted.

The third is to recognize that, until someone looks at the result, it doesn't matter whether something has been done or not, so you can fake it. In your example, you could just store a timestamp when the timer starts and then do nothing until the user reloads the page, at which point you look at the timer, calculate that 12 minutes has passed and that 12/5 (rounded down) = 2, so you print "5 minutes passed!" twice. This is the most resource-friendly method, but isn't very well-suited to handling complex series of time-dependent events, since you have to do all of your catching up at once whenever somebody looks at the state of the world.

#1 is the most-commonly used. #2 is extremely rare unless you have a dedicated server, since long-running processes are generally banned on shared hosts. #3 is the best way to go if your time-dependent processes are simple enough to be handled this way.
Was This Post Helpful? 2
  • +
  • -

#4 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 821
  • View blog
  • Posts: 7,692
  • Joined: 05-May 05

Re: online game - server updates

Posted 26 February 2010 - 01:20 PM

So that's what the hell cron is, it's really useful. I'll keep it in mind if I develop any network game services.
Was This Post Helpful? 0
  • +
  • -

#5 Arbaces  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 25-February 10

Re: online game - server updates

Posted 27 February 2010 - 08:50 AM

Thanks a lot! Methods #1 and #3 would be most suited in my case. I was considering #3, but #1 is what I needed and didn't knew of. Since I'm writing my online game from a laptop, with low-end internet connection, I can't actually use cron since I can't make it a server, but on the good side, there are online services (setcronjob is what I found) which offer to visit a link/access my script on schedule, and that's all I need for now, at least while developing and testing my game! Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1