PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/classes/missions/MissionCaseSpy.class.php

https://bitbucket.org/VolCh/2moons
PHP | 235 lines | 163 code | 46 blank | 26 comment | 14 complexity | 67ab7a361f953f47058aefc349a738e4 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.2 (2013-03-18)
  24. * @info $Id$
  25. * @link http://2moons.cc/
  26. */
  27. class MissionCaseSpy extends MissionFunctions implements Mission
  28. {
  29. function __construct($Fleet)
  30. {
  31. $this->_fleet = $Fleet;
  32. }
  33. function TargetEvent()
  34. {
  35. global $pricelist, $reslist, $resource;
  36. $db = Database::get();
  37. $sql = 'SELECT * FROM %%USERS%% WHERE id = :userId;';
  38. $senderUser = $db->selectSingle($sql, array(
  39. ':userId' => $this->_fleet['fleet_owner']
  40. ));
  41. $targetUser = $db->selectSingle($sql, array(
  42. ':userId' => $this->_fleet['fleet_target_owner']
  43. ));
  44. $sql = 'SELECT * FROM %%PLANETS%% WHERE id = :planetId;';
  45. $targetPlanet = $db->selectSingle($sql, array(
  46. ':planetId' => $this->_fleet['fleet_end_id']
  47. ));
  48. $sql = 'SELECT name FROM %%PLANETS%% WHERE id = :planetId;';
  49. $senderPlanetName = $db->selectSingle($sql, array(
  50. ':planetId' => $this->_fleet['fleet_start_id']
  51. ), 'name');
  52. $LNG = $this->getLanguage($senderUser['lang']);
  53. $senderUser['factor'] = getFactors($senderUser, 'basic', $this->_fleet['fleet_start_time']);
  54. $targetUser['factor'] = getFactors($targetUser, 'basic', $this->_fleet['fleet_start_time']);
  55. $planetUpdater = new ResourceUpdate();
  56. list($targetUser, $targetPlanet) = $planetUpdater->CalcResource($targetUser, $targetPlanet, true, $this->_fleet['fleet_start_time']);
  57. $sql = 'SELECT * FROM %%FLEETS%% WHERE fleet_end_id = :planetId AND fleet_mission = 5 AND fleet_end_stay > :time;';
  58. $targetStayFleets = $db->select($sql, array(
  59. ':planetId' => $this->_fleet['fleet_end_id'],
  60. ':time' => $this->_fleet['fleet_start_time'],
  61. ));
  62. foreach($targetStayFleets as $fleetRow)
  63. {
  64. $fleetData = FleetFunctions::unserialize($fleetRow['fleet_array']);
  65. foreach($fleetData as $shipId => $shipAmount)
  66. {
  67. $targetPlanet[$resource[$shipId]] += $shipAmount;
  68. }
  69. }
  70. $fleetAmount = $this->_fleet['fleet_amount'] * (1 + $senderUser['factor']['SpyPower']);
  71. $senderSpyTech = max($senderUser['spy_tech'], 1);
  72. $targetSpyTech = max($targetUser['spy_tech'], 1);
  73. $techDifference = abs($senderSpyTech - $targetSpyTech);
  74. $MinAmount = ($senderSpyTech > $targetSpyTech ? -1 : 1) * pow($techDifference * SPY_DIFFENCE_FACTOR, 2);
  75. $SpyFleet = $fleetAmount >= $MinAmount;
  76. $SpyDef = $fleetAmount >= $MinAmount + 1 * SPY_VIEW_FACTOR;
  77. $SpyBuild = $fleetAmount >= $MinAmount + 3 * SPY_VIEW_FACTOR;
  78. $SpyTechno = $fleetAmount >= $MinAmount + 5 * SPY_VIEW_FACTOR;
  79. $classIDs[900] = array_merge($reslist['resstype'][1], $reslist['resstype'][2]);
  80. if($SpyFleet)
  81. {
  82. $classIDs[200] = $reslist['fleet'];
  83. }
  84. if($SpyDef)
  85. {
  86. $classIDs[400] = array_merge($reslist['defense'], $reslist['missile']);
  87. }
  88. if($SpyBuild)
  89. {
  90. $classIDs[0] = $reslist['build'];
  91. }
  92. if($SpyTechno)
  93. {
  94. $classIDs[100] = $reslist['tech'];
  95. }
  96. $targetChance = mt_rand(0, min(($fleetAmount/4) * ($targetSpyTech / $senderSpyTech), 100));
  97. $spyChance = mt_rand(0, 100);
  98. $spyData = array();
  99. foreach($classIDs as $classID => $elementIDs)
  100. {
  101. foreach($elementIDs as $elementID)
  102. {
  103. if(isset($targetUser[$resource[$elementID]]))
  104. {
  105. $spyData[$classID][$elementID] = $targetUser[$resource[$elementID]];
  106. }
  107. else
  108. {
  109. $spyData[$classID][$elementID] = $targetPlanet[$resource[$elementID]];
  110. }
  111. }
  112. if($senderUser['spyMessagesMode'] == 1)
  113. {
  114. $spyData[$classID] = array_filter($spyData[$classID]);
  115. }
  116. }
  117. // I'm use template class here, because i want to exclude HTML in PHP.
  118. require_once 'includes/classes/class.template.php';
  119. $template = new template;
  120. $template->caching = true;
  121. $template->compile_id = $senderUser['lang'];
  122. $template->loadFilter('output', 'trimwhitespace');
  123. list($tplDir) = $template->getTemplateDir();
  124. $template->setTemplateDir($tplDir.'game/');
  125. $template->assign_vars(array(
  126. 'spyData' => $spyData,
  127. 'targetPlanet' => $targetPlanet,
  128. 'targetChance' => $targetChance,
  129. 'spyChance' => $spyChance,
  130. 'isBattleSim' => ENABLE_SIMULATOR_LINK == true && isModulAvalible(MODULE_SIMULATOR),
  131. 'title' => sprintf($LNG['sys_mess_head'], $targetPlanet['name'], $targetPlanet['galaxy'], $targetPlanet['system'], $targetPlanet['planet'], _date($LNG['php_tdformat'], $this->_fleet['fleet_end_time'], $targetUser['timezone'], $LNG)),
  132. ));
  133. $template->assign_vars(array(
  134. 'LNG' => $LNG
  135. ), false);
  136. $spyReport = $template->fetch('shared.mission.spyReport.tpl');
  137. PlayerUtil::sendMessage($this->_fleet['fleet_owner'], 0, $LNG['sys_mess_qg'], 0, $LNG['sys_mess_spy_report'],
  138. $spyReport, $this->_fleet['fleet_start_time'], NULL, 1, $this->_fleet['fleet_universe']);
  139. $LNG = $this->getLanguage($targetUser['lang']);
  140. $targetMessage = $LNG['sys_mess_spy_ennemyfleet'] ." ". $senderPlanetName;
  141. if($this->_fleet['fleet_start_type'] == 3)
  142. {
  143. $targetMessage .= $LNG['sys_mess_spy_report_moon'].' ';
  144. }
  145. $text = '<a href="game.php?page=galaxy&amp;galaxy=%1$s&amp;system=%2$s">[%1$s:%2$s:%3$s]</a> %7$s
  146. %8$s <a href="game.php?page=galaxy&amp;galaxy=%4$s&amp;system=%5$s">[%4$s:%5$s:%6$s]</a> %9$s';
  147. $targetMessage .= sprintf($text,
  148. $this->_fleet['fleet_start_galaxy'],
  149. $this->_fleet['fleet_start_system'],
  150. $this->_fleet['fleet_start_planet'],
  151. $this->_fleet['fleet_end_galaxy'],
  152. $this->_fleet['fleet_end_system'],
  153. $this->_fleet['fleet_end_planet'],
  154. $LNG['sys_mess_spy_seen_at'],
  155. $targetPlanet['name'],
  156. $LNG['sys_mess_spy_seen_at2']
  157. );
  158. PlayerUtil::sendMessage($this->_fleet['fleet_target_owner'], 0, $LNG['sys_mess_spy_control'], 0,
  159. $LNG['sys_mess_spy_activity'], $targetMessage, $this->_fleet['fleet_start_time'], NULL, 1, $this->_fleet['fleet_universe']);
  160. if ($targetChance >= $spyChance)
  161. {
  162. $config = Config::get($this->_fleet['fleet_universe']);
  163. $whereCol = $this->_fleet['fleet_end_type'] == 3 ? "id_luna" : "id";
  164. $sql = 'UPDATE %%PLANETS%% SET
  165. der_metal = der_metal + :metal,
  166. der_crystal = der_crystal + :crystal
  167. WHERE '.$whereCol.' = :planetId;';
  168. $db->update($sql, array(
  169. ':metal' => $fleetAmount * $pricelist[210]['cost'][901] * $config->Fleet_Cdr / 100,
  170. ':crystal' => $fleetAmount * $pricelist[210]['cost'][902] * $config->Fleet_Cdr / 100,
  171. ':planetId' => $this->_fleet['fleet_end_id']
  172. ));
  173. $this->KillFleet();
  174. }
  175. else
  176. {
  177. $this->setState(FLEET_RETURN);
  178. $this->SaveFleet();
  179. }
  180. }
  181. function EndStayEvent()
  182. {
  183. return;
  184. }
  185. function ReturnEvent()
  186. {
  187. $this->RestoreFleet();
  188. }
  189. }