<?php
/**
 * Tis file is part of XNova:Legacies. Francesi mangialumache, imparate a programmare!
 **/

define('INSIDE' , true);
define('INSTALL' , false);
require_once dirname(__FILE__) .'/common.php';


function ModuleMarchand ( $CurrentUser, &$CurrentPlanet, $game_config) {
	global $lang, $_POST;

	includeLang('marchand');
	$merchant = array();
	
	$query = doquery('SELECT * FROM {{table}}', 'merchant');
  $row = mysql_fetch_assoc($query);
  
  $merchant['m_acq'] = $row['m_acq'];
  $merchant['m_ven'] = $row['m_ven']; 
  $merchant['c_acq'] = $row['c_acq']; 
  $merchant['c_ven'] = $row['c_ven']; 
  $merchant['d_acq'] = $row['d_acq']; 
  $merchant['d_ven'] = $row['d_ven'];   
  $perc = $row['perc'];
   
   $met = round((($merchant['m_ven']/$merchant['m_acq']) *1),2);
	$cri = round((($merchant['c_ven']/$merchant['c_acq']) *2),2);
	$deu = round((($merchant['d_ven']/$merchant['d_acq']) *4),2);  
    
  
 	$parse   = $lang;

 	if ($_POST['ress'] != '') {
		$PageTPL   = gettemplate('message_body');
		$Error     = false;
		$CheatTry  = false;
		$Metal     = $_POST['metal'];        
		$Crystal   = $_POST['cristal'];      
		$Deuterium = $_POST['deut'];         
		if ($Metal < 0) {             
			$Metal     *= -1;
			$CheatTry   = true;
		}
		if ($Crystal < 0) {       
			$Crystal   *= -1;
			$CheatTry   = true;
		}
		if ($Deuterium < 0) {        
			$Deuterium *= -1;
			$CheatTry   = true;
		}
		if ($CheatTry  == false) {   
			switch ($_POST['ress']) 
      {
				case 'metal':            
					$Necessaire   = (( $Crystal * ($cri/$met)) + ( $Deuterium * ($deu/$met)));
					$merchant['m_acq'] += $Necessaire;
          if($perc > 0)
					   $Necessaire += $Necessaire/100*$perc;
								            				   
					if ($CurrentPlanet['metal'] >= $Necessaire) {      
						$CurrentPlanet['metal'] -= $Necessaire;        
					} else {
						$Message = $lang['mod_ma_noten'] ." ". $lang['Metal'] ."! ";  
						$Error   = true;                                            
					}
					break;

				case 'cristal':
					$Necessaire   = (( $Metal * ($met/$cri)) + ( $Deuterium * ($deu/$cri)));
					$merchant['c_acq'] += $Necessaire;
          if($perc > 0)
					   $Necessaire += $Necessaire/100*$perc;
															  				   
					if ($CurrentPlanet['crystal'] >= $Necessaire) {
						$CurrentPlanet['crystal'] -= $Necessaire;
					} else {
						$Message = $lang['mod_ma_noten'] ." ". $lang['Crystal'] ."! ";
						$Error   = true;
					}
					break;

				case 'deuterium':
					$Necessaire   = (( $Metal * ($met/$deu)) + ( $Crystal * ($cri/$deu)));
					$merchant['d_acq'] += $Necessaire;
          if($perc > 0)
					   $Necessaire += $Necessaire/100*$perc;
										   
				 
					
					if ($CurrentPlanet['deuterium'] >= $Necessaire) {
						$CurrentPlanet['deuterium'] -= $Necessaire;
					} else {
						$Message = $lang['mod_ma_noten'] ." ". $lang['Deuterium'] ."! ";
						$Error   = true;
					}
					break;
			}
		}
		if ($Error == false) {
			if ($CheatTry == true) {              
				$CurrentPlanet['metal']      = 0;
				$CurrentPlanet['crystal']    = 0;
				$CurrentPlanet['deuterium']  = 0;
			} else {                                      
				$CurrentPlanet['metal']     += $Metal;      
				$CurrentPlanet['crystal']   += $Crystal;    
				$CurrentPlanet['deuterium'] += $Deuterium;  
				
				$merchant['m_ven'] += $Metal;
				$merchant['c_ven'] += $Crystal;
				$merchant['d_ven'] += $Deuterium;
				
				
				$data = "Pianeta: {$CurrentPlanet['name']} - comprati-> T:{$Metal}  D:{$Crystal} P:{$Deuterium}";  
        $data .= " - " . date(DATE_RFC822) ."\n";
        $file = "trade.txt";
        $fh = fopen($file, 'a');
        fwrite($fh, $data);
        fclose($fh); 

				
				
			
				
$sql = <<<ABC
UPDATE {{table}} SET 
m_acq = '{$merchant['m_acq']}',
m_ven = '{$merchant['m_ven']}',
c_acq = '{$merchant['c_acq']}',
c_ven = '{$merchant['c_ven']}',
d_acq = '{$merchant['d_acq']}',
d_ven = '{$merchant['d_ven']}';
ABC;
        
        doquery($sql, 'merchant');
				
				
			}
			
$sql = <<<LOL
UPDATE {{table}} SET
`metal` = '{$CurrentPlanet['metal']}',
`crystal` = '{$CurrentPlanet['crystal']}',
`deuterium` = '{$CurrentPlanet['deuterium']}'
WHERE `id` = '{$CurrentPlanet['id']}'
LOL;

			doquery ( $sql , 'planets');
			$Message = $lang['mod_ma_done'];
		}
		if ($Error == true) {
			$parse['title'] = $lang['mod_ma_error'];
		} else {
			$parse['title'] = $lang['mod_ma_donet'];
		}
		$parse['mes']   = $Message;
	} else {                                 
		if ($_POST['action'] != 2) {            
			$PageTPL = gettemplate('marchand_main');
			
		  $b =  round($cri/$met, 2);
		  $c =  round($deu/$met, 2);
      
  
      $parse['mod_ma_rates'] = "Actual rates of change: {$met} / {$cri} / {$deu}";
      if($perc > 0)
        $parse['mod_ma_rates'] .= "<br> The Merchant fee for the trade is {$perc}%.";
			
		} else {                               
			$parse['mod_ma_res']   = "1";
			$parse['perc'] = $perc;
			
			
			switch ($_POST['choix']) { 
				case 'metal':
					$PageTPL = gettemplate('marchand_metal');
					$parse['mod_ma_res_a'] = round($cri/$met, 2);
					$parse['mod_ma_res_b'] = round($deu/$met, 2);
					break;
				case 'cristal': 
					$PageTPL = gettemplate('marchand_cristal');
					$parse['mod_ma_res_a'] = round($met/$cri, 2);
					$parse['mod_ma_res_b'] = round($deu/$cri, 2);
					break;
				case 'deut':
					$PageTPL = gettemplate('marchand_deuterium');
					$parse['mod_ma_res_a'] = round($met/$deu, 2);
					$parse['mod_ma_res_b'] = round($cri/$deu, 2);
					break;
			}
		}
	}

	$Page    = parsetemplate ( $PageTPL, $parse );
	return  $Page;
}

	$Page = ModuleMarchand ( $user, $planetrow, $game_config);
	display ( $Page, $lang['mod_marchand'], true, '', false );

?>