PageRenderTime 122ms CodeModel.GetById 57ms app.highlight 32ms RepoModel.GetById 26ms app.codeStats 1ms

/includes/classes/missions/MissionCaseDestruction.php

https://bitbucket.org/VolCh/2moons
PHP | 523 lines | 420 code | 75 blank | 28 comment | 24 complexity | a4cec114c1f71d782a1f48e096263061 MD5 | raw file
  1<?php
  2
  3/**
  4 *  2Moons
  5 *  Copyright (C) 2012 Jan Kr??pke
  6 *
  7 * This program is free software: you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License as published by
  9 * the Free Software Foundation, either version 3 of the License, or
 10 * (at your option) any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 19 *
 20 * @package 2Moons
 21 * @author Jan Kr??pke <info@2moons.cc>
 22 * @copyright 2012 Jan Kr??pke <info@2moons.cc>
 23 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
 24 * @version 1.7.1 (2013-01-18)
 25 * @info $Id$
 26 * @link http://2moons.cc/
 27 */
 28
 29class MissionCaseDestruction extends MissionFunctions
 30{
 31	function __construct($Fleet)
 32	{
 33		$this->_fleet	= $Fleet;
 34	}
 35	
 36	function TargetEvent()
 37	{	
 38		global $resource, $reslist;
 39		
 40		$fleetAttack	= array();
 41		$fleetDefend	= array();
 42		
 43		$userAttack		= array();
 44		$userDefend		= array();
 45		
 46		$stealResource	= array(
 47			901	=> 0,
 48			902	=> 0,
 49			903	=> 0,
 50		);
 51		
 52		$debris			= array();
 53		$planetDebris	= array();
 54		
 55		$raportInfo		= array();
 56		
 57		$debrisRessource	= array(901, 902);
 58		
 59		$messageHTML	= <<<HTML
 60<div class="raportMessage">
 61	<table>
 62		<tr>
 63			<td colspan="2"><a href="CombatReport.php?raport=%s" target="_blank"><span class="%s">%s %s (%s)</span></a></td>
 64		</tr>
 65		<tr>
 66			<td>%s</td><td><span class="%s">%s: %s</span>&nbsp;<span class="%s">%s: %s</span></td>
 67		</tr>
 68		<tr>
 69			<td>%s</td><td><span>%s:&nbsp;<span class="raportSteal element901">%s</span>&nbsp;</span><span>%s:&nbsp;<span class="raportSteal element902">%s</span>&nbsp;</span><span>%s:&nbsp;<span class="raportSteal element903">%s</span></span></td>
 70		</tr>
 71		<tr>
 72			<td>%s</td><td><span>%s:&nbsp;<span class="raportDebris element901">%s</span>&nbsp;</span><span>%s:&nbsp;<span class="raportDebris element902">%s</span></span></td>
 73		</tr>
 74	</table>
 75</div>
 76HTML;
 77		//Minize HTML
 78		$messageHTML	= str_replace(array("\n", "\t", "\r"), "", $messageHTML);
 79		
 80		$targetPlanet 	= $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".PLANETS." WHERE id = '".$this->_fleet['fleet_end_id']."';");
 81		$targetUser   	= $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".USERS." WHERE id = '".$targetPlanet['id_owner']."';");
 82		
 83		$targetUser['factor']	= getFactors($targetUser, 'basic', $this->_fleet['fleet_start_time']);
 84		$planetUpdater	= new ResourceUpdate();
 85		
 86		list($targetUser, $targetPlanet)	= $planetUpdater->CalcResource($targetUser, $targetPlanet, true, $this->_fleet['fleet_start_time']);
 87		
 88		if($this->_fleet['fleet_group'] != 0)
 89		{
 90			$GLOBALS['DATABASE']->query("DELETE FROM ".AKS." WHERE id = '".$this->_fleet['fleet_group']."';");
 91			$incomingFleetsResult = $GLOBALS['DATABASE']->query("SELECT * FROM ".FLEETS." WHERE fleet_group = '".$this->_fleet['fleet_group']."';");
 92		
 93			while ($incomingFleetsRow = $GLOBALS['DATABASE']->fetch_array($incomingFleetsResult))
 94			{
 95				$incomingFleets[$incomingFleetsRow['fleet_id']] = $incomingFleetsRow;
 96			}
 97			
 98			$GLOBALS['DATABASE']->free_result($incomingFleetsResult);
 99		}
100		else
101		{
102			$incomingFleets = array($this->_fleet['fleet_id'] => $this->_fleet);
103		}
104		
105		foreach($incomingFleets as $fleetID => $fleetDetail)
106		{
107			$fleetAttack[$fleetID]['fleetDetail']		= $fleetDetail;
108			$fleetAttack[$fleetID]['player']			= $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".USERS." WHERE id = '".$fleetDetail['fleet_owner']."';");
109			$fleetAttack[$fleetID]['player']['factor']	= getFactors($fleetAttack[$fleetID]['player'], 'attack', $this->_fleet['fleet_start_time']);
110			$fleetAttack[$fleetID]['unit']				= fleetAmountToArray($fleetDetail['fleet_array']);
111			
112			$userAttack[$fleetAttack[$fleetID]['player']['id']]	= $fleetAttack[$fleetID]['player']['username'];
113		}
114				
115		$targetFleetsResult = $GLOBALS['DATABASE']->query("SELECT * FROM ".FLEETS." WHERE fleet_mission = '5' AND fleet_end_id = '".$this->_fleet['fleet_end_id']."' AND fleet_start_time <= '".TIMESTAMP."' AND fleet_end_stay >= '".TIMESTAMP."';");
116		while ($fleetDetail = $GLOBALS['DATABASE']->fetch_array($targetFleetsResult))
117		{
118			$fleetID	= $fleetDetail['fleet_id'];
119			
120			$fleetDefend[$fleetID]['fleetDetail']		= $fleetDetail;
121			$fleetDefend[$fleetID]['player']			= $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".USERS." WHERE id = '".$fleetDetail['fleet_owner']."';");
122			$fleetDefend[$fleetID]['player']['factor']	= getFactors($fleetDefend[$fleetID]['player'], 'attack', $this->_fleet['fleet_start_time']);
123			$fleetDefend[$fleetID]['unit']				= fleetAmountToArray($fleetDetail['fleet_array']);
124			
125			$userDefend[$fleetDefend[$fleetID]['player']['id']]	= $fleetDefend[$fleetID]['player']['username'];
126		}
127			
128		$GLOBALS['DATABASE']->free_result($targetFleetsResult);
129		
130		$fleetDefend[0]['player']			= $targetUser;
131		$fleetDefend[0]['player']['factor']	= getFactors($fleetDefend[0]['player'], 'attack', $this->_fleet['fleet_start_time']);
132		$fleetDefend[0]['fleetDetail']		= array(
133			'fleet_start_galaxy'	=> $targetPlanet['galaxy'], 
134			'fleet_start_system'	=> $targetPlanet['system'], 
135			'fleet_start_planet'	=> $targetPlanet['planet'], 
136			'fleet_start_type'		=> $targetPlanet['planet_type'], 
137		);
138		
139		$fleetDefend[0]['unit']				= array();
140		
141		foreach(array_merge($reslist['fleet'], $reslist['defense']) as $elementID)
142		{
143			if (empty($targetPlanet[$resource[$elementID]])) continue;
144
145			$fleetDefend[0]['unit'][$elementID] = $targetPlanet[$resource[$elementID]];
146		}
147			
148		$userDefend[$fleetDefend[0]['player']['id']]	= $fleetDefend[0]['player']['username'];
149		
150		require_once('calculateAttack.php');
151		
152		$fleetIntoDebris	= $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Fleet_Cdr'];
153		$defIntoDebris		= $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Defs_Cdr'];
154		
155		$combatResult 		= calculateAttack($fleetAttack, $fleetDefend, $fleetIntoDebris, $defIntoDebris);
156		
157		$sqlQuery	= "";
158		
159		foreach ($fleetAttack as $fleetID => $fleetDetail)
160		{
161			$fleetArray = '';
162			$totalCount = 0;
163			
164			$fleetDetail['unit']	= array_filter($fleetDetail['unit']);
165			foreach ($fleetDetail['unit'] as $elementID => $amount)
166			{				
167				$fleetArray .= $elementID.','.floattostring($amount).';';
168				$totalCount += $amount;
169			}
170			
171			if($totalCount == 0)
172			{
173				if($this->_fleet['fleet_id'] == $fleetID)
174				{
175					$this->KillFleet();
176				}
177				else
178				{
179					$sqlQuery .= "DELETE FROM ".FLEETS." WHERE fleet_id = ".$fleetID.";";
180					$sqlQuery .= "DELETE FROM ".FLEETS_EVENT." WHERE fleetID = ".$fleetID.";";
181				}
182				
183				$sqlQuery .= "UPDATE ".LOG_FLEETS." SET fleet_state = 2 WHERE fleet_id = '".$fleetID."';";
184			}
185			elseif($totalCount > 0)
186			{
187				$sqlQuery .= "UPDATE ".FLEETS." SET fleet_array = '".substr($fleetArray, 0, -1)."', fleet_amount = '".$totalCount."' WHERE fleet_id = '".$fleetID."';";
188				$sqlQuery .= "UPDATE ".LOG_FLEETS." SET fleet_array = '".substr($fleetArray, 0, -1)."', fleet_amount = '".$totalCount."', fleet_state = 1 WHERE fleet_id = '".$fleetID."';";
189			}
190			else
191			{
192				throw new Exception("Negative Fleet amount ....");
193			}
194		}
195		
196		foreach ($fleetDefend as $fleetID => $fleetDetail)
197		{
198			if($fleetID != 0)
199			{
200				$fleetArray = '';
201				$totalCount = 0;
202				
203				$fleetDetail['unit']	= array_filter($fleetDetail['unit']);
204				foreach ($fleetDetail['unit'] as $elementID => $amount)
205				{				
206					$fleetArray .= $elementID.','.floattostring($amount).';';
207					$totalCount += $amount;
208				}
209			
210				if($totalCount == 0)
211				{
212					$sqlQuery .= "DELETE FROM ".FLEETS." WHERE fleet_id = ".$fleetID.";";
213					$sqlQuery .= "DELETE FROM ".FLEETS_EVENT." WHERE fleetID = ".$fleetID.";";
214					$sqlQuery .= "UPDATE ".LOG_FLEETS." SET fleet_state = 2 WHERE fleet_id = '".$fleetID."';";
215				}
216				elseif($totalCount > 0)
217				{
218					$sqlQuery .= "UPDATE ".FLEETS." SET fleet_array = '".substr($fleetArray, 0, -1)."', fleet_amount = '".$totalCount."' WHERE fleet_id = '".$fleetID."';";
219					$sqlQuery .= "UPDATE ".LOG_FLEETS." SET fleet_array = '".substr($fleetArray, 0, -1)."', fleet_amount = '".$totalCount."', fleet_state = 1 WHERE fleet_id = '".$fleetID."';";
220				}
221				else
222				{
223					throw new Exception("Negative Fleet amount ....");
224				}
225			}
226			else
227			{
228				$fleetArray = array();
229				foreach ($fleetDetail['unit'] as $elementID => $amount)
230				{				
231					$fleetArray[] = $resource[$elementID]." = ".$amount;
232				}
233				
234				if(!empty($fleetArray))
235				{
236					$sqlQuery .= "UPDATE ".PLANETS." SET ".implode(', ', $fleetArray)." WHERE id = '".$this->_fleet['fleet_end_id']."';";
237				}
238			}
239		}
240		
241		$GLOBALS['DATABASE']->multi_query($sqlQuery);
242		
243		if ($combatResult['won'] == "a")
244		{
245			require_once('calculateSteal.php');
246			$stealResource = calculateSteal($fleetAttack, $targetPlanet);
247		}
248		
249		if($this->_fleet['fleet_end_type'] == 3)
250		{
251			// Use planet debris, if attack on moons
252			$targetPlanet 		= array_merge(
253				$targetPlanet,
254				$GLOBALS['DATABASE']->getFirstRow("SELECT der_metal, der_crystal FROM ".PLANETS." WHERE id_luna = ".$this->_fleet['fleet_end_id'].";")
255			);
256		}
257		
258		foreach($debrisRessource as $elementID)
259		{
260			$debris[$elementID]			= $combatResult['debris']['attacker'][$elementID] + $combatResult['debris']['defender'][$elementID];
261			$planetDebris[$elementID]	= $targetPlanet['der_'.$resource[$elementID]] + $debris[$elementID];
262		}
263		
264		$debrisTotal		= array_sum($debris);
265		
266		$raportInfo	= array(
267			'thisFleet'				=> $this->_fleet,
268			'debris'				=> $debris,
269			'stealResource'			=> $stealResource,
270			'moonChance'			=> null,
271			'moonDestroy'			=> true,
272			'moonName'				=> null,
273			'moonDestroyChance'		=> null,
274			'moonDestroySuccess'	=> null,
275			'fleetDestroyChance'	=> null,
276			'fleetDestroySuccess'	=> false,
277		);
278
279        $destext        = "";
280		
281		switch($combatResult['won'])
282		{
283			case "a":
284				$moonDestroyChance	= round((100 - sqrt($targetPlanet['diameter'])) * sqrt($fleetAttack[$this->_fleet['fleet_id']]['unit'][214]), 1);
285				
286				// Max 100% | Min 0%
287				$moonDestroyChance	= min($moonDestroyChance, 100);
288				$moonDestroyChance	= max($moonDestroyChance, 0);
289				
290				$randChance	= mt_rand(1, 100);
291				if ($randChance <= $moonDestroyChance)
292				{
293					$planetID	= $GLOBALS['DATABASE']->getFirstCell("SELECT id FROM ".PLANETS." WHERE id_luna = ".$targetPlanet['id'].";");
294					$GLOBALS['DATABASE']->multi_query("
295					UPDATE ".FLEETS." SET 
296					fleet_start_type = 1, 
297					fleet_start_id = ".$planetID."
298					WHERE fleet_start_id = ".$targetPlanet['id'].";
299					UPDATE ".FLEETS." SET 
300					fleet_end_type = 1, 
301					fleet_end_id = ".$planetID.",
302					fleet_mission = IF(fleet_mission = 9, 1, fleet_mission) 
303					WHERE fleet_end_id = ".$this->_fleet['fleet_end_id']."
304					AND fleet_id != ".$this->_fleet['fleet_id'].";
305					UPDATE ".PLANETS." 
306					SET id_luna = 0
307					WHERE id_luna = ".$targetPlanet['id'].";
308					DELETE FROM ".PLANETS." 
309					WHERE id = ".$targetPlanet['id'].";");
310					
311					$raportInfo['moonDestroySuccess'] = 1;
312				} else {
313					$raportInfo['moonDestroySuccess'] = 0;
314				}
315				
316				$fleetDestroyChance	= round(sqrt($targetPlanet['diameter']) / 2);
317				
318				$randChance	= mt_rand(1, 100);
319				if ($randChance <= $fleetDestroyChance)
320				{
321					$this->KillFleet();
322					$raportInfo['fleetDestroySuccess'] = true;
323				}
324				else
325				{
326					$raportInfo['fleetDestroySuccess'] = false;
327				}
328			
329			
330				$raportInfo['moonDestroyChance']	= $moonDestroyChance;
331				$raportInfo['fleetDestroyChance']	= $fleetDestroyChance;
332				
333				$attackStatus	= 'wons';
334				$defendStatus	= 'loos';
335				$attackClass	= 'raportWin';
336				$defendClass	= 'raportLose';
337			break;
338			case "w":
339				$attackStatus	= 'draws';
340				$defendStatus	= 'draws';
341				$attackClass	= 'raportDraw';
342				$defendClass	= 'raportDraw';
343				$raportInfo['moonDestroySuccess'] = -1;
344			break;
345			case "r":
346				$attackStatus	= 'loos';
347				$defendStatus	= 'wons';
348				$attackClass	= 'raportLose';
349				$defendClass	= 'raportWin';
350				$raportInfo['moonDestroySuccess'] = -1;
351			break;
352		}
353		
354		require_once('GenerateReport.php');
355		$raportData	= GenerateReport($combatResult, $raportInfo);
356		
357		$raportID	= md5(uniqid('', true).TIMESTAMP);
358		$sqlQuery	= "INSERT INTO ".RW." SET 
359		rid = '".$raportID."',
360		raport = '".serialize($raportData)."',
361		time = '".$this->_fleet['fleet_start_time']."',
362		attacker = '".implode(',', array_keys($userAttack))."',
363		defender = '".implode(',', array_keys($userDefend))."';";
364		$GLOBALS['DATABASE']->query($sqlQuery);
365		
366		$sqlQuery		= "";
367		foreach($userAttack as $userID => $userName)
368		{
369			$LNG		= $this->getLanguage(NULL, $userID);
370			
371			$message	= sprintf($messageHTML,
372				$raportID,
373				$attackClass,
374				$LNG['sys_mess_attack_report'],
375				sprintf(
376					$LNG['sys_adress_planet'],
377					$this->_fleet['fleet_end_galaxy'],
378					$this->_fleet['fleet_end_system'],
379					$this->_fleet['fleet_end_planet']
380				),
381				$LNG['type_planet_short'][$this->_fleet['fleet_end_type']],
382				$LNG['sys_lost'],
383				$attackClass,
384				$LNG['sys_attack_attacker_pos'],
385				pretty_number($combatResult['unitLost']['attacker']),
386				$defendClass,
387				$LNG['sys_attack_defender_pos'],
388				pretty_number($combatResult['unitLost']['defender']),
389				$LNG['sys_gain'],
390				$LNG['tech'][901],
391				pretty_number($stealResource[901]),
392				$LNG['tech'][902],
393				pretty_number($stealResource[902]),
394				$LNG['tech'][903],
395				pretty_number($stealResource[903]),
396				$LNG['sys_debris'],
397				$LNG['tech'][901],
398				pretty_number($debris[901]), 
399				$LNG['tech'][902],
400				pretty_number($debris[902])
401			);
402				
403			SendSimpleMessage($userID, 0, $this->_fleet['fleet_start_time'], 3, $LNG['sys_mess_tower'], $LNG['sys_mess_attack_report'], $message);
404			
405			$sqlQuery	.= "INSERT INTO ".TOPKB_USERS." SET ";
406			$sqlQuery	.= "rid = '".$raportID."', ";
407			$sqlQuery	.= "role = 1, ";
408			$sqlQuery	.= "username = '".$GLOBALS['DATABASE']->escape($userName)."', ";
409			$sqlQuery	.= "uid = ".$userID.";";
410		}
411		
412		
413		foreach($userDefend as $userID => $userName)
414		{
415			$LNG		= $this->getLanguage(NULL, $userID);
416			
417			$message	= sprintf($messageHTML,
418				$raportID,
419				$defendClass,
420				$LNG['sys_mess_attack_report'],
421				sprintf(
422					$LNG['sys_adress_planet'],
423					$this->_fleet['fleet_end_galaxy'],
424					$this->_fleet['fleet_end_system'],
425					$this->_fleet['fleet_end_planet']
426				),
427				$LNG['type_planet_short'][$this->_fleet['fleet_end_type']],
428				$LNG['sys_lost'],
429				$defendClass,
430				$LNG['sys_attack_attacker_pos'],
431				pretty_number($combatResult['unitLost']['attacker']),
432				$attackClass,
433				$LNG['sys_attack_defender_pos'],
434				pretty_number($combatResult['unitLost']['defender']),
435				$LNG['sys_gain'],
436				$LNG['tech'][901],
437				pretty_number($stealResource[901]),
438				$LNG['tech'][902],
439				pretty_number($stealResource[902]),
440				$LNG['tech'][903],
441				pretty_number($stealResource[903]),
442				$LNG['sys_debris'],
443				$LNG['tech'][901],
444				pretty_number($debris[901]), 
445				$LNG['tech'][902],
446				pretty_number($debris[902])
447			);
448				
449			SendSimpleMessage($userID, 0, $this->_fleet['fleet_start_time'], 3, $LNG['sys_mess_tower'], $LNG['sys_mess_attack_report'], $message);
450			
451			$sqlQuery	.= "INSERT INTO ".TOPKB_USERS." SET ";
452			$sqlQuery	.= "rid = '".$raportID."', ";
453			$sqlQuery	.= "role = 2, ";
454			$sqlQuery	.= "username = '".$GLOBALS['DATABASE']->escape($userName)."', ";
455			$sqlQuery	.= "uid = ".$userID.";";
456		}
457		
458		if($this->_fleet['fleet_end_type'] == 3)
459		{
460			$debrisType	= 'id_luna';
461		}
462		else
463		{
464			$debrisType	= 'id';
465		}
466		
467		$sqlQuery	.= "UPDATE ".PLANETS." SET
468						der_metal = ".$planetDebris[901].",
469						der_crystal = ".$planetDebris[902]."
470						WHERE
471						".$debrisType." = ".$this->_fleet['fleet_end_id'].";
472						UPDATE ".PLANETS." SET
473						metal = metal - ".$stealResource[901].",
474						crystal = crystal - ".$stealResource[902].",
475						deuterium = deuterium - ".$stealResource[903]."
476						WHERE
477						id = ".$this->_fleet['fleet_end_id'].";
478						INSERT INTO ".TOPKB." SET
479						units = ".($combatResult['unitLost']['attacker'] + $combatResult['unitLost']['defender']).",
480						rid = '".$raportID."',
481						time = ".$this->_fleet['fleet_start_time'].",
482						universe = ".$this->_fleet['fleet_universe'].",
483						result = '".$combatResult['won'] ."';
484						UPDATE ".USERS." SET
485						".$attackStatus." = ".$attackStatus." + 1,
486						kbmetal = kbmetal + ".$debris[901].",
487						kbcrystal = kbcrystal + ".$debris[902].",
488						lostunits = lostunits + ".$combatResult['unitLost']['attacker'].",
489						desunits = desunits + ".$combatResult['unitLost']['defender']."
490						WHERE
491						id IN (".implode(',', array_keys($userAttack)).");
492						UPDATE ".USERS." SET
493						".$defendStatus." = ".$defendStatus." + 1,
494						kbmetal = kbmetal + ".$debris[901].",
495						kbcrystal = kbcrystal + ".$debris[902].",
496						lostunits = lostunits + ".$combatResult['unitLost']['attacker'].",
497						desunits = desunits + ".$combatResult['unitLost']['defender']."
498						WHERE
499						id IN (".implode(',', array_keys($userDefend)).");";
500						
501		$GLOBALS['DATABASE']->multi_query($sqlQuery);
502		
503		$this->setState(FLEET_RETURN);
504		$this->SaveFleet();
505	}
506	
507	function EndStayEvent()
508	{
509		return;
510	}
511	
512	function ReturnEvent()
513	{
514		$LNG		= $this->getLanguage(NULL, $this->_fleet['fleet_owner']);
515		$TargetName	= $GLOBALS['DATABASE']->getFirstCell("SELECT name FROM ".PLANETS." WHERE id = ".$this->_fleet['fleet_start_id'].";");
516		$Message	= sprintf($LNG['sys_fleet_won'], $TargetName, GetTargetAdressLink($this->_fleet, ''), pretty_number($this->_fleet['fleet_resource_metal']), $LNG['tech'][901], pretty_number($this->_fleet['fleet_resource_crystal']), $LNG['tech'][902], pretty_number($this->_fleet['fleet_resource_deuterium']), $LNG['tech'][903]);
517
518		SendSimpleMessage($this->_fleet['fleet_owner'], 0, $this->_fleet['fleet_end_time'], 3, $LNG['sys_mess_tower'], $LNG['sys_mess_fleetback'], $Message);
519			
520		$this->RestoreFleet();
521	}
522}
523