ragrim Posted September 27, 2010 Share Posted September 27, 2010 Hi, i dont know how to explain this so ill start with what i want to do. Id like to create a simple text based game in php for learning purposes but im stuck on 1 thing, making a page refresh based on a server timer. Basically every 2 minutes is a server tick and at such time your character gets +gold based on resources etc. im just not sure what such a thing would be called so i havnt been able to google it, obviously the ticks need to be server controlled so a user cant just refresh a page to get a new tick. i would also need the page to display how long left till next tick. the only way i can see it possible is to have a server side application controlling timers, and the php page requests time remaining or something. Any help would be muchly appreciated, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/214498-making-a-page-refresh-based-on-server-timer/ Share on other sites More sharing options...
jcbones Posted September 27, 2010 Share Posted September 27, 2010 This should get you started, be aware, I know it doesn't function 100% correctly. <<This is where you come into play. Consider it a simple tutorial, much more needs to be considered before the final implementation. <?php session_start(); $timestamp = time(); $time = 120; //120 seconds in 2 minutes. $collectGold = 10; //10 pieces of gold every 2 minutes. $gold = (isset($_SESSION['gold'])) ? $_SESSION['gold'] : 10; //If the gold session is set, collect it, otherwise set the starting value of gold. if(isset($_SESSION['ts'])) { //if timestamp is in the session. $slice = ($timestamp - $_SESSION['ts']); //timestamp minus the last page refresh. //Need to work on the following two lines to get the Gold to figure right. $gold += ($slice > $time) ?(int)($collectGold * ($slice / $time)) : 0; //if the timer runs out, increment gold based on a calculation(in case the timer has been out more than 2 minutes). $gold = floor($gold / 10) * 10; //clean up the gold, rounding down to the nearest ten if necessary. $diff = $time - $slice; //get our time difference. } if(!isset($_SESSION['ts']) || $diff > $time || $diff < 0) { //if the timestamp is NOT in session, or the difference is greater than the max timer amount, reset the timer, and the timestamp into the session. $diff = $time; $_SESSION['ts'] = $timestamp; } $_SESSION['gold'] = $gold; //collect the gold to the session. //Below is demonstration of output. Seconds could be passed to Javascript. $diff; //$diff holds seconds less than 3600 (1 hour); $hours = floor($diff / 3600) . ' : '; $diff = $diff % 3600; $minutes = floor($diff / 60) . ' : '; $diff = $diff % 60; $seconds = $diff; ?> <div id="strclock">Clock Here!</div> <div id="gold">Your current inventory is: <?php echo $gold; ?> pieces of Gold!</div> <script type="text/javascript"> var hour = <?php echo floor($hours); ?>; var min = <?php echo floor($minutes); ?>; var sec = <?php echo floor($seconds); ?> function countdown() { if(sec <= 0 && min > 0) { sec = 59; min -= 1; } else if(min <= 0 && sec <= 0) { min = 0; sec = 0; } else { sec -= 1; } if(min <= 0 && hour > 0) { min = 59; hour -= 1; } if(hour == 0 && min == 0 && sec == 0) { document.getElementById('strclock').innerHTML = 'You have more Gold, refresh to collect.'; } else { var pat = /^[0-9]{1}$/; sec = (pat.test(sec) == true) ? '0'+sec : sec; min = (pat.test(min) == true) ? '0'+min : min; hour = (pat.test(hour) == true) ? '0'+hour : hour; document.getElementById('strclock').innerHTML = hour+":"+min+":"+sec; } setTimeout("countdown()",1000); } countdown(); </script> Quote Link to comment https://forums.phpfreaks.com/topic/214498-making-a-page-refresh-based-on-server-timer/#findComment-1116167 Share on other sites More sharing options...
Rifts Posted September 27, 2010 Share Posted September 27, 2010 why dont you just use cron job Quote Link to comment https://forums.phpfreaks.com/topic/214498-making-a-page-refresh-based-on-server-timer/#findComment-1116169 Share on other sites More sharing options...
jcbones Posted September 27, 2010 Share Posted September 27, 2010 why dont you just use cron job A viable option, if the OP is using linux. This would make all of the users get gold at the same time. Then all you need to do is pass the database timestamp to a countdown script, and echo the gold out. Quote Link to comment https://forums.phpfreaks.com/topic/214498-making-a-page-refresh-based-on-server-timer/#findComment-1116170 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.