PageRenderTime 314ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/classes/missions/MissionCaseExpedition.php

http://2moons.googlecode.com/
PHP | 478 lines | 379 code | 72 blank | 27 comment | 35 complexity | b88174de4aa6d7143e7de0600e483ba5 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-3.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * 2Moons
  4. * Copyright (C) 2012 Jan Kr??pke
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package 2Moons
  20. * @author Jan Kr??pke <info@2moons.cc>
  21. * @copyright 2012 Jan Kr??pke <info@2moons.cc>
  22. * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
  23. * @version 1.7.1 (2013-01-18)
  24. * @info $Id: MissionCaseExpedition.php 2604 2013-01-18 11:35:42Z slaver7 $
  25. * @link http://2moons.cc/
  26. */
  27. class MissionCaseExpedition extends MissionFunctions
  28. {
  29. function __construct($fleet)
  30. {
  31. $this->_fleet = $fleet;
  32. }
  33. function TargetEvent()
  34. {
  35. $this->setState(FLEET_HOLD);
  36. $this->SaveFleet();
  37. }
  38. function EndStayEvent()
  39. {
  40. global $pricelist, $reslist;
  41. $LNG = $this->getLanguage(NULL, $this->_fleet['fleet_owner']);
  42. $expeditionPoints = array();
  43. foreach($reslist['fleet'] as $ID)
  44. {
  45. $expeditionPoints[$ID] = ($pricelist[$ID]['cost'][901] + $pricelist[$ID]['cost'][902]) / 1000;
  46. }
  47. $expeditionPoints[202] = 12;
  48. $expeditionPoints[203] = 47;
  49. $expeditionPoints[204] = 12;
  50. $expeditionPoints[205] = 110;
  51. $expeditionPoints[206] = 47;
  52. $expeditionPoints[207] = 160;
  53. $fleetRaw = explode(";", $this->_fleet['fleet_array']);
  54. $fleetPoints = 0;
  55. $fleetCapacity = 0;
  56. $fleetArray = array();
  57. foreach ($fleetRaw as $Group)
  58. {
  59. if (empty($Group)) continue;
  60. $Class = explode (",", $Group);
  61. $fleetArray[$Class[0]] = $Class[1];
  62. $fleetCapacity += $Class[1] * $pricelist[$Class[0]]['capacity'];
  63. $fleetPoints += $Class[1] * $expeditionPoints[$Class[0]];
  64. }
  65. $fleetCapacity -= $this->_fleet['fleet_resource_metal'] + $this->_fleet['fleet_resource_crystal'] + $this->_fleet['fleet_resource_deuterium'] + $this->_fleet['fleet_resource_darkmatter'];
  66. $GetEvent = mt_rand(1, 9);
  67. $Message = $LNG['sys_expe_nothing_'.mt_rand(1,8)];
  68. switch($GetEvent)
  69. {
  70. case 1:
  71. $WitchFound = mt_rand(1,3);
  72. $FindSize = mt_rand(0, 100);
  73. $Factor = 0;
  74. if(10 < $FindSize) {
  75. $Factor = (mt_rand(10, 50) / $WitchFound) * $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['resource_multiplier'];
  76. $Message = $LNG['sys_expe_found_ress_1_'.mt_rand(1,4)];
  77. } elseif(0 < $FindSize && 10 >= $FindSize) {
  78. $Factor = (mt_rand(52, 100) / $WitchFound) * $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['resource_multiplier'];
  79. $Message = $LNG['sys_expe_found_ress_2_'.mt_rand(1,3)];
  80. } elseif(0 == $FindSize) {
  81. $Factor = (mt_rand(102, 200) / $WitchFound) * $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['resource_multiplier'];
  82. $Message = $LNG['sys_expe_found_ress_3_'.mt_rand(1,2)];
  83. }
  84. $StatFactor = $GLOBALS['DATABASE']->getFirstRow("SELECT MAX(total_points) as total FROM `".STATPOINTS."` WHERE `stat_type` = 1 AND `universe` = '".$this->_fleet['fleet_universe']."';");
  85. $MaxPoints = ($StatFactor['total'] < 5000000) ? 9000 : 12000;
  86. $Size = min($Factor * MAX(MIN($fleetPoints / 1000, $MaxPoints), 200), $fleetCapacity);
  87. switch($WitchFound)
  88. {
  89. case 1:
  90. $this->UpdateFleet('fleet_resource_metal', $this->_fleet['fleet_resource_metal'] + $Size);
  91. break;
  92. case 2:
  93. $this->UpdateFleet('fleet_resource_crystal', $this->_fleet['fleet_resource_crystal'] + $Size);
  94. break;
  95. case 3:
  96. $this->UpdateFleet('fleet_resource_deuterium', $this->_fleet['fleet_resource_deuterium'] + $Size);
  97. break;
  98. }
  99. break;
  100. case 2:
  101. $FindSize = mt_rand(0, 100);
  102. $Size = 0;
  103. if(10 < $FindSize) {
  104. $Size = mt_rand(100, 300);
  105. $Message = $LNG['sys_expe_found_dm_1_'.mt_rand(1,5)];
  106. } elseif(0 < $FindSize && 10 >= $FindSize) {
  107. $Size = mt_rand(301, 600);
  108. $Message = $LNG['sys_expe_found_dm_2_'.mt_rand(1,3)];
  109. } elseif(0 == $FindSize) {
  110. $Size = mt_rand(601, 3000);
  111. $Message = $LNG['sys_expe_found_dm_3_'.mt_rand(1,2)];
  112. }
  113. $this->UpdateFleet('fleet_resource_darkmatter', $this->_fleet['fleet_resource_darkmatter'] + $Size);
  114. break;
  115. case 3:
  116. $FindSize = mt_rand(0, 100);
  117. $Size = 0;
  118. $Message = "";
  119. if(10 < $FindSize) {
  120. $Size = mt_rand(10, 50);
  121. $Message = $LNG['sys_expe_found_ships_1_'.mt_rand(1,4)];
  122. } elseif(0 < $FindSize && 10 >= $FindSize) {
  123. $Size = mt_rand(52, 100);
  124. $Message = $LNG['sys_expe_found_ships_2_'.mt_rand(1,2)];
  125. } elseif(0 == $FindSize) {
  126. $Size = mt_rand(102, 200);
  127. $Message = $LNG['sys_expe_found_ships_3_'.mt_rand(1,2)];
  128. }
  129. $StatFactor = $GLOBALS['DATABASE']->getFirstCell("SELECT MAX(total_points) FROM `".STATPOINTS."` WHERE `stat_type` = 1 AND `universe` = '".$this->_fleet['fleet_universe']."';");
  130. $MaxPoints = ($StatFactor < 5000000) ? 4500 : 6000;
  131. $FoundShips = max(round($Size * min($fleetPoints, $MaxPoints)), 10000);
  132. $FoundShipMess = "";
  133. $NewFleetArray = "";
  134. $Found = array();
  135. foreach($reslist['fleet'] as $ID)
  136. {
  137. if(!isset($fleetArray[$ID]) || $ID == 208 || $ID == 209 || $ID == 214)
  138. continue;
  139. $MaxFound = floor($FoundShips / ($pricelist[$ID]['cost'][901] + $pricelist[$ID]['cost'][902]));
  140. if($MaxFound <= 0)
  141. continue;
  142. $Count = mt_rand(0, $MaxFound);
  143. if($Count <= 0)
  144. continue;
  145. $Found[$ID] = $Count;
  146. $FoundShips -= $Count * ($pricelist[$ID]['cost'][901] + $pricelist[$ID]['cost'][902]);
  147. $FoundShipMess .= '<br>'.$LNG['tech'][$ID].': '.pretty_number($Count);
  148. if($FoundShips <= 0)
  149. break;
  150. }
  151. if (empty($Found)) {
  152. $FoundShipMess .= '<br><br>'.$LNG['sys_expe_found_ships_nothing'];
  153. }
  154. foreach($fleetArray as $ID => $Count)
  155. {
  156. if(!empty($Found[$ID]))
  157. {
  158. $Count += $Found[$ID];
  159. }
  160. $NewFleetArray .= $ID.",".floattostring($Count).';';
  161. }
  162. $Message .= $FoundShipMess;
  163. $this->UpdateFleet('fleet_array', $NewFleetArray);
  164. $this->UpdateFleet('fleet_amount', array_sum($fleetArray));
  165. break;
  166. case 4:
  167. $Chance = mt_rand(1,2);
  168. if($Chance == 1) {
  169. $Points = array(-3,-5,-8);
  170. $Which = 1;
  171. $Def = -3;
  172. $Name = $LNG['sys_expe_attackname_1'];
  173. $Add = 0;
  174. $Rand = array(5,3,2);
  175. $DefenderFleetArray = "204,5;206,3;207,2;";
  176. } else {
  177. $Points = array(-4,-6,-9);
  178. $Which = 2;
  179. $Def = 3;
  180. $Name = $LNG['sys_expe_attackname_2'];
  181. $Add = 0.1;
  182. $Rand = array(4,3,2);
  183. $DefenderFleetArray = "205,5;215,3;213,2;";
  184. }
  185. $messageHTML = <<<HTML
  186. <div class="raportMessage">
  187. <table>
  188. <tr>
  189. <td colspan="2"><a href="CombatReport.php?raport=%s" target="_blank"><span class="%s">%s %s (%s)</span></a></td>
  190. </tr>
  191. <tr>
  192. <td>%s</td><td><span class="%s">%s: %s</span>&nbsp;<span class="%s">%s: %s</span></td>
  193. </tr>
  194. <tr>
  195. <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>
  196. </tr>
  197. <tr>
  198. <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>
  199. </tr>
  200. </table>
  201. </div>
  202. HTML;
  203. //Minize HTML
  204. $messageHTML = str_replace(array("\n", "\t", "\r"), "", $messageHTML);
  205. $FindSize = mt_rand(0, 100);
  206. $maxAttack = 0;
  207. if(10 < $FindSize) {
  208. $Message = $LNG['sys_expe_attack_'.$Which.'_1_'.$Rand[0]];
  209. $maxAttack = 0.3 + $Add + (mt_rand($Points[0], abs($Points[0])) * 0.01);
  210. } elseif(0 < $FindSize && 10 >= $FindSize) {
  211. $Message = $LNG['sys_expe_attack_'.$Which.'_2_'.$Rand[1]];
  212. $maxAttack = 0.3 + $Add + (mt_rand($Points[1], abs($Points[1])) * 0.01);
  213. } elseif(0 == $FindSize) {
  214. $Message = $LNG['sys_expe_attack_'.$Which.'_3_'.$Rand[2]];
  215. $maxAttack = 0.3 + $Add + (mt_rand($Points[2], abs($Points[2])) * 0.01);
  216. }
  217. foreach($fleetArray as $ID => $count)
  218. {
  219. $DefenderFleetArray .= $ID.",".round($count * $maxAttack).";";
  220. }
  221. $AttackerTechno = $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".USERS." WHERE id = ".$this->_fleet['fleet_owner'].";");
  222. $DefenderTechno = array(
  223. 'id' => 0,
  224. 'username' => $Name,
  225. 'military_tech' => (min($AttackerTechno['military_tech'] + $Def,0)),
  226. 'defence_tech' => (min($AttackerTechno['defence_tech'] + $Def,0)),
  227. 'shield_tech' => (min($AttackerTechno['shield_tech'] + $Def,0)),
  228. 'rpg_amiral' => 0,
  229. 'dm_defensive' => 0,
  230. 'dm_attack' => 0
  231. );
  232. $fleetID = $this->_fleet['fleet_id'];
  233. $fleetAttack[$fleetID]['fleetDetail'] = $this->_fleet;
  234. $fleetAttack[$fleetID]['player'] = $AttackerTechno;
  235. $fleetAttack[$fleetID]['player']['factor'] = getFactors($fleetAttack[$this->_fleet['fleet_id']]['player'], 'attack', $this->_fleet['fleet_start_time']);
  236. $fleetAttack[$fleetID]['unit'] = array();
  237. $temp = explode(';', $this->_fleet['fleet_array']);
  238. foreach ($temp as $temp2)
  239. {
  240. $temp2 = explode(',', $temp2);
  241. if ($temp2[0] < 100)
  242. {
  243. continue;
  244. }
  245. if (!isset($fleetAttack[$fleetID]['unit'][$temp2[0]]))
  246. {
  247. $fleetAttack[$fleetID]['unit'][$temp2[0]] = 0;
  248. }
  249. $fleetAttack[$fleetID]['unit'][$temp2[0]] += $temp2[1];
  250. }
  251. $fleetDefend = array();
  252. $defRowDef = explode(';', $DefenderFleetArray);
  253. foreach ($defRowDef as $Element)
  254. {
  255. $Element = explode(',', $Element);
  256. if ($Element[0] < 100) continue;
  257. if (!isset($fleetDefend[0]['unit'][$Element[0]]))
  258. $fleetDefend[0]['unit'][$Element[0]] = 0;
  259. $fleetDefend[0]['unit'][$Element[0]] += $Element[1];
  260. }
  261. $fleetDefend[0]['fleetDetail'] = array(
  262. 'fleet_start_galaxy' => $this->_fleet['fleet_end_galaxy'],
  263. 'fleet_start_system' => $this->_fleet['fleet_end_system'],
  264. 'fleet_start_planet' => $this->_fleet['fleet_end_planet'],
  265. 'fleet_start_type' => 1,
  266. 'fleet_end_galaxy' => $this->_fleet['fleet_end_galaxy'],
  267. 'fleet_end_system' => $this->_fleet['fleet_end_system'],
  268. 'fleet_end_planet' => $this->_fleet['fleet_end_planet'],
  269. 'fleet_end_type' => 1,
  270. 'fleet_resource_metal' => 0,
  271. 'fleet_resource_crystal' => 0,
  272. 'fleet_resource_deuterium' => 0
  273. );
  274. $fleetDefend[0]['player'] = $DefenderTechno;
  275. $fleetDefend[0]['player']['factor'] = 0;
  276. require_once('calculateAttack.php');
  277. $fleetIntoDebris = $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Fleet_Cdr'];
  278. $defIntoDebris = $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Defs_Cdr'];
  279. $combatResult = calculateAttack($fleetAttack, $fleetDefend, $fleetIntoDebris, $defIntoDebris);
  280. $fleetArray = '';
  281. $totalCount = 0;
  282. $fleetAttack[$fleetID]['unit'] = array_filter($fleetAttack[$fleetID]['unit']);
  283. foreach ($fleetAttack[$fleetID]['unit'] as $element => $amount)
  284. {
  285. $fleetArray .= $element.','.$amount.';';
  286. $totalCount += $amount;
  287. }
  288. if ($totalCount <= 0)
  289. {
  290. $this->KillFleet();
  291. }
  292. else
  293. {
  294. $this->UpdateFleet('fleet_array', substr($fleetArray, 0, -1));
  295. $this->UpdateFleet('fleet_amount', $totalCount);
  296. }
  297. require_once('GenerateReport.php');
  298. $debrisRessource = array(901, 902);
  299. foreach($debrisRessource as $elementID)
  300. {
  301. $debris[$elementID] = 0;
  302. }
  303. $stealResource = array(901 => 0, 902 => 0, 903 => 0);
  304. $raportInfo = array(
  305. 'thisFleet' => $this->_fleet,
  306. 'debris' => $debris,
  307. 'stealResource' => $stealResource,
  308. 'moonChance' => 0,
  309. 'moonDestroy' => false,
  310. 'moonName' => null,
  311. 'moonDestroyChance' => null,
  312. 'moonDestroySuccess' => null,
  313. 'fleetDestroyChance' => null,
  314. 'fleetDestroySuccess' => null,
  315. );
  316. $raportData = GenerateReport($combatResult, $raportInfo);
  317. $raportID = md5(uniqid('', true).TIMESTAMP);
  318. $sqlQuery = "INSERT INTO ".RW." SET rid = '".$raportID."', raport = '".serialize($raportData)."', time = '".$this->_fleet['fleet_start_time']."', attacker = '".$this->_fleet['fleet_owner']."';";
  319. $GLOBALS['DATABASE']->query($sqlQuery);
  320. switch($combatResult['won'])
  321. {
  322. case "a":
  323. $attackClass = 'raportWin';
  324. $defendClass = 'raportLose';
  325. break;
  326. case "w":
  327. $attackClass = 'raportDraw';
  328. $defendClass = 'raportDraw';
  329. break;
  330. case "r":
  331. $attackClass = 'raportLose';
  332. $defendClass = 'raportWin';
  333. break;
  334. }
  335. $message = sprintf($messageHTML,
  336. $raportID,
  337. $attackClass,
  338. $LNG['sys_mess_attack_report'],
  339. sprintf(
  340. $LNG['sys_adress_planet'],
  341. $this->_fleet['fleet_end_galaxy'],
  342. $this->_fleet['fleet_end_system'],
  343. $this->_fleet['fleet_end_planet']
  344. ),
  345. $LNG['type_planet_short'][$this->_fleet['fleet_end_type']],
  346. $LNG['sys_lost'],
  347. $attackClass,
  348. $LNG['sys_attack_attacker_pos'],
  349. pretty_number($combatResult['unitLost']['attacker']),
  350. $defendClass,
  351. $LNG['sys_attack_defender_pos'],
  352. pretty_number($combatResult['unitLost']['defender']),
  353. $LNG['sys_gain'],
  354. $LNG['tech'][901],
  355. pretty_number($stealResource[901]),
  356. $LNG['tech'][902],
  357. pretty_number($stealResource[902]),
  358. $LNG['tech'][903],
  359. pretty_number($stealResource[903]),
  360. $LNG['sys_debris'],
  361. $LNG['tech'][901],
  362. pretty_number($debris[901]),
  363. $LNG['tech'][902],
  364. pretty_number($debris[902])
  365. );
  366. SendSimpleMessage($this->_fleet['fleet_owner'], 0, $this->_fleet['fleet_end_stay'], 3, $LNG['sys_mess_tower'], $LNG['sys_mess_attack_report'], $message);
  367. break;
  368. case 5:
  369. $this->KillFleet();
  370. $Message = $LNG['sys_expe_lost_fleet_'.mt_rand(1,4)];
  371. break;
  372. case 6:
  373. # http://owiki.de/Expedition#Ver.C3.A4nderte_Flugzeit
  374. $MoreTime = mt_rand(0, 100);
  375. $Wrapper = array();
  376. $Wrapper[] = 2;
  377. $Wrapper[] = 2;
  378. $Wrapper[] = 2;
  379. $Wrapper[] = 2;
  380. $Wrapper[] = 2;
  381. $Wrapper[] = 2;
  382. $Wrapper[] = 2;
  383. $Wrapper[] = 3;
  384. $Wrapper[] = 3;
  385. $Wrapper[] = 5;
  386. if($MoreTime < 75) {
  387. $this->UpdateFleet('fleet_end_time', $this->_fleet['fleet_end_stay'] + (($this->_fleet['fleet_end_time'] - $this->_fleet['fleet_end_stay']) + ($this->_fleet['fleet_end_stay'] - $this->_fleet['fleet_start_time']) * $Wrapper[mt_rand(0, 9)]));
  388. $Message = $LNG['sys_expe_time_slow_'.mt_rand(1,6)];
  389. } else {
  390. $this->UpdateFleet('fleet_end_time', $this->_fleet['fleet_end_stay'] + max(1, ($this->_fleet['fleet_end_time'] - $this->_fleet['fleet_end_stay']) - ($this->_fleet['fleet_end_stay'] - $this->_fleet['fleet_start_time']) / 3 * $Wrapper[mt_rand(0, 9)]));
  391. $Message = $LNG['sys_expe_time_fast_'.mt_rand(1,3)];
  392. }
  393. break;
  394. }
  395. SendSimpleMessage($this->_fleet['fleet_owner'], 0, $this->_fleet['fleet_end_stay'], 15, $LNG['sys_mess_tower'], $LNG['sys_expe_report'], $Message);
  396. $this->setState(FLEET_RETURN);
  397. $this->SaveFleet();
  398. }
  399. function ReturnEvent()
  400. {
  401. $LNG = $this->getLanguage(NULL, $this->_fleet['fleet_owner']);
  402. $Message = sprintf($LNG['sys_expe_back_home'], $LNG['tech'][901], pretty_number($this->_fleet['fleet_resource_metal']), $LNG['tech'][902], pretty_number($this->_fleet['fleet_resource_crystal']), $LNG['tech'][903], pretty_number($this->_fleet['fleet_resource_deuterium']), $LNG['tech'][921], pretty_number($this->_fleet['fleet_resource_darkmatter']));
  403. SendSimpleMessage($this->_fleet['fleet_owner'], 0, $this->_fleet['fleet_end_time'], 15, $LNG['sys_mess_tower'], $LNG['sys_expe_report'], $Message);
  404. $this->RestoreFleet();
  405. }
  406. }