PageRenderTime 36ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/classes/missions/MissionCaseExpedition.class.php

https://bitbucket.org/VolCh/2moons
PHP | 546 lines | 434 code | 83 blank | 29 comment | 32 complexity | 4ecb09a22747ea7376d89ba2477f51f2 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.2 (2013-03-18)
 25 * @info $Id$
 26 * @link http://2moons.cc/
 27 */
 28
 29class MissionCaseExpedition extends MissionFunctions implements Mission
 30{
 31	function __construct($fleet)
 32	{
 33		$this->_fleet	= $fleet;
 34	}
 35	
 36	function TargetEvent()
 37	{
 38		$this->setState(FLEET_HOLD);
 39		$this->SaveFleet();
 40	}
 41	
 42	function EndStayEvent()
 43	{
 44		global $pricelist, $reslist, $resource;
 45		$LNG	= $this->getLanguage(NULL, $this->_fleet['fleet_owner']);
 46
 47		$config	= Config::get($this->_fleet['fleet_universe']);
 48
 49        $expeditionPoints       = array();
 50
 51		foreach($reslist['fleet'] as $shipId)
 52		{
 53			$expeditionPoints[$shipId]	= ($pricelist[$shipId]['cost'][901] + $pricelist[$shipId]['cost'][902]) * 5 / 1000;
 54		}
 55			
 56		$fleetArray		= FleetFunctions::unserialize($this->_fleet['fleet_array']);
 57		$fleetPoints 	= 0;
 58		$fleetCapacity	= 0;
 59
 60		foreach ($fleetArray as $shipId => $shipAmount)
 61		{
 62			$fleetCapacity 			   += $shipAmount * $pricelist[$shipId]['capacity'];
 63			$fleetPoints   			   += $shipAmount * $expeditionPoints[$shipId];
 64		}
 65
 66		$fleetCapacity  -= $this->_fleet['fleet_resource_metal'] + $this->_fleet['fleet_resource_crystal']
 67			+ $this->_fleet['fleet_resource_deuterium'] + $this->_fleet['fleet_resource_darkmatter'];
 68
 69		$GetEvent       = mt_rand(1, 9);
 70
 71        $Message        = $LNG['sys_expe_nothing_'.mt_rand(1,8)];
 72
 73		switch($GetEvent)
 74		{
 75			case 1:
 76				$eventSize		= mt_rand(0, 100);
 77                $factor			= 0;
 78
 79				if(10 < $eventSize)
 80				{
 81					$Message	= $LNG['sys_expe_found_ress_1_'.mt_rand(1,4)];
 82					$factor		= mt_rand(10, 50);
 83				}
 84				elseif(0 < $eventSize && 10 >= $eventSize)
 85				{
 86					$Message	= $LNG['sys_expe_found_ress_2_'.mt_rand(1,3)];
 87					$factor		= mt_rand(50, 100);
 88				}
 89				elseif(0 == $eventSize)
 90				{
 91					$Message	= $LNG['sys_expe_found_ress_3_'.mt_rand(1,2)];
 92					$factor		= mt_rand(100, 200);
 93				}
 94
 95				$chanceToFound	= mt_rand(1, 6);
 96				if($chanceToFound > 3)
 97				{
 98					$resourceId	= 901;
 99				}
100				elseif($chanceToFound > 1)
101				{
102					$resourceId	= 902;
103					$factor		= $factor / 2;
104				}
105				else
106				{
107					$resourceId	= 903;
108					$factor		= $factor / 3;
109				}
110
111				$sql		= "SELECT MAX(total_points) as total FROM %%STATPOINTS%%
112				WHERE `stat_type` = :type AND `universe` = :universe;";
113
114				$topPoints	= Database::get()->selectSingle($sql, array(
115					':type'		=> 1,
116					':universe'	=> $this->_fleet['fleet_universe']
117				), 'total');
118
119				if($topPoints > 5000000)
120				{
121					$maxFactor		= 12000;
122				}
123				elseif($topPoints > 1000000)
124				{
125					$maxFactor		= 9000;
126				}
127				elseif($topPoints > 100000)
128				{
129					$maxFactor		= 6000;
130				}
131				else
132				{
133					$maxFactor		= 2400;
134				}
135
136				$founded		= round(min($maxFactor, max(200, $factor)) * $fleetPoints);
137
138				$fleetColName	= 'fleet_resource_'.$resource[$resourceId];
139				$this->UpdateFleet($fleetColName, $this->_fleet[$fleetColName] + $founded);
140			break;
141			case 2:
142				$eventSize   = mt_rand(0, 100);
143                $Size       = 0;
144
145				if(10 < $eventSize) {
146					$Size		= mt_rand(100, 300);
147					$Message	= $LNG['sys_expe_found_dm_1_'.mt_rand(1,5)];
148				} elseif(0 < $eventSize && 10 >= $eventSize) {
149					$Size		= mt_rand(301, 600);
150					$Message	= $LNG['sys_expe_found_dm_2_'.mt_rand(1,3)];
151				} elseif(0 == $eventSize) {
152					$Size	 	= mt_rand(601, 3000);
153					$Message	= $LNG['sys_expe_found_dm_3_'.mt_rand(1,2)];
154				}
155
156				$this->UpdateFleet('fleet_resource_darkmatter', $this->_fleet['fleet_resource_darkmatter'] + $Size);
157			break;
158			case 3:
159				$eventSize	= mt_rand(0, 100);
160                $Size       = 0;
161                $Message    = "";
162
163				if(10 < $eventSize) {
164					$Size		= mt_rand(10, 50);
165					$Message	= $LNG['sys_expe_found_ships_1_'.mt_rand(1,4)];
166				} elseif(0 < $eventSize && 10 >= $eventSize) {
167					$Size		= mt_rand(52, 100);
168					$Message	= $LNG['sys_expe_found_ships_2_'.mt_rand(1,2)];
169				} elseif(0 == $eventSize) {
170					$Size	 	= mt_rand(102, 200);
171					$Message	= $LNG['sys_expe_found_ships_3_'.mt_rand(1,2)];
172				}
173
174				$sql		= "SELECT MAX(total_points) as total FROM %%STATPOINTS%%
175				WHERE `stat_type` = :type AND `universe` = :universe;";
176
177				$topPoints	= Database::get()->selectSingle($sql, array(
178					':type'		=> 1,
179					':universe'	=> $this->_fleet['fleet_universe']
180				), 'total');
181
182				$MaxPoints 		= ($topPoints < 5000000) ? 4500 : 6000;
183
184				$FoundShips		= max(round($Size * min($fleetPoints, $MaxPoints)), 10000);
185				
186				$FoundShipMess	= "";	
187				$NewFleetArray 	= "";
188				
189				$Found			= array();
190				foreach($reslist['fleet'] as $ID) 
191				{
192					if(!isset($fleetArray[$ID]) || $ID == 208 || $ID == 209 || $ID == 214)
193						continue;
194					
195					$MaxFound			= floor($FoundShips / ($pricelist[$ID]['cost'][901] + $pricelist[$ID]['cost'][902]));
196					if($MaxFound <= 0) 
197						continue;
198						
199					$Count				= mt_rand(0, $MaxFound);
200					if($Count <= 0) 
201						continue;
202						
203					$Found[$ID]			= $Count;
204					$FoundShips	 		-= $Count * ($pricelist[$ID]['cost'][901] + $pricelist[$ID]['cost'][902]);
205					$FoundShipMess   	.= '<br>'.$LNG['tech'][$ID].': '.pretty_number($Count);
206					if($FoundShips <= 0)
207						break;
208				}
209				
210				if (empty($Found)) {
211					$FoundShipMess .= '<br><br>'.$LNG['sys_expe_found_ships_nothing'];
212				}
213
214				foreach($fleetArray as $ID => $Count)
215				{
216					if(!empty($Found[$ID]))
217					{
218						$Count	+= $Found[$ID];
219					}
220					
221					$NewFleetArray  	.= $ID.",".floattostring($Count).';';
222				}	
223				
224				$Message	.= $FoundShipMess;
225							
226				$this->UpdateFleet('fleet_array', $NewFleetArray);
227				$this->UpdateFleet('fleet_amount', array_sum($fleetArray));
228			break;
229			case 4:
230			
231				$messageHTML	= <<<HTML
232<div class="raportMessage">
233<table>
234<tr>
235<td colspan="2"><a href="CombatReport.php?raport=%s" target="_blank"><span class="%s">%s %s (%s)</span></a></td>
236</tr>
237<tr>
238<td>%s</td><td><span class="%s">%s: %s</span>&nbsp;<span class="%s">%s: %s</span></td>
239</tr>
240<tr>
241<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>
242</tr>
243<tr>
244<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>
245</tr>
246</table>
247</div>
248HTML;
249				//Minize HTML
250				$messageHTML	= str_replace(array("\n", "\t", "\r"), "", $messageHTML);
251
252				// pirate or alien
253				$attackType	= mt_rand(1,2);
254				$eventSize	= mt_rand(0, 100);
255
256				$targetFleetData	= array();
257
258				if($attackType == 1)
259				{
260					$techBonus		= -3;
261					$targetName		= $LNG['sys_expe_attackname_1'];
262					$roundFunction	= 'floor';
263
264					if(10 < $eventSize)
265					{
266						$Message    			= $LNG['sys_expe_attack_1_1_5'];
267						$attackFactor			= 30 + mt_rand(-3, 3) / 100;
268						$targetFleetData[204]	= 5;
269					}
270					elseif(0 < $eventSize && 10 >= $eventSize)
271					{
272						$Message    			= $LNG['sys_expe_attack_1_2_3'];
273						$attackFactor			= 50 + mt_rand(-5, 5) / 100;
274						$targetFleetData[206]	= 3;
275					}
276					else
277					{
278						$Message   				= $LNG['sys_expe_attack_1_3_2'];
279						$attackFactor			= 80 + mt_rand(-8, 8) / 100;
280						$targetFleetData[207]	= 2;
281					}
282				}
283				else
284				{
285					$techBonus		= 3;
286					$targetName		= $LNG['sys_expe_attackname_2'];
287					$roundFunction	= 'ceil';
288
289					if(10 < $eventSize)
290					{
291						$Message    			= $LNG['sys_expe_attack_1_1_5'];
292						$attackFactor			= 40 + mt_rand(-4, 4) / 100;
293						$targetFleetData[204]	= 5;
294					}
295					elseif(0 < $eventSize && 10 >= $eventSize)
296					{
297						$Message    			= $LNG['sys_expe_attack_1_3_3'];
298						$attackFactor			= 60 + mt_rand(-6, 6) / 100;
299						$targetFleetData[215]	= 3;
300					}
301					else
302					{
303						$Message    			= $LNG['sys_expe_attack_1_3_2'];
304						$attackFactor			= 90 + mt_rand(-9, 9) / 100;
305						$targetFleetData[213]	= 2;
306					}
307				}
308					
309				foreach($fleetArray as $shipId => $shipAmount)
310				{
311					if(isset($targetFleetData[$shipId]))
312					{
313						$targetFleetData[$shipId]	= 0;
314					}
315
316					$targetFleetData[$shipId]	= $roundFunction($shipAmount * $attackFactor);
317				}
318
319				$targetFleetData	= array_filter($targetFleetData);
320
321				$sql = 'SELECT * FROM %%USERS%% WHERE id = :userId;';
322
323				$senderData	= Database::get()->selectSingle($sql, array(
324					':userId'	=> $this->_fleet['fleet_owner']
325				));
326
327				$targetData	= array(
328					'id'			=> 0,
329					'username'		=> $targetName,
330					'military_tech'	=> min($senderData['military_tech'] + $techBonus, 0),
331					'defence_tech'	=> min($senderData['defence_tech'] + $techBonus, 0),
332					'shield_tech'	=> min($senderData['shield_tech'] + $techBonus, 0),
333					'rpg_amiral'	=> 0,
334					'dm_defensive'	=> 0,
335					'dm_attack' 	=> 0
336				);
337				
338				$fleetID	= $this->_fleet['fleet_id'];
339				
340				$fleetAttack[$fleetID]['fleetDetail']		= $this->_fleet;
341				$fleetAttack[$fleetID]['player']			= $senderData;
342				$fleetAttack[$fleetID]['player']['factor']	= getFactors($fleetAttack[$this->_fleet['fleet_id']]['player'], 'attack', $this->_fleet['fleet_start_time']);
343				$fleetAttack[$fleetID]['unit']				= $fleetArray;
344				
345				$fleetDefend = array();
346
347				$fleetDefend[0]['fleetDetail'] = array(
348					'fleet_start_galaxy'		=> $this->_fleet['fleet_end_galaxy'],
349					'fleet_start_system'		=> $this->_fleet['fleet_end_system'],
350					'fleet_start_planet'		=> $this->_fleet['fleet_end_planet'],
351					'fleet_start_type'			=> 1,
352					'fleet_end_galaxy'			=> $this->_fleet['fleet_end_galaxy'],
353					'fleet_end_system'			=> $this->_fleet['fleet_end_system'],
354					'fleet_end_planet'			=> $this->_fleet['fleet_end_planet'],
355					'fleet_end_type'			=> 1,
356					'fleet_resource_metal'		=> 0,
357					'fleet_resource_crystal'	=> 0,
358					'fleet_resource_deuterium'	=> 0
359				);
360
361				$bonusList	= BuildFunctions::getBonusList();
362
363				$fleetDefend[0]['player']	= $targetData;
364				$fleetDefend[0]['player']['factor']	= ArrayUtil::combineArrayWithSingleElement($bonusList, 0);
365				$fleetDefend[0]['unit']		= $targetFleetData;
366
367				require_once 'includes/classes/missions/functions/calculateAttack.php';
368				$combatResult	= calculateAttack($fleetAttack, $fleetDefend, $config->Fleet_Cdr, $config->Defs_Cdr);
369
370				$fleetArray = '';
371				$totalCount = 0;
372				
373				$fleetAttack[$fleetID]['unit']	= array_filter($fleetAttack[$fleetID]['unit']);
374				foreach ($fleetAttack[$fleetID]['unit'] as $element => $amount)
375				{
376					$fleetArray .= $element.','.$amount.';';
377					$totalCount += $amount;
378				}
379
380				if ($totalCount <= 0)
381				{
382					$this->KillFleet();
383				}
384				else
385				{
386					$this->UpdateFleet('fleet_array', substr($fleetArray, 0, -1));
387					$this->UpdateFleet('fleet_amount', $totalCount);
388				}
389
390				require_once('includes/classes/missions/functions/GenerateReport.php');
391			
392			
393				$debrisResource	= array(901, 902);
394				$debris			= array();
395
396				foreach($debrisResource as $elementID)
397				{
398					$debris[$elementID]			= 0;
399				}
400				
401				$stealResource	= array(901 => 0, 902 => 0, 903 => 0);
402			
403				$reportInfo	= array(
404					'thisFleet'				=> $this->_fleet,
405					'debris'				=> $debris,
406					'stealResource'			=> $stealResource,
407					'moonChance'			=> 0,
408					'moonDestroy'			=> false,
409					'moonName'				=> NULL,
410					'moonDestroyChance'		=> NULL,
411					'moonDestroySuccess'	=> NULL,
412					'fleetDestroyChance'	=> NULL,
413					'fleetDestroySuccess'	=> NULL,
414				);
415				
416				$reportData	= GenerateReport($combatResult, $reportInfo);
417			
418				$reportID	= md5(uniqid('', true).TIMESTAMP);
419
420				$sql		= "INSERT INTO %%RW%% SET
421				rid			= :reportId,
422				raport		= :reportData,
423				time		= :time,
424				attacker	= :attacker;";
425
426				Database::get()->insert($sql, array(
427					':reportId'		=> $reportID,
428					':reportData'	=> serialize($reportData),
429					':time'			=> $this->_fleet['fleet_start_time'],
430					':attacker'		=> $this->_fleet['fleet_owner'],
431				));
432			
433				switch($combatResult['won'])
434				{
435					case "a":
436						$attackClass	= 'raportWin';
437						$defendClass	= 'raportLose';
438					break;
439					case "r":
440						$attackClass	= 'raportLose';
441						$defendClass	= 'raportWin';
442					break;
443					default:
444						$attackClass	= 'raportDraw';
445						$defendClass	= 'raportDraw';
446					break;
447				}
448
449				$message	= sprintf($messageHTML,
450					$reportID,
451					$attackClass,
452					$LNG['sys_mess_attack_report'],
453					sprintf(
454						$LNG['sys_adress_planet'],
455						$this->_fleet['fleet_end_galaxy'],
456						$this->_fleet['fleet_end_system'],
457						$this->_fleet['fleet_end_planet']
458					),
459					$LNG['type_planet_short'][$this->_fleet['fleet_end_type']],
460					$LNG['sys_lost'],
461					$attackClass,
462					$LNG['sys_attack_attacker_pos'], pretty_number($combatResult['unitLost']['attacker']),
463					$defendClass,
464					$LNG['sys_attack_defender_pos'], pretty_number($combatResult['unitLost']['defender']),
465					$LNG['sys_gain'],
466					$LNG['tech'][901], pretty_number($stealResource[901]),
467					$LNG['tech'][902], pretty_number($stealResource[902]),
468					$LNG['tech'][903], pretty_number($stealResource[903]),
469					$LNG['sys_debris'],
470					$LNG['tech'][901], pretty_number($debris[901]),
471					$LNG['tech'][902], pretty_number($debris[902])
472				);
473				
474				PlayerUtil::sendMessage($this->_fleet['fleet_owner'], 0, $this->_fleet['fleet_end_stay'], 3,
475					$LNG['sys_mess_tower'], $LNG['sys_mess_attack_report'], $message);
476
477			break;
478			case 5:
479				$this->KillFleet();
480				$Message	= $LNG['sys_expe_lost_fleet_'.mt_rand(1,4)];
481			break;
482			case 6:
483				# http://owiki.de/Expedition#Ver.C3.A4nderte_Flugzeit
484				$chance	= mt_rand(0, 100);
485
486				$Wrapper	= array();
487				$Wrapper[]	= 2;
488				$Wrapper[]	= 2;
489				$Wrapper[]	= 2;
490				$Wrapper[]	= 2;
491				$Wrapper[]	= 2;
492				$Wrapper[]	= 2;
493				$Wrapper[]	= 2;
494				$Wrapper[]	= 3;
495				$Wrapper[]	= 3;
496				$Wrapper[]	= 5;
497			
498				if($chance < 75)
499				{
500					// More return time
501
502					$normalBackTime	= $this->_fleet['fleet_end_time'] - $this->_fleet['fleet_end_stay'];
503					$stayTime		= $this->_fleet['fleet_end_stay'] - $this->_fleet['fleet_start_time'];
504					$factor			= $Wrapper[mt_rand(0, 9)];
505
506					$endTime		= $this->_fleet['fleet_end_stay'] + $normalBackTime + $stayTime + $factor;
507					$this->UpdateFleet('fleet_end_time', $endTime);
508					$Message = $LNG['sys_expe_time_slow_'.mt_rand(1,6)];
509				}
510				else
511				{
512					$normalBackTime	= $this->_fleet['fleet_end_time'] - $this->_fleet['fleet_end_stay'];
513					$stayTime		= $this->_fleet['fleet_end_stay'] - $this->_fleet['fleet_start_time'];
514					$factor			= $Wrapper[mt_rand(0, 9)];
515
516					$endTime		= max(1, $normalBackTime - $stayTime / 3 * $factor);
517					$this->UpdateFleet('fleet_end_time', $endTime);
518					$Message = $LNG['sys_expe_time_fast_'.mt_rand(1,3)];
519				}
520			break;
521		}
522
523		PlayerUtil::sendMessage($this->_fleet['fleet_owner'], 0, $LNG['sys_mess_tower'], 15,
524			$LNG['sys_expe_report'], $Message, $this->_fleet['fleet_end_stay'], NULL, 1, $this->_fleet['fleet_universe']);
525
526		$this->setState(FLEET_RETURN);
527		$this->SaveFleet();
528	}
529	
530	function ReturnEvent()
531	{
532		$LNG		= $this->getLanguage(NULL, $this->_fleet['fleet_owner']);
533		$Message 	= sprintf(
534			$LNG['sys_expe_back_home'],
535			$LNG['tech'][901], pretty_number($this->_fleet['fleet_resource_metal']),
536			$LNG['tech'][902], pretty_number($this->_fleet['fleet_resource_crystal']),
537			$LNG['tech'][903], pretty_number($this->_fleet['fleet_resource_deuterium']),
538			$LNG['tech'][921], pretty_number($this->_fleet['fleet_resource_darkmatter'])
539		);
540
541		PlayerUtil::sendMessage($this->_fleet['fleet_owner'], 0, $LNG['sys_mess_tower'], 4, $LNG['sys_mess_fleetback'],
542			$Message, $this->_fleet['fleet_end_time'], NULL, 1, $this->_fleet['fleet_universe']);
543
544		$this->RestoreFleet();
545	}
546}