<commit_before><?php
if (file_exists('root.php'))
  include_once('root.php');
?><?php
//<NAME> //
$mail = '';

include_once(root.'class/db.class.php');
include_once(root.'class/construction.class.php');
include_once(root.'fonction/time.inc.php');
include_once(root.'fonction/action.inc.php');

//Récupère le timestamp en milliseconde de début de création de la page
$debut = getmicrotime();

//Récupération des variables de connexion à la base et connexion à cette base
include_once(root.'connect.php');

//Inclusion du fichier contenant toutes les variables indispensablent
include_once(root.'inc/variable.inc.php');

//Inclusion du fichier contenant toutes les informations sur les races
include_once(root.'inc/race.inc.php');

//Inclusion du fichier contenant toutes les variables du terrain (nom, pa)
include_once(root.'inc/type_terrain.inc.php');

include_once(root.'inc/ressource.inc.php');
//Inclusion du fichier contenant toutes les fonctions de base
include_once(root.'fonction/base.inc.php');

$date = date("Y-m-d", mktime(0, 0, 0, date("m") , date("d") - 1, date("Y")));
$date_hier = date("Y-m-d", mktime(0, 0, 0, date("m") , date("d") - 2, date("Y")));


$ressources = array();

$requete = "SELECT royaume.race as race, info, FLOOR(COUNT(*) / 10) as tot, COUNT(*) as tot_terrain FROM `map` LEFT JOIN royaume ON map.royaume = royaume.id WHERE royaume <> 0 GROUP BY info, royaume";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	if($row['tot'] > 0)
	{
		$typeterrain = type_terrain($row['info']);
		$ressources[$row['race']][$typeterrain[1]] = $row['tot'];
		$terrain[$row['race']][$typeterrain[1]] = $row['tot_terrain'];
	}
}

//print_r($ress);
//Ressource normale
$i = 0;
$key = array_keys($ressources);
foreach($ressources as $res)
{
	$j = 0;
	$keys = array_keys($res);
	while($j < count($res))
	{
		$k = 0;
		$kei = array_keys($ress[utf8_decode($keys[$j])]);
		foreach($ress[utf8_decode($keys[$j])] as $rr)
		{
			$ressource_final[$key[$i]][$kei[$k]] += $rr * $ressources[$key[$i]][$keys[$j]];
			if($kei[$k] == 'Nourriture') $tot_nou += $rr * $ressources[$key[$i]][$keys[$j]];
			$k++;
		}
		$j++;
	}
	$i++;
}


//Suppression des mines sans bourg
$requete = "SELECT a.id as id, a.rechargement, c.id as bourg_id FROM `construction` as a LEFT JOIN construction as c ON a.rechargement = c.id WHERE a.type ='mine'";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	if($row['bourg_id'] == '')
	{
		$mine = new construction($row['id']);
		$mine->supprimer();
	}
}

//Ressource mine
//On récupère la liste des batiments de type mine
$batiment = array();
$requete = "SELECT * FROM batiment WHERE type = 'mine'";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	$batiment[$row['id']] = $row;
}
//@TODO gérer les mines dans construction
$requete = "SELECT * FROM construction LEFT JOIN map ON map.id = (construction.y * 1000) + construction.x WHERE construction.type = 'mine'";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	$terrain = type_terrain($row['info']);
	echo utf8_decode($terrain[1]);
	$ress_terrain = $ress[utf8_decode($terrain[1])];
	$royaume = get_royaume_info($row['royaume'], $row['royaume']);
	if($batiment[$row['id_batiment']]['bonus2'] != 0)
	{
		switch($batiment[$row['id_batiment']]['bonus2'])
		{
			case 1 :
				$ress_final = array('Pierre' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Pierre']);
			break;
			case 2 :
				$ress_final = array('Bois' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Bois']);
			break;
			case 3 :
				$ress_final = array('Eau' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Eau']);
			break;
			case 4 :
				$ress_final = array('Sable' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Sable']);
			break;
			case 5 :
				$ress_final = array('Nourriture' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Nourriture']);
			break;
			case 6 :
				$ress_final = array('Star' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Star']);
			break;
			case 7 :
				$ress_final = array('Charbon' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Charbon']);
			break;
			case 8 :
				$ress_final = array('Essence Magique' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Essence Magique']);
			break;
		}
	}
	else
	{
		$ress_final = array();
		foreach($ress_terrain as $key => $value)
		{
			$ress_final[$key] = $batiment[$row['id_batiment']]['bonus1'] * $value;
		}
	}
	foreach($ress_final as $key => $value)
	{
		$ressource_final[$royaume['race']][$key] += $value;
		if($key == 'Nourriture') $tot_nou += $value;
	}
}

foreach($ressource_final as $key => $value)
{
	$requete = "SELECT ".$key." FROM stat_jeu WHERE date = '".$date."'";
	$req_stat_jeu = $db->query($requete);
	$row_stat_jeu = $db->read_assoc($req_stat_jeu);
	$explode_stat = explode(';', $row_stat_jeu[$key]);
	$explode_stat[18] = $value['Pierre'];
	$explode_stat[19] = $value['Bois'];
	$explode_stat[20] = $value['Eau'];
	$explode_stat[21] = $value['Sable'];
	$explode_stat[22] = $value['Charbon'];
	$explode_stat[23] = $value['Essence Magique'];
	$explode_stat[24] = $value['Star'];
	$explode_stat[25] = $value['Nourriture'];
	$implode_stat = implode(';', $explode_stat);
	$requete = "UPDATE royaume SET pierre = pierre + ".$value['Pierre'].", bois = bois + ".$value['Bois'].", eau = eau + ".$value['Eau'].", sable = sable + ".$value['Sable'].", charbon = charbon + ".$value['Charbon'].", essence = essence + ".$value['Essence Magique'].", star = star + ".$value['Star'].", food = food + ".$value['Nourriture']." WHERE race = '".$key."'";
	$db->query($requete);
	$requete = "UPDATE stat_jeu SET ".$key." = '".$implode_stat."' WHERE date = '".$date."'";
	$mail .= $requete."\n";
	$db->query($requete);
}

//Mis à jour de la nourriture totales
$food_total = ceil($tot_nou * 1.01);
$requete = "UPDATE stat_jeu SET food = ".$food_total." WHERE date = '".$date."'";
$db->query($requete);

//Nourriture
//On réduit de 1 les debuff famines
$requete = "UPDATE buff SET effet = effet - 3 WHERE type= 'famine'";
$db->query($requete);
$requete = "DELETE FROM buff WHERE type = 'famine' AND effet <= 0";
$db->query($requete);
//On récupère la food nécessaire par habitant
$requete = "SELECT food, nombre_joueur FROM stat_jeu WHERE date = '".$date_hier."'";
$req = $db->query($requete);
$row = $db->read_assoc($req);
if($row['nombre_joueur'] != 0) $food_necessaire = $row['food'] / $row['nombre_joueur'];
else $food_necessaire = 0;

$mail .= 'Nourriture nécessaire'.$food_necessaire."\n";

//On récupère les infos des royaumes
$requete = "SELECT id, race, food FROM royaume WHERE id != 0";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	$tab_royaume[$row['race']] = array('id' => $row['id'], 'food' => $row['food'], 'actif' => nb_habitant($row['race']));
}
foreach($tab_royaume as $race => $royaume)
{
	$royaume['food_necessaire'] = floor($food_necessaire * $royaume['actif']);
	//echo $royaume['race'].' '.$royaume['food_necessaire'].'<br />';
	//Si ya assez de food
	$mail .= "Race : ".$race." - Nécessaire : ".$royaume['food_necessaire']." / Possède : ".$royaume['food']."\n";
	if($royaume['food_necessaire'] < $royaume['food'])
	{
		$requete = "UPDATE royaume SET food = food - ".$royaume['food_necessaire']." WHERE id = ".$royaume['id'];
		$db->query($requete);
	}
	//Sinon
	else
	{
		//Calcul du debuff
		$royaume['food_doit'] = $royaume['food_necessaire'] - $royaume['food'];
		$ratio = $royaume['food_doit'] / $royaume['food_necessaire'];
		$debuff = ceil($ratio * 9) - 1;
		if($debuff > 6) $debuff = 6;
		elseif($debuff > 0)
		{
			$persos = array();
			$requete = "SELECT id FROM perso WHERE race = '".$race."' AND statut = 'actif'";
			$req = $db->query($requete);
			while($row = $db->read_assoc($req))
			{
				$persos[$row['id']] = $row['id'];
			}
			$perso_implode = implode(',', $persos);
			//On sélectionne les buffs à modifier
			$ids_buff = array();
			$requete = "SELECT id FROM buff WHERE type = 'famine' AND id_perso IN (".$perso_implode.")";
			$req = $db->query($requete);
			while($row = $db->read_assoc($req))
			{
				$ids_buff[] = $row;
			}
			$buffs = array();
			//On supprime les perso qui ont déjà un buff pour mettre à jour
			foreach($ids_buff as $buff)
			{
				unset($persos[$buff['id_joueur']]);
				$buffs[] = $buff['id'];
			}
			//30 jours
			$duree = 30 * 24 * 60 * 60;
			$fin = time() + $duree;
			$buffs_implode = implode(',', $buffs);
			if(count($buffs) > 0)
			{
				$requete = "UPDATE buff SET effet = effet + ".$debuff.", duree = ".$duree.", fin = ".$fin." WHERE id IN (".$buffs_implode.")";
				$db->query($requete);
			}
			$mail .= "Mis à jour du buff famine sur ".count($buffs_implode)." ".$race.", effet + ".$debuff.".\n";
			foreach($persos as $joueur)
			{
				//Lancement du buff
				lance_buff('famine', $joueur, $debuff, 0, $duree, 'Famine', 'Vos HP et MP max sont réduits de %effet%%', 'perso', 1, 0, 0, 0);
			}
			$mail .= "Lancement du buff famine sur ".count($persos)." ".$race.", effet : ".$debuff.".\n";
			$requete = "UPDATE royaume SET food = 0 WHERE id = ".$royaume['id'];
			$mail .= $requete."\n";
			$db->query($requete);
		}
	}
	$requete = "UPDATE buff SET effet = 50 WHERE type = 'famine' AND effet > 50";
	$db->query($requete);
}

mail('<EMAIL>', 'Starshine - Script journalier 4 du '.$date, $mail);

?><commit_msg>- bug gestion famine si tous les royaumes ont pas des joueurs + mail controle par env SSO_MAIL<commit_after><?php
if (file_exists('root.php'))
  include_once('root.php');
?><?php
//JOURNALIER RESSOURCES DE ROYAUME //
$mail = '';

include_once(root.'class/db.class.php');
include_once(root.'class/construction.class.php');
include_once(root.'fonction/time.inc.php');
include_once(root.'fonction/action.inc.php');

//Récupère le timestamp en milliseconde de début de création de la page
$debut = getmicrotime();

//Récupération des variables de connexion à la base et connexion à cette base
include_once(root.'connect.php');

//Inclusion du fichier contenant toutes les variables indispensablent
include_once(root.'inc/variable.inc.php');

//Inclusion du fichier contenant toutes les informations sur les races
include_once(root.'inc/race.inc.php');

//Inclusion du fichier contenant toutes les variables du terrain (nom, pa)
include_once(root.'inc/type_terrain.inc.php');

include_once(root.'inc/ressource.inc.php');
//Inclusion du fichier contenant toutes les fonctions de base
include_once(root.'fonction/base.inc.php');

$date = date("Y-m-d", mktime(0, 0, 0, date("m") , date("d") - 1, date("Y")));
$date_hier = date("Y-m-d", mktime(0, 0, 0, date("m") , date("d") - 2, date("Y")));


$ressources = array();

$requete = "SELECT royaume.race as race, info, FLOOR(COUNT(*) / 10) as tot, COUNT(*) as tot_terrain FROM `map` LEFT JOIN royaume ON map.royaume = royaume.id WHERE royaume <> 0 GROUP BY info, royaume";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	if($row['tot'] > 0)
	{
		$typeterrain = type_terrain($row['info']);
		$ressources[$row['race']][$typeterrain[1]] = $row['tot'];
		$terrain[$row['race']][$typeterrain[1]] = $row['tot_terrain'];
	}
}

//print_r($ress);
//Ressource normale
$i = 0;
$key = array_keys($ressources);
foreach($ressources as $res)
{
	$j = 0;
	$keys = array_keys($res);
	while($j < count($res))
	{
		$k = 0;
		$kei = array_keys($ress[utf8_decode($keys[$j])]);
		foreach($ress[utf8_decode($keys[$j])] as $rr)
		{
			$ressource_final[$key[$i]][$kei[$k]] += $rr * $ressources[$key[$i]][$keys[$j]];
			if($kei[$k] == 'Nourriture') $tot_nou += $rr * $ressources[$key[$i]][$keys[$j]];
			$k++;
		}
		$j++;
	}
	$i++;
}


//Suppression des mines sans bourg
$requete = "SELECT a.id as id, a.rechargement, c.id as bourg_id FROM `construction` as a LEFT JOIN construction as c ON a.rechargement = c.id WHERE a.type ='mine'";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	if($row['bourg_id'] == '')
	{
		$mine = new construction($row['id']);
		$mine->supprimer();
	}
}

//Ressource mine
//On récupère la liste des batiments de type mine
$batiment = array();
$requete = "SELECT * FROM batiment WHERE type = 'mine'";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	$batiment[$row['id']] = $row;
}
//@TODO gérer les mines dans construction
$requete = "SELECT * FROM construction LEFT JOIN map ON map.id = (construction.y * 1000) + construction.x WHERE construction.type = 'mine'";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	$terrain = type_terrain($row['info']);
	echo utf8_decode($terrain[1]);
	$ress_terrain = $ress[utf8_decode($terrain[1])];
	$royaume = get_royaume_info($row['royaume'], $row['royaume']);
	if($batiment[$row['id_batiment']]['bonus2'] != 0)
	{
		switch($batiment[$row['id_batiment']]['bonus2'])
		{
			case 1 :
				$ress_final = array('Pierre' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Pierre']);
			break;
			case 2 :
				$ress_final = array('Bois' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Bois']);
			break;
			case 3 :
				$ress_final = array('Eau' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Eau']);
			break;
			case 4 :
				$ress_final = array('Sable' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Sable']);
			break;
			case 5 :
				$ress_final = array('Nourriture' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Nourriture']);
			break;
			case 6 :
				$ress_final = array('Star' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Star']);
			break;
			case 7 :
				$ress_final = array('Charbon' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Charbon']);
			break;
			case 8 :
				$ress_final = array('Essence Magique' => $batiment[$row['id_batiment']]['bonus1'] * $ress_terrain['Essence Magique']);
			break;
		}
	}
	else
	{
		$ress_final = array();
		foreach($ress_terrain as $key => $value)
		{
			$ress_final[$key] = $batiment[$row['id_batiment']]['bonus1'] * $value;
		}
	}
	foreach($ress_final as $key => $value)
	{
		$ressource_final[$royaume['race']][$key] += $value;
		if($key == 'Nourriture') $tot_nou += $value;
	}
}

foreach($ressource_final as $key => $value)
{
	$requete = "SELECT ".$key." FROM stat_jeu WHERE date = '".$date."'";
	$req_stat_jeu = $db->query($requete);
	$row_stat_jeu = $db->read_assoc($req_stat_jeu);
	$explode_stat = explode(';', $row_stat_jeu[$key]);
	$explode_stat[18] = $value['Pierre'];
	$explode_stat[19] = $value['Bois'];
	$explode_stat[20] = $value['Eau'];
	$explode_stat[21] = $value['Sable'];
	$explode_stat[22] = $value['Charbon'];
	$explode_stat[23] = $value['Essence Magique'];
	$explode_stat[24] = $value['Star'];
	$explode_stat[25] = $value['Nourriture'];
	$implode_stat = implode(';', $explode_stat);
	$requete = "UPDATE royaume SET pierre = pierre + ".$value['Pierre'].", bois = bois + ".$value['Bois'].", eau = eau + ".$value['Eau'].", sable = sable + ".$value['Sable'].", charbon = charbon + ".$value['Charbon'].", essence = essence + ".$value['Essence Magique'].", star = star + ".$value['Star'].", food = food + ".$value['Nourriture']." WHERE race = '".$key."'";
	$db->query($requete);
	$requete = "UPDATE stat_jeu SET ".$key." = '".$implode_stat."' WHERE date = '".$date."'";
	$mail .= $requete."\n";
	$db->query($requete);
}

//Mis à jour de la nourriture totales
$food_total = ceil($tot_nou * 1.01);
$requete = "UPDATE stat_jeu SET food = ".$food_total." WHERE date = '".$date."'";
$db->query($requete);

//Nourriture
//On réduit de 1 les debuff famines
$requete = "UPDATE buff SET effet = effet - 3 WHERE type= 'famine'";
$db->query($requete);
$requete = "DELETE FROM buff WHERE type = 'famine' AND effet <= 0";
$db->query($requete);
//On récupère la food nécessaire par habitant
$requete = "SELECT food, nombre_joueur FROM stat_jeu WHERE date = '".$date_hier."'";
$req = $db->query($requete);
$row = $db->read_assoc($req);
if($row['nombre_joueur'] != 0) $food_necessaire = $row['food'] / $row['nombre_joueur'];
else $food_necessaire = 0;

$mail .= 'Nourriture nécessaire'.$food_necessaire."\n";

//On récupère les infos des royaumes
$requete = "SELECT id, race, food FROM royaume WHERE id != 0";
$req = $db->query($requete);
while($row = $db->read_assoc($req))
{
	$tab_royaume[$row['race']] = array('id' => $row['id'], 'food' => $row['food'], 'actif' => nb_habitant($row['race']));
}
foreach($tab_royaume as $race => $royaume)
{
	$royaume['food_necessaire'] = floor($food_necessaire * $royaume['actif']);
	//echo $royaume['race'].' '.$royaume['food_necessaire'].'<br />';
	//Si ya assez de food
	$mail .= "Race : ".$race." - Nécessaire : ".$royaume['food_necessaire']." / Possède : ".$royaume['food']."\n";
	//$royaume['food'] = 0; //-- test --
	//$royaume['food_necessaire'] = 1000; //-- test --
	if($royaume['food_necessaire'] < $royaume['food'])
	{
		$requete = "UPDATE royaume SET food = food - ".$royaume['food_necessaire']." WHERE id = ".$royaume['id'];
		$db->query($requete);
	}
	//Sinon
	else
	{
		//Calcul du debuff
		$royaume['food_doit'] = $royaume['food_necessaire'] - $royaume['food'];
		$ratio = $royaume['food_doit'] / $royaume['food_necessaire'];
		$debuff = ceil($ratio * 9) - 1;
		if($debuff > 6) $debuff = 6;
		if($debuff > 0)
		{
			$persos = array();
			$requete = "SELECT id FROM perso WHERE race = '".$race."' AND statut = 'actif'";
			$req = $db->query($requete);
			while($row = $db->read_assoc($req))
			{
				$persos[$row['id']] = $row['id'];
			}
			if (count($persos) == 0)
				continue;
			$perso_implode = implode(',', $persos);
			//On sélectionne les buffs à modifier
			$ids_buff = array();
			$requete = "SELECT id FROM buff WHERE type = 'famine' AND id_perso IN (".$perso_implode.")";
			$req = $db->query($requete);
			while($row = $db->read_assoc($req))
			{
				$ids_buff[] = $row;
			}
			$buffs = array();
			//On supprime les perso qui ont déjà un buff pour mettre à jour
			foreach($ids_buff as $buff)
			{
				unset($persos[$buff['id_joueur']]);
				$buffs[] = $buff['id'];
			}
			//30 jours
			$duree = 30 * 24 * 60 * 60;
			$fin = time() + $duree;
			$buffs_implode = implode(',', $buffs);
			if(count($buffs) > 0)
			{
				$requete = "UPDATE buff SET effet = effet + ".$debuff.", duree = ".$duree.", fin = ".$fin." WHERE id IN (".$buffs_implode.")";
				$db->query($requete);
			}
			$mail .= "Mis à jour du buff famine sur ".count($buffs_implode)." ".$race.", effet + ".$debuff.".\n";
			foreach($persos as $joueur)
			{
				//Lancement du buff
				lance_buff('famine', $joueur, $debuff, 0, $duree, 'Famine', 'Vos HP et MP max sont réduits de %effet%%', 'perso', 1, 0, 0, 0);
			}
			$mail .= "Lancement du buff famine sur ".count($persos)." ".$race.", effet : ".$debuff.".\n";
			$requete = "UPDATE royaume SET food = 0 WHERE id = ".$royaume['id'];
			$mail .= $requete."\n";
			$db->query($requete);
		}
	}
	$requete = "UPDATE buff SET effet = 50 WHERE type = 'famine' AND effet > 50";
	$db->query($requete);
}

$mail_send = getenv('SSO_MAIL');
if ($mail_send == null || $mail_send == '')
		 $mail_send = '<EMAIL>';
mail($mail_send, 'Starshine - Script journalier 4 du '.$date, $mail);

?>
{'fraction_non_alphanumeric': 0.22178683385579936, 'fraction_numerical': 0.010658307210031349, 'mean_word_length': 4.575589863093504, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2705857', 'n_tokens_mistral': 8150, 'n_tokens_neox': 7513, 'n_words': 1605}