AltarofScience Posted September 22, 2011 Share Posted September 22, 2011 I am coding a browser game in php/mysql. the economy code needs to run at a regular interval. How can I get my code to run say, every 6/12/24 minutes? I heard of something called a cron job, but im not sure if it is going to be what I need. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/ Share on other sites More sharing options...
jcbones Posted September 22, 2011 Share Posted September 22, 2011 If you are on a *nix server, then yes a cron job (crontab) is what you will need. Crontab quick reference If your host has cpanel or equiv. then there will be a easy user interface for you to set up a crontab. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271630 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 I am running apache 2.2 with php 5.3.8 and MySQL Community Server 5.5.15 with MySQL Workbench. It is running on localhost. I don't know what a *nix server is. One guy I know from a game said I should be using C++ or some other logic code to do the economy code and such, but I would rather not learn another language. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271631 Share on other sites More sharing options...
xyph Posted September 22, 2011 Share Posted September 22, 2011 *nix is a unix-based OS (Linux, FreeBSD etc.) If you are running Windows, use a scheduled task. Both crontabs and scheduled tasks with php can be googled. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271633 Share on other sites More sharing options...
jcbones Posted September 22, 2011 Share Posted September 22, 2011 Is your localhost running Windows? If so, you will have to schedule using task scheduler. C++ is an option, but if the economy scripts output to the web, PHP is the better choice IMHO. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271634 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 the game is massively multiplayer, the econ script changes entries in a database, which are called by php to a page. I did find a link to scheduled tasks. That will work? Are there any common problems involved? Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271636 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 Does anyone know of a good tutorial for scheduled tasks? A lot of google results are actually about linux and ubuntu and stuff. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271640 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 Task Scheduler only works for daily things. It doesn't have an option to run a task every hour, or 12 minutes. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271647 Share on other sites More sharing options...
PFMaBiSmAd Posted September 22, 2011 Share Posted September 22, 2011 If you investigate the Window's scheduler menu choices, you will find that you can schedule a task to repeat every x minutes or every x hours. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271650 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 I dug around, and I did find a function under advanced settings to run programs at minute periods. I didn't see advanced settings the first time. Not sure why. If I want the program to run a php script on my server, how do I configure that? Do I just browse and have that program in the bar? Will it run the php like that? Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271734 Share on other sites More sharing options...
PFMaBiSmAd Posted September 22, 2011 Share Posted September 22, 2011 You would use php's command line interpreter (CLI) - http://us2.php.net/cli Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271738 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 final question. econ code and possibly garbage collection are the only 2 things i need to run repeatedly. but for one off delays, what is my best bet? Basically ships have to fly through space to other planets. Although it would be nice to have them move across a coordinate plane over time, that is only necessary for the war and combat system, for the beta test colony system really all I need is to put a fleet on a timer, and then teleport it to the new location. this is not a problem, i just change the planet or colony location to make them accessible in the new area and not in the old. But how do i put them on a timer? basically, when i click on the fleet page i have an element that says remaining travel time, and it needs to have a timer that counts down from the travel time in minutes, and when it reaches 0, activate the query to change the fleet location. i can't use schedule task for that, even a once off, as far as i'm aware, since the command comes from the player across the internet and not me from my computer. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271740 Share on other sites More sharing options...
xyph Posted September 22, 2011 Share Posted September 22, 2011 Store a timestamp of when they should arrive. If the current timestamp is less, then there's still the difference left time to travel. If it's greater than or equal, it's arrived. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271813 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 how would i be able to control the test of when its arrived? like, say i write a code, //choose target location code //find distance from current location code //calculate flight time code //create arrival time stamp code //call time code //call arrival timestamps //whatever timestamps are <= current time then //change planet location in database to target location i doubt i can have it check every second whether anything has arrived. would it be a large server load if say i had 1000 fleets moving, so 1000 timestamps. i could use windows task scheduler to compare stamps to current time every minute, or 5 minutes, or 10 minutes. would that require a larger amount of processing power than say, a 2 gigs of ram? Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271842 Share on other sites More sharing options...
xyph Posted September 22, 2011 Share Posted September 22, 2011 You have an odd way of approaching these problems from an http perspective. Next time you need to check which ships are at a given destination, also check which ships are on route to that destination. You can then check if that stamp is less than the current stamp. -or- Next time you check on that ship's status, check the stamp. You don't need this information updated in real time. The only time you need to update the information is when a user checks it. At that point, you need to update any stale events that have happened since it was last checked. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271850 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 well, i do need to update this in real time. i blame myself for not communication the end goal though. ships sent on a colony mission can land themselves, whether or not the player is online. and when the combat code is made, players fleets can interact whether either of the players or even any player at all, is online. combat is initiated by fleets being within a certain distance of another fleet. if i send a colony ship on a transfer mission, as long as i am not implementing combat, then yes i wouldn't have to check it until the player logged in, and went to it. and then i could run the check. but if i want colony ships to colonize a planet as soon as they arrive at a planet, i need to be able to have the ship arrive within a small amount of time after it is supposed to. if by http perspective you mean that for this kind of function i might want to run another kind of language, that is possible, but i'm trying to avoid it. would it cause server load problems to run a query at an interval to see if a ship had arrived? aka, compare time stamps and current time. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271856 Share on other sites More sharing options...
xyph Posted September 22, 2011 Share Posted September 22, 2011 If you want to update in real time, you want a standalone game. You are approaching this the wrong way. I'm saying it can 'appear' to update in real time without doing so. I'll put this into basics. 14:00 - UserA sends ship to attack PlanetC owned by UserC. This process takes until 18:00 14:01 - UserA logs off 15:00 - UserB checks the status of PlanetC. The script checks for any ships en route to PlanetC. Since UserA's ship won't get there until 18:00, the script only shows UserC's ships at PlanetC 15:05 - UserB sends his ships to attack PlanetC. Will arrive at 18:30. 15:06 - UserB logs off 18:25 - UserC logs on, checks PlanetC. The script checks any ships en route to PlanetC. It finds UserA's ships arrived there 25 minutes ago, and UserB's will be there in 5 minutes. It then uses the battle algorithm to calculate the 25 minute battle that has been happening, and updates the script. It didn't matter WHAT user checked PlanetC, the same calculations would have been done. 18:26 - UserC logs out in anger. 18:35 - UserB logs in and checks on PlanetC. The script detects that UserB's ships arrived 5 minutes ago, and the planet has been under attack with a last update performed at 18:25. It then uses the battle algorithm again to determine what happened in the 5 minutes before UserB's ship arrived, then calculates the next 5 minutes including the 3rd ship... Do you see what I mean? Things don't have to happen in real time if no one is there to see the changes. All you need to be sure of is that the information is current WHEN it's needed. Object travels 200m at 2m/s. We don't need to update it's path in real time to know that 45 seconds after we start this event the object should be 90 meters along the path. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271861 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 okay that makes sense for the battle logic. sort of. but for the colony ship, on the colony mission, that arrives when no one is online, it needs to be affected by the econ system. if it doesn't exist how does that happen? when the player logs on does it just check that it has arrived and then run the econ script just on that colony multiple times related to how long its been since it landed and then display that? Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271863 Share on other sites More sharing options...
xyph Posted September 22, 2011 Share Posted September 22, 2011 Why can't the economy script perform those checks? Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271865 Share on other sites More sharing options...
AltarofScience Posted September 22, 2011 Author Share Posted September 22, 2011 Why can't the economy script perform those checks? well for certain reasons it is advantageous to land a colony in between the running of the econ script. i guess technically it is not a huge deal, but it is supposed to be possible to land them in between. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271873 Share on other sites More sharing options...
xyph Posted September 23, 2011 Share Posted September 23, 2011 Why doesn't the economy script check to see if any new colony ships have landed BEFORE it calculates the increase? I think you need to start over. You keep thinking about this as some sort of clock ticking rather than looking at it as a bunch of requests to a web server. Program to the protocol, or you're going to be fighting it all the way. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271879 Share on other sites More sharing options...
AltarofScience Posted September 23, 2011 Author Share Posted September 23, 2011 no i know that. but what i mean is, i need to be able to land a colony ship at any time from the last econ tick to the one coming up. say there is a 24 minute interval. i want to be able to land the colony at 23-1 minutes as a player as well. so yes if the player is offline, letting the script land the colony before it ticks is fine. i guess i can just have a button that lands the colony manually, and let the script land the colonies otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/247628-running-scripts-at-an-interval/#findComment-1271883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.