I'm redeveloping my online text based MMO, And I'm building a "Travel" Script Where a user clicks where they want to go and it takes X minutes to get there.
The only problem is - Due to limitations of my host, I cannot run 1 cronjob every minute to take away time spent travelling.
Can someone tell me how i could achieve this without using crons? Delaying a query from being run for multiples of 1 minute or so?
Once they set off, A countdown from that moment of X minutes begins, And once their travel time reaches 0 their "location" is set to "destination"
Thanks!
Use PHP sleep() method.
Like sleep(60); will result in the script to wait execution for 1 minute.
Do NOT set a script timeout as it wont make any sense.
I suggest you add a destination time column, set it to when the animals should arrive, and then in the client display a countdown until that time. You could use a query such as
select * from arrivals where arrival_time < now();
To get a list of animals that have not yet arrived, and of course the arrival_time is when they will arrive.
Related
So here is what I am trying to accomplish:
User selects how often they want their post to be moved to the top of the page, whether it be every 30 minutes or every hour or every 2 hours, etc. They will be able to select how many times that it will do this. So if they select it to update every hour for 5 hours, it will then update the date/time in the database to the current time and then updates that time every hour for the next 5 hours and cancels it after the 5 hours are up.
I was thinking of running a PHP script like this to update the table since the ads are displayed by date DESC:
<?php
//cronjob.php
$id = $_SESSION['ad_id'];
$date = date('Y-m-d H:i:s');
$query = "UPDATE ads SET ad_date = :date WHERE ad_id = :id";
?>
Is there a best way to do that or will a CRON job that is selected by the user going to be too much load on the server to have users setting up CRON jobs continuously.
I saw this one approach and wonder if it is possible to set it up for this purpose. I am still getting familiar with CRON jobs.
How to start/stop a cronjob using PHP?
I will be setting this up on Godaddy's server:
Godaddy Cron jobs
Appreciate the help.
One method is to store the required updates in a new database table, containing fields such as the post id, how many times to update, interval, how many updates are left, the time at which the first update should take place ( or the time for next update ) .
Then, create a php script that fetches data from this table and take necessary action for each post. Check if it's time to update the post, if yes then update, else don't ( check time with accuracy upto minutes, don't check seconds. If you can, do this time checking with SQL and not in php to reduce the data being fetched ). It's better to perform all updates in a single query to maximise efficiency.
Then set up a cron job on the server that runs this php script every minute or whatever minimum time interval you need.
This way, you can get away with just one cron job😉
If you need even more efficiency, then at the end of this script, check if there are any more updates pending, if none then delete the cron job. Then, set up your code such that whenever a user creates a new update, check if the cron job exists, if it doesn't then create it ( this additional dynamic cron job is only for efficiency freaks. If there are frequent update requests, it might be better to avoid creating/deleting cron jobs )
As the title reads, I am looking for a way to update a mysql field after 10 minutes has elapsed of a query being run.
Something like below is an example without the time restraint:
mysql_query("UPDATE `players` SET `playcoins`=TRUNCATE(ROUND((`playcoins`+$amount),9),8) WHERE `id`=$player[id] LIMIT 1");
Any ideas?
MySQL databases have a class of object called an EVENT. It's basically a hunk of SQL code that runs at particular time, or on a particular interval.
You could use code like this to create an event to do what you require at the right time in history. This particular code will create an event that runs just once, ten minutes in the future.
DELIMITER $$
DROP EVENT IF EXISTS coins_user12345$$
CREATE EVENT coins_user12345
ON SCHEDULE
AT NOW() + INTERVAL 10 MINUTE
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
UPDATE players
SET playcoins=TRUNCATE(ROUND((playcoins+123),9),8)
WHERE id=12345
LIMIT 1;
END$$
DELIMITER ;
To use these EVENT objects, you have to configure the event scheduler correctly. Read this. http://dev.mysql.com/doc/refman/5.6/en/events-configuration.html Some cheap shared hosting providers don't allow the use of events, so this is not guaranteed to work.
You go it the wrong way. Sure you can do it. And you can do it with PHP. But you shouldn't. PHP is not the right language to do such a task. Before I starting talk about shell_execute and sleep, which would be the core elements, you need to do this, I offer you another solution.
If I see right, you want to give a player every 10 minutes, some coins.
The right approach would´basicly be:
Save the last time the player has get coins in the database. If you get the player coins, you first want to check, the last time you give the player coins. Now calculate, how much he has earned in this time difference. Finaly add this to his balance and update the field, where you save the last time, the player has earned coins.
An alternative would be a Cronjob/Scheduled Task to a PHP file, which is called every 10 minutes, to give each player the coins, he should get.
Database Values For User Return After Prolonged Amount Of Time?
So I have values in my database e.g. stamina, max_stamina, ...
What I want to do is this:
If stamina is < max_stamina then
after 60 seconds stamina=stamina+1
else
nothing
I will need to loop this untill stamina equals max_stamina
I can easily create up the if statement, the problem I need help with is coding the 60 seconds, how could i go about it?
All so this will need to run when the player is and isnt logged in.
take a look at mysql events and schedules think that is what you are looking for. Also if you are having more than 1 users it would be smart to update them all in that 60 seconds interval you need.
Without knowing more about your code architecture, you most likely will need a cronjob to run every minute and to update all stamina records according to your criteria.
The easiest I could explain this is by giving this simple example:
Imagine an eBay auction, which ends at let's say 5:00 pm, this is set as an SQL value for the auction entry. (I have all date/times in unixtime).
Now, when the auction ends (the time hits 5:00 pm), the system sends an e-mail to the winner, plus executes other many actions (specified inside a php file?).
How would I do this? I can write the php, etc, but I don't know how to make the 'site' execute it every time some auction ends?
I think I need to use CRON, but I'm not sure about it.
If you use CRON jobs then they will be fired after every fixed interval.. So here is the solution..
Make a CRON job and schedule it every 1 or 2 minute or even 5 minute. and in that PHP script check any auction which has expired and then send email to the winner..
e.g.
$sql = "select * from auctions where auctionEndTime < UNIX_TIMESTAMP()";
//execute query
foreach($rows as $row)
{
//send email and other tasks
}
In my web app, user is given 5 min to do some job, and after that 5 min, the job should be passed to other person. So, each job would have user_id and that user_id value has to be changed every 5 min. The thing is that there are multiple jobs, and we want to use 15sec interval for script to be periodically run to take care of this change. And job will passed to other person maximum of 4~5 times, and it will be disregarded after that.
The job & user data is stored in MySQL database.
My initial thought was to use cron-job with PHP script file that let the cron-job runs PHP script every 15 sec. PHP file will read the job table, select all the jobs which have time value past 5 min ago, and fetch them to other users (doesn't matter if one user get the all jobs or not). I just want to periodically run it without taking too much resources.. but is it a good idea? Wouldn't that take too much resources once the number of jobs increases?
While it does not have to be consistence (13~16 sec interval is just fine) we want that script does not stop.
Create a cronjob that reads the table status updated time.
-- show table status like table;
example: update_time: 2011-04-10 15:27:32
than you can do the logic in the cronjob to check that date. and if that date is greater than 15 seconds ago or whatever, than you have an updated records.