<?php

require_once("connect.inc.php");

/* ------------------------------------------------------------------------------------
creationCarte

construction du code XHTML de la carte pour les terrains dans la zone demandee
------------------------------------------------------------------------------------ */

function getGrounds( $xCentre, $yCentre, $width, $height, $caseSize )
{
	global $connexion;
	
// calcul du rectangle de coordonnees --------------------------------------------------
	
	$width2  = abs($width/2);
	$height2 = abs($height/2);
	
	$xMin = $xCentre - $width2;
	$xMax = $xCentre + $width2;
	$yMin = $yCentre - $height2;
	$yMax = $yCentre + $height2;

// requete SQL --------------------------------------------------------------------------
	
	$selection= 'WHERE ground_pos[0]>='.$xMin.' AND ground_pos[0]<'.$xMax.' AND ground_pos[1]>='.$yMin.' AND ground_pos[1]<'.$yMax;
	$req = 'SELECT type_id, user_id, ground_pos[0] AS x, ground_pos[1] AS y, ground_build, deco_id FROM grounds '.$selection;
	$cases    = pg_query($connexion, $req) or die( pg_last_error().'<br />'.$req );
	
// creation de chaque case existante ----------------------------------------------------

	while( $g = pg_fetch_array($cases) )
	{
		$x = $g['x'];
		$y = $g['y'];
		$gbuild = $g['ground_build'];
		
		$pos = ' style="left:'.($x-$xMin)*$caseSize.'px; top:'.($y-$yMin)*$caseSize.'px;"';
		$script = ' onclick="clic('.$x.','.$y.')"';
		
		if( strcmp($g['ground_build'],"0") )
			$build = '<img src="builds/b'.$gbuild.'.gif" alt="" />';
		else 
			$build = '';
		
		// $ground['deco_id']
		$html .= '<div class="t'.$g['type_id'].'" '.$pos.$script.'>'.$build.'</div>
';
	}
	echo $html;
}
//getGrounds( 0, 0, 8, 8, 56 );
//SELECT * FROM grounds WHERE ground_pos[0]>=-4 AND ground_pos[0]<4 AND ground_pos[1]>=-4 AND ground_pos[1]<4
getGrounds( $_GET['x'], $_GET['y'], $_GET['w'], $_GET['h'], $_GET['caseSize'] );
?>
