PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-3.0, GPL-2.0, Apache-2.0, AGPL-3.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$
  25. * @link http://2moons.cc/
  26. */
  27. class MissionCaseDestruction extends MissionFunctions
  28. {
  29. function __construct($Fleet)
  30. {
  31. $this->_fleet = $Fleet;
  32. }
  33. function TargetEvent()
  34. {
  35. global $resource, $reslist;
  36. $fleetAttack = array();
  37. $fleetDefend = array();
  38. $userAttack = array();
  39. $userDefend = array();
  40. $stealResource = array(
  41. 901 => 0,
  42. 902 => 0,
  43. 903 => 0,
  44. );
  45. $debris = array();
  46. $planetDebris = array();
  47. $raportInfo = array();
  48. $debrisRessource = array(901, 902);
  49. $messageHTML = <<<HTML
  50. <div class="raportMessage">
  51. <table>
  52. <tr>
  53. <td colspan="2"><a href="CombatReport.php?raport=%s" target="_blank"><span class="%s">%s %s (%s)</span></a></td>
  54. </tr>
  55. <tr>
  56. <td>%s</td><td><span class="%s">%s: %s</span>&nbsp;<span class="%s">%s: %s</span></td>
  57. </tr>
  58. <tr>
  59. <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>
  60. </tr>
  61. <tr>
  62. <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>
  63. </tr>
  64. </table>
  65. </div>
  66. HTML;
  67. //Minize HTML
  68. $messageHTML = str_replace(array("\n", "\t", "\r"), "", $messageHTML);
  69. $targetPlanet = $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".PLANETS." WHERE id = '".$this->_fleet['fleet_end_id']."';");
  70. $targetUser = $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".USERS." WHERE id = '".$targetPlanet['id_owner']."';");
  71. $targetUser['factor'] = getFactors($targetUser, 'basic', $this->_fleet['fleet_start_time']);
  72. $planetUpdater = new ResourceUpdate();
  73. list($targetUser, $targetPlanet) = $planetUpdater->CalcResource($targetUser, $targetPlanet, true, $this->_fleet['fleet_start_time']);
  74. if($this->_fleet['fleet_group'] != 0)
  75. {
  76. $GLOBALS['DATABASE']->query("DELETE FROM ".AKS." WHERE id = '".$this->_fleet['fleet_group']."';");
  77. $incomingFleetsResult = $GLOBALS['DATABASE']->query("SELECT * FROM ".FLEETS." WHERE fleet_group = '".$this->_fleet['fleet_group']."';");
  78. while ($incomingFleetsRow = $GLOBALS['DATABASE']->fetch_array($incomingFleetsResult))
  79. {
  80. $incomingFleets[$incomingFleetsRow['fleet_id']] = $incomingFleetsRow;
  81. }
  82. $GLOBALS['DATABASE']->free_result($incomingFleetsResult);
  83. }
  84. else
  85. {
  86. $incomingFleets = array($this->_fleet['fleet_id'] => $this->_fleet);
  87. }
  88. foreach($incomingFleets as $fleetID => $fleetDetail)
  89. {
  90. $fleetAttack[$fleetID]['fleetDetail'] = $fleetDetail;
  91. $fleetAttack[$fleetID]['player'] = $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".USERS." WHERE id = '".$fleetDetail['fleet_owner']."';");
  92. $fleetAttack[$fleetID]['player']['factor'] = getFactors($fleetAttack[$fleetID]['player'], 'attack', $this->_fleet['fleet_start_time']);
  93. $fleetAttack[$fleetID]['unit'] = fleetAmountToArray($fleetDetail['fleet_array']);
  94. $userAttack[$fleetAttack[$fleetID]['player']['id']] = $fleetAttack[$fleetID]['player']['username'];
  95. }
  96. $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."';");
  97. while ($fleetDetail = $GLOBALS['DATABASE']->fetch_array($targetFleetsResult))
  98. {
  99. $fleetID = $fleetDetail['fleet_id'];
  100. $fleetDefend[$fleetID]['fleetDetail'] = $fleetDetail;
  101. $fleetDefend[$fleetID]['player'] = $GLOBALS['DATABASE']->getFirstRow("SELECT * FROM ".USERS." WHERE id = '".$fleetDetail['fleet_owner']."';");
  102. $fleetDefend[$fleetID]['player']['factor'] = getFactors($fleetDefend[$fleetID]['player'], 'attack', $this->_fleet['fleet_start_time']);
  103. $fleetDefend[$fleetID]['unit'] = fleetAmountToArray($fleetDetail['fleet_array']);
  104. $userDefend[$fleetDefend[$fleetID]['player']['id']] = $fleetDefend[$fleetID]['player']['username'];
  105. }
  106. $GLOBALS['DATABASE']->free_result($targetFleetsResult);
  107. $fleetDefend[0]['player'] = $targetUser;
  108. $fleetDefend[0]['player']['factor'] = getFactors($fleetDefend[0]['player'], 'attack', $this->_fleet['fleet_start_time']);
  109. $fleetDefend[0]['fleetDetail'] = array(
  110. 'fleet_start_galaxy' => $targetPlanet['galaxy'],
  111. 'fleet_start_system' => $targetPlanet['system'],
  112. 'fleet_start_planet' => $targetPlanet['planet'],
  113. 'fleet_start_type' => $targetPlanet['planet_type'],
  114. );
  115. $fleetDefend[0]['unit'] = array();
  116. foreach(array_merge($reslist['fleet'], $reslist['defense']) as $elementID)
  117. {
  118. if (empty($targetPlanet[$resource[$elementID]])) continue;
  119. $fleetDefend[0]['unit'][$elementID] = $targetPlanet[$resource[$elementID]];
  120. }
  121. $userDefend[$fleetDefend[0]['player']['id']] = $fleetDefend[0]['player']['username'];
  122. require_once('calculateAttack.php');
  123. $fleetIntoDebris = $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Fleet_Cdr'];
  124. $defIntoDebris = $GLOBALS['CONFIG'][$this->_fleet['fleet_universe']]['Defs_Cdr'];
  125. $combatResult = calculateAttack($fleetAttack, $fleetDefend, $fleetIntoDebris, $defIntoDebris);
  126. $sqlQuery = "";
  127. foreach ($fleetAttack as $fleetID => $fleetDetail)
  128. {
  129. $fleetArray = '';
  130. $totalCount = 0;
  131. $fleetDetail['unit'] = array_filter($fleetDetail['unit']);
  132. foreach ($fleetDetail['unit'] as $elementID => $amount)
  133. {
  134. $fleetArray .= $elementID.','.floattostring($amount).';';
  135. $totalCount += $amount;
  136. }
  137. if($totalCount == 0)
  138. {
  139. if($this->_fleet['fleet_id'] == $fleetID)
  140. {
  141. $this->KillFleet();
  142. }
  143. else
  144. {
  145. $sqlQuery .= "DELETE FROM ".FLEETS." WHERE fleet_id = ".$fleetID.";";
  146. $sqlQuery .= "DELETE FROM ".FLEETS_EVENT." WHERE fleetID = ".$fleetID.";";
  147. }
  148. $sqlQuery .= "UPDATE ".LOG_FLEETS." SET fleet_state = 2 WHERE fleet_id = '".$fleetID."';";
  149. }
  150. elseif($totalCount > 0)
  151. {
  152. $sqlQuery .= "UPDATE ".FLEETS." SET fleet_array = '".substr($fleetArray, 0, -1)."', fleet_amount = '".$totalCount."' WHERE fleet_id = '".$fleetID."';";
  153. $sqlQuery .= "UPDATE ".LOG_FLEETS." SET fleet_array = '".substr($fleetArray, 0, -1)."', fleet_amount = '".$totalCount."', fleet_state = 1 WHERE fleet_id = '".$fleetID."';";
  154. }
  155. else
  156. {
  157. throw new Exception("Negative Fleet amount ....");
  158. }
  159. }
  160. foreach ($fleetDefend as $fleetID => $fleetDetail)
  161. {
  162. if($fleetID != 0)
  163. {
  164. $fleetArray = '';
  165. $totalCount = 0;
  166. $fleetDetail['unit'] = array_filter($fleetDetail['unit']);
  167. foreach ($fleetDetail['unit'] as $elementID => $amount)
  168. {
  169. $fleetArray .= $elementID.','.floattostring($amount).';';
  170. $totalCount += $amount;
  171. }
  172. if($totalCount == 0)
  173. {
  174. $sqlQuery .= "DELETE FROM ".FLEETS." WHERE fleet_id = ".$fleetID.";";
  175. $sqlQuery .= "DELETE FROM ".FLEETS_EVENT." WHERE fleetID = ".$fleetID.";";
  176. $sqlQuery .= "UPDATE ".LOG_FLEETS." SET fleet_state = 2 WHERE fleet_id = '".$fleetID."';";
  177. }
  178. elseif($totalCount > 0)
  179. {
  180. $sqlQuery .= "UPDATE ".FLEETS." SET fleet_array = '".substr($fleetArray, 0, -1)."', fleet_amount = '".$totalCount."' WHERE fleet_id = '".$fleetID."';";
  181. $sqlQuery .= "UPDATE ".LOG_FLEETS." SET fleet_array = '".substr($fleetArray, 0, -1)."', fleet_amount = '".$totalCount."', fleet_state = 1 WHERE fleet_id = '".$fleetID."';";
  182. }
  183. else
  184. {
  185. throw new Exception("Negative Fleet amount ....");
  186. }
  187. }
  188. else
  189. {
  190. $fleetArray = array();
  191. foreach ($fleetDetail['unit'] as $elementID => $amount)
  192. {
  193. $fleetArray[] = $resource[$elementID]." = ".$amount;
  194. }
  195. if(!empty($fleetArray))
  196. {
  197. $sqlQuery .= "UPDATE ".PLANETS." SET ".implode(', ', $fleetArray)." WHERE id = '".$this->_fleet['fleet_end_id']."';";
  198. }
  199. }
  200. }
  201. $GLOBALS['DATABASE']->multi_query($sqlQuery);
  202. if ($combatResult['won'] == "a")
  203. {
  204. require_once('calculateSteal.php');
  205. $stealResource = calculateSteal($fleetAttack, $targetPlanet);
  206. }
  207. if($this->_fleet['fleet_end_type'] == 3)
  208. {
  209. // Use planet debris, if attack on moons
  210. $targetPlanet = array_merge(
  211. $targetPlanet,
  212. $GLOBALS['DATABASE']->getFirstRow("SELECT der_metal, der_crystal FROM ".PLANETS." WHERE id_luna = ".$this->_fleet['fleet_end_id'].";")
  213. );
  214. }
  215. foreach($debrisRessource as $elementID)
  216. {
  217. $debris[$elementID] = $combatResult['debris']['attacker'][$elementID] + $combatResult['debris']['defender'][$elementID];
  218. $planetDebris[$elementID] = $targetPlanet['der_'.$resource[$elementID]] + $debris[$elementID];
  219. }
  220. $debrisTotal = array_sum($debris);
  221. $raportInfo = array(
  222. 'thisFleet' => $this->_fleet,
  223. 'debris' => $debris,
  224. 'stealResource' => $stealResource,
  225. 'moonChance' => null,
  226. 'moonDestroy' => true,
  227. 'moonName' => null,
  228. 'moonDestroyChance' => null,
  229. 'moonDestroySuccess' => null,
  230. 'fleetDestroyChance' => null,
  231. 'fleetDestroySuccess' => false,
  232. );
  233. $destext = "";
  234. switch($combatResult['won'])
  235. {
  236. case "a":
  237. $moonDestroyChance = round((100 - sqrt($targetPlanet['diameter'])) * sqrt($fleetAttack[$this->_fleet['fleet_id']]['unit'][214]), 1);
  238. // Max 100% | Min 0%
  239. $moonDestroyChance = min($moonDestroyChance, 100);
  240. $moonDestroyChance = max($moonDestroyChance, 0);
  241. $randChance = mt_rand(1, 100);
  242. if ($randChance <= $moonDestroyChance)
  243. {
  244. $planetID = $GLOBALS['DATABASE']->getFirstCell("SELECT id FROM ".PLANETS." WHERE id_luna = ".$targetPlanet['id'].";");
  245. $GLOBALS['DATABASE']->multi_query("
  246. UPDATE ".FLEETS." SET
  247. fleet_start_type = 1,
  248. fleet_start_id = ".$planetID."
  249. WHERE fleet_start_id = ".$targetPlanet['id'].";
  250. UPDATE ".FLEETS." SET
  251. fleet_end_type = 1,
  252. fleet_end_id = ".$planetID.",
  253. fleet_mission = IF(fleet_mission = 9, 1, fleet_mission)
  254. WHERE fleet_end_id = ".$this->_fleet['fleet_end_id']."
  255. AND fleet_id != ".$this->_fleet['fleet_id'].";
  256. UPDATE ".PLANETS."
  257. SET id_luna = 0
  258. WHERE id_luna = ".$targetPlanet['id'].";
  259. DELETE FROM ".PLANETS."
  260. WHERE id = ".$targetPlanet['id'].";");
  261. $raportInfo['moonDestroySuccess'] = 1;
  262. } else {
  263. $raportInfo['moonDestroySuccess'] = 0;
  264. }
  265. $fleetDestroyChance = round(sqrt($targetPlanet['diameter']) / 2);
  266. $randChance = mt_rand(1, 100);
  267. if ($randChance <= $fleetDestroyChance)
  268. {
  269. $this->KillFleet();
  270. $raportInfo['fleetDestroySuccess'] = true;
  271. }
  272. else
  273. {
  274. $raportInfo['fleetDestroySuccess'] = false;
  275. }
  276. $raportInfo['moonDestroyChance'] = $moonDestroyChance;
  277. $raportInfo['fleetDestroyChance'] = $fleetDestroyChance;
  278. $attackStatus = 'wons';
  279. $defendStatus = 'loos';
  280. $attackClass = 'raportWin';
  281. $defendClass = 'raportLose';
  282. break;
  283. case "w":
  284. $attackStatus = 'draws';
  285. $defendStatus = 'draws';
  286. $attackClass = 'raportDraw';
  287. $defendClass = 'raportDraw';
  288. $raportInfo['moonDestroySuccess'] = -1;
  289. break;
  290. case "r":
  291. $attackStatus = 'loos';
  292. $defendStatus = 'wons';
  293. $attackClass = 'raportLose';
  294. $defendClass = 'raportWin';
  295. $raportInfo['moonDestroySuccess'] = -1;
  296. break;
  297. }
  298. require_once('GenerateReport.php');
  299. $raportData = GenerateReport($combatResult, $raportInfo);
  300. $raportID = md5(uniqid('', true).TIMESTAMP);
  301. $sqlQuery = "INSERT INTO ".RW." SET
  302. rid = '".$raportID."',
  303. raport = '".serialize($raportData)."',
  304. time = '".$this->_fleet['fleet_start_time']."',
  305. attacker = '".implode(',', array_keys($userAttack))."',
  306. defender = '".implode(',', array_keys($userDefend))."';";
  307. $GLOBALS['DATABASE']->query($sqlQuery);
  308. $sqlQuery = "";
  309. foreach($userAttack as $userID => $userName)
  310. {
  311. $LNG = $this->getLanguage(NULL, $userID);
  312. $message = sprintf($messageHTML,
  313. $raportID,
  314. $attackClass,
  315. $LNG['sys_mess_attack_report'],
  316. sprintf(
  317. $LNG['sys_adress_planet'],
  318. $this->_fleet['fleet_end_galaxy'],
  319. $this->_fleet['fleet_end_system'],
  320. $this->_fleet['fleet_end_planet']
  321. ),
  322. $LNG['type_planet_short'][$this->_fleet['fleet_end_type']],
  323. $LNG['sys_lost'],
  324. $attackClass,
  325. $LNG['sys_attack_attacker_pos'],
  326. pretty_number($combatResult['unitLost']['attacker']),
  327. $defendClass,
  328. $LNG['sys_attack_defender_pos'],
  329. pretty_number($combatResult['unitLost']['defender']),
  330. $LNG['sys_gain'],
  331. $LNG['tech'][901],
  332. pretty_number($stealResource[901]),
  333. $LNG['tech'][902],
  334. pretty_number($stealResource[902]),
  335. $LNG['tech'][903],
  336. pretty_number($stealResource[903]),
  337. $LNG['sys_debris'],
  338. $LNG['tech'][901],
  339. pretty_number($debris[901]),
  340. $LNG['tech'][902],
  341. pretty_number($debris[902])
  342. );
  343. SendSimpleMessage($userID, 0, $this->_fleet['fleet_start_time'], 3, $LNG['sys_mess_tower'], $LNG['sys_mess_attack_report'], $message);
  344. $sqlQuery .= "INSERT INTO ".TOPKB_USERS." SET ";
  345. $sqlQuery .= "rid = '".$raportID."', ";
  346. $sqlQuery .= "role = 1, ";
  347. $sqlQuery .= "username = '".$GLOBALS['DATABASE']->escape($userName)."', ";
  348. $sqlQuery .= "uid = ".$userID.";";
  349. }
  350. foreach($userDefend as $userID => $userName)
  351. {
  352. $LNG = $this->getLanguage(NULL, $userID);
  353. $message = sprintf($messageHTML,
  354. $raportID,
  355. $defendClass,
  356. $LNG['sys_mess_attack_report'],
  357. sprintf(
  358. $LNG['sys_adress_planet'],
  359. $this->_fleet['fleet_end_galaxy'],
  360. $this->_fleet['fleet_end_system'],
  361. $this->_fleet['fleet_end_planet']
  362. ),
  363. $LNG['type_planet_short'][$this->_fleet['fleet_end_type']],
  364. $LNG['sys_lost'],
  365. $defendClass,
  366. $LNG['sys_attack_attacker_pos'],
  367. pretty_number($combatResult['unitLost']['attacker']),
  368. $attackClass,
  369. $LNG['sys_attack_defender_pos'],
  370. pretty_number($combatResult['unitLost']['defender']),
  371. $LNG['sys_gain'],
  372. $LNG['tech'][901],
  373. pretty_number($stealResource[901]),
  374. $LNG['tech'][902],
  375. pretty_number($stealResource[902]),
  376. $LNG['tech'][903],
  377. pretty_number($stealResource[903]),
  378. $LNG['sys_debris'],
  379. $LNG['tech'][901],
  380. pretty_number($debris[901]),
  381. $LNG['tech'][902],
  382. pretty_number($debris[902])
  383. );
  384. SendSimpleMessage($userID, 0, $this->_fleet['fleet_start_time'], 3, $LNG['sys_mess_tower'], $LNG['sys_mess_attack_report'], $message);
  385. $sqlQuery .= "INSERT INTO ".TOPKB_USERS." SET ";
  386. $sqlQuery .= "rid = '".$raportID."', ";
  387. $sqlQuery .= "role = 2, ";
  388. $sqlQuery .= "username = '".$GLOBALS['DATABASE']->escape($userName)."', ";
  389. $sqlQuery .= "uid = ".$userID.";";
  390. }
  391. if($this->_fleet['fleet_end_type'] == 3)
  392. {
  393. $debrisType = 'id_luna';
  394. }
  395. else
  396. {
  397. $debrisType = 'id';
  398. }
  399. $sqlQuery .= "UPDATE ".PLANETS." SET
  400. der_metal = ".$planetDebris[901].",
  401. der_crystal = ".$planetDebris[902]."
  402. WHERE
  403. ".$debrisType." = ".$this->_fleet['fleet_end_id'].";
  404. UPDATE ".PLANETS." SET
  405. metal = metal - ".$stealResource[901].",
  406. crystal = crystal - ".$stealResource[902].",
  407. deuterium = deuterium - ".$stealResource[903]."
  408. WHERE
  409. id = ".$this->_fleet['fleet_end_id'].";
  410. INSERT INTO ".TOPKB." SET
  411. units = ".($combatResult['unitLost']['attacker'] + $combatResult['unitLost']['defender']).",
  412. rid = '".$raportID."',
  413. time = ".$this->_fleet['fleet_start_time'].",
  414. universe = ".$this->_fleet['fleet_universe'].",
  415. result = '".$combatResult['won'] ."';
  416. UPDATE ".USERS." SET
  417. ".$attackStatus." = ".$attackStatus." + 1,
  418. kbmetal = kbmetal + ".$debris[901].",
  419. kbcrystal = kbcrystal + ".$debris[902].",
  420. lostunits = lostunits + ".$combatResult['unitLost']['attacker'].",
  421. desunits = desunits + ".$combatResult['unitLost']['defender']."
  422. WHERE
  423. id IN (".implode(',', array_keys($userAttack)).");
  424. UPDATE ".USERS." SET
  425. ".$defendStatus." = ".$defendStatus." + 1,
  426. kbmetal = kbmetal + ".$debris[901].",
  427. kbcrystal = kbcrystal + ".$debris[902].",
  428. lostunits = lostunits + ".$combatResult['unitLost']['attacker'].",
  429. desunits = desunits + ".$combatResult['unitLost']['defender']."
  430. WHERE
  431. id IN (".implode(',', array_keys($userDefend)).");";
  432. $GLOBALS['DATABASE']->multi_query($sqlQuery);
  433. $this->setState(FLEET_RETURN);
  434. $this->SaveFleet();
  435. }
  436. function EndStayEvent()
  437. {
  438. return;
  439. }
  440. function ReturnEvent()
  441. {
  442. $LNG = $this->getLanguage(NULL, $this->_fleet['fleet_owner']);
  443. $TargetName = $GLOBALS['DATABASE']->getFirstCell("SELECT name FROM ".PLANETS." WHERE id = ".$this->_fleet['fleet_start_id'].";");
  444. $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]);
  445. SendSimpleMessage($this->_fleet['fleet_owner'], 0, $this->_fleet['fleet_end_time'], 3, $LNG['sys_mess_tower'], $LNG['sys_mess_fleetback'], $Message);
  446. $this->RestoreFleet();
  447. }
  448. }