PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/game-core/src/game/lib/data/planet/Planet.class.old.php

https://github.com/Biggerskimo/WOT-Game
PHP | 444 lines | 236 code | 89 blank | 119 comment | 76 complexity | 34a8b0084240936d50eb188871bac4c1 MD5 | raw file
  1. <?php
  2. /*
  3. This file is part of WOT Game.
  4. WOT Game is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. WOT Game is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with WOT Game. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. // wcf
  16. require_once(WCF_DIR.'lib/data/user/User.class.php');
  17. require_once(WCF_DIR.'lib/data/DatabaseObject.class.php');
  18. require_once(LW_DIR.'lib/data/user/LWUser.class.php');
  19. /**
  20. * Holds all planet-specific functions
  21. */
  22. class Planet extends DatabaseObject {
  23. protected $update = false;
  24. public $planetID = null;
  25. public static $planets = array();
  26. const STANDARD_CLASS = 'Debris';
  27. /**
  28. * Reads the needed class from the database and returns the planet object
  29. *
  30. * @param int planet id
  31. * @param array planet row
  32. * @param bool update last activity
  33. *
  34. * @return Planet planet
  35. */
  36. public static function getInstance($planetID = null, $row = null, $updateLastActivity = true) {
  37. if($planetID !== null) $planetID = intval($planetID);
  38. if(isset(self::$planets[$planetID])) $planet = self::$planets[$planetID];
  39. else if(isset(self::$planets[$row['id']])) $planet = self::$planets[$row['id']];
  40. else if($planetID === null) {
  41. $planetID = $row['id'];
  42. $className = $row['className'];
  43. if(!$row) $className = self::STANDARD_CLASS;
  44. require_once(LW_DIR.'lib/data/planet/'.$className.'.class.php');
  45. $planet = new $className($planetID, $row);
  46. } else {
  47. $sql = "SELECT *
  48. FROM ugml".LW_N."_planets
  49. WHERE id = ".$planetID;
  50. $row = WCF::getDB()->getFirstRow($sql);
  51. $className = $row['className'];
  52. if(!$row) $className = self::STANDARD_CLASS;
  53. require_once(LW_DIR.'lib/data/planet/'.$className.'.class.php');
  54. $planet = new $className($planetID, $row);
  55. }
  56. // workaround for Orbit [::] Bug
  57. if(!is_numeric($planetID)) return $planet;
  58. // update resources and activity
  59. if(class_exists('LWEventHandler') && $updateLastActivity) {
  60. if($planet->last_update != LWEventHandler::getTime()) {
  61. $planet->calculateResources(LWEventHandler::getTime());
  62. //if($updateLastActivity) $planet->updateLastActivity();
  63. }
  64. } else if($updateLastActivity) {
  65. //$planet->calculateResources(TIME_NOW);
  66. }
  67. self::$planets[$planetID] = $planet;
  68. return $planet;
  69. }
  70. /**
  71. * Cleans the cache
  72. */
  73. public static function clean() {
  74. self::$planets = array();
  75. }
  76. /**
  77. * Updates the activity timestamp
  78. */
  79. public function updateLastActivity() {
  80. if(!class_exists('LWEventHandler') || !is_numeric($this->planetID)) return;
  81. $sql = "UPDATE ugml_planets
  82. SET last_update = ".LWEventHandler::getTime()."
  83. WHERE id = ".$this->planetID;
  84. WCF::getDB()->registerShutdownUpdate($sql);
  85. }
  86. /**
  87. * Returns the buildings which can be built on this planet
  88. *
  89. * @return array buildings
  90. */
  91. public function getBuildableBuildings() {
  92. return array();
  93. }
  94. /**
  95. * Returns if there is free place on the planet
  96. *
  97. * @return bool free fields
  98. */
  99. public function checkFields() {
  100. $fields = $this->getMaxFields();
  101. $usedFields = $this->getUsedFields();
  102. if($usedFields < $fields) return true;
  103. else return false;
  104. }
  105. /**
  106. * Returns the fields which can be built
  107. *
  108. * @return int fields
  109. */
  110. public function getMaxFields() {
  111. $fields = floor(pow(($this->diameter / 1000), 2));
  112. $fields += ($this->terraformer * 5);
  113. return $fields;
  114. }
  115. /**
  116. * Returns the used fields
  117. */
  118. public function getUsedFields() {
  119. global $resource;
  120. $buildableBuildings = $this->getBuildableBuildings();
  121. $usedFields = 0;
  122. foreach($buildableBuildings as $buildingID) $usedFields += $this->{$resource[$buildingID]};
  123. return $usedFields;
  124. }
  125. /**
  126. * Recalculates the production
  127. *
  128. * @return bool production changed
  129. */
  130. public function calculateProduction($sqlUpdate = true) {
  131. global $game_config, $planetrow, $production, $resource;
  132. $planetrow = array_merge((array)$planetrow, (array)$this, $this->data);
  133. $buildableBuildings = $this->getBuildableBuildings();
  134. $oldMetalPerHour = $this->metal_perhour;
  135. $oldCrystalPerHour = $this->crystal_perhour;
  136. $oldDeuteriumPerHour = $this->deuterium_perhour;
  137. $oldMaxEnergy = $this->energy_max;
  138. $oldUsedEnergy = $this->energy_used;
  139. $this->metal_perhour = $this->crystal_perhour = $this->deuterium_perhour = $this->energy_max = $this->energy_used = 0;
  140. if(!WCF::getUser()->urlaubs_modus) {
  141. foreach($buildableBuildings as $buildingID) {
  142. if(isset($production[$buildingID]["formular"]["metal"])) {
  143. @$metal = floor(eval($production[$buildingID]["formular"]["metal"]) * $game_config['resource_multiplier']);
  144. @$crystal = floor(eval($production[$buildingID]["formular"]["crystal"]) * $game_config['resource_multiplier']);
  145. @$deuterium = floor(eval($production[$buildingID]["formular"]["deuterium"]) * $game_config['resource_multiplier']);
  146. @$energy = floor(eval($production[$buildingID]["formular"]["energy"]) * $game_config['resource_multiplier']);
  147. if($metal != 0) $this->metal_perhour += $metal;
  148. if($crystal != 0) $this->crystal_perhour += $crystal;
  149. if($deuterium != 0) $this->deuterium_perhour += $deuterium;
  150. if($energy > 0) $this->energy_max += $energy;
  151. else $this->energy_used -= $energy;
  152. }
  153. }
  154. // satellites
  155. $this->energy_max += floor(eval($production[212]["formular"]["energy"])* $game_config['resource_multiplier']);
  156. }
  157. // update old ugamela vars
  158. $planetrow['metal_perhour'] = $this->metal_perhour;
  159. $planetrow['crystal_perhour'] = $this->crystal_perhour;
  160. $planetrow['deuterium_perhour'] = $this->deuterium_perhour;
  161. $planetrow['energy_used'] = $this->energy_used;
  162. $planetrow['energy_max'] = $this->energy_max;
  163. if($oldMetalPerHour == $this->metal_perhour && $oldCrystalPerHour == $this->crystal_perhour && $oldDeuteriumPerHour == $this->deuterium_perhour && $oldMaxEnergy == $this->energy_max && $oldUsedEnergy == $this->energy_used) return false;
  164. if(!$sqlUpdate || !is_numeric($this->planetID)) return true;
  165. $sql = "UPDATE ugml".LW_N."_planets
  166. SET metal_perhour = ".$this->metal_perhour.",
  167. crystal_perhour = ".$this->crystal_perhour.",
  168. deuterium_perhour = ".$this->deuterium_perhour.",
  169. energy_used = ".$this->energy_used.",
  170. energy_max = ".$this->energy_max."
  171. WHERE id = ".$this->planetID;
  172. WCF::getDB()->sendQuery($sql);
  173. return true;
  174. }
  175. /**
  176. * Calculates the produced ressources since the last update
  177. */
  178. public function calculateResources($time = null) {
  179. global $game_config, $planetrow;
  180. if($time === null) {
  181. if(class_exists('LWEventHandler')) $time = LWEventHandler::getTime();
  182. else $time = TIME_NOW;
  183. }
  184. $this->calculateProduction(false);
  185. $timeDiff = $time - $this->last_update;
  186. //if(WCF::getUser()->userID == 143) echo $timeDiff;
  187. $hangarSQL = $this->checkHangar($timeDiff);
  188. // production level
  189. @$prodLevel = min(100, (($this->energy_max / $this->energy_used) * 100)) / 100;
  190. // metal
  191. $prodMetal = $timeDiff * $this->metal_perhour / 3600 * $prodLevel;
  192. if(($this->metal + $prodMetal) > $this->metal_max && $this->metal < $this->metal_max) $this->metal = $this->metal_max;
  193. else $this->metal += $prodMetal;
  194. // crystal
  195. $prodCrystal = $timeDiff * $this->crystal_perhour / 3600 * $prodLevel;
  196. if(($this->crystal + $prodCrystal) > $this->crystal_max && $this->crystal < $this->crystal_max) $this->crystal = $this->crystal_max;
  197. else $this->crystal += $prodCrystal;
  198. // deuterium
  199. $prodDeuterium = $timeDiff * $this->deuterium_perhour / 3600 * $prodLevel;
  200. if(($this->deuterium + $prodDeuterium) > $this->deuterium_max && $this->deuterium < $this->deuterium_max) $this->deuterium = $this->deuterium_max;
  201. else $this->deuterium += $prodDeuterium;
  202. // basic income
  203. if($this->planet_type == 1) {
  204. $basicIncomeMetal = $timeDiff / 3600 * $game_config['metal_basic_income'];
  205. $basicIncomeCrystal = $timeDiff / 3600 * $game_config['crystal_basic_income'];
  206. $basicIncomeDeuterium = $timeDiff / 3600 * $game_config['deuterium_basic_income'];
  207. }
  208. //if(WCF::getUser()->userID == 143) echo ','.$prodLevel.','.$this->metal_perhour.','.$prodMetal.','.$this->metal.'<br />';
  209. $this->metal += $basicIncomeMetal;
  210. $this->crystal += $basicIncomeCrystal;
  211. $this->deuterium += $basicIncomeDeuterium;
  212. if(is_numeric($this->planetID)) {
  213. $sql = "UPDATE ugml".LW_N."_planets
  214. SET metal_perhour = ".$this->metal_perhour.",
  215. crystal_perhour = ".$this->crystal_perhour.",
  216. deuterium_perhour = ".$this->deuterium_perhour.",
  217. metal = metal + ".($prodMetal + $basicIncomeMetal).",
  218. crystal = crystal + ".($prodCrystal + $basicIncomeCrystal).",
  219. deuterium = deuterium + ".($prodDeuterium + $basicIncomeDeuterium).",
  220. energy_used = ".$this->energy_used.",
  221. last_update = ".$time.",
  222. ".$hangarSQL."
  223. energy_max = ".$this->energy_max."
  224. WHERE id = ".$this->planetID;
  225. WCF::getDB()->sendQuery($sql);
  226. /*if(WCF::getUser()->userID == 361) {
  227. echo $sql;
  228. }*/
  229. }
  230. $this->last_update = $time;
  231. // update old ugamela vars
  232. $planetrow['metal'] = $this->metal;
  233. $planetrow['crystal'] = $this->crystal;
  234. $planetrow['deuterium'] = $this->deuterium;
  235. $planetrow['last_update'] = $this->last_update;
  236. }
  237. /**
  238. * Checks the production of the hangar
  239. *
  240. * @return string sql update (set) string
  241. */
  242. protected function checkHangar($timeDiff/* = null*/) {
  243. global $game_config, $planetrow, $pricelist, $resource;
  244. $hangarSQL = "";
  245. if($this->b_hangar_id != '') {
  246. //if($timeDiff === null) $timeDiff = TIME_NOW;
  247. $timeDiff += $this->b_hangar;
  248. $newShips = array();
  249. $remainingShips = "";
  250. $stopProduction = false;
  251. /**
  252. * read list
  253. */
  254. // get individual parts
  255. $parts = explode(';', substr($this->b_hangar_id, 0, -1));
  256. foreach($parts as $part) {
  257. // get shiptype and shipcount
  258. $shipDetails = explode(',', $part);
  259. $shipTypeID = $shipDetails[0];
  260. $shipCount = $shipDetails[1];
  261. // check whether ships can be produced
  262. if($stopProduction) {
  263. $remainingShips .= $shipTypeID.','.$shipCount.';';
  264. continue;
  265. }
  266. // check time
  267. $timePerShip = (($pricelist[$shipTypeID]['metal']
  268. + $pricelist[$shipTypeID]['crystal'])
  269. / $game_config['game_speed'])
  270. * (1 / ($this->hangar + 1 ))
  271. * pow(0.5, $this->nano_factory)
  272. * 60 * 60;
  273. $maxShips = floor($timeDiff / $timePerShip);
  274. $builtShips = min($maxShips, $shipCount);
  275. // write to built ships
  276. if(isset($newShips[$shipTypeID])) $newShips[$shipTypeID] += $this->checkShipsCount($shipTypeID, $builtShips);
  277. else $newShips[$shipTypeID] = $this->checkShipsCount($shipTypeID, $builtShips);
  278. // subtract time
  279. $timeDiff -= ($builtShips * $timePerShip);
  280. if($shipCount > $builtShips) {
  281. // write remaining ships to new hangar string
  282. $remainingShips .= $shipTypeID.','.($shipCount - $builtShips).';';
  283. // update time that the ship is produced
  284. $this->b_hangar = $timeDiff;
  285. // set production flag (no ships can be produced any more)
  286. $stopProduction = true;
  287. }
  288. }
  289. /**
  290. * add ships to planet
  291. */
  292. foreach($newShips as $shipTypeID => $shipCount) {
  293. // add to actual planet
  294. $this->{$resource[$shipTypeID]} += $shipCount;
  295. $planetrow[$resource[$shipTypeID]] += $shipCount;
  296. // build sql
  297. $hangarSQL .= $resource[$shipTypeID]." = ".$resource[$shipTypeID]." + ".$shipCount.", ";
  298. }
  299. if(empty($remainingShips)) $this->b_hangar = 0;
  300. /**
  301. * handle remaining ships
  302. */
  303. // build sql
  304. $hangarSQL .= "b_hangar_id = '".$remainingShips."',
  305. b_hangar = ".$this->b_hangar.", ";
  306. // update actual planet
  307. $this->b_hangar_id = $planetrow['b_hangar_id'] = $remainingShips;
  308. $planetrow['b_hangar'] = $this->b_hangar;
  309. } else $this->b_hangar = 0;
  310. return $hangarSQL;
  311. }
  312. /**
  313. * Checks for missiles
  314. *
  315. * @param int shiptypeid
  316. * @param int shipcount
  317. * @return int corrected shipcount
  318. */
  319. protected function checkShipsCount($shipTypeID, $shipCount) {
  320. if($shipTypeID != 502 && $shipTypeID != 503) return $shipCount;
  321. $maxSlots = $this->silo * 10;
  322. $usedSlots = $this->interceptor_misil + $this->interplanetary_misil * 2;
  323. if($shipTypeID == 502) {
  324. $maxShips = $maxSlots - $usedSlots;
  325. return min($maxShips, $shipCount);
  326. } else {
  327. $maxShips = ($maxSlots - $usedSlots) / 2;
  328. return min($maxShips, $shipCount);
  329. }
  330. }
  331. public function __toString() {
  332. if($this->id_owner == WCF::getUser()->userID) {
  333. return '<a href="overview.php?cp='.$this->planetID.'" target="Mainframe">Planeten '.$this->name.'</a> <a href="galaxy.php?g='.$this->galaxy.'&amp;s='.$this->system.'" target="Mainframe">['.$this->galaxy.':'.$this->system.':'.$this->planet.']</a>';
  334. } else {
  335. return 'Planeten '.$this->name.' <a href="galaxy.php?g='.$this->galaxy.'&amp;s='.$this->system.'" target="Mainframe">['.$this->galaxy.':'.$this->system.':'.$this->planet.']</a>';
  336. }
  337. }
  338. /**
  339. * Returns the upper limit by the noob protection
  340. *
  341. * @return float limit
  342. */
  343. public function noobProtectionLimit() {
  344. // LIMIT = POINTS * ((POINTS / 2500) ^ 3 + 2)
  345. if(intval($this->id_owner)) {
  346. $owner = new LWUser($this->id_owner);
  347. return $owner->points * (pow($owner->points / 2500, 3) + 2);
  348. }
  349. return 0;
  350. }
  351. }
  352. ?>