Jump to content

update user in DB every hour


AviNahum

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/275647-update-user-in-db-every-hour/
Share on other sites

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;
   }     
 }

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!

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;
   }     
 }

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.

  On 3/18/2013 at 2:53 PM, PaulRyan said:

 

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!

 

  On 3/18/2013 at 2:57 PM, DaveyK said:

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 ^^

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.