PHP/Msql Timers

C++, C#, Java, PHP, ect...
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

PHP/Msql Timers

Post by hallsofvallhalla »

I have rarely messed timers for browser based games. Well in urban realms I am going to use buffs. You can activate or use items to enable buffs that last for a certain period of time. I have figured out how to store the data in a mysql table and pull it just working on displaying it and using it. I have had tons of people ask me about it so I thought I would post all my progression here along with a tutorial on how I did it. Once I finish it :)
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: PHP/Msql Timers

Post by OldRod »

Awesome! That will save me scratching my head trying to figure it out :lol:
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP/Msql Timers

Post by hallsofvallhalla »

yippee I did it!

here is the code

Code: Select all

<?php
include_once 'connect.php';

 $timeinfo="SELECT * from buffs where pid='1'";
    $timeinfo2=mysql_query($timeinfo) or die("Could not get user stats");
    $timeinfo3=mysql_fetch_array($timeinfo2);
	
	
$newtime = time();
$bufftime = time() + $timeinfo3[bufftime];

$finaltime = $bufftime - $newtime;
?>


<form name="counter"><input type="text" size="8" 
name="d2"></form> 

<script language="javascript">
 var bufftime = <?php echo $finaltime;?>;
 var milisec=0;
 var seconds=bufftime;
 document.counter.d2.value='30' 

function display(){ 
 if (milisec<=0){ 
    milisec=9 
    seconds-=1 
 } 
 if (seconds<=-1){ 
    milisec=0 
    seconds+=1 
 } 
 else 
    milisec-=1 
    document.counter.d2.value=seconds+"."+milisec 
    setTimeout("display()",100) 
} 
display() 




</script>

now the explanation

add a new table to your mysql called buffs
give it two fields pid and bufftime

you can add more later but for a test its all you need. Now this is not a final version for game but it gives you the basics to set it up.

Buff time for now will reset after every refresh, not ideal for buffs in a game. However you should insert the actual time of the buff started in the db then minus that for a real working version. It depends on how your version is going to work so I will leave that up to you.

Anyways this gives you what you need to make it all work. Enjoy :)
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: PHP/Msql Timers

Post by Torniquet »

depends if u want it to countdown live or just show how ever long you have left the moment the page was loaded...

this is how i have done similar in the past...

to set it...

Code: Select all

$finishTime = time() + 600

//10 minute length
then to display how much time you have left

Code: Select all

$timeRemaining = mysql_fetch_assoc(mysql_query("SELECT bufftime FROM `table` WHERE id='$uid'"));

$remaining = $timeRemaining - time()

if($remaining > 0){
  if($remaining > 59){
    $remaining = round($remaining / 60) . " Minutes Remaining";
  } else {
    $remaining = $remaining . " Seconds Remaining";
  }
} else {
  $remaining = "buffer ended";
}
thats just an alteration of a 'last active' code on my website for users. but i am sure the principles are the same. so use it how u will n hope it helped :p
New Site Coming Soon! Stay tuned :D
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP/Msql Timers

Post by hallsofvallhalla »

k got mine in game, it actually counts down the timer of your buff after you activate it then deletes itself when gone.

Image
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: PHP/Msql Timers

Post by OldRod »

Nice work! :)
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: PHP/Msql Timers

Post by Torniquet »

any chance on telling us how u get it to delete itself? would be cool to use

also do you think it could be altered to restore health / stamina etc rather than cron script? obv the health or w.e would only replensih while the user was logged in?
New Site Coming Soon! Stay tuned :D
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP/Msql Timers

Post by hallsofvallhalla »

well to do it in 10 minute increments you don't actually have to have the script run every 10 minutes.

Instead think of it this way. The user doesn't see his health or whatever until he logs in or reloads a page. Upon logging in have some code that calculates how many 10 minute intervals went since the user last logged in. Then heal on that number.

To delete the buff just have it delete upon reload of the page. The timer will hit zero and just show zero then when they go to another page or reload page it will erase it.
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP/Msql Timers

Post by Chris »

What if the buff is for defence right. And say it only lasts an hour, but the player is offline for 4. In those 4 hours he gets attacked, and gets extra defence when he shouldn't have?

I guess on the battle script you could check to see if he has any buffs that need to be removed before attacking. But after a while, with so many different types of buffs that will just turn into a mess through the whole site.

Cron would be the best way to do this.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: PHP/Msql Timers

Post by Torniquet »

Chris wrote:What if the buff is for defence right. And say it only lasts an hour, but the player is offline for 4. In those 4 hours he gets attacked, and gets extra defence when he shouldn't have?

I guess on the battle script you could check to see if he has any buffs that need to be removed before attacking. But after a while, with so many different types of buffs that will just turn into a mess through the whole site.

Cron would be the best way to do this.
i dont think you could do this through cron tbh (unless you can set a cron to run x amount of time after some1 is buffed).
New Site Coming Soon! Stay tuned :D
Post Reply

Return to “Coding”