View Full Version : [help] Game counter?
cjj
December 11th, 2007, 03:41 AM
Im not sure how to go about this script.
Basically I have games on my website and users are only allowed to play a specific game 5 times a day. And the next day the counter will start at 0 again.
I created a database table with fields like "users", "game_title", "times_played" and "date".
But I'm not sure how to create a script that will determine if the user has played the allocated amount times or if they can still play that day and how do I get the counter to start at 0 again every day?
I would really appreciate any help or advice.
Thanks
simplistik
December 11th, 2007, 09:55 AM
you don't really need the times_played, i personally wouldn't use it with that being said i'd do something like (if you're using php)
when a user plays a game it passes their info
$today = time( 'ymd',date() );
$result = mysql_query('SELECT * FROM stat_table WHERE game_id = $game_id AND user_id = $user_id AND date = $today');
$count = mysql_num_rows($result);
if ( $count != 5 )
{
// play game code here
}
else
{
// can't play game code here
mysql_query('INSERT INTO stat_table (game_id, user_id, date) VALUES ($game_id, $user_id, $today)');
}
note that the way i'm showing it assumes a few things.
You're using relational data
You call the rows by their id not their names (cause names can change)
The date is as yearmonthday
with that being said the way i'd set up my tables would be as such
game table
game_id, title, description, whateverelse
user table
user_id, name, password, whateverelse
stat table
stat_id, game_id, user_id, date
as a side note i recommend storing full unixtimestamps as your date... but here i just stored ymd cause if figured it'd be easiest for you...
cjj
December 12th, 2007, 01:54 AM
Hey...Thanks for the reply. Im just wondering how do I make the counter start at 0 again the next day?
icio
December 12th, 2007, 07:23 AM
Look into cron jobs for that. You should be able to configure them in your web host control panel.
simplistik
December 12th, 2007, 08:33 AM
well... in my example i dropped the counter column, and use a counter based on the date so for a player that played a game x on dec. 3, 2007 it adds 1 each time to the PHP counter, so that means it will by default automatically "reset" on dec. 4.
icio
December 12th, 2007, 08:40 AM
Ah, didn't catch that - good call. But your database is going to get pretty full if there isn't some method of emptying it.
wiifanatic
December 13th, 2007, 02:22 PM
then you could run a cron task at 12:00 AM that just drops everything :)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.