Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am scripting a web game that works using turns, where 1 turn=5 minutes. However i do not know the best way to go about updating their stats every 5 minutes. Would I be better off with cronjobs or running a script server side to update?

Maybe something like this.

for ($cou=0;$cou<100;$cou+=0) { ##update code goes here; sleep 300; }

I don't know if a webserver would allow this though(I know I would't).

  • Comment on Running a script periodically to update player statistics on a web game.
  • Download Code

Replies are listed 'Best First'.
Re: Running a script periodically to update player statistics on a web game.
by kcott (Archbishop) on Nov 02, 2010 at 01:24 UTC

    Without knowing your overall framework, it's a bit hard to say. However, I'd probably aim to tie the update into whatever page refresh code you have. Something like:

    # In your initialisation code my $stats_update_frequency = 300; my $last_stats_update = time; # In your page refresh code if (time - $last_stats_update >= $stats_update_frequency) { update_stats(); $last_stats_update = time; }

    Unrelated but the $cou+=0 looks a bit dodgy: ++$cou perhaps. I could be wrong as I've no idea what $cou refers to.

    -- Ken

      Unrelated but the $cou+=0 looks a bit dodgy: ++$cou perhaps. I could be wrong as I've no idea what $cou refers to.
      Dodgy indeed. He appears to be trying to build an infinite loop to keep the game running forever, but he hasn't yet discovered the
      while (1) { ... }
      construct that most of us use. $cou is presumably a generic counter and I'm guessing the +=0 is intentional so that the loop will never terminate.

        I wondered if it was intended as an infinite loop but AM also seems to want to stop it after 100 iterations.

        I shall wait patiently for AM to enlighten us. :-)

        -- Ken

Re: Running a script periodically to update player statistics on a web game.
by dsheroh (Monsignor) on Nov 02, 2010 at 09:48 UTC
    If it's just once every five minutes, my initial inclination is to say cron is probably the way to go.

    However, as kcott pointed out, you may be able to instead do this processing on-demand when players are loading pages, with something along the lines of

    for (1 .. ($time_since_last_turn / 300)) { process_turn($player); }
    ...but that's not really suitable if turn processing has side-effects visible to other players which would require the turns to actually be processed at the same time as the turn (nominally) occurs.

    While I'm often fond of building back-end daemons to run my games, I don't really see the point of it for turns that are only processed once every five minutes, regardless of whether your host will allow long-running processes or not.

Re: Running a script periodically to update player statistics on a web game.
by locked_user sundialsvc4 (Abbot) on Nov 02, 2010 at 12:56 UTC

    Is this wall time, or game time?  

    Does time continue to advance even at some unreasonable hour like 4:00 AM?   (Nevermind:   of course they would still be playing then...) :-D