<commit_before><?php

/**
 *  2Moons
 *  Copyright (C) 2011  Slaver
 *
 * 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/>.
 *
 * @package 2Moons
 * @author Slaver <<EMAIL>>
 * @copyright 2009 Lucky <<EMAIL>> (XGProyecto)
 * @copyright 2011 Slaver <<EMAIL>> (Fork/2Moons)
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
 * @version 1.6.1 (2011-11-19)
 * @info $Id$
 * @link http://code.google.com/p/2moons/
 */

class MissionCaseDestruction extends MissionFunctions
{
	function __construct($Fleet)
	{
		$this->_fleet	= $Fleet;
	}
	
	function TargetEvent()
	{
		global $resource, $LANG;

		$targetPlanet = $GLOBALS['DATABASE']->uniquequery("SELECT * FROM ".PLANETS." WHERE `id` = '".$this->_fleet['fleet_end_id']."' ;");
		$targetUser   = $GLOBALS['DATABASE']->uniquequery("SELECT * FROM ".USERS." WHERE `id` = '".$targetPlanet['id_owner']."';");
        
		$attackFleets = array();
		
		$targetUser['factor']				= getFactors($targetUser, 'basic', $this->_fleet['fleet_start_time']);
		$PlanetRess 						= new ResourceUpdate();
		list($targetUser, $targetPlanet)	= $PlanetRess->CalcResource($targetUser, $targetPlanet, true, $this->_fleet['fleet_start_time']);

		
		$attackFleets[$this->_fleet['fleet_id']]['fleet'] = $this->_fleet;
		$attackFleets[$this->_fleet['fleet_id']]['user'] = $GLOBALS['DATABASE']->uniquequery("SELECT `id`, `username`, `military_tech`, `defence_tech`, `shield_tech`, `rpg_amiral`, `dm_defensive`, `dm_attack` FROM ".USERS." WHERE id = '".$this->_fleet['fleet_owner']."';");
		$attackFleets[$this->_fleet['fleet_id']]['user']['factor'] 	= getFactors($attackFleets[$this->_fleet['fleet_id']]['user'], 'attack', $this->_fleet['fleet_start_time']);
		$attackFleets[$this->_fleet['fleet_id']]['detail'] = array();
		$temp = explode(';', $this->_fleet['fleet_array']);

        $AttackerRow    = array();

		foreach ($temp as $temp2)
		{
			$temp2 = explode(',', $temp2);
			if ($temp2[0] < 100) continue;

			if (!isset($attackFleets[$this->_fleet['fleet_id']]['detail'][$temp2[0]]))
				$attackFleets[$this->_fleet['fleet_id']]['detail'][$temp2[0]] = 0;

			$attackFleets[$this->_fleet['fleet_id']]['detail'][$temp2[0]] += $temp2[1];
			$AttackerRow['id'][] 	= $attackFleets[$this->_fleet['fleet_id']]['user']['id'];
		}
		
		$defense = array();

		$def = $GLOBALS['DATABASE']->query('SELECT * FROM '.FLEETS.' WHERE `fleet_end_galaxy` = '. $this->_fleet['fleet_end_galaxy'] .' AND `fleet_end_system` = '. $this->_fleet['fleet_end_system'] .' AND `fleet_end_type` = '. $this->_fleet['fleet_end_type'] .' AND `fleet_end_planet` = '. $this->_fleet['fleet_end_planet'] .' AND fleet_start_time<'.TIMESTAMP.' AND fleet_end_stay>='.TIMESTAMP.';');
		while ($defRow = $GLOBALS['DATABASE']->fetch_array($def))
		{
			$defRowDef = explode(';', $defRow['fleet_array']);
			foreach ($defRowDef as $Element)
			{
				$Element = explode(',', $Element);
				if ($Element[0] < 100) continue;

				if (!isset($defense[$defRow['fleet_id']]['def'][$Element[0]]))
					$defense[$defRow['fleet_id']][$Element[0]] = 0;

				$defense[$defRow['fleet_id']]['def'][$Element[0]] 	+= $Element[1];
				$defense[$defRow['fleet_id']]['user']				= $GLOBALS['DATABASE']->uniquequery("SELECT `id`, `username`, `military_tech`, `defence_tech`, `shield_tech`, `rpg_amiral`, `dm_defensive`, `dm_attack` FROM ".USERS." WHERE id = '".$defRow['fleet_owner']."';");
				$defense[$defRow['fleet_id']]['user']['factor'] 	= getFactors($defense[$defRow['fleet_id']]['user'], 'attack', $this->_fleet['fleet_start_time']);
			}
			$DefenderRow['id'][] 	= $defense[$defRow['fleet_id']]['user']['id'];
		}

		$defense[0]['def'] = array();
		$defense[0]['user'] = $targetUser;
		$defense[0]['user']['factor'] 	= getFactors($defense[0]['user'], 'attack', $this->_fleet['fleet_start_time']);		
		$DefenderRow['id'][] 	= $defense[0]['user']['id'];
		for ($i = 200; $i < 500; $i++)
		{
			if (isset($resource[$i]) && isset($targetPlanet[$resource[$i]]))
			{
				$defense[0]['def'][$i] = $targetPlanet[$resource[$i]];
			}
		}

		$Attacker['id']		= array_unique($AttackerRow['id']);
		$Defender['id']		= array_unique($DefenderRow['id']);
		
		require_once('calculateAttack.php');
		$result 	= calculateAttack($attackFleets, $defense, $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Fleet_Cdr'], $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Defs_Cdr']);

		$SQL		= "";
			
		foreach ($attackFleets as $fleetID => $attacker)
		{
			$fleetArray = '';
			$totalCount = 0;
			foreach ($attacker['detail'] as $element => $amount)
			{				
				if ($amount)
					$fleetArray .= $element.','.floattostring($amount).';';

				$totalCount += $amount;
			}
			
			if($totalCount <= 0) {
				$SQL .= "DELETE FROM ".FLEETS." WHERE `fleet_id`= '".$fleetID."';";
				$SQL .= "UPDATE ".LOG_FLEETS." SET `fleet_state` = 1 WHERE `fleet_id`= '".$fleetID."';";
			} else {
				$SQL .= "UPDATE ".FLEETS." SET `fleet_array` = '".substr($fleetArray, 0, -1)."', `fleet_amount` = '".$totalCount."' WHERE `fleet_id` = '".$fleetID."';";
				$SQL .= "UPDATE ".LOG_FLEETS." SET `fleet_array` = '".substr($fleetArray, 0, -1)."', `fleet_amount` = '".$totalCount."', `fleet_state` = 1 WHERE `fleet_id` = '".$fleetID."';";
			}
		}	
	
		$GLOBALS['DATABASE']->multi_query($SQL);
		$SQL	= "";
		
		$steal	= array(
			'metal'		=> 0,
			'crystal'	=> 0,
			'deuterium'	=> 0,
		);
		
		if ($result['won'] == "a")
		{
			require_once('calculateSteal.php');
			$steal = calculateSteal($attackFleets, $targetPlanet);
		}
		
		foreach ($defense as $fleetID => $defender)
		{
			if ($fleetID != 0)
			{
				$fleetArray = '';
				$totalCount = 0;

				foreach ($defender['def'] as $element => $amount)
				{
					if ($amount)
						$fleetArray .= $element.','.floattostring($amount).';';
						
					$totalCount += $amount;
				}
				
				if($totalCount <= 0) {
					$SQL .= "DELETE FROM ".FLEETS." WHERE `fleet_id`= '".$fleetID."';";
					$SQL .= "UPDATE ".LOG_FLEETS." SET `fleet_state` = 1 WHERE `fleet_id`= '".$fleetID."';";
				} else {
					$SQL .= "UPDATE ".FLEETS." SET `fleet_array` = '".substr($fleetArray, 0, -1)."', `fleet_amount` = '".$totalCount."' WHERE `fleet_id` = '".$fleetID."';";
					$SQL .= "UPDATE ".LOG_FLEETS." SET `fleet_array` = '".substr($fleetArray, 0, -1)."', `fleet_amount` = '".$totalCount."' , `fleet_state` = 1 WHERE `fleet_id` = '".$fleetID."';";
				}
			}
			else
			{
				$fleetArray = '';

				foreach ($defender['def'] as $element => $amount)
				{
					$fleetArray .= "`".$resource[$element]."` = '".floattostring($amount)."', ";
				}

				$SQL .= "UPDATE ".PLANETS." SET ";
				$SQL .= $fleetArray;
				$SQL .= "metal = metal - ".$steal['metal'].",
				         crystal = crystal - ".$steal['crystal'].",
				         deuterium = deuterium - ".$steal['deuterium']."
				         WHERE `id` = ".$this->_fleet['fleet_end_id'].";";
			}
		}
		
		$GLOBALS['DATABASE']->multi_query($SQL);
		$INFO						= $this->_fleet;
		$INFO['steal']				= $steal;
		$INFO['moon']['des']		= 0;
		$INFO['moon']['desfail']	= false;
		$INFO['moon']['chance2']	= false;
		$INFO['moon']['fleetfail']	= false;
		
		$INFO['moon']['chance'] 	= $MoonChance;
		$INFO['moon']['name']		= "";
		
		$EndPlanet		= $GLOBALS['DATABASE']->uniquequery("SELECT `id` FROM ".PLANETS." WHERE `id_luna` = '".$this->_fleet['fleet_end_id']."';");

        $destext        = "";

		switch ($result['won']) {
			case "a":
				$chance		 	= max(min(round((100 - sqrt($targetPlanet['diameter'])) * sqrt($attackFleets[$this->_fleet['fleet_id']]['detail'][214]), 1), 100), 0);
				$chance2  		= round(sqrt($targetPlanet['diameter'])/2);                 

				$tirage 		= rand(0, 100);
				$tirage2  		= rand(0, 100);

				$INFO['moon']['chance']		= $chance;<commit_msg>Fix Notice errors on destruction. (Bug #34)<commit_after><?php

/**
 *  2Moons
 *  Copyright (C) 2011  Slaver
 *
 * 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/>.
 *
 * @package 2Moons
 * @author Slaver <<EMAIL>>
 * @copyright 2009 Lucky <<EMAIL>> (XGProyecto)
 * @copyright 2011 Slaver <<EMAIL>> (Fork/2Moons)
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
 * @version 1.6.1 (2011-11-19)
 * @info $Id$
 * @link http://code.google.com/p/2moons/
 */

class MissionCaseDestruction extends MissionFunctions
{
	function __construct($Fleet)
	{
		$this->_fleet	= $Fleet;
	}
	
	function TargetEvent()
	{
		global $resource, $LANG;

		$targetPlanet = $GLOBALS['DATABASE']->uniquequery("SELECT * FROM ".PLANETS." WHERE `id` = '".$this->_fleet['fleet_end_id']."' ;");
		$targetUser   = $GLOBALS['DATABASE']->uniquequery("SELECT * FROM ".USERS." WHERE `id` = '".$targetPlanet['id_owner']."';");
        
		$attackFleets = array();
		
		$targetUser['factor']				= getFactors($targetUser, 'basic', $this->_fleet['fleet_start_time']);
		$PlanetRess 						= new ResourceUpdate();
		list($targetUser, $targetPlanet)	= $PlanetRess->CalcResource($targetUser, $targetPlanet, true, $this->_fleet['fleet_start_time']);

		
		$attackFleets[$this->_fleet['fleet_id']]['fleet'] = $this->_fleet;
		$attackFleets[$this->_fleet['fleet_id']]['user'] = $GLOBALS['DATABASE']->uniquequery("SELECT `id`, `username`, `military_tech`, `defence_tech`, `shield_tech`, `rpg_amiral`, `dm_defensive`, `dm_attack` FROM ".USERS." WHERE id = '".$this->_fleet['fleet_owner']."';");
		$attackFleets[$this->_fleet['fleet_id']]['user']['factor'] 	= getFactors($attackFleets[$this->_fleet['fleet_id']]['user'], 'attack', $this->_fleet['fleet_start_time']);
		$attackFleets[$this->_fleet['fleet_id']]['detail'] = array();
		$temp = explode(';', $this->_fleet['fleet_array']);

        $AttackerRow    = array();

		foreach ($temp as $temp2)
		{
			$temp2 = explode(',', $temp2);
			if ($temp2[0] < 100) continue;

			if (!isset($attackFleets[$this->_fleet['fleet_id']]['detail'][$temp2[0]]))
				$attackFleets[$this->_fleet['fleet_id']]['detail'][$temp2[0]] = 0;

			$attackFleets[$this->_fleet['fleet_id']]['detail'][$temp2[0]] += $temp2[1];
			$AttackerRow['id'][] 	= $attackFleets[$this->_fleet['fleet_id']]['user']['id'];
		}
		
		$defense = array();

		$def = $GLOBALS['DATABASE']->query('SELECT * FROM '.FLEETS.' WHERE `fleet_end_galaxy` = '. $this->_fleet['fleet_end_galaxy'] .' AND `fleet_end_system` = '. $this->_fleet['fleet_end_system'] .' AND `fleet_end_type` = '. $this->_fleet['fleet_end_type'] .' AND `fleet_end_planet` = '. $this->_fleet['fleet_end_planet'] .' AND fleet_start_time<'.TIMESTAMP.' AND fleet_end_stay>='.TIMESTAMP.';');
		while ($defRow = $GLOBALS['DATABASE']->fetch_array($def))
		{
			$defRowDef = explode(';', $defRow['fleet_array']);
			foreach ($defRowDef as $Element)
			{
				$Element = explode(',', $Element);
				if ($Element[0] < 100) continue;

				if (!isset($defense[$defRow['fleet_id']]['def'][$Element[0]]))
					$defense[$defRow['fleet_id']][$Element[0]] = 0;

				$defense[$defRow['fleet_id']]['def'][$Element[0]] 	+= $Element[1];
				$defense[$defRow['fleet_id']]['user']				= $GLOBALS['DATABASE']->uniquequery("SELECT `id`, `username`, `military_tech`, `defence_tech`, `shield_tech`, `rpg_amiral`, `dm_defensive`, `dm_attack` FROM ".USERS." WHERE id = '".$defRow['fleet_owner']."';");
				$defense[$defRow['fleet_id']]['user']['factor'] 	= getFactors($defense[$defRow['fleet_id']]['user'], 'attack', $this->_fleet['fleet_start_time']);
			}
			$DefenderRow['id'][] 	= $defense[$defRow['fleet_id']]['user']['id'];
		}

		$defense[0]['def'] = array();
		$defense[0]['user'] = $targetUser;
		$defense[0]['user']['factor'] 	= getFactors($defense[0]['user'], 'attack', $this->_fleet['fleet_start_time']);		
		$DefenderRow['id'][] 	= $defense[0]['user']['id'];
		for ($i = 200; $i < 500; $i++)
		{
			if (isset($resource[$i]) && isset($targetPlanet[$resource[$i]]))
			{
				$defense[0]['def'][$i] = $targetPlanet[$resource[$i]];
			}
		}

		$Attacker['id']		= array_unique($AttackerRow['id']);
		$Defender['id']		= array_unique($DefenderRow['id']);
		
		require_once('calculateAttack.php');
		$result 	= calculateAttack($attackFleets, $defense, $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Fleet_Cdr'], $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Defs_Cdr']);

		$SQL		= "";
			
		foreach ($attackFleets as $fleetID => $attacker)
		{
			$fleetArray = '';
			$totalCount = 0;
			foreach ($attacker['detail'] as $element => $amount)
			{				
				if ($amount)
					$fleetArray .= $element.','.floattostring($amount).';';

				$totalCount += $amount;
			}
			
			if($totalCount <= 0) {
				$SQL .= "DELETE FROM ".FLEETS." WHERE `fleet_id`= '".$fleetID."';";
				$SQL .= "UPDATE ".LOG_FLEETS." SET `fleet_state` = 1 WHERE `fleet_id`= '".$fleetID."';";
			} else {
				$SQL .= "UPDATE ".FLEETS." SET `fleet_array` = '".substr($fleetArray, 0, -1)."', `fleet_amount` = '".$totalCount."' WHERE `fleet_id` = '".$fleetID."';";
				$SQL .= "UPDATE ".LOG_FLEETS." SET `fleet_array` = '".substr($fleetArray, 0, -1)."', `fleet_amount` = '".$totalCount."', `fleet_state` = 1 WHERE `fleet_id` = '".$fleetID."';";
			}
		}	
	
		$GLOBALS['DATABASE']->multi_query($SQL);
		$SQL	= "";
		
		$steal	= array(
			'metal'		=> 0,
			'crystal'	=> 0,
			'deuterium'	=> 0,
		);
		
		if ($result['won'] == "a")
		{
			require_once('calculateSteal.php');
			$steal = calculateSteal($attackFleets, $targetPlanet);
		}
		
		foreach ($defense as $fleetID => $defender)
		{
			if ($fleetID != 0)
			{
				$fleetArray = '';
				$totalCount = 0;

				foreach ($defender['def'] as $element => $amount)
				{
					if ($amount)
						$fleetArray .= $element.','.floattostring($amount).';';
						
					$totalCount += $amount;
				}
				
				if($totalCount <= 0) {
					$SQL .= "DELETE FROM ".FLEETS." WHERE `fleet_id`= '".$fleetID."';";
					$SQL .= "UPDATE ".LOG_FLEETS." SET `fleet_state` = 1 WHERE `fleet_id`= '".$fleetID."';";
				} else {
					$SQL .= "UPDATE ".FLEETS." SET `fleet_array` = '".substr($fleetArray, 0, -1)."', `fleet_amount` = '".$totalCount."' WHERE `fleet_id` = '".$fleetID."';";
					$SQL .= "UPDATE ".LOG_FLEETS." SET `fleet_array` = '".substr($fleetArray, 0, -1)."', `fleet_amount` = '".$totalCount."' , `fleet_state` = 1 WHERE `fleet_id` = '".$fleetID."';";
				}
			}
			else
			{
				$fleetArray = '';

				foreach ($defender['def'] as $element => $amount)
				{
					$fleetArray .= "`".$resource[$element]."` = '".floattostring($amount)."', ";
				}

				$SQL .= "UPDATE ".PLANETS." SET ";
				$SQL .= $fleetArray;
				$SQL .= "metal = metal - ".$steal['metal'].",
				         crystal = crystal - ".$steal['crystal'].",
				         deuterium = deuterium - ".$steal['deuterium']."
				         WHERE `id` = ".$this->_fleet['fleet_end_id'].";";
			}
		}
		
		$GLOBALS['DATABASE']->multi_query($SQL);
		$INFO						= $this->_fleet;
		$INFO['steal']				= $steal;
		$INFO['moon']['des']		= 0;
		$INFO['moon']['desfail']	= false;
		$INFO['moon']['chance2']	= false;
		$INFO['moon']['fleetfail']	= false;
		
		$INFO['moon']['chance'] 	= 0;
		$INFO['moon']['name']		= "";
		
		$EndPlanet		= $GLOBALS['DATABASE']->uniquequery("SELECT `id` FROM ".PLANETS." WHERE `id_luna` = '".$this->_fleet['fleet_end_id']."';");

        $destext        = "";

		switch ($result['won']) {
			case "a":
				$chance		 	= max(min(round((100 - sqrt($targetPlanet['diameter'])) * sqrt($attackFleets[$this->_fleet['fleet_id']]['detail'][214]), 1), 100), 0);
				$chance2  		= round(sqrt($targetPlanet['diameter'])/2);                 

				$tirage 		= rand(0, 100);
				$tirage2  		= rand(0, 100);

				$INFO['moon']['chance']		= $chance;
{'fraction_non_alphanumeric': 0.23142135642135642, 'fraction_numerical': 0.013528138528138528, 'mean_word_length': 4.12731196054254, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5904431', 'n_tokens_mistral': 7464, 'n_tokens_neox': 6208, 'n_words': 1097}