Page 1 of 2
Asking for help - Timer question
Posted: Thu Jan 28, 2010 2:42 pm
by SpHawk
Hello,
I'm learning PHP and writing a game for my kids. Maybe a web game someday. I need a timer that can add numbers, etc every few minutes. You know add money every few hours, add health every 5 minutes, or cash in a farm every 30 hours. ljust be able to set a time and a task. Kind of like mafia wars, where your health or energy goes up.
I used the unix timestamp and tried adding 5 minutes, and running that when health dropped below its' max. That was a disaster. So I thought I would seek a guru. I still want to learn it and write it, I just am having a problem with this hurdle. Anybody have any ideas?
Thanks in advance
Bob
P.S. It a math game. Kids get answers wrong, they lose health, get them they get prizes, etc. They go up in lvl the problems get harder and so forth. bt
Re: Asking for help - Timer question
Posted: Thu Jan 28, 2010 8:53 pm
by JakeJ
**WARNING - I am NOT an expert on this**
In fact, I'm just theorizing because I haven't done it before. BUT... here's my opinion anyway.
It seems to me this would be a bulk update sort of thing. Have a timer running in the background and have it return a value every 5 minutes. This would initiate a trigger that would update stats for all those appropriate (max health constraints, etc.). It should be pretty low overhead and it's not like it's an industrial application that depends on precise timing so if you had TONS of users and it took time to write the change, it would be ok.
This way, you wouldn't have to worry about an actual time stamp, you'd just have a cron job throwing values at you.
With lots of users, you could even stagger some cron jobs which would update a subset of the users in a manner of your choosing so it wouldn't suck up the CPU for everyone at once.
Furthermore, the screen displayed clock would actually be taken over on the users browser so you wouldn't have to be sending out time updates. I believe that's how Mafia Wars does it just from observation of behavior. It's nowhere near exact.
Come to think of it, the trigger wouldn't even have to be php based, the cron job could connect directly to the database. Yeah, that's the ticket!
Re: Asking for help - Timer question
Posted: Thu Jan 28, 2010 9:21 pm
by SpHawk
Hey Jake, thanks for the help!
But I missed it, there's a timer function in MySql? I wouldn't mind running one timer for a player, and having energy, bonuses, health etc go up at different times. My original thought was a small snipet of code that I could change the time on that would be used over and over. One for health, one for energy, etc. But I still don't know how to do that. Although if there's a function in MySql that handles it, I'm good. I'll find that.
We only have about 10 players, and I doubt anybody outside our family will ever use it. Although I'll post it if I ever finish the silly thing.
Bob

Re: Asking for help - Timer question
Posted: Thu Jan 28, 2010 9:34 pm
by JakeJ
Well, PHP can run, change, stop cron jobs, so that's what I was thinking. But bypassing php completely would be better. If you're running it on windows, you can either find the cron equivalent or install cygwin.
From a design perspective, think of it this way. You're not really updating time, you're incrementing a value based on an event. That event just happens to be a timer. So use the event in a mysql trigger in order to provide the updates to the database.
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 2:33 am
by VladSun
If you have a recent MySQL version you can use the EVENT command:
http://dev.mysql.com/tech-resources/art ... vents.html
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 9:44 am
by SpHawk
We're all laughing over here, we never saw this coming!
Ok, so the question will cost an amount of energy based on the level of the question. They get 1 pt energy every 3 min until their max. Missing a question takes away health that regenerates 1 pt every 5 min. And they can spend money on businesses that produce a revenue every 30 hours.
Ok, so I would make an event like an if-then statement, that if health is less than HealthMax every 5 min health=health + 1? And create one that always runs from the day you buy your lemonade stand, that gives an amount of cash every 30 hours?
And really, Thanks Vladsun & Jake. I've been asking at the university, and PHP groups, this is the first place that's said something beside "Hire a programmer."

And this was something my wife and I really wanted to do ourselves.
Bob
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 11:12 am
by pickle
I'd personally do it in cron jobs & PHP.
For example, run cron.php?energy every 3 minutes, cron.php?health every 5 minutes, cron.php?cash every 30 hours. cron.php will then do different stuff depending on whether $_GET['energy'], $_GET['health'], or $_GET['cash'] is set.
I think this would be easier to maintain. If you need to change the period, you just update the cron file. If you need to do more complex work than just incrementing a value, you can add that to a PHP file more easily than you can modify an SQL event.
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 11:24 am
by SpHawk
OK, my problem is not understanding what a "cron job" is. But you guys have been great, steering me in the right direction. I was going to use $_Get and so forth going between the pages, so it seems like with what you all have given me, I should be able to push the ball forward a bit, then have some more intelligent questions to ask.
Thank you all, I'll keep you all updated.

And am really looking forward being a part of this community.
Bob
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 12:42 pm
by VladSun
What I've seen in other people software is a dedicated include file that assembles the cron job functionality- i.e. if you include the same PHP file (e.g. cron.inc.php) in all *.php entry point files (i.e. files that are directly accessed by the user) and perform some DB checks and updates then you can solve this problem.
So ... in every .php entry point file you have:
And the cron.inc.php is (meta language):
Code: Select all
$db->query("
UPDATE
health
set
health.value = health.value + 1,
where
health.status = 'wrong_answer_still_reflecting'
and
punish_time < CURRENT_TIME() + INTERVAL 5 minutes
");
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 1:49 pm
by SpHawk
Ok, that actually makes sense to me. At least I know where to plug it in and play with it. This is pretty exciting, as I actually started studying PHP and MySql to learn how to build this game.
Thank you,
Bob
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 1:53 pm
by Christopher
SpHawk wrote:OK, my problem is not understanding what a "cron job" is.
cron is a unix program that runs other programs on a scheduled basis. You may not have access to schedule cron jobs on your hosting provider.
SpHawk wrote:But you guys have been great, steering me in the right direction. I was going to use $_Get and so forth going between the pages, so it seems like with what you all have given me, I should be able to push the ball forward a bit, then have some more intelligent questions to ask.
One solution not discussed here is to use Javascript timers to submit these requests. You would need to make your game an Ajax application -- meaning that it is just one HTML page that uses Javascript to submit requests to the server and make changes to the web page, rather than having links to other pages when things change. If you use a Javascript library like Ext or jQuery, the library will make programming the game in Javascript manageable. If you did this, the PHP program would mainly be providing bits of HTML, and XML or JSON data.
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 3:11 pm
by JakeJ
One solution not discussed here is to use Javascript timers to submit these requests. You would need to make your game an Ajax application -- meaning that it is just one HTML page that uses Javascript to submit requests to the server and make changes to the web page, rather than having links to other pages when things change. If you use a Javascript library like Ext or jQuery, the library will make programming the game in Javascript manageable. If you did this, the PHP program would mainly be providing bits of HTML, and XML or JSON data.
The only problem with that would be that the users health, etc. would not be able to accumulate without an active browser session. If you've played Mafia Wars on Facebook, you'd understand the problem. It really needs to be server-side to be effective.
That would be the right solution though if it was only updating during a live session. Always off load as much as you can to the client while accounting for security, bandwidth and scalability.
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 4:41 pm
by SpHawk
Well and part of the good news here is that the server is mine. I have a Windows 2003 server and have no real plans to go looking for an external server. I have WAMP running on it as I'm trying to learn PHP and MySql. So while someday I may consider making this thing live, I don't have too many illusions about it. I have eight kids that love facebook games. And Castle Age, Mafia Wars, Farmville and such are their favorites. So my wife and I joked around about an educational game. We started putting together things that we could give them as rewards, Pillow Parties, trips to Chuck e cheese, Park trips, cake or cookie batches. We put together a random routing that could make math problems to give them stuff to use in craft-making to use some more math and reading skills, with a little problem solving, and finishing out with investments that make enough for them to get the big family favors. And tie it together with a loose story line. While it could be the next "Where in the World is Carmen Santiago?" that's not the way I'd bet.
But I started learning PHP to do this game, and I want it to look a lot like mafia wars. My wife has been designing the web pages for it, and having a blast. But if I'm on a dead end, and should be learning ajax or java, I'm on board. But the above script looked like PHP, and I was going to try it. if I have that and MySql, will it run, or do I have to have a Unix operating system? Or did I really miss the point?
Bob
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 4:44 pm
by Christopher
JakeJ wrote:The only problem with that would be that the users health, etc. would not be able to accumulate without an active browser session. If you've played Mafia Wars on Facebook, you'd understand the problem. It really needs to be server-side to be effective.
Yes, but is this a multi-player game? I didn't get the sense it was. If it is not, then you could recalculate those values when the user logs-in -- based on the last time they were online.
Re: Asking for help - Timer question
Posted: Fri Jan 29, 2010 4:51 pm
by VladSun
arborint wrote:One solution not discussed here is to use Javascript timers to submit these requests.
I think, it's not discussed because it's hackable
