<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>


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


# d'abord les dynasties
main_db_query("DELETE FROM Dynasties");

$RootRows = main_db_query("SELECT * FROM Players WHERE FatherPId = 0 AND OffSprings>0");

	echo "Mise à jour des dynastie <br>";

	$i=1;
while ($RootRow=db_fetch_array($RootRows))
{
	flush();
	if (isset($DISPLAY))
	{
		echo "<span class=smallDark>".$i.". ".$RootRow[NickName]." : ";
	}
	# calcul de tous les points de la dynastie
	$ScoreTotalRow = db_fetch_array(main_db_query("SELECT SUM(Score) AS ScoreTotal FROM Players WHERE RootPId='".$RootRow[Id]."'"));
	$ScoreTotal = $ScoreTotalRow[ScoreTotal];

	# calcul de l'age de la dynastie
	$Time = $RootRow['Time'];

	# recup des filleuls
	$Population = $RootRow[Offsprings]+1; //db_fetch_array(main_db_query("SELECT COUNT(*) AS Population FROM Players WHERE RootPId='".$RootRow[Id]."'"));

	$ScoreMoyen=intval($ScoreTotal/$Population);

/*	$CurRows=main_db_query("SELECT * FROM Dynasties WHERE Dynasties.RootPId='".$RootRow[Id]."'");
	if ($CurRow=db_fetch_array($CurRows))
	{
		$CurRows=main_db_query("UPDATE Dynasties Set Score='".$ScoreTotal."', ScoreMoyen='".$ScoreMoyen."', Population='".$Population."', Time='".$Time."' WHERE RootPId='".$RootRow[Id]."'");
	}
	else
	{
		$CurRows=main_db_query("INSERT INTO Dynasties (Score, ScoreMoyen, Population, Time, RootPId) VALUES ('".$ScoreTotal."', '".$ScoreMoyen."', '".$Population."', '".$Time."', '".$RootRow[Id]."')");
	}
*/
	$CurRows=main_db_query("INSERT INTO Dynasties (Score, ScoreMoyen, Population, Time, RootPId) VALUES ('".$ScoreTotal."', '".$ScoreMoyen."', '".$Population."', '".$Time."', '".$RootRow[Id]."')");

	if (isset($DISPLAY))
	{
	echo "Score=".$ScoreTotal.", Population=".$Population.", ScoreMoyen=".$ScoreMoyen.", Début=".$Time."</span><br>";
	}
	$i++;

}

if (isset($DISPLAY))
{
	echo "Fin de la mise à jour des dynastie <br>";
}

?>