<?php
	if (get_magic_quotes_gpc()) die ('get_magic_quotes_gpc');
	$location=$_SERVER["REMOTE_ADDR"];
	$timestamp=$_GET["t"];
	$guid=$_GET["g"];
	$p=$_GET["p"];
	$u=$_GET["u"];
	$n=$_GET["n"];
	$s=$_GET["s"]; //s=='' if a request
	$bhost=false;
	$bupdate=strlen($s)>0;

	if ($bupdate)
		$link=mysql_connect('localhost', 'inters_phpUpdate', 'flask3301') or die('Could not connect: ' . mysql_error());
	else //read-only
		$link=mysql_connect('localhost', 'inters_php', 'flask3301') or die('Could not connect: ' . mysql_error());
	mysql_select_db('inters_Flask') or die('Could not select database');

	$query="select nKey,Active,Players,User,Updates,Requests,Turn,Action from Package where Timestamp='".addslashes($timestamp)."' and Guid='".addslashes($guid)."' ";
	$result=mysql_query($query);
	$numresult=mysql_num_rows($result);

	$valid=true;
	$slot='(Unknown)';
	$active='(Unknown)';
	$players='(Unknown)';
	$updates='(Unknown)';
	$requests='(Unknown)';
	$turn='(Unknown)';
	$action='(Unknown)';
	$user='(Unknown)';

	if ($numresult!=1)
	{ //Match
		$valid=false;
		$reason= ($numresult==0)?'Stamp not found.':'Duplicate timestamps.';
	}
	if ($valid)
	{ //Active
		$row=mysql_fetch_assoc($result);

		$nKey=$row['nKey'];
		$turn=$row['Turn'];
		$active=$row['Active'];
		$action=$row['Action'];
		$user=$row['User'];
		$updates=$row['Updates'];
		$requests=$row['Requests'];
		$players=$row['Players'];
		if ($user==$u) $bhost=true;

		if (!$active)
		{
			$valid=false;
			$reason='Certificate is not active';
		}
		if ($turn>$u)
		{ 
			if ($u==0 && $p==$user)
			{ //new game
				$valid=true;
				$reason='Host reset game.';
			}
			else
			{ //earlier turn
				$valid=false;
				$reason='Turn update is stale.  Players action is ignored.';
				if ($u==0) $reason='Awaiting host to reset game.';
			}
		}
	}

	//if the game active and the turn is correct, add player action to the database
	if (!$valid) $bupdate=false;

	if ($bupdate)
	{ //Update
		$ame=$p.':'.$s.';';
		$action=$action.$ame;
		$updates=$updates+1;
		if ($turn!=$u)
		{ //new turn
			$action=$ame;
			$updates=1;
		}

		//escape literals and protect from sql injection
		$action=addslashes($action);
		$turn=doubleval($u);
		//update
		if ($updates)
		{ //update allowed
			$update="update Package set LastLocation='".$location."', LastTime=CURRENT_TIMESTAMP, Turn=$turn, Updates=$updates, Action='".$action."' where nKey='".$nKey."' ";
			$resultu=mysql_query($update);
			if (!$resultu) die ("Could not UPDATE ($update): " . mysql_error());
		}
	}

	$active=$active==1;

	//Key based on timestamp and location
	$pbpE=0;
	if ($valid)
	{
		$pbp=$location.$timestamp.$guid;
		for($i=0; $i<strlen($pbp); $i++)
		{
			$c=$pbp{$i};
			$ii=(int)$c;
			$pbpE=$pbpE+$ii*$ii;
		}
	}
	$pbpE=$pbpE*3301;
?>

<HTML><BODY>
	<H3 id=<?php echo $pbpE; ?>>Floodgate - Multiplayer Game<?php echo ($bpv?' (PREVIEW)':'') ?></H3>
	<P><hr>u=<?php echo $u; ?> s=<?php echo $s; ?> p=<?php echo $p; ?>.</P>
	<table>
		<tr><td>Host ID:</td><td><?php echo $guid; ?></td></tr>
		<tr><td>Game:</td><td><?php echo $timestamp; ?></td></tr>
		<tr><td>Active:</td><td><input type="checkbox" size="3" maxlength="3" <?php echo $active?CHECKED:UNCHECKED ?> DISABLED></td></tr>
		<tr><td>Host Name:</td><td><?php echo $user; ?></td></tr>
		<tr><td>Players:</td><td><?php echo $players; ?></td></tr>
		<tr><td>Turn:</td><td><?php echo $turn; ?></td></tr>
		<tr><td>Nthstep:</td><td><?php echo $n; ?></td></tr>
		<tr><td>Updates:</td><td><?php echo $updates; ?></td></tr>
		<tr><td>Requests:</td><td><?php echo $requests; ?></td></tr>
		<tr><td>Action:</td><td><?php echo $action; ?></td></tr>
	</table>
	<p>Query: <?php echo $query?></p>
	<p>Update: <?php echo $update?></p>
	<p><strong>
		<?php echo $valid?'<FONT color=green>Valid':'<FONT color=maroon>Invalid'; ?>
		<?php echo $bupdate?' Update':' Request'; ?>
		<?php echo ($reason?' - ':'') . $reason; ?>
	</FONT></strong></p>
	<p><hr><?php echo date('H:i jS F'); ?>, from: <?php echo $location; ?>.</p>
</BODY></HTML>
