PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/pages/class.ShowBuildingsPage.php

http://2moons.googlecode.com/
PHP | 343 lines | 254 code | 62 blank | 27 comment | 86 complexity | a8326f843601c4e13819af5a132bb154 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) 2011 Slaver
  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 Slaver <slaver7@gmail.com>
  21. * @copyright 2009 Lucky <lucky@xgproyect.net> (XGProyecto)
  22. * @copyright 2011 Slaver <slaver7@gmail.com> (Fork/2Moons)
  23. * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
  24. * @version 1.6.1 (2011-11-19)
  25. * @info $Id: class.ShowBuildingsPage.php 2105 2011-12-30 17:09:19Z slaver7 $
  26. * @link http://code.google.com/p/2moons/
  27. */
  28. class ShowBuildingsPage
  29. {
  30. public function CancelBuildingFromQueue($PlanetRess)
  31. {
  32. global $PLANET, $USER, $resource;
  33. $CurrentQueue = unserialize($PLANET['b_building_id']);
  34. if (empty($CurrentQueue))
  35. {
  36. $PLANET['b_building_id'] = '';
  37. $PLANET['b_building'] = 0;
  38. return;
  39. }
  40. $Element = $CurrentQueue[0][0];
  41. $BuildMode = $CurrentQueue[0][4];
  42. $costRessources = BuildFunctions::getElementPrice($USER, $PLANET, $Element, $BuildMode == 'destroy');
  43. if(isset($costRessources[901])) { $PLANET[$resource[901]] += $costRessources[901]; }
  44. if(isset($costRessources[902])) { $PLANET[$resource[902]] += $costRessources[902]; }
  45. if(isset($costRessources[903])) { $PLANET[$resource[903]] += $costRessources[903]; }
  46. if(isset($costRessources[921])) { $USER[$resource[921]] += $costRessources[921]; }
  47. array_shift($CurrentQueue);
  48. if (count($CurrentQueue) == 0) {
  49. $PLANET['b_building'] = 0;
  50. $PLANET['b_building_id'] = '';
  51. } else {
  52. $BuildEndTime = TIMESTAMP;
  53. $NewQueueArray = array();
  54. foreach($CurrentQueue as $ListIDArray) {
  55. if($Element == $ListIDArray[0])
  56. continue;
  57. $BuildEndTime += BuildFunctions::getBuildingTime($USER, $PLANET, $ListIDArray[0], NULL, $ListIDArray[4] == 'destroy');
  58. $ListIDArray[3] = $BuildEndTime;
  59. $NewQueueArray[] = $ListIDArray;
  60. }
  61. if(!empty($NewQueueArray)) {
  62. $PLANET['b_building'] = TIMESTAMP;
  63. $PLANET['b_building_id'] = serialize($NewQueueArray);
  64. $PlanetRess->USER = $USER;
  65. $PlanetRess->PLANET = $PLANET;
  66. $PlanetRess->SetNextQueueElementOnTop();
  67. $USER = $PlanetRess->USER;
  68. $PLANET = $PlanetRess->PLANET;
  69. } else {
  70. $PLANET['b_building'] = 0;
  71. $PLANET['b_building_id'] = '';
  72. FirePHP::getInstance(true)->log("Queue(Build): ".$PLANET['b_building_id']);
  73. }
  74. }
  75. }
  76. public function RemoveBuildingFromQueue($QueueID, $PlanetRess)
  77. {
  78. global $USER, $PLANET;
  79. if ($QueueID <= 1 || empty($PLANET['b_building_id']))
  80. return;
  81. $CurrentQueue = unserialize($PLANET['b_building_id']);
  82. $ActualCount = count($CurrentQueue);
  83. if($ActualCount <= 1)
  84. return $this->CancelBuildingFromQueue($PlanetRess);
  85. $Element = $CurrentQueue[$QueueID - 2][0];
  86. $BuildEndTime = $CurrentQueue[$QueueID - 2][3];
  87. unset($CurrentQueue[$QueueID - 1]);
  88. $NewQueueArray = array();
  89. foreach($CurrentQueue as $ID => $ListIDArray)
  90. {
  91. if ($ID < $QueueID - 1) {
  92. $NewQueueArray[] = $ListIDArray;
  93. } else {
  94. if($Element == $ListIDArray[0] || empty($ListIDArray[0]))
  95. continue;
  96. $BuildEndTime += BuildFunctions::getBuildingTime($USER, $CPLANET, $ListIDArray[0]);
  97. $ListIDArray[3] = $BuildEndTime;
  98. $NewQueueArray[] = $ListIDArray;
  99. }
  100. }
  101. if(!empty($NewQueueArray))
  102. $PLANET['b_building_id'] = serialize($NewQueueArray);
  103. else
  104. $PLANET['b_building_id'] = "";
  105. FirePHP::getInstance(true)->log("Queue(Build): ".$PLANET['b_building_id']);
  106. }
  107. public function AddBuildingToQueue($Element, $AddMode = true)
  108. {
  109. global $PLANET, $USER, $resource, $CONF, $reslist, $pricelist;
  110. if(!in_array($Element, $reslist['allow'][$PLANET['planet_type']])
  111. || !BuildFunctions::isTechnologieAccessible($USER, $PLANET, $Element)
  112. || ($Element == 31 && $USER["b_tech_planet"] != 0)
  113. || (($Element == 15 || $Element == 21) && !empty($PLANET['b_hangar_id']))
  114. || (!$AddMode && $PLANET[$resource[$Element]] == 0)
  115. )
  116. return;
  117. $CurrentQueue = unserialize($PLANET['b_building_id']);
  118. if (!empty($CurrentQueue)) {
  119. $ActualCount = count($CurrentQueue);
  120. } else {
  121. $CurrentQueue = array();
  122. $ActualCount = 0;
  123. }
  124. $CurrentMaxFields = CalculateMaxPlanetFields($PLANET);
  125. if (($CONF['max_elements_build'] != 0 && $ActualCount == $CONF['max_elements_build']) || ($AddMode && $PLANET["field_current"] >= ($CurrentMaxFields - $ActualCount)))
  126. return;
  127. $BuildMode = $AddMode ? 'build' : 'destroy';
  128. $BuildLevel = $PLANET[$resource[$Element]] + (int) $AddMode;
  129. if($ActualCount == 0)
  130. {
  131. if($pricelist[$Element]['max'] < $BuildLevel)
  132. return;
  133. $costRessources = BuildFunctions::getElementPrice($USER, $PLANET, $Element, !$AddMode);
  134. if(!BuildFunctions::isElementBuyable($USER, $PLANET, $Element, $costRessources))
  135. return;
  136. if(isset($costRessources[901])) { $PLANET[$resource[901]] -= $costRessources[901]; }
  137. if(isset($costRessources[902])) { $PLANET[$resource[902]] -= $costRessources[902]; }
  138. if(isset($costRessources[903])) { $PLANET[$resource[903]] -= $costRessources[903]; }
  139. if(isset($costRessources[921])) { $USER[$resource[921]] -= $costRessources[921]; }
  140. $elementTime = BuildFunctions::getBuildingTime($USER, $PLANET, $Element, $costRessources);
  141. $BuildEndTime = TIMESTAMP + $elementTime;
  142. $PLANET['b_building_id'] = serialize(array(array($Element, $BuildLevel, $elementTime, $BuildEndTime, $BuildMode)));
  143. $PLANET['b_building'] = $BuildEndTime;
  144. } else {
  145. $addLevel = 0;
  146. foreach($CurrentQueue as $QueueSubArray)
  147. {
  148. if($QueueSubArray[0] != $Element)
  149. continue;
  150. if($QueueSubArray[4] == 'build')
  151. $addLevel++;
  152. else
  153. $addLevel--;
  154. }
  155. $BuildLevel += $addLevel;
  156. if(!$AddMode && $BuildLevel == 0)
  157. return;
  158. if($pricelist[$Element]['max'] < $BuildLevel)
  159. return;
  160. $elementTime = BuildFunctions::getBuildingTime($USER, $PLANET, $Element, NULL, !$AddMode, $BuildLevel);
  161. $BuildEndTime = $CurrentQueue[$ActualCount - 1][3] + $elementTime;
  162. $CurrentQueue[] = array($Element, $BuildLevel, $elementTime, $BuildEndTime, $BuildMode);
  163. $PLANET['b_building_id'] = serialize($CurrentQueue);
  164. }
  165. FirePHP::getInstance(true)->log("Queue(Build): ".$PLANET['b_building_id']);
  166. }
  167. public function getQueueData()
  168. {
  169. global $LNG, $CONF, $PLANET, $USER;
  170. $scriptData = array();
  171. $buildLevels = array();
  172. if ($PLANET['b_building'] == 0 || $PLANET['b_building_id'] == "")
  173. return array();
  174. $buildQueue = unserialize($PLANET['b_building_id']);
  175. foreach($buildQueue as $BuildArray) {
  176. if ($BuildArray[3] < TIMESTAMP)
  177. continue;
  178. $scriptData[] = array(
  179. 'element' => $BuildArray[0],
  180. 'level' => $BuildArray[1],
  181. 'time' => $BuildArray[2],
  182. 'resttime' => ($BuildArray[3] - TIMESTAMP),
  183. 'destroy' => ($BuildArray[4] == 'destroy'),
  184. 'endtime' => $BuildArray[3]
  185. );
  186. }
  187. return $scriptData;
  188. }
  189. public function show()
  190. {
  191. global $ProdGrid, $LNG, $resource, $reslist, $CONF, $db, $PLANET, $USER, $pricelist;
  192. $TheCommand = request_var('cmd', '');
  193. $PlanetRess = new ResourceUpdate();
  194. $PlanetRess->CalcResource();
  195. // wellformed buildURLs
  196. if(!empty($TheCommand) && $_SERVER['REQUEST_METHOD'] === 'POST' && $USER['urlaubs_modus'] == 0)
  197. {
  198. $Element = request_var('building', 0);
  199. $ListID = request_var('listid', 0);
  200. switch($TheCommand)
  201. {
  202. case 'cancel':
  203. $this->CancelBuildingFromQueue($PlanetRess);
  204. break;
  205. case 'remove':
  206. $this->RemoveBuildingFromQueue($ListID, $PlanetRess);
  207. break;
  208. case 'insert':
  209. $this->AddBuildingToQueue($Element, true);
  210. break;
  211. case 'destroy':
  212. $this->AddBuildingToQueue($Element, false);
  213. break;
  214. }
  215. }
  216. $PlanetRess->SavePlanetToDB();
  217. $Queue = $this->getQueueData();
  218. $QueueCount = count($Queue);
  219. $CanBuildElement = isVacationMode($USER) || $CONF['max_elements_build'] == 0 || $QueueCount < $CONF['max_elements_build'];
  220. $CurrentMaxFields = CalculateMaxPlanetFields($PLANET);
  221. $RoomIsOk = $PLANET['field_current'] < ($CurrentMaxFields - $QueueCount);
  222. $BuildEnergy = $USER[$resource[113]];
  223. $BuildLevelFactor = 10;
  224. $BuildTemp = $PLANET['temp_max'];
  225. $Elements = $reslist['allow'][$PLANET['planet_type']];
  226. foreach($Elements as $ID => $Element)
  227. {
  228. if (!BuildFunctions::isTechnologieAccessible($USER, $PLANET, $Element))
  229. continue;
  230. $infoEnergy = "";
  231. if(in_array($Element, $reslist['prod']))
  232. {
  233. $BuildLevel = $PLANET[$resource[$Element]];
  234. $Need = round(eval($ProdGrid[$Element][911]) * $CONF['resource_multiplier']);
  235. if($Need > 0)
  236. $Need = $Need * $USER['factor']['ressource'][911];
  237. $BuildLevel += 1;
  238. $Prod = round(eval($ProdGrid[$Element][911]) * $CONF['resource_multiplier']);
  239. if($Prod > 0)
  240. $Need = $Need * $USER['factor']['ressource'][911];
  241. $requireEnergy = $Prod - $Need;
  242. if($requireEnergy < 0) {
  243. $infoEnergy = sprintf($LNG['bd_need_engine'], pretty_number(abs($requireEnergy)), $LNG['tech'][911]);
  244. } else {
  245. $infoEnergy = sprintf($LNG['bd_more_engine'], pretty_number(abs($requireEnergy)), $LNG['tech'][911]);
  246. }
  247. }
  248. $costRessources = BuildFunctions::getElementPrice($USER, $PLANET, $Element);
  249. $costOverflow = BuildFunctions::getRestPrice($USER, $PLANET, $Element, $costRessources);
  250. $elementTime = BuildFunctions::getBuildingTime($USER, $PLANET, $Element, $costRessources);
  251. $destroyRessources = BuildFunctions::getElementPrice($USER, $PLANET, $Element, true);
  252. $destroyTime = BuildFunctions::getBuildingTime($USER, $PLANET, $Element, $destroyRessources);
  253. $buyable = $QueueCount != 0 || BuildFunctions::isElementBuyable($USER, $PLANET, $Element, $costRessources);
  254. $BuildInfoList[$Element] = array(
  255. 'level' => $PLANET[$resource[$Element]],
  256. 'maxLevel' => $pricelist[$Element]['max'],
  257. 'infoEnergy' => $infoEnergy,
  258. 'costRessources' => $costRessources,
  259. 'costOverflow' => $costOverflow,
  260. 'elementTime' => $elementTime,
  261. 'destroyRessources' => $destroyRessources,
  262. 'destroyTime' => $destroyTime,
  263. 'buyable' => $buyable,
  264. );
  265. }
  266. $template = new template();
  267. if ($QueueCount != 0) {
  268. $template->loadscript('buildlist.js');
  269. }
  270. $template->assign_vars(array(
  271. 'BuildInfoList' => $BuildInfoList,
  272. 'CanBuildElement' => $CanBuildElement,
  273. 'RoomIsOk' => $RoomIsOk,
  274. 'Queue' => $Queue,
  275. 'isBusy' => array('shipyard' => !empty($PLANET['b_hangar_id']), 'research' => $USER['b_tech_planet'] != 0),
  276. 'HaveMissiles' => (bool) $PLANET[$resource[503]] + $PLANET[$resource[502]],
  277. ));
  278. $template->show("buildings_overview.tpl");
  279. }
  280. }
  281. ?>