AM-
AM-

Reputation: 881

PHP: execute an action after a given time

Good morning/afternoon/evening, Internet. I am currently developing a wap game and have encountered a problem: applying long-term effects on other players. Let me explain.

If a user uses an 'instant' ability, it is just a normal command link that does executeAbility(); If a user casts a spell, the script does the following:

setPlayerState('casting');
sleep($cast_time);
executeAbility();

This is but a simple part, though. Now if we go to some more advanced techiques, like putting a curse on a player, I get lost. I've already considered the following possibility:

$time=$_SERVER['REQUEST_TIME'];
$time_when_the_curse_finishes=$time+$curseDuration;
updateEnemyCurses($curseID,$endTime);
if ($time<$endTime) removeCurse();

This might look pretty and all, but it doesn't take two things into consideration: if the user logged off and logged back in in 2 hours, he would spend the whole two hours cursed and this method cannot support effect every x seconds for y seconds kind of curses. For example:

Curse One. You take 1 damage every 2 seconds for 10 seconds.

To summarise, I simply require a tip or two how I could make the line above possible. The curse itself would need to have its time checked globally (not with page reloads) and be able to complete queries (in this case damage ticks) every x seconds until it is removed in 10 seconds.

I am thankful in advance for any tips offered here!

Upvotes: 1

Views: 2668

Answers (4)

jlb
jlb

Reputation: 19970

You might want to look into setting up a cron script for this type of "maintenance" work.

It could run at a fixed interval (ie. every second) and handle curse etc. effects for all players

Start here :) http://en.wikipedia.org/wiki/Cron

Edit: As the others have mentioned (@mark-b), it's best to clarify: Use a cronjob for a single script that manages all of the work, not multiple cronjobs for each piece of work.

Upvotes: 5

Marc B
Marc B

Reputation: 360562

You'd be best to implement an event queue in a database. Rather than creating a cron job for every action that needs to be done on a user or by a user, you just add an entry to the DB job queue.

Then a SINGLE cron job can pull jobs out of that queue and process them. So for your 'curse' event, you'd set the curse flag on the user, and schedule a job to be executed at now + 5 minutes saying "remove curse X from user Y".

The cron job would fire up periodically (at whatever rate a 'tick' is in your game, perhaps) and request the list of jobs that need to be executed in that particular time period.

Upvotes: 2

Jes&#250;s Flores
Jes&#250;s Flores

Reputation: 153

I fully understand your problem because here at work I had to deal with a feature that required a solution that might help you. What I did was setup a db table that stored all the required/pending actions and the owner of the action. This table had a timestamp and under a certain interval I checked what actions were required to perform for this user. Other users checked also that table and removed past actions that were not retrieved (i.e. the owner of that action was logged out). The problem here was what to do if no users were logged at all and what I did was that when a user logs in, it checks the pending actions table first and cleans it if needed. I hope I can give you some insight. Good Luck!

Upvotes: 2

Kevin
Kevin

Reputation: 1240

Do you know gearman? I think, you have to create asynchronous jobs.

look at: http://php.net/manual/en/book.gearman.php

Upvotes: 0

Related Questions