<?php
if(!defined('INDEX')) { header('Location: ../'); exit; }
$titre = 'Bourse';
include_once(_FCORE."../game/Class/class.bourse.php");

define('MAX_ACTION_PAR_JOUR', 5);

/*if (date('G') < 7 && date('G') >= 0 && date('w') != 0 && date('w') != 6 && $queryUser['auth_level'] < 3)
	erreur('La bourse est actuellement fermée !<br />Réouverture à 7h00', "orange");
else*/if ($queryPlanetes[0]['id'] != $planete->id)
	erreur('Vous devez être sur votre planète mère pour utiliser la bourse.');
else {
	$a = gpc('a');
	$v = gpc('v');
	$nb = gpc('a'.$a.$v, 'post');
	if (!empty($a) && is_numeric($nb)) {
		$action = new Bourse($a, $id_user);
		$poss24 = $action->actionIn24Hours();
		if ($poss24 + $nb > MAX_ACTION_PAR_JOUR) erreur('Vous ne pouvez pas acheter plus de 5 mêmes actions dans les mêmes 24h.');
		$ressources = $action->prixAchat($nb);
		if ($ressources[0] > $planete->metal || $ressources[1] > $planete->cristal) erreur('Vous n\'avez pas assez de ressources pour acheter '.$nb.' actions.');
		$action->addAction($nb);
		$planete->metal -= $ressources[0];
		$planete->cristal -= $ressources[1];

		header('Location: ?p=bourse&d='.$a);
		exit;
	}
	elseif (!empty($v) && is_numeric($nb)) {
		$action = new Bourse($v, $id_user);
		$ressources = $action->delAction($nb);
		$planete->metal += $ressources[0];
		$planete->cristal += $ressources[1];

		header('Location: ?p=bourse&d='.$v);
		exit;
	}
	elseif ($e = gpc('e') && $queryUser['auth_level'] >= 3) {
		if (isset($_POST['nomaction']) && isset($_POST['descaction'])) {
			$nom = gpc('nomaction', 'post');
			$description = gpc('descaction', 'post');
			$bdd->connexion();
			$bdd->escape($nom);
			$bdd->escape($description);
			$bdd->escape($e);
			$bdd->query("UPDATE $table_bourse SET description = '$description', nom = '$nom' WHERE id = $e;");
			$bdd->deconnexion();
		}
		elseif (isset($_POST['prixAM']) && isset($_POST['prixAC'])) {
			$prixAM = gpc('prixAM', 'post');
			$prixAC = gpc('prixAC', 'post');
			$bdd->connexion();
			$bdd->escape($prixAM);
			$bdd->escape($prixAC);
			$bdd->escape($e);
			$bdd->query("UPDATE $table_bourse SET metal = $prixAM, cristal = $prixAC WHERE id = $e;");
			$bdd->deconnexion();
		}
		elseif (isset($_POST['newsaction'])) {
			$news = gpc('newsaction', 'post');
			$time = time();
			$bdd->connexion();
			$bdd->escape($news);
			$bdd->escape($e);
			$bdd->query("UPDATE $table_bourse SET news = '$news', timeDesc = '$time' WHERE id = $e;");
			$bdd->deconnexion();
		}

		header('Location: ?p=bourse&d='.$e);
		exit;
	}
	elseif (gpc('c') == "new" && $queryUser['auth_level'] >= 3 && is_numeric(gpc('prixAM', 'post')) && is_numeric(gpc('prixAC', 'post'))) {
		if (isset($_POST['nomaction']) && isset($_POST['descaction'])) {
			$nom = gpc('nomaction', 'post');
			$description = gpc('descaction', 'post');
			$prixAM = gpc('prixAM', 'post');
			$prixAC = gpc('prixAC', 'post');
			$news = gpc('newsaction', 'post');
			$time = time();
			$bdd->connexion();
			$bdd->escape($nom);
			$bdd->escape($description);
			$bdd->escape($prixAM);
			$bdd->escape($prixAC);
			$bdd->escape($news);
			if (!empty($news)) $bdd->query("INSERT INTO $table_bourse (nom, description, news, timeDesc, metal, cristal) VALUES ('$nom', '$description', '$news', '$time', '$prixAM', '$prixAC');");
			else $bdd->query("INSERT INTO $table_bourse (nom, description, metal, cristal) VALUES ('$nom', '$description', '$prixAM', '$prixAC');");
			$bdd->deconnexion();
		}

		header('Location: ?p=bourse');
		exit;
	}
	elseif ($d = gpc('d')) {
		$action = new Bourse($d, $id_user);
		$template->assign('taxeA', $action->taxeA);
		$template->assign('taxeV', $action->taxeV);
		$template->assign('poss', $action->action());
		$template->assign('poss24', $action->actionIn24Hours());
		$action = null;

		$bdd->connexion();
		$bdd->escape($d);
		$bourse = $bdd->unique_query("SELECT id, nom, description, news, metal, cristal, (metal/metalAnc-1)*100 AS evolution FROM $table_bourse WHERE id = $d;");
		$bdd->deconnexion();

		$page = 'bourseDetails';
		$template->assign('bourse', $bourse);
	}
	else {
		$bdd->connexion();
		$infos = $bdd->query("SELECT id, nom, news AS description FROM $table_bourse WHERE news != '' ORDER BY timeDesc DESC LIMIT 7;");
		$bourse = $bdd->query("SELECT id, nom, metal, cristal, (metal/metalAnc-1)*100 AS evolution FROM $table_bourse;");
		$bdd->deconnexion();

		$page = 'bourse';
		$template->assign('bourse', $bourse);
		$template->assign('infos', $infos);
	}
}
?>