<?php
/**
 * Tis file is part of XNova:Legacies
 *
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
 * @see http://www.xnova-ng.org/
 *
 * Copyright (c) 2009-Present, XNova Support Team <http://www.xnova-ng.org>
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *                                --> NOTICE <--
 *  This file is part of the core development branch, changing its contents will
 * make you unable to use the automatic updates manager. Please refer to the
 * documentation for further information about customizing XNova.
 *
 */

define('INSIDE'  , true);
define('INSTALL' , false);
define('IN_ADMIN', true);
require_once dirname(__FILE__) .'/common.php';

$parse = $lang;     
$parse['dpath'] = DEFAULT_SKINPATH;


$requer = 0;

	switch ($_GET['pp']){
		case 'exit':
			message('Vous pouvez r&eacute;-acc&eacute;der au tutoriel dans le menu. Bonne chance','Tutorial', 'overview.php');
			break;
			
		case 'finish':
			message('Vous avez termin&eacute; le didacticiel correctement. Bonne chance','Tutorial', 'overview.php');
			break;	
			
		case 1:
			if($planetrow[$resource[1]] >= 4){
				$parse['met_4'] = 'check';
				++$requer;
			}else{
				$parse['met_4'] = 'none';
			}
			if($planetrow[$resource[2]] >= 2){
				$parse['cris_2'] = 'check';
				++$requer;
			}else{
				$parse['cris_2'] = 'none';
			}
			if($planetrow[$resource[4]] >= 4){
				$parse['sol_4'] = 'check';
				++$requer;
			}else{
				$parse['sol_4'] = 'none';
			}
			if($_GET['continue'] == 1 and $requer == 3 and $user['tut_1'] == 0){
				$planetrow['metal'] += 150;
				$planetrow['crystal'] += 75;
				doquery("UPDATE {{table}} SET `tut_1` = '1' WHERE `id` = '".$user['id']."';", 'users');
				PlanetResourceUpdate ( $user, $planetrow, time());
				$user['tut_1'] = 1;
				if($user['tut_1'] == 1 and $user['tut_2'] == 1 and $user['tut_3'] == 1 and $user['tut_4'] == 1 and $user['tut_5'] == 1 and $user['tut_6'] == 1 and $user['tut_7'] == 1 and $user['tut_8'] == 1 and $user['tut_9'] == 1){
					header('Location: tutorial.php?pp=finish');
					die();
				}
				header('Location: tutorial.php?pp=1');
			}
			if($requer == 3 and $user['tut_1'] == 0){
				$parse['button'] = '<input type="button" onclick="window.location = \'tutorial.php?pp=1&continue=1\'" value="R&eacute;compense" style="width:150px;height:30px;color:orange;"/></th>';
			}elseif($user['tut_1'] == 1){
				$parse['button'] = '<input type="button" value="Termin&eacute;e" style="width:150px;height:30px;color:green;" disabled="true" />';
			}else{
				$parse['button'] = '<input type="button" onclick="document.getElementById(\'tutorial_solution\').style.display = \'block\';this.disabled = true;" value="Conseils" style="width:130px;"/>';
			}
			$page = parsetemplate(gettemplate('tutorial/tutorial_1'), $parse);
			break;

		case 2:
			if($planetrow[$resource[3]] >= 2){
				$parse['deu_4'] = 'check';
				++$requer;
			}else{
				$parse['deu_4'] = 'none';
			}
			if($planetrow[$resource[14]] >= 2){
				$parse['robot_2'] = 'check';
				++$requer;
			}else{
				$parse['robot_2'] = 'none';
			}
			if($planetrow[$resource[21]] >= 1){
				$parse['han_1'] = 'check';
				++$requer;
			}else{
				$parse['han_1'] = 'none';
			}
			if($planetrow[$resource[401]] >= 1){
				$parse['lanz_1'] = 'check';
				++$requer;
			}else{
				$parse['lanz_1'] = 'none';
			}
			if($_GET['continue'] == 1 and $requer == 4 and $user['tut_2'] == 0){
				doquery("UPDATE {{table}} SET `".$resource[401]."` = `".$resource[401]."` + 2 WHERE `id` = '".$planetrow['id']."';", 'planets');
				doquery("UPDATE {{table}} SET `tut_2` = '1' WHERE `id` = '".$user['id']."';", 'users');
				$user['tut_2'] = 1;
				if($user['tut_1'] == 1 and $user['tut_2'] == 1 and $user['tut_3'] == 1 and $user['tut_4'] == 1 and $user['tut_5'] == 1 and $user['tut_6'] == 1 and $user['tut_7'] == 1 and $user['tut_8'] == 1 and $user['tut_9'] == 1){
					header('Location: tutorial.php?pp=finish');
					die();
				}
				header('Location: tutorial.php?pp=2');
			}
			if($requer == 4 and $user['tut_2'] == 0){
				$parse['button'] = '<input type="button" onclick="window.location = \'tutorial.php?pp=2&continue=1\'" value="R&eacute;compense" style="width:150px;height:30px;color:orange;"/></th>';
			}elseif($user['tut_2'] == 1){
				$parse['button'] = '<input type="button" value="Termin&eacute;e" style="width:150px;height:30px;color:green;" disabled="true" />';
			}else{
				$parse['button'] = '<input type="button" onclick="document.getElementById(\'tutorial_solution\').style.display = \'block\';this.disabled = true;" value="Conseils" style="width:130px;"/>';
			}
			$page = parsetemplate(gettemplate('tutorial/tutorial_2'), $parse);
			break;

		case 3:
			if($planetrow[$resource[1]] >= 10){
				$parse['met_10'] = 'check';
				++$requer;
			}else{
				$parse['met_10'] = 'none';
			}
			if($planetrow[$resource[2]] >= 7){
				$parse['cris_7'] = 'check';
				++$requer;
			}else{
				$parse['cris_7'] = 'none';
			}
			if($planetrow[$resource[3]] >= 5){
				$parse['deut_5'] = 'check';
				++$requer;
			}else{
				$parse['deut_5'] = 'none';
			}
			if($_GET['continue'] == 1 and $requer == 3 and $user['tut_3'] == 0){
				$planetrow['metal'] += 2000;
				$planetrow['crystal'] += 500;
				doquery("UPDATE {{table}} SET `tut_3` = '1' WHERE `id` = '".$user['id']."';", 'users');
				PlanetResourceUpdate ( $user, $planetrow, time());
				$user['tut_3'] = 1;
				if($user['tut_1'] == 1 and $user['tut_2'] == 1 and $user['tut_3'] == 1 and $user['tut_4'] == 1 and $user['tut_5'] == 1 and $user['tut_6'] == 1 and $user['tut_7'] == 1 and $user['tut_8'] == 1 and $user['tut_9'] == 1){
					header('Location: tutorial.php?pp=finish');
					die();
				}
				header('Location: tutorial.php?pp=4');
			}
			if($requer == 3 and $user['tut_3'] == 0){
				$parse['button'] = '<input type="button" onclick="window.location = \'tutorial.php?pp=3&continue=1\'" value="R&eacute;compense" style="width:150px;height:30px;color:orange;"/></th>';
			}elseif($user['tut_3'] == 1){
				$parse['button'] = '<input type="button" value="Termin&eacute;e" style="width:150px;height:30px;color:green;" disabled="true" />';
			}else{
				$parse['button'] = '<input type="button" onclick="document.getElementById(\'tutorial_solution\').style.display = \'block\';this.disabled = true;" value="Conseils" style="width:130px;"/>';
			}
			$page = parsetemplate(gettemplate('tutorial/tutorial_3'), $parse);
			break;
			
		case 4:
			if($planetrow[$resource[31]] >= 1){
				$parse['inv_1'] = 'check';
				++$requer;
			}else{
				$parse['inv_1'] = 'none';
			}
						if($planetrow[$resource[202]] >= 1){
				$parse['navp_1'] = 'check';
				++$requer;
			}else{
				$parse['navp_1'] = 'none';
			}
			if($user[$resource[115]] >= 2){
				$parse['comb_2'] = 'check';
				++$requer;
			}else{
				$parse['comb_2'] = 'none';
			}
			if($_GET['continue'] == 1 and $requer == 3 and $user['tut_4'] == 0){
				$planetrow['deuterium'] += 400;
				doquery("UPDATE {{table}} SET `tut_4` = '1' WHERE `id` = '".$user['id']."';", 'users');
				PlanetResourceUpdate ( $user, $planetrow, time());
				$user['tut_4'] = 1;
				if($user['tut_1'] == 1 and $user['tut_2'] == 1 and $user['tut_3'] == 1 and $user['tut_4'] == 1 and $user['tut_5'] == 1 and $user['tut_6'] == 1 and $user['tut_7'] == 1 and $user['tut_8'] == 1 and $user['tut_9'] == 1){
					header('Location: tutorial.php?pp=finish');
					die();
				}
				header('Location: tutorial.php?pp=5');
			}
			if($requer == 3 and $user['tut_4'] == 0){
				$parse['button'] = '<input type="button" onclick="window.location = \'tutorial.php?pp=4&continue=1\'" value="R&eacute;compense" style="width:150px;height:30px;color:orange;"/></th>';
			}elseif($user['tut_4'] == 1){
				$parse['button'] = '<input type="button" value="Termin&eacute;e" style="width:150px;height:30px;color:green;" disabled="true" />';
			}else{
				$parse['button'] = '<input type="button" onclick="document.getElementById(\'tutorial_solution\').style.display = \'block\';this.disabled = true;" value="Conseils" style="width:130px;"/>';
			}
			$page = parsetemplate(gettemplate('tutorial/tutorial_4'), $parse);
			break;

		case 5:
			if(isset($_POST['forum_content']) and strpos($_POST['forum_content'], $game_config['forum_url']) !== false){
				doquery("UPDATE {{table}} SET `tut_5_forum` = '1' WHERE `id` = '".$user['id']."';", 'users');
				$user['tut_5_forum'] = 1;
			}
			if($user['tut_5_forum'] == 1){
				$parse['forum'] = 'check';
				++$requer;
			}else{
				$parse['forum'] = 'none';
			}
			$buddyrow = doquery( "SELECT count(*) AS `total` FROM {{table}} WHERE `sender` = '" . $user["id"]."' OR `owner` = '" . $user["id"]."';", 'buddy', true );
			if($buddyrow['total'] >= 1){
				$parse['buddy'] = 'check';
				++$requer;
			}else{
				$parse['buddy'] = 'none';
			}
			$allyrow = doquery( "SELECT count(*) AS `total` FROM {{table}} WHERE `ally_id` = '" . $user["ally_id"]."';", 'users', true );
			if($user['ally_id'] != 0 and $allyrow['total'] >= 1){
				$parse['ally'] = 'check';
				++$requer;
			}else{
				$parse['ally'] = 'none';
			}
			if($_GET['continue'] == 1 and $requer == 3 and $user['tut_5'] == 0){
				$planetrow['metal'] += 15000;
				$planetrow['crystal'] += 10000;
				$planetrow['deuterium'] += 5000;
				doquery("UPDATE {{table}} SET `tut_5` = '1' WHERE `id` = '".$user['id']."';", 'users');
				PlanetResourceUpdate ( $user, $planetrow, time());
				$user['tut_5'] = 1;
				if($user['tut_1'] == 1 and $user['tut_2'] == 1 and $user['tut_3'] == 1 and $user['tut_4'] == 1 and $user['tut_5'] == 1 and $user['tut_6'] == 1 and $user['tut_7'] == 1 and $user['tut_8'] == 1 and $user['tut_9'] == 1){
					header('Location: tutorial.php?pp=finish');
					die();
				}
				header('Location: tutorial.php?pp=6');
			}
			if($requer == 3 and $user['tut_5'] == 0){
				$parse['button'] = '<input type="button" onclick="window.location = \'tutorial.php?pp=5&continue=1\'" value="R&eacute;compense" style="width:150px;height:30px;color:orange;"/></th>';
			}elseif($user['tut_5'] == 1){
				$parse['button'] = '<input type="button" value="Termin&eacute;e" style="width:150px;height:30px;color:green;" disabled="true" />';
			}else{
				$parse['button'] = '<input type="button" onclick="document.getElementById(\'tutorial_solution\').style.display = \'block\';this.disabled = true;" value="Conseils" style="width:130px;"/>';
			}
			$page = parsetemplate(gettemplate('tutorial/tutorial_5'), $parse);
			break;		

		case 6:
		
		$missionspy = doquery( "SELECT count(*) AS `total` FROM {{table}} WHERE `message_owner` = '" . $user["id"]."' AND `message_subject` = 'Rapport d\'espionnage';", 'messages', true );
		
		if($missionspy['total'] >= 1){
				$parse['esp'] = 'check';
				++$requer;
			}else{
				$parse['esp'] = 'none';
			}
			
			if($planetrow[$resource[210]] >= 1){
				$parse['sond'] = 'check';
				++$requer;
			}else{
				$parse['sond'] = 'none';
			}
			
			if($_GET['continue'] == 1 and $requer == 2 and $user['tut_6'] == 0){
				doquery("UPDATE {{table}} SET `".$resource[210]."` = `".$resource[210]."` + 2 WHERE `id` = '".$planetrow['id']."';", 'planets');
				doquery("UPDATE {{table}} SET `tut_6` = '1' WHERE `id` = '".$user['id']."';", 'users');
				$user['tut_6'] = 1;
				if($user['tut_1'] == 1 and $user['tut_2'] == 1 and $user['tut_3'] == 1 and $user['tut_4'] == 1 and $user['tut_5'] == 1 and $user['tut_6'] == 1 and $user['tut_7'] == 1 and $user['tut_8'] == 1 and $user['tut_9'] == 1){
					header('Location: tutorial.php?pp=finish');
					die();
				}
				header('Location: tutorial.php?pp=7');
			}
			if($requer == 2 and $user['tut_6'] == 0){
				$parse['button'] = '<input type="button" onclick="window.location = \'tutorial.php?pp=6&continue=1\'" value="R&eacute;compense" style="width:150px;height:30px;color:orange;"/></th>';
			}elseif($user['tut_6'] == 1){
				$parse['button'] = '<input type="button" value="Termin&eacute;e" style="width:150px;height:30px;color:green;" disabled="true" />';
			}else{
				$parse['button'] = '<input type="button" onclick="document.getElementById(\'tutorial_solution\').style.display = \'block\';this.disabled = true;" value="Conseils" style="width:130px;"/>';
			}
			$page = parsetemplate(gettemplate('tutorial/tutorial_6'), $parse);
			break;
		
		case 7:
			$missionexpedition = doquery( "SELECT count(*) AS `total` FROM {{table}} WHERE `message_owner` = '" . $user["id"]."' AND `message_subject` = 'Rapport d\'expédition';", 'messages', true );
		
		if($missionexpedition['total'] >= 1){
				$parse['exp'] = 'check';
				++$requer;
			}else{
				$parse['exp'] = 'none';
			}
			if($_GET['continue'] == 1 and $requer == 1 and $user['tut_7'] == 0){
				doquery("UPDATE {{table}} SET `".$resource[202]."` = `".$resource[202]."` + 5 , `".$resource[205]."` = `".$resource[205]."` + 2 WHERE `id` = '".$planetrow['id']."';", 'planets');
				doquery("UPDATE {{table}} SET `tut_7` = '1' WHERE `id` = '".$user['id']."';", 'users');
				$user['tut_7'] = 1;
				if($user['tut_1'] == 1 and $user['tut_2'] == 1 and $user['tut_3'] == 1 and $user['tut_4'] == 1 and $user['tut_5'] == 1 and $user['tut_6'] == 1 and $user['tut_7'] == 1 and $user['tut_8'] == 1 and $user['tut_9'] == 1){
					header('Location: tutorial.php?pp=finish');
					die();
				}
				header('Location: tutorial.php?pp=8');
			}
			if($requer == 1 and $user['tut_7'] == 0){
				$parse['button'] = '<input type="button" onclick="window.location = \'tutorial.php?pp=7&continue=1\'" value="R&eacute;compense" style="width:150px;height:30px;color:orange;"/></th>';
			}elseif($user['tut_7'] == 1){
				$parse['button'] = '<input type="button" value="Termin&eacute;e" style="width:150px;height:30px;color:green;" disabled="true" />';
			}else{
				$parse['button'] = '<input type="button" onclick="document.getElementById(\'tutorial_solution\').style.display = \'block\';this.disabled = true;" value="Conseils" style="width:130px;"/>';
			}
			$page = parsetemplate(gettemplate('tutorial/tutorial_7'), $parse);
			break;

		case 8:
			$planets = doquery( "SELECT count(*) AS `total` FROM {{table}} WHERE `id_owner` = '" . $user["id"]."';", 'planets', true );
			if($planets['total'] >= 2){
				$parse['colonia'] = 'check';
				++$requer;
			}else{
				$parse['colonia'] = 'none';
			}
			if($_GET['continue'] == 1 and $requer == 1 and $user['tut_8'] == 0){
				doquery("UPDATE {{table}} SET `".$resource[150]."` = `".$resource[150]."` + 1 WHERE `id` = '".$planetrow['id']."';", 'planets');
				doquery("UPDATE {{table}} SET `tut_8` = '1' WHERE `id` = '".$user['id']."';", 'users');
				$user['tut_8'] = 1;
				if($user['tut_1'] == 1 and $user['tut_2'] == 1 and $user['tut_3'] == 1 and $user['tut_4'] == 1 and $user['tut_5'] == 1 and $user['tut_6'] == 1 and $user['tut_7'] == 1 and $user['tut_8'] == 1 and $user['tut_9'] == 1){
					header('Location: tutorial.php?pp=finish');
					die();
				}
				header('Location: tutorial.php?pp=9');
			}
			if($requer == 1 and $user['tut_8'] == 0){
				$parse['button'] = '<input type="button" onclick="window.location = \'tutorial.php?pp=8&continue=1\'" value="R&eacute;compense" style="width:150px;height:30px;color:orange;"/></th>';
			}elseif($user['tut_8'] == 1){
				$parse['button'] = '<input type="button" value="Termin&eacute;e" style="width:150px;height:30px;color:green;" disabled="true" />';
			}else{
				$parse['button'] = '<input type="button" onclick="document.getElementById(\'tutorial_solution\').style.display = \'block\';this.disabled = true;" value="Conseils" style="width:130px;"/>';
			}
			$page = parsetemplate(gettemplate('tutorial/tutorial_8'), $parse);
			break;


		case 9:
			$missionexploitation = doquery( "SELECT count(*) AS `total` FROM {{table}} WHERE `message_owner` = '" . $user["id"]."' AND `message_subject` = 'Rapport d\'exploitation';", 'messages', true );
		
		if($missionexploitation['total'] >= 1){
				$parse['exl'] = 'check';
				++$requer;
			}else{
				$parse['exl'] = 'none';
			}
			if($_GET['continue'] == 1 and $requer == 1 and $user['tut_9'] == 0){
				doquery("UPDATE {{table}} SET `".$resource[209]."` = `".$resource[209]."` + 1 WHERE `id` = '".$planetrow['id']."';", 'planets');
				$user['tut_9'] = 1;
				doquery("UPDATE {{table}} SET `tut_9` = '1' WHERE `id` = '".$user['id']."';", 'users');
				$user['tut_9'] = 1;
				if($user['tut_1'] == 1 and $user['tut_2'] == 1 and $user['tut_3'] == 1 and $user['tut_4'] == 1 and $user['tut_5'] == 1 and $user['tut_6'] == 1 and $user['tut_7'] == 1 and $user['tut_8'] == 1 and $user['tut_9'] == 1){
					header('Location: tutorial.php?pp=finish');
					die();
				}
				header('Location: tutorial.php');

			}
			if($requer == 1 and $user['tut_9'] == 0){
				$parse['button'] = '<input type="button" onclick="window.location = \'tutorial.php?pp=9&continue=1\'" value="R&eacute;compense" style="width:150px;height:30px;color:orange;"/></th>';
			}elseif($user['tut_9'] == 1){
				$parse['button'] = '<input type="button" value="Termin&eacute;e" style="width:150px;height:30px;color:green;" disabled="true" />';
			}else{
				$parse['button'] = '<input type="button" onclick="document.getElementById(\'tutorial_solution\').style.display = \'block\';this.disabled = true;" value="Conseils" style="width:130px;"/>';
			}
			$page = parsetemplate(gettemplate('tutorial/tutorial_9'), $parse);
			break;		
		
		default:
			 for($e = 1; $e <= 9; ++$e ){
				if($user['tut_'.$e ] == 1){
					$parse['tut_'.$e ] = 'check';
				}elseif($user['tut_'.$e ] == 0){
					$parse['tut_'.$e ] = 'none';
				}
			}
			$parse['db_game_name'] = $game_config['game_name'];
			$page = parsetemplate(gettemplate('tutorial/tutorial'), $parse);
			break;
	}

	display("<div id='content'>".$page."</div>");

?>