<?php
require_once("connect.inc.php");
require_once("getGroundChars.php");

/* ------------------------------------------------------------------------------------
getGroundInfos
Recupere les informations detaillees sur une case (x,y) de proprietaire "uid"
------------------------------------------------------------------------------------ */

function getGroundInfos( $x, $y )
{
global $connexion;

$req = '
SELECT x, y, uid, gid, tid, build, deco, login, emblem, master_id,
       user_guild_emblem as guild_emblem,
       user_guild_name   as guild_name
FROM (
      SELECT x, y, uid, gid, tid, build, deco,
             user_login           as login,
	     user_guild_emblem    as emblem,
             user_guild_master_id as master_id 
      FROM (
            SELECT ground_pos[0] as x, 
                   ground_pos[1] as y,
                   user_id       as uid,
                   ground_id     as gid,
                   type_id       as tid,
                   ground_build  as build,
                   deco_id       as deco
            FROM grounds 
            WHERE ground_pos ~= point ('.$x.','.$y.')
      ) AS g LEFT JOIN users
      ON users.user_id = g.uid
) AS gu LEFT JOIN users 
ON users.user_id = gu.master_id';

$res = pg_query($connexion, $req) or die( pg_last_error().'<br />'.$req );
$r   = pg_fetch_array($res);
$numRes = pg_num_rows($res);

if($numRes==0) {
	$x = 'x'; $y = 'y'; $img = $img2 = ''; $gtype = 0;
	$login = 'inconnu'; $gname = 'inconnue';
} else { 
	$build   = $r['build'];
	$login   = $r['login'];
	$gname   = $r['guild_name'];
	$gemblem = $r['guild_emblem'];
	
	if($r['uid']!=null && $r['uid']==$r['master_id'] ) 
		$login .= ' [Guild Master]';
	if($gemblem!=null) 
		$img = ' <img src="'.$gemblem.'" alt="'.$gname.'" />';
	if($build!=null) 
		$img2 = ' <img src="builds/b'.$build.'" alt="" />';
	$gtype = $r['tid'];
}
echo 'Case ('.$x.', '.$y.')
<div id="infos_ground" class="t'.$gtype.'">'.$img2.$img.'</div>
P: '.$login.'<br/>
G: '.$gname;

// sortie d'une liste <select> affichant les mimichants sur (x,y)
if($numRes!=0) getGroundChars( $x, $y );
}
getGroundInfos( $_GET['x'], $_GET['y'] );
?>