Jump to content

2P turn based game crashes...


madmenyo

Recommended Posts

Hi folks,

 

If thought i finally managed to make a 2 player game with PHP and Jquery but i my whole computer gets stucked when the first player has gets his 2nd turn.

 

What i basically have are 2 .txt files where both players units are listed. I will read this file into an array. Each unit has a speed key wich value increases when that unit has made an action, i write the new speed to the file and the script runs again.

 

Heres the script that decides which unit goes first.

 

$time = 0;
$found = 0;
while ($found != 1)
{
foreach ($P1recruit as $p1)
{
	if ($found == 1) break;
	if ($p1['speed']<$time)
	{
		$p1['speed'] =  $p1['speed'] * ceil ($time / $p1['speed']);
	}		

	elseif ($p1['speed']==$time)
	{			
		$p1turn = true;
		$p2turn = false;
		$found ++;						
	}
}

foreach ($P2recruit as $p2)
{
	if ($found == 1) break;
	if ($p2['speed']<$time)
	{
		$p2['speed'] =  $p2['speed'] * ceil ($time / $p2['speed']);
	}		

	elseif ($p2['speed']==$time)
	{			
		$p1turn = false;
		$p2turn = true;		
		$found ++;			
	}
}


$time ++;

}

 

Then follows all the HTML to build up the page. The player who has to act gets a form with choices to do with his unit. The other player gets "Opponents turn" in that window.

 

So in the above code you see $p1turn or $p2turn gets true or false. This is for the Jquery script at the end of all the HTML to tell wich player needs to keep polling the unit files until theres a unit for him to act. Heres the script:

 

if ($p1turn == true)
{
if ($battle['player2'] == $_SESSION['SESS_MEMBER_ID'])
{
	echo "
			<script type='text/javascript'>

				function turnlistener() {
				   $('#main').load('battlegetnextunit.php');
				};

				$(function() {
					setInterval('turnlistener()', 5000 );
				});

			</script>
			";
}

}

if ($p2turn == true)
{
if ($battle['player1'] == $_SESSION['SESS_MEMBER_ID'])
{
	echo "
	<script type='text/javascript'>

	function turnlistener() {				         $('#main').load('battlegetnextunit.php');
};

$(function() {
setInterval('turnlistener()', 5000 );
});

</script>
			";
}
}

 

Now for the first player (the one who starts the battle) he can act properly. The next player can act, but he still is longpolling the .txt files as the form resets. If he acts withing the 5 seconds (setInterval('turnlistener()', 5000 )) the other player gets his turn again but my whole computer starts strugling and i'm not able to select anything in the form.

 

Maybe it's like the players get double longpolling the 2nd turn. But i only have the last code once and obviously only 1 of 2 can run as either $p1turn or $p2turn can be true and it can only run for one player.

 

What could this be? Is doing it this way even possible? I'm trying to figure this out for days now, i really hope someone can help me out.

 

Thanks!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.