Jump to content

PHP Scheduling...


gamesmstr

Recommended Posts

I am writing a browser based multiplayer RTS game.  I am looking for a feasible way of scheduling the timed tasks.  (like it takes 1m 30s to build something)  What  I figured I would do is create a mysql database with a list of commands to be executed at a specific time.  PHP would have to check that file every second to execute and remove completed tasks.  I've done some major hunting but I have not yet found any way to do this.  Cron only goes down to minutes.  I'd love some help or maybe suggestions on a better way to accomplish this.

Link to comment
Share on other sites

When the user gives the order to start build something then store the time it will be done (now() + time to build) in the database with the structure. Use JavaScript or Flash to animate the building process while this is merely fake because even if the user now cheats he would not  be able to use the building as in the back-end it is still building... This also allows you to give the user special items like "reduce built time with 20 minutes" where after you update 1 or multiple rows and deduct 20 minutes of 1 or multiple rows for which the finished build time is higher then now()

Link to comment
Share on other sites

You can't php is called thru the webserver.

if u can have a background task to update your db is a better solution, as this wont execute the script at particular intervals (which takes time, to load php, load the script, parse it and execute), but requires a dedicated server, and disabling php max runtime (set_time_limit(0);)

 

 

 

Link to comment
Share on other sites

Look into php fork :)

but hopefully your web hosting has it enabled.

 

The problem you will encounter is finding out if your background task is running,

but I found a site that may do the trick: http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/

 

 

but instead of refreshing the page, you store it in your scheduling database.

 

Link to comment
Share on other sites

You don't need a background task for something like that. You will eat up all resources and even crash your server if you ever get lots of simultaneous users (imagine 10k users each having 10 or more tasks running?? Like building, upgrading and researching). Just use Ajax and a field in your database table that specifies when the build will be complete.

Link to comment
Share on other sites

Reason I said:

The problem you will encounter is finding out if your background task is running,

 

If the Scheuleder handles all tasks, than users dont matter. as you only want 1 scheduler running.

All You have to do is check if your Scheuleder is running, if not, start it up.

And it wont use up all resources.

 

Link to comment
Share on other sites

Using Ajax to me in this situation is silly.

Because You need a user to update the db (No Ajax, nothing gets built)

but your user scheduler will also need to update all users, not the ones just logged in or the current user logged in.

so shure your getting that 1 sec update, however this script is gonna be hammered. in order to update all tasks completed, and return the result to the ajax script.

 

In short, I recommend using a LRP (Long Running Process).

 

 

Link to comment
Share on other sites

There shouldnt be an issue with cron. It runs at specified intervals.

but like I said, since this is going to be an ongoing process. You may as well run it as a background task instead (continually running).

 

if your updates takes awhile (depends on how many tasks it has to update) and you find that its taking more than a minute, you may encounter problems with overlapped background processes. The same issue as stated before.

 

with an LRP solution, you wouldnt have that issue. but you would find that optimizing the code to minimize queries is going to be the hard part.

 

 

Link to comment
Share on other sites

Reason I said:

The problem you will encounter is finding out if your background task is running,

 

If the Scheuleder handles all tasks, than users dont matter. as you only want 1 scheduler running.

All You have to do is check if your Scheuleder is running, if not, start it up.

And it wont use up all resources.

 

 

It doesn't matter if you use Scheduler. Each new task will eat up more resources. It's not because you only use one instance of something that apparently uses 50 MB after startup that if you initialize 10k of tasks on this instance the amount of RAM used won't increase.

 

It's not because windows uses 2GB of ram that if you launch each and every game you own your RAM usage won't increase because it runs in Windows. Imagine what would happen if I would launch Avatar, The Saboteur, Star Wars: TFU, TF2, Operation Flashpoint, DIRT 2, Left 4 Dead 2, Left 4 Dead, COD MW 2 all simultaneous...

 

I have most of the site ajaxed anyways, but I need the server to update while the player is offline too

 

It doesn't matter if the user is offline or not. Your database keeps track when which building is completed which is when build_complete_time <= now() Ajax and Flash are just a fancy way of displaying the building process and can aswell be done without.

Link to comment
Share on other sites

you dont need to update every minute

this is the thing

when users are online, they send u commands when they want, and you have them send u the command each 5 minutes or so

this shouldnt be a problem

 

the updating process is just about calculation, for instance, a user has 1000 gold per hour, and he is building a market that will be finished in 1 hour, and he left at 30 minute later, that market will increase 10 gold per hour

$new_increment_rating=(((currentTime-finishTime)/3600)*10)>0?((currentTime-finishTime)/3600)*10):0;

//the above line basically, do this math, ((currentTime-finishTime)/3600)*10), if it is negative, give it a zero

//it is to calculate how much will the new market will give in that time

$currentGold=$currentGold+(currentTime-OldUpdateTime)/3600*$currentRating+$new_increment_rating

//calculate how much time passed, in hour, and times the gold increment rate, then plus the new market's number

//now, update the currentRating

$currentRating+=$new_increment_Rating;

//delete the finished building stuff

 

 

 

u can see, my idea is the best, u dont eat up much extra resource

 

it is basically, update all stuff when the user request it

without cron, scheduler, or timer

 

Link to comment
Share on other sites

the updating process is just about calculation, for instance, a user has 1000 gold per hour, and he is building a market that will be finished in 1 hour

 

This is a scenario where you would use CRON. To update player stats (+1000 gold) based on a few criteria (has a market?). Not to immitate the building process.

Link to comment
Share on other sites

the updating process is just about calculation, for instance, a user has 1000 gold per hour, and he is building a market that will be finished in 1 hour

 

This is a scenario where you would use CRON. To update player stats (+1000 gold) based on a few criteria (has a market?). Not to immitate the building process.

 

i dotn use cron, i just update when the user sends request

i dont update each minute, or each hour, or any time interval

this idea is better when there are less people coming to the website

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.