<?php
include("config.php3");
include($BIB."bib_generic.php3");

# retourne un tableau des players du player à son root
function getRootChain($player_row,$specialMember = false)
{
	$PId = $player_row[FatherPId];
	for($r=$player_row[Rank]-1;$r>=0;$r--)
	{
		if ($specialMember and $r==$player_row[Rank]-1)
		{
			$row = db_fetch_array(main_db_query("SELECT NickName,Players.Id AS Id,FatherPId,Email FROM Players ".
				" LEFT JOIN Members ON Members.Id=Players.MemberId".
				" WHERE Players.Id='".$PId."'"));
			$tab[$r]["Email"]=$row[Email];
		}
		else
			$row = db_fetch_array(main_db_query("SELECT NickName,Id,FatherPId FROM Players WHERE Id='".$PId."'"));
		$tab[$r]["NickName"]=$row[NickName];
		$tab[$r]["Id"]=$row[Id];
		$PId = $row[FatherPId];
	}
	return $tab;
}


$sess = open_page($sessid);

# si c'est un membre, il faudra afficher + d'infos
$specialMember=false;

# Retrieve search_player record
# Either from id, nickname, or current member or root
if (!empty($id))
{
	$rows_search_player = main_db_query("SELECT * FROM Players WHERE Id='$id'");
}
else if ($name)
{
 	$rows_search_player = main_db_query("SELECT * FROM Players WHERE NickName = '$name'");
}

if (!$rows_search_player)
{
 	if ($sess->memberid != -1)
	{
    	$search_player = getplayer($sess->memberid);
	}
	else
	{
		$error=C_JOUEURINCONNU;
	}
}
else
{
	$search_player = db_fetch_array($rows_search_player);
	if (!$search_player)
	{
		$error = "$name ".C_INTROUVABLE;
	}
}

$title="Les invités de $search_player[NickName]";
include("header_popup.inc");

if (empty($error))
{
	# si c'est le membre qui visualise ses propres filleuls (en mode connecté)
	# il voit des infos en plus
	if ($search_player[MemberId]==$sess->memberid)
		$specialMember=true;
	
	# recup du fondateur de la dynastie s'il existe
	if ($search_player[FatherPId]!=0)
	{
		$rootTab = getRootChain($search_player,$specialMember);
		$root = getplayer2($rootTab[0][Id]);
	}
	else
	{
		$root = $search_player;
	}
	
	# calcul de tous les points de la dynastie
	$root_points = db_fetch_array(main_db_query("SELECT SUM(Score) AS AllScores FROM Players WHERE RootPId='".$root[Id]."'"));
	
	# calcul de l'age de la dynastie
	$age = intval(time_diff_now($root['Time'])/86400);
	
	# recup des filleuls
	if ($specialMember)
		$childs = main_db_query("SELECT Players.Id AS Id,NickName,Score,Sons,Fruit,Email".
			" FROM Players ".
			" LEFT JOIN Members ON Members.Id=Players.MemberId".
			" WHERE FatherPid='".$search_player[Id]."'");
	else
		$childs = main_db_query("SELECT Id,NickName,Score,Sons,Fruit FROM Players WHERE FatherPid='".$search_player[Id]."'");

	include("fiche-joueur.inc");
}
else
{
	echo $error;
}

include("footer_popup.inc");

close_page($sess);
?>