AviNahum Posted March 14, 2013 Share Posted March 14, 2013 (edited) hey, im building a web based browser game. what im trying to do is to give the user 15 tickets every hour. what i did is to store the time the user got the tickets last time, and when the user loging in again, or refresh the page, i check how many hours passed from the last time he got tickets, then i update his tickets to 15*$hours. the problem is that it updates the tickets all the time :| here is my code so far: $member_id = intval($this->member['id']); if ($member_id != 0) { $ticketsToAdd = 0; $lastTicket = $this->member['last_tickets']; // last time the user got tickets $ticketsJump = $INFO['tickets_jump']; // how many tickets to add the user, set to 15 $ticketsJumpTime = $INFO['tickets_jump_time']; // after how many hours update the user tickets, set to 1 $timeDiff = $std->timePassed($lastTicket); // calculate the time diffrence between now and the last time the user got tikets if ($timeDiff['hours'] >= $ticketsJumpTime) { $ticketsToAdd = $ticketsJump * $timeDiff['hours']; if ($timeDiff['minutes'] != 0) { $lastTicketWorkAround = time() - ($timeDiff['minutes']*60); } else { $lastTicketWorkAround = time(); } $DB->query("UPDATE users SET tickets = tickets+".$ticketsToAdd.", last_tickets='".$lastTicketWorkAround."' WHERE id='".$this->member['id']."'"); } } public function timePassed($str) { $sec = time() - $str; return array( "hours" => intval($sec / 60 / 60), "minutes" => intval($sec / 60)); # if($sec < 60) { /* if less than a minute, return seconds */ # return $sec . " seconds ago"; # } # else if($sec < 60*60) { /* if less than an hour, return minutes */ # return intval($sec / 60) . " minutes ago"; # } # else if($sec < 24*60*60) { /* if less than a day, return hours */ # return intval($sec / 60 / 60) . " hours ago"; # } # else { /* else returns days */ # return intval($sec / 60 / 60 / 24) . " days ago"; # } } i add some comments to the code so you can understend better. the whole code is a part from a class... Thanks in advance! Edited March 14, 2013 by aviavi Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/ Share on other sites More sharing options...
AviNahum Posted March 18, 2013 Author Share Posted March 18, 2013 *UP* still can't make it work :| Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419274 Share on other sites More sharing options...
PaulRyan Posted March 18, 2013 Share Posted March 18, 2013 (edited) Wouldn't it be alot simpler to just run a cron job ever hour to add 15 tickets to every user? Edit* - In what format is the times you are using? timestamp? or date/time? Edited March 18, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419276 Share on other sites More sharing options...
AviNahum Posted March 18, 2013 Author Share Posted March 18, 2013 (edited) im using the time() function... i dont using a cron cuz the time the user have to get the tickets depends on the time he registered... i dont want someone who registered in 13:59 to get tickets on 14:00 Edited March 18, 2013 by aviavi Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419279 Share on other sites More sharing options...
PaulRyan Posted March 18, 2013 Share Posted March 18, 2013 Why not? Does it really make so much of a difference? This way would be a lot simpler for you, and to edit in future also. Surely someone getting extra tickets isn't so much of an issue? Maybes something like this: $member_id = intval($this->member['id']); if($member_id >= 1) { $lastTickets = $this->member['last_tickets']; $ticketsPerHour = $INFO['tickets_jump']; $timeDifference = $_SERVER['REQUEST_TIME'] - $lastTickets; $timeHours = floor($timeDifference/60/60); $lastHour = (int)floor($_SERVER['REQUEST_TIME']/60/60)*60*60; if($timeHours >= 1) { $ticketsToAdd = ($ticketsPerHour*$timeHours); $query = "UPDATE `users` SET `tickets` = `tickets`+{$ticketsToAdd}, `last_tickets` = {$lastHour} WHERE `id` = {$member_id}"; echo $ticketsToAdd .' - '. $lastHour .'<br>'; echo $query; } } Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419281 Share on other sites More sharing options...
AviNahum Posted March 18, 2013 Author Share Posted March 18, 2013 (edited) first of all Thanks! second, it almost work... i set the time i have to get tickets to 14:19, and i did get this tickets, so the next time should be 15:19... insted it tells the next time is 15:00... another thing is that i have to handle minutes too... for example, lets say the last time i got tickets wast 12:20, then the next time i logged in is 14:10 so i have to get 30 tickets for 2 hours, and take in account that i have to get tickets again in the next 10 minutes! and yea, for the purpose im doing this tickets it is make a diffrence if a user will get tickets a few minutes after he registered :| Thanks again! Edited March 18, 2013 by aviavi Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419282 Share on other sites More sharing options...
Jessica Posted March 18, 2013 Share Posted March 18, 2013 You should still use a cron job. Determine how long ago someone had to have registered in order to be eligible to get tickets the next time, and update only users who registered before then. Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419286 Share on other sites More sharing options...
AviNahum Posted March 18, 2013 Author Share Posted March 18, 2013 well, i still insist not to use cron :| Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419303 Share on other sites More sharing options...
DaveyK Posted March 18, 2013 Share Posted March 18, 2013 If you ask me, if the tickets you are referring to are not publicly visible I would not use a cron job, simply because no one cares and the script is the same. Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419304 Share on other sites More sharing options...
Jessica Posted March 18, 2013 Share Posted March 18, 2013 well, i still insist not to use cron :|Yeah, why do things the correct, easy way, when you could hack at it for weeks and come up with something that sort of works? Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419307 Share on other sites More sharing options...
Solution PaulRyan Posted March 18, 2013 Solution Share Posted March 18, 2013 What about this? I'm about 90% clear on what you require, so I'm using my judgement. $member_id = intval($this->member['id']); if($member_id >= 1) { $lastTickets = $this->member['last_tickets']; $ticketsPerHour = $INFO['tickets_jump']; $timeDifference = $_SERVER['REQUEST_TIME'] - $lastTickets; $timeHours = floor($timeDifference/60/60); $lastHour = $lastTickets+($timeHours*60*60); if($timeHours >= 1) { $ticketsToAdd = ($ticketsPerHour*$timeHours); $query = "UPDATE `users` SET `tickets` = `tickets`+{$ticketsToAdd}, `last_tickets` = {$lastHour} WHERE `id` = {$member_id}"; echo $ticketsToAdd .' - '. $lastHour .'<br>'; echo $query; } } Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419311 Share on other sites More sharing options...
bylletski Posted March 18, 2013 Share Posted March 18, 2013 You can always use a time function i think? Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419312 Share on other sites More sharing options...
DaveyK Posted March 18, 2013 Share Posted March 18, 2013 No one benefits from sarcastic posts, get over it. The way I look at it, relating it to games: - One uses a cron job to calculate something that is publicly available. For instance, a score in a leaderboard in a game. Its imporatnt that this is always up to date even if the user itself is no longer online - One uses a regular page load /script to calculate stuff that only the user itself sees. For instance, the "resources" I have for a game. I dont care if I have a lot of them, or even a little. As long as the number is correct the moment I log in, I will not see any difference. In this case, I dont see how a cron job would improve the script. Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419313 Share on other sites More sharing options...
AviNahum Posted March 18, 2013 Author Share Posted March 18, 2013 What about this? I'm about 90% clear on what you require, so I'm using my judgement. $member_id = intval($this->member['id']); if($member_id >= 1) { $lastTickets = $this->member['last_tickets']; $ticketsPerHour = $INFO['tickets_jump']; $timeDifference = $_SERVER['REQUEST_TIME'] - $lastTickets; $timeHours = floor($timeDifference/60/60); $lastHour = $lastTickets+($timeHours*60*60); if($timeHours >= 1) { $ticketsToAdd = ($ticketsPerHour*$timeHours); $query = "UPDATE `users` SET `tickets` = `tickets`+{$ticketsToAdd}, `last_tickets` = {$lastHour} WHERE `id` = {$member_id}"; echo $ticketsToAdd .' - '. $lastHour .'<br>'; echo $query; } } as i see it now, it works perfectly! Thank you! No one benefits from sarcastic posts, get over it. The way I look at it, relating it to games: - One uses a cron job to calculate something that is publicly available. For instance, a score in a leaderboard in a game. Its imporatnt that this is always up to date even if the user itself is no longer online - One uses a regular page load /script to calculate stuff that only the user itself sees. For instance, the "resources" I have for a game. I dont care if I have a lot of them, or even a little. As long as the number is correct the moment I log in, I will not see any difference. In this case, I dont see how a cron job would improve the script. Thanks for clearing this up to everyone ^^ Quote Link to comment https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/#findComment-1419327 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.