1 Replies - 838 Views - Last Post: 01 February 2012 - 01:03 PM Rate Topic: -----

Topic Sponsor:

#1 Astrofish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-February 12

PHP/MySQL/jQuery Countdown Timer Priority Queue

Posted 01 February 2012 - 03:17 AM

So I'm working a game feature similar to how OGame's construction queue functions. You initiate an action and then it adds it to a queue, the queue is processed based on how long it takes for that action to complete.

For example: You initiate the process to build a building that takes 90 seconds to complete. It is the first added to your queue so it starts counting down immediately. Then you initiate the build of another building and it is also added to the queue. But before it starts counting down it has to wait for the first action to be completed.

I'm doing this with PHP/MySQL and jQuery and I have the timer system down and you can add actions to a queue but the part I'm stuck on is having other actions wait their turn. I'm currently storing all events in a table which stores the time the event was initiated by the player, the time started by the countdown timer, and the time required for completion. Here's the code, it's a little dirty. As of now this is not functioning correctly.

// process completed items
		$sql = 'SELECT timer_queue.id AS tq_id, timer_queue.player_id, timer_queue.type, timer_queue.type_id, timer_queue.time_started, timer_queue.time_init, 
					kingdom_buildings.id AS kb_id, kingdom_buildings.name, kingdom_buildings.time_cost
					FROM timer_queue JOIN kingdom_buildings
					ON timer_queue.type = "kingdom_buildings"
					AND timer_queue.type_id = kingdom_buildings.id AND player_id = '.$user->id.'
					ORDER BY timer_queue.time_init ASC';
		$result = mysql_query($sql);
		$num_events = mysql_num_rows($result);
		if( $num_events > 0 )
		{
			$count = 1;
			while( $building = mysql_fetch_object($result) )
			{
				if( $building->time_started == 0 )
					$completion_time = $building->time_init + ($building->time_cost*$count);
				else
					$completion_time = $building->time_started + $building->time_cost;
					
				if( $completion_time < time() )
				{
					mysql_query('INSERT INTO kingdom_buildings_users ( building_id, kingdom_id, time_built ) VALUES ( '.$building->kb_id.', '.$user->kingdom_id.', '.$completion_time.' )');
					mysql_query('DELETE FROM timer_queue WHERE id = '.$building->tq_id);
				}
				$count++;
			}
		}
		
		// list remaining items
		$sql = 'SELECT timer_queue.id AS tq_id, timer_queue.player_id, timer_queue.type, timer_queue.type_id, timer_queue.time_started, timer_queue.time_init, 
					kingdom_buildings.id AS kb_id, kingdom_buildings.name, kingdom_buildings.time_cost
					FROM timer_queue JOIN kingdom_buildings
					ON timer_queue.type = "kingdom_buildings"
					AND timer_queue.type_id = kingdom_buildings.id AND player_id = '.$user->id.'
					ORDER BY timer_queue.time_init ASC';
		$result = mysql_query($sql);
		if( mysql_num_rows($result) <= 0 )
			echo 'Nothing in queue.';
		else
		{
			$count = 0;
			$finished_count = 0;
			while( $building = mysql_fetch_object($result) )
			{
				if( $count == 0 )
				{
					if( $building->time_started == 0 )
					{
						$time_started = time() - (time()-$building->time_init);
						mysql_query('UPDATE timer_queue SET time_started = '.$time_started.' WHERE id = '.$building->tq_id);
					}
					else
						$time_started = $building->time_started;
						
					$completion_time = $time_started + $building->time_cost;
					$time_remaining = $completion_time - time();
					
					?>
					<script type="text/javascript">
					<!--
					$(document).ready(function(){
						$('#timer-<?php echo $count; ?>').countdown({until: +<?php echo $time_remaining; ?>,  significant: 3,
						layout: '{d<}{dnn}{sep}{d>}{h<}{hnn}{sep}{h>}{m<}{mnn}{sep}{m>}{s<}{snn}{s>}',
						expiryText: 'Building Complete! - <a href="kingdoms.php">Refresh</a>'
						});
					});
					// -->
					</script>
					
					- <strong><?php echo $building->name; ?></strong>: <span id="timer-<?php echo $count; ?>">0</span><br/>
					
					<?php
				}
				else
				{
					echo '- <strong>',$building->name,'</strong>: ',sec2hms($building->time_cost,true),'<br/>';
				}
				$count++;
			}
		}


In my initial loop to process completed events the completion time is being calculated incorrectly but I haven't been able to figure out a fix to that either. If anyone could either guide me in the right direction or tell me I'm doing it all wrong and need to rethink the whole thing that would be amazing. Also if I'm using the wrong word for this functionality please let me know what this functionality is really called.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP/MySQL/jQuery Countdown Timer Priority Queue

#2 ButchDean  Icon User is offline

  • Pro Games Programmer
  • member icon


Reputation: 689
  • View blog
  • Posts: 2,504
  • Joined: 26-November 10

Re: PHP/MySQL/jQuery Countdown Timer Priority Queue

Posted 01 February 2012 - 01:03 PM

Is there any reason why you are specifically using MySQL etc. for this, as opposed to a compiled language like C++ or whatever?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1