<?php
if(!defined('CRON')) { header('Location: ../'); exit; }
print 'Début de la génération du classement ; il est : '.date('d/m/Y H:i:s');

//*
//Classement Joueurs
$bdd->connexion();
$bdd->query("TRUNCATE TABLE $table_classement;");
$users = $bdd->query("SELECT * FROM $table_user;");
$bdd->deconnexion();

$cnt = $bdd->num_rows;
for($i = 0; $i < $cnt; $i++){
	$id_user = $users[$i]['id'];
	$queryUser = $users[$i];
	$pointsbat = 0;
	$pointsterr = 0;
	$pointsvais = 0;

	$bdd->connexion();
	$planetes = $bdd->query("SELECT * FROM $table_planete WHERE id_user = $id_user;");
	$bdd->deconnexion();

	$cntp = $bdd->num_rows;
	for($j = 0; $j < $cntp; $j++){
		$queryPlanete = $planetes[$j];

		$nbbat = count($batimentVAR);
		for($k = 0; $k < $nbbat; $k++){
			if ($queryPlanete[$batimentVAR[$k]] == 0) continue;
			else {
				$n = $queryPlanete[$batimentVAR[$k]];
				eval($batimentCALC[$k][0]);
				eval($batimentCALC[$k][1]);
				eval($batimentCALC[$k][2]);
				$pointsbat += $a + $b + $c;
			}
		}

		$nbterr = count($nomterrnVAR);
		for($k = 0; $k < $nbterr; $k++){
			if ($queryPlanete[$nomterrnVAR[$k]] == 0) continue;
			else {
				$n = $queryPlanete[$nomterrnVAR[$k]];
				$pointsterr += ($nomterrnCALC[$k][0] + $nomterrnCALC[$k][1] + $nomterrnCALC[$k][2]) * $n;
			}
		}

		$nbvais = count($nomvaisnVAR);
		for($k = 0; $k < $nbvais; $k++){
			if ($queryPlanete[$nomvaisnVAR[$k]] == 0) continue;
			else {
				$n = $queryPlanete[$nomvaisnVAR[$k]];
				$pointsvais += ($nomvaisnCALC[$k][0] + $nomvaisnCALC[$k][1] + $nomvaisnCALC[$k][2]) * $n;
			}
		}
	}

	$nbtech = count($technoloVAR);
	$pointstech = 0;
	for($k = 0; $k < $nbtech; $k++){
		if ($queryUser[$technoloVAR[$k]] == 0) continue;
		else {
			$n = $queryUser[$technoloVAR[$k]];
			eval($technoloCALC[$k][0]);
			eval($technoloCALC[$k][1]);
			eval($technoloCALC[$k][2]);
			$pointstech += $a + $b + $c;
		}
	}

	$pointstech /= 1000;
	$pointsvais /= 1000;
	$pointsterr /= 1000;
	$pointsbat /= 1000;
	$points = $pointsbat + $pointsterr + $pointsvais + $pointstech;
	$bdd->connexion();
	$bdd->query("UPDATE $table_user SET points = $points, batiments = $pointsbat, recherches = $pointstech, flotte = $pointsvais, terrestre = $pointsterr WHERE id = $id_user;");
	if ($queryUser['auth_level'] < 2 && $queryUser['mv'] < 2) $bdd->query("INSERT INTO $table_classement (id_user, points, flottes, terrestres, recherches, batiments) VALUES ($id_user, $points, $pointsvais, $pointsterr, $pointstech, $pointsbat);");
	$bdd->deconnexion();
}

$bdd->connexion();
$classement = $bdd->query("SELECT U.id, U.auth_level FROM $table_classement C INNER JOIN $table_user U ON C.id_user = U.id ORDER BY C.points DESC;");
$cnt = $bdd->num_rows;
$p = 0;
for($i = 0; $i < $cnt; $i++){
	if ($classement[$i]['auth_level'] < 2) {
		$p++;
		$bdd->query("UPDATE $table_user SET place_points = $p WHERE id = ".$classement[$i]['id']);
	}
	else $bdd->query("UPDATE $table_user SET place_points = 0 WHERE id = ".$classement[$i]['id']);
}
$bdd->deconnexion();
//*/
print '<br />Fin de la génération du classement joueur ; il est : '.date('d/m/Y H:i:s');

//Classement Alliances
$bdd->connexion();
$bdd->query("TRUNCATE TABLE $table_classement_alliances;");
$alliances = $bdd->query("SELECT U.id_alliance, SUM(U.id) AS nombre, SUM(U.points)/SUM(U.id) AS points, SUM(U.batiments)/SUM(U.id) AS batiments, SUM(U.recherches)/SUM(U.id) AS recherches, SUM(U.flotte)/SUM(U.id) AS flotte, SUM(U.terrestre)/SUM(U.id) AS terrestre FROM $table_user U GROUP BY U.id_alliance;");
$cnt = $bdd->num_rows;
for($i = 0; $i < $cnt; $i++){
	if ($alliances[$i]['id_alliance'] != 0) $bdd->query("INSERT INTO $table_classement_alliances (id_alliance, nbr_membres, points, batiments, recherches, flottes, terrestres) VALUES (".$alliances[$i]['id_alliance'].", ".$alliances[$i]['nombre'].", ".$alliances[$i]['points'].", ".$alliances[$i]['batiments'].", ".$alliances[$i]['recherches'].", ".$alliances[$i]['flotte'].", ".$alliances[$i]['terrestre'].");");
}
$bdd->deconnexion();

print '<br />Fin de la génération du classement ; il est : '.date('d/m/Y H:i:s');
?>