PageRenderTime 31ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/build_decoded.php

https://github.com/minea94/DboorZ
PHP | 1682 lines | 1618 code | 42 blank | 22 comment | 510 complexity | e534ff882a0cea1154609995335272e0 MD5 | raw file
  1. <?php
  2. /*********************/
  3. /* */
  4. /* Dezend for PHP5 */
  5. /* NWS */
  6. /* Nulled.WS */
  7. /* */
  8. /*********************/
  9. require( ".".DIRECTORY_SEPARATOR."app".DIRECTORY_SEPARATOR."boot.php" );
  10. require_once( MODEL_PATH."build.php" );
  11. class GPage extends VillagePage
  12. {
  13. public $productionPane = TRUE;
  14. public $buildingView = "";
  15. public $buildingIndex = -1;
  16. public $buildProperties = NULL;
  17. public $newBuilds = NULL;
  18. public $troopsUpgrade;
  19. public $troopsUpgradeType;
  20. public $buildingTribeFactor;
  21. public $troops = array( );
  22. public $selectedTabIndex = 0;
  23. public $villageOases;
  24. public $childVillages;
  25. public $hasHero = FALSE;
  26. public $totalCpRate;
  27. public $totalCpValue;
  28. public $neededCpValue;
  29. public $childVillagesCount;
  30. public $showBuildingForm;
  31. public $embassyProperty;
  32. public $merchantProperty;
  33. public $rallyPointProperty;
  34. public $crannyProperty = array( 'buildingCount' => 0, 'totalSize' => 0 );
  35. public $warriorMessage = "";
  36. public $dataList;
  37. public $pageSize = 40;
  38. public $pageCount;
  39. public $pageIndex;
  40. public function GPage( )
  41. {
  42. parent::villagepage( );
  43. $this->viewFile = "build.phtml";
  44. $this->contentCssClass = "build";
  45. }
  46. public function onLoadBuildings( $building )
  47. {
  48. $GameMetadata = $GLOBALS['GameMetadata'];
  49. if ( $this->buildingIndex == 0 - 1 && isset( $_GET['bid'] ) && is_numeric( $_GET['bid'] ) && $_GET['bid'] == $building['item_id'] )
  50. {
  51. $this->buildingIndex = $building['index'];
  52. }
  53. if ( $building['item_id'] == 23 && 0 < $building['level'] )
  54. {
  55. ++$this->crannyProperty['buildingCount'];
  56. $this->crannyProperty['totalSize'] += $this->crannyProperty['buildingCount'];
  57. }
  58. }
  59. public function load( )
  60. {
  61. parent::load( );
  62. if ( $this->buildingIndex == 0 - 1 && isset( $_GET['id'] ) && is_numeric( $_GET['id'] ) && isset( $this->buildings[$_GET['id']] ) )
  63. {
  64. $this->buildingIndex = intval( $_GET['id'] );
  65. }
  66. $this->buildProperties = $this->getBuildingProperties( $this->buildingIndex );
  67. if ( $this->buildProperties == NULL )
  68. {
  69. $this->redirect( "village1.php" );
  70. }
  71. else if ( $this->buildProperties['emptyPlace'] )
  72. {
  73. $this->villagesLinkPostfix .= "&id=".$this->buildingIndex;
  74. $this->newBuilds = array(
  75. "available" => array( ),
  76. "soon" => array( )
  77. );
  78. foreach ( $this->gameMetadata['items'] as $item_id => $build )
  79. {
  80. if ( $item_id <= 4 || !isset( $build['for_tribe_id'][$this->tribeId] ) )
  81. {
  82. continue;
  83. }
  84. $canBuild = $this->canCreateNewBuild( $item_id );
  85. if ( $canBuild != 0 - 1 )
  86. {
  87. if ( $canBuild )
  88. {
  89. if ( !isset( $this->newBuilds['available'][$build['levels'][0]['time_consume']] ) )
  90. {
  91. $this->newBuilds['available'][$build['levels'][0]['time_consume']] = array( );
  92. }
  93. $this->newBuilds['available'][$build['levels'][0]['time_consume']][$item_id] = $build;
  94. }
  95. else
  96. {
  97. $dependencyCount = 0;
  98. foreach ( $build['pre_requests'] as $reqId => $reqValue )
  99. {
  100. if ( $reqValue != NULL )
  101. {
  102. $build['pre_requests_dependencyCount'][$reqId] = $reqValue - $this->_getMaxBuildingLevel( $reqId );
  103. $dependencyCount += $build['pre_requests_dependencyCount'][$reqId];
  104. }
  105. }
  106. if ( !isset( $this->newBuilds['soon'][$dependencyCount] ) )
  107. {
  108. $this->newBuilds['soon'][$dependencyCount] = array( );
  109. }
  110. $this->newBuilds['soon'][$dependencyCount][$item_id] = $build;
  111. }
  112. }
  113. }
  114. ksort( $this->newBuilds['available'], SORT_NUMERIC );
  115. ksort( $this->newBuilds['soon'], SORT_NUMERIC );
  116. }
  117. else
  118. {
  119. $bitemId = $this->buildProperties['building']['item_id'];
  120. $this->villagesLinkPostfix .= "&id=".$this->buildingIndex;
  121. if ( 4 < $bitemId )
  122. {
  123. $this->villagesLinkPostfix .= "&bid=".$bitemId;
  124. }
  125. $this->buildingTribeFactor = isset( $this->gameMetadata['items'][$bitemId]['for_tribe_id'][$this->data['tribe_id']] ) ? $this->gameMetadata['items'][$bitemId]['for_tribe_id'][$this->data['tribe_id']] : 1;
  126. if ( $this->buildings[$this->buildingIndex]['level'] == 0 )
  127. {
  128. return;
  129. }
  130. switch ( $bitemId )
  131. {
  132. case 12 :
  133. case 13 :
  134. $this->productionPane = FALSE;
  135. $this->buildingView = "Blacksmith_Armoury";
  136. $this->handleBlacksmithArmoury( );
  137. break;
  138. case 15 :
  139. if ( 10 <= $this->buildings[$this->buildingIndex]['level'] )
  140. {
  141. $this->buildingView = "MainBuilding";
  142. $this->handleMainBuilding( );
  143. }
  144. break;
  145. case 16 :
  146. $this->productionPane = FALSE;
  147. $this->buildingView = "RallyPoint";
  148. $this->handleRallyPoint( );
  149. break;
  150. case 17 :
  151. $this->productionPane = FALSE;
  152. $this->buildingView = "Marketplace";
  153. $this->handleMarketplace( );
  154. break;
  155. case 18 :
  156. $this->productionPane = FALSE;
  157. $this->buildingView = "Embassy";
  158. $this->handleEmbassy( );
  159. break;
  160. case 19 :
  161. case 20 :
  162. case 21 :
  163. case 29 :
  164. case 30 :
  165. case 36 :
  166. $this->_getOnlyMyTroops( );
  167. $this->productionPane = $bitemId == 36;
  168. $this->buildingView = "TroopBuilding";
  169. $this->handleTroopBuilding( );
  170. break;
  171. case 22 :
  172. $this->productionPane = FALSE;
  173. $this->buildingView = "Academy";
  174. $this->handleAcademy( );
  175. break;
  176. case 23 :
  177. $this->productionPane = TRUE;
  178. $this->buildingView = "Cranny";
  179. break;
  180. case 24 :
  181. $this->productionPane = FALSE;
  182. $this->buildingView = "TownHall";
  183. $this->handleTownHall( );
  184. break;
  185. case 25 :
  186. case 26 :
  187. $this->productionPane = FALSE;
  188. $this->buildingView = "Residence_Palace";
  189. $this->handleResidencePalace( );
  190. break;
  191. case 37 :
  192. $this->productionPane = FALSE;
  193. $this->buildingView = "HerosMansion";
  194. $this->handleHerosMansion( );
  195. break;
  196. case 40 :
  197. $this->productionPane = FALSE;
  198. break;
  199. case 42 :
  200. $this->_getOnlyMyTroops( );
  201. $this->productionPane = TRUE;
  202. $this->buildingView = "Warrior";
  203. $this->handleWarrior( );
  204. }
  205. }
  206. }
  207. public function handleBlacksmithArmoury( )
  208. {
  209. $this->troopsUpgradeType = $this->buildings[$this->buildingIndex]['item_id'] == 12 ? QS_TROOP_UPGRADE_ATTACK : QS_TROOP_UPGRADE_DEFENSE;
  210. $this->troopsUpgrade = array( );
  211. $_arr = explode( ",", $this->data['troops_training'] );
  212. $_c = 0;
  213. foreach ( $_arr as $troopStr )
  214. {
  215. ++$_c;
  216. $attack_level = explode( " ", $troopStr );
  217. $defense_level = explode( " ", $troopStr );
  218. $researches_done = explode( " ", $troopStr );
  219. $troopId = explode( " ", $troopStr );
  220. list( $troopId, $researches_done, $defense_level, $attack_level ) = $troopId;
  221. $tlevel = $this->troopsUpgradeType == QS_TROOP_UPGRADE_ATTACK ? $attack_level : $defense_level;
  222. if ( $troopId != 99 && $_c <= 8 && $tlevel < 20 && $researches_done == 1 )
  223. {
  224. $this->troopsUpgrade[$troopId] = $tlevel;
  225. }
  226. }
  227. if ( isset( $_GET['a'], $_GET['k'] ) && $_GET['k'] == $this->data['update_key'] && !isset( $this->queueModel->tasksInQueue[$this->troopsUpgradeType] ) && isset( $this->troopsUpgrade[intval( $_GET['a'] )] ) && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  228. {
  229. $troopId = intval( $_GET['a'] );
  230. $level = $this->troopsUpgrade[$troopId];
  231. $buildingMetadata = $this->gameMetadata['items'][$this->buildProperties['building']['item_id']]['troop_upgrades'][$troopId][$level];
  232. if ( !$this->isResourcesAvailable( $buildingMetadata['resources'] ) )
  233. {
  234. }
  235. else
  236. {
  237. $calcConsume = intval( $buildingMetadata['time_consume'] / $this->gameSpeed * ( 10 / ( $this->buildProperties['building']['level'] + 9 ) ) );
  238. $newTask = new QueueTask( $this->troopsUpgradeType, $this->player->playerId, $calcConsume );
  239. $newTask->villageId = $this->data['selected_village_id'];
  240. $newTask->procParams = $troopId." ".( $level + 1 );
  241. $newTask->tag = $buildingMetadata['resources'];
  242. $this->queueModel->addTask( $newTask );
  243. }
  244. }
  245. }
  246. public function handleMainBuilding( )
  247. {
  248. if ( $this->isPost( ) && isset( $_POST['drbid'] ) && 19 <= intval( $_POST['drbid'] ) && intval( $_POST['drbid'] ) <= sizeof( $this->buildings ) && isset( $this->buildings[$_POST['drbid']] ) && 0 < $this->buildings[$_POST['drbid']]['level'] && !isset( $this->queueModel->tasksInQueue[QS_BUILD_DROP] ) && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  249. {
  250. $item_id = $this->buildings[$_POST['drbid']]['item_id'];
  251. $calcConsume = intval( $this->gameMetadata['items'][$item_id]['levels'][$this->buildings[$_POST['drbid']]['level'] - 1]['time_consume'] / $this->gameSpeed * ( $this->data['time_consume_percent'] / 400 ) );
  252. $newTask = new QueueTask( QS_BUILD_DROP, $this->player->playerId, $calcConsume );
  253. $newTask->villageId = $this->data['selected_village_id'];
  254. $newTask->buildingId = $item_id;
  255. $newTask->procParams = $this->buildings[$_POST['drbid']]['index'];
  256. $this->queueModel->addTask( $newTask );
  257. }
  258. else if ( isset( $_GET['qid'] ) && is_numeric( $_GET['qid'] ) && isset( $_GET['k'] ) && $_GET['k'] == $this->data['update_key'] && isset( $_GET['d'] ) && isset( $this->queueModel->tasksInQueue[QS_BUILD_DROP] ) && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  259. {
  260. $this->queueModel->cancelTask( $this->player->playerId, intval( $_GET['qid'] ) );
  261. }
  262. }
  263. public function handleRallyPoint( )
  264. {
  265. if ( isset( $_GET['d'] ) )
  266. {
  267. $this->queueModel->cancelTask( $this->player->playerId, intval( $_GET['d'] ) );
  268. }
  269. $this->rallyPointProperty = array(
  270. "troops_in_village" => array(
  271. "troopsTable" => $this->_getTroopsList( "troops_num" ),
  272. "troopsIntrapTable" => $this->_getTroopsList( "troops_intrap_num" )
  273. ),
  274. "troops_out_village" => array(
  275. "troopsTable" => $this->_getTroopsList( "troops_out_num" ),
  276. "troopsIntrapTable" => $this->_getTroopsList( "troops_out_intrap_num" )
  277. ),
  278. "troops_in_oases" => array( ),
  279. "war_to_village" => $this->queueModel->tasksInQueue['war_troops']['to_village'],
  280. "war_from_village" => $this->queueModel->tasksInQueue['war_troops']['from_village'],
  281. "war_to_oasis" => $this->queueModel->tasksInQueue['war_troops']['to_oasis']
  282. );
  283. $village_oases_id = trim( $this->data['village_oases_id'] );
  284. if ( $village_oases_id != "" )
  285. {
  286. $m = new BuildModel( );
  287. $result = $m->getOasesDataById( $village_oases_id );
  288. while ( $result->next( ) )
  289. {
  290. $this->rallyPointProperty['troops_in_oases'][$result->row['id']] = array(
  291. "oasisRow" => $result->row,
  292. "troopsTable" => $this->_getOasisTroopsList( $result->row['troops_num'] ),
  293. "war_to" => isset( $this->rallyPointProperty['war_to_oasis'][$result->row['id']] ) ? $this->rallyPointProperty['war_to_oasis'][$result->row['id']] : NULL
  294. );
  295. }
  296. $m->dispose( );
  297. }
  298. }
  299. public function _canCancelWarTask( $taskType, $taskId )
  300. {
  301. if ( !QueueTask::iscancelabletask( $taskType ) )
  302. {
  303. return FALSE;
  304. }
  305. $timeout = QueueTask::getmaxcanceltimeout( $taskType );
  306. if ( 0 - 1 < $timeout )
  307. {
  308. $_task = NULL;
  309. foreach ( $this->queueModel->tasksInQueue[$taskType] as $t )
  310. {
  311. if ( !( $t['id'] == $taskId ) )
  312. {
  313. continue;
  314. }
  315. $_task = $t;
  316. break;
  317. break;
  318. }
  319. if ( $_task == NULL )
  320. {
  321. return FALSE;
  322. }
  323. $elapsedTime = $t['elapsedTime'];
  324. if ( $timeout < $elapsedTime )
  325. {
  326. return FALSE;
  327. }
  328. }
  329. return TRUE;
  330. }
  331. public function _getOasisTroopsList( $troops_num )
  332. {
  333. $GameMetadata = $GLOBALS['GameMetadata'];
  334. $m = new BuildModel( );
  335. $returnTroops = array( );
  336. if ( trim( $troops_num ) != "" )
  337. {
  338. $t_arr = explode( "|", $troops_num );
  339. foreach ( $t_arr as $t_str )
  340. {
  341. $t2_arr = explode( ":", $t_str );
  342. $vid = $t2_arr[0];
  343. $villageData = $m->getVillageData2ById( $vid );
  344. $returnTroops[$vid] = array( "villageData" => $villageData, "cropConsumption" => 0, "hasHero" => FALSE, "troops" => array( ) );
  345. $t2_arr = explode( ",", $t2_arr[1] );
  346. foreach ( $t2_arr as $t2_str )
  347. {
  348. $tnum = explode( " ", $t2_str );
  349. $tid = explode( " ", $t2_str );
  350. list( $tid, $tnum ) = $tid;
  351. if ( $tid == 99 )
  352. {
  353. continue;
  354. }
  355. if ( $tnum == 0 - 1 )
  356. {
  357. $tnum = 1;
  358. $returnTroops[$vid]['hasHero'] = TRUE;
  359. }
  360. else
  361. {
  362. $returnTroops[$vid]['troops'][$tid] = $tnum;
  363. }
  364. $returnTroops[$vid]['cropConsumption'] += $GameMetadata['troops'][$tid]['crop_consumption'] * $tnum;
  365. }
  366. }
  367. }
  368. $m->dispose( );
  369. return $returnTroops;
  370. }
  371. public function _getTroopsList( $key )
  372. {
  373. $GameMetadata = $GLOBALS['GameMetadata'];
  374. $m = new BuildModel( );
  375. $returnTroops = array( );
  376. if ( trim( $this->data[$key] ) != "" )
  377. {
  378. $t_arr = explode( "|", $this->data[$key] );
  379. foreach ( $t_arr as $t_str )
  380. {
  381. $t2_arr = explode( ":", $t_str );
  382. $vid = intval( $t2_arr[0] );
  383. $villageData = NULL;
  384. if ( $vid == 0 - 1 )
  385. {
  386. $vid = $this->data['selected_village_id'];
  387. $villageData = array( "id" => $vid, "village_name" => $this->data['village_name'], "player_id" => $this->player->playerId, "player_name" => buildings_p_thisvillage );
  388. }
  389. else
  390. {
  391. $villageData = $m->getVillageData2ById( $vid );
  392. }
  393. $returnTroops[$vid] = array( "villageData" => $villageData, "cropConsumption" => 0, "hasHero" => FALSE, "troops" => array( ) );
  394. if ( $vid == $this->data['selected_village_id'] )
  395. {
  396. $returnTroops[$vid]['hasHero'] = intval( $this->data['hero_in_village_id'] ) == intval( $this->data['selected_village_id'] );
  397. if ( $returnTroops[$vid]['hasHero'] )
  398. {
  399. $returnTroops[$vid]['cropConsumption'] += $GameMetadata['troops'][$this->data['hero_troop_id']]['crop_consumption'];
  400. }
  401. }
  402. $t2_arr = explode( ",", $t2_arr[1] );
  403. foreach ( $t2_arr as $t2_str )
  404. {
  405. $tnum = explode( " ", $t2_str );
  406. $tid = explode( " ", $t2_str );
  407. list( $tid, $tnum ) = $tid;
  408. if ( $tid == 99 )
  409. {
  410. continue;
  411. }
  412. if ( $tnum == 0 - 1 )
  413. {
  414. $tnum = 1;
  415. $returnTroops[$vid]['hasHero'] = TRUE;
  416. }
  417. else
  418. {
  419. $returnTroops[$vid]['troops'][$tid] = $tnum;
  420. }
  421. $returnTroops[$vid]['cropConsumption'] += $GameMetadata['troops'][$tid]['crop_consumption'] * $tnum;
  422. }
  423. }
  424. }
  425. $m->dispose( );
  426. return $returnTroops;
  427. }
  428. public function handleMarketplace( )
  429. {
  430. $this->selectedTabIndex = isset( $_GET['t'] ) && is_numeric( $_GET['t'] ) && 1 <= intval( $_GET['t'] ) && intval( $_GET['t'] ) <= 3 ? intval( $_GET['t'] ) : 0;
  431. $itemId = $this->buildings[$this->buildingIndex]['item_id'];
  432. $itemLevel = $this->buildings[$this->buildingIndex]['level'];
  433. $tribeMetadata = $this->gameMetadata['tribes'][$this->data['tribe_id']];
  434. $tradeOfficeLevel = $this->_getMaxBuildingLevel( 28 );
  435. $capacityFactor = $tradeOfficeLevel == 0 ? 1 : $this->gameMetadata['items'][28]['levels'][$tradeOfficeLevel - 1]['value'] / 100;
  436. $capacityFactor *= $this->gameMetadata['game_speed'];
  437. $total_merchants_num = $this->gameMetadata['items'][$itemId]['levels'][$itemLevel - 1]['value'];
  438. $exist_num = $total_merchants_num - $this->queueModel->tasksInQueue['out_merchants_num'] - $this->data['offer_merchants_count'];
  439. if ( $exist_num < 0 )
  440. {
  441. $exist_num = 0;
  442. }
  443. $this->merchantProperty = array( "speed" => $tribeMetadata['merchants_velocity'] * $this->gameMetadata['game_speed'], "capacity" => floor( $tribeMetadata['merchants_capacity'] * $capacityFactor ), "total_num" => $total_merchants_num, "exits_num" => $exist_num, "confirm_snd" => FALSE, "same_village" => FALSE, "vRow" => NULL );
  444. if ( $this->selectedTabIndex == 0 )
  445. {
  446. $m = new BuildModel( );
  447. if ( $this->isPost( ) || isset( $_GET['vid2'] ) )
  448. {
  449. $resources = array( "1" => isset( $_POST['r1'] ) ? intval( $_POST['r1'] ) : 0, "2" => isset( $_POST['r2'] ) ? intval( $_POST['r2'] ) : 0, "3" => isset( $_POST['r3'] ) ? intval( $_POST['r3'] ) : 0, "4" => isset( $_POST['r4'] ) ? intval( $_POST['r4'] ) : 0 );
  450. $this->merchantProperty['confirm_snd'] = $this->isPost( ) ? isset( $_POST['act'] ) && $_POST['act'] == 1 : isset( $_GET['vid2'] );
  451. $map_size = $this->setupMetadata['map_size'];
  452. $doSend = FALSE;
  453. if ( $this->merchantProperty['confirm_snd'] )
  454. {
  455. $vRow = NULL;
  456. if ( trim( $_POST['y'] ) != "" )
  457. {
  458. $vid = $this->__getVillageId( $map_size, $this->__getCoordInRange( $map_size, intval( $_POST['x'] ) ), $this->__getCoordInRange( $map_size, intval( $_POST['y'] ) ) );
  459. $vRow = $m->getVillageDataById( $vid );
  460. }
  461. else
  462. {
  463. if ( isset( $_POST['vname'] ) && trim( $_POST['vname'] ) != "" )
  464. {
  465. $vRow = $m->getVillageDataByName( trim( $_POST['vname'] ) );
  466. }
  467. else
  468. {
  469. if ( isset( $_GET['vid2'] ) )
  470. {
  471. $vRow = $m->getVillageDataById( intval( $_GET['vid2'] ) );
  472. if ( $vRow != NULL )
  473. {
  474. $_POST['x'] = $vRow['rel_x'];
  475. $_POST['y'] = $vRow['rel_y'];
  476. }
  477. }
  478. }
  479. }
  480. }
  481. else
  482. {
  483. $doSend = TRUE;
  484. $vRow = $m->getVillageDataById( intval( $_POST['vid2'] ) );
  485. $this->merchantProperty['showError'] = FALSE;
  486. $_POST['r1'] = $_POST['r2'] = $_POST['r3'] = $_POST['r4'] = "";
  487. }
  488. if ( 0 < intval( $vRow['player_id'] ) && $m->getPlayType( intval( $vRow['player_id'] ) ) == PLAYERTYPE_ADMIN )
  489. {
  490. $this->merchantProperty['showError'] = FALSE;
  491. $this->merchantProperty['confirm_snd'] = FALSE;
  492. }
  493. else
  494. {
  495. $this->merchantProperty['vRow'] = $vRow;
  496. $vid = $this->merchantProperty['to_vid'] = $vRow != NULL ? $vRow['id'] : 0;
  497. $rel_x = $vRow['rel_x'];
  498. $rel_y = $vRow['rel_y'];
  499. $this->merchantProperty['same_village'] = $vid == $this->data['selected_village_id'];
  500. $this->merchantProperty['available_res'] = $this->isResourcesAvailable( $resources );
  501. $this->merchantProperty['vRow_merchant_num'] = ceil( ( $resources[1] + $resources[2] + $resources[3] + $resources[4] ) / $this->merchantProperty['capacity'] );
  502. $this->merchantProperty['confirm_snd'] = 0 < $vid && $this->merchantProperty['available_res'] && 0 < $this->merchantProperty['vRow_merchant_num'] && $this->merchantProperty['vRow_merchant_num'] <= $this->merchantProperty['exits_num'] && !$this->merchantProperty['same_village'];
  503. $this->merchantProperty['showError'] = !$this->merchantProperty['confirm_snd'];
  504. $distance = WebHelper::getdistance( $this->data['rel_x'], $this->data['rel_y'], $rel_x, $rel_y, $this->setupMetadata['map_size'] / 2 );
  505. $this->merchantProperty['vRow_time'] = intval( $distance / $this->merchantProperty['speed'] * 3600 );
  506. if ( !$this->merchantProperty['showError'] && $doSend && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  507. {
  508. $this->merchantProperty['confirm_snd'] = FALSE;
  509. $this->merchantProperty -= "exits_num";
  510. $newTask = new QueueTask( QS_MERCHANT_GO, $this->player->playerId, $this->merchantProperty['vRow_time'] );
  511. $newTask->villageId = $this->data['selected_village_id'];
  512. $newTask->toPlayerId = $vRow['player_id'];
  513. $newTask->toVillageId = $vid;
  514. $newTask->procParams = $this->merchantProperty['vRow_merchant_num']."|".( $resources[1]." ".$resources[2]." ".$resources[3]." ".$resources[4] );
  515. $newTask->tag = $resources;
  516. $this->queueModel->addTask( $newTask );
  517. }
  518. }
  519. }
  520. $m->dispose( );
  521. }
  522. else if ( $this->selectedTabIndex == 1 )
  523. {
  524. $m = new BuildModel( );
  525. $showOfferList = TRUE;
  526. if ( isset( $_GET['oid'] ) && 0 < intval( $_GET['oid'] ) )
  527. {
  528. $oRow = $m->getOffer2( intval( $_GET['oid'] ), $this->data['rel_x'], $this->data['rel_y'], $this->setupMetadata['map_size'] / 2 );
  529. if ( $oRow != NULL )
  530. {
  531. $aid = 0;
  532. if ( $oRow['alliance_only'] && 0 < intval( $this->data['alliance_id'] ) )
  533. {
  534. $aid = $m->getPlayerAllianceId( $oRow['player_id'] );
  535. }
  536. $res2 = explode( "|", $oRow['offer'] );
  537. $res1 = explode( "|", $oRow['offer'] );
  538. list( $res1, $res2 ) = $res1;
  539. $resArr1 = explode( " ", $res1 );
  540. $needResources = array( "1" => $resArr1[0], "2" => $resArr1[1], "3" => $resArr1[2], "4" => $resArr1[3] );
  541. $res1_item_id = 0;
  542. $res1_value = 0;
  543. $i = 0;
  544. $_c = sizeof( $resArr1 );
  545. while ( $i < $_c )
  546. {
  547. if ( 0 < $resArr1[$i] )
  548. {
  549. $res1_item_id = $i + 1;
  550. $res1_value = $resArr1[$i];
  551. break;
  552. }
  553. ++$i;
  554. }
  555. $resArr1 = explode( " ", $res2 );
  556. $giveResources = array( "1" => $resArr1[0], "2" => $resArr1[1], "3" => $resArr1[2], "4" => $resArr1[3] );
  557. $res2_item_id = 0;
  558. $res2_value = 0;
  559. $i = 0;
  560. $_c = sizeof( $resArr1 );
  561. while ( $i < $_c )
  562. {
  563. if ( 0 < $resArr1[$i] )
  564. {
  565. $res2_item_id = $i + 1;
  566. $res2_value = $resArr1[$i];
  567. break;
  568. }
  569. ++$i;
  570. }
  571. $distance = $oRow['timeInSeconds'] / 3600 * $oRow['merchants_speed'];
  572. $acceptResult = $this->_canAcceptOffer( $needResources, $giveResources, $oRow['village_id'], $oRow['alliance_only'], $aid, $oRow['max_time'], $distance );
  573. if ( $acceptResult == 5 && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  574. {
  575. $showOfferList = FALSE;
  576. $this->merchantProperty['offerProperty'] = array( "player_id" => $oRow['player_id'], "player_name" => $oRow['player_name'], "res1_item_id" => $res1_item_id, "res1_value" => $res1_value, "res2_item_id" => $res2_item_id, "res2_value" => $res2_value );
  577. $merchantNum = ceil( ( $giveResources[1] + $giveResources[2] + $giveResources[3] + $giveResources[4] ) / $this->merchantProperty['capacity'] );
  578. $newTask = new QueueTask( QS_MERCHANT_GO, $this->player->playerId, $distance / ( $this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity'] * $this->gameMetadata['game_speed'] ) * 3600 );
  579. $newTask->villageId = $this->data['selected_village_id'];
  580. $newTask->toPlayerId = $oRow['player_id'];
  581. $newTask->toVillageId = $oRow['village_id'];
  582. $newTask->procParams = $merchantNum."|".( $giveResources[1]." ".$giveResources[2]." ".$giveResources[3]." ".$giveResources[4] );
  583. $newTask->tag = $giveResources;
  584. $this->queueModel->addTask( $newTask );
  585. $newTask = new QueueTask( QS_MERCHANT_GO, $oRow['player_id'], $oRow['timeInSeconds'] );
  586. $newTask->villageId = $oRow['village_id'];
  587. $newTask->toPlayerId = $this->player->playerId;
  588. $newTask->toVillageId = $this->data['selected_village_id'];
  589. $newTask->procParams = $oRow['merchants_num']."|".( $needResources[1]." ".$needResources[2]." ".$needResources[3]." ".$needResources[4] );
  590. $newTask->tag = array( "1" => 0, "2" => 0, "3" => 0, "4" => 0 );
  591. $this->queueModel->addTask( $newTask );
  592. $m->removeMerchantOffer( intval( $_GET['oid'] ), $oRow['player_id'], $oRow['village_id'] );
  593. }
  594. }
  595. }
  596. $this->merchantProperty['showOfferList'] = $showOfferList;
  597. if ( $showOfferList )
  598. {
  599. $rowsCount = $m->getAllOffersCount( $this->data['selected_village_id'], $this->data['rel_x'], $this->data['rel_y'], $this->setupMetadata['map_size'] / 2, $this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity'] * $this->gameMetadata['game_speed'] );
  600. $this->pageCount = 0 < $rowsCount ? ceil( $rowsCount / $this->pageSize ) : 1;
  601. $this->pageIndex = isset( $_GET['p'] ) && is_numeric( $_GET['p'] ) && intval( $_GET['p'] ) < $this->pageCount ? intval( $_GET['p'] ) : 0;
  602. $this->merchantProperty['all_offers'] = $m->getAllOffers( $this->data['selected_village_id'], $this->data['rel_x'], $this->data['rel_y'], $this->setupMetadata['map_size'] / 2, $this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity'] * $this->gameMetadata['game_speed'], $this->pageIndex, $this->pageSize );
  603. }
  604. $m->dispose( );
  605. }
  606. else
  607. {
  608. if ( $this->selectedTabIndex == 2 )
  609. {
  610. $m = new BuildModel( );
  611. $this->merchantProperty['showError'] = FALSE;
  612. $this->merchantProperty['showError2'] = FALSE;
  613. $this->merchantProperty['showError3'] = FALSE;
  614. if ( $this->isPost( ) )
  615. {
  616. if ( isset( $_POST['m1'] ) && 0 < intval( $_POST['m1'] ) && isset( $_POST['m2'] ) && 0 < intval( $_POST['m2'] ) && isset( $_POST['rid1'] ) && 0 < intval( $_POST['rid1'] ) && isset( $_POST['rid2'] ) && 0 < intval( $_POST['rid2'] ) )
  617. {
  618. $resources1 = array( "1" => isset( $_POST['rid1'] ) && intval( $_POST['rid1'] ) == 1 ? intval( $_POST['m1'] ) : 0, "2" => isset( $_POST['rid1'] ) && intval( $_POST['rid1'] ) == 2 ? intval( $_POST['m1'] ) : 0, "3" => isset( $_POST['rid1'] ) && intval( $_POST['rid1'] ) == 3 ? intval( $_POST['m1'] ) : 0, "4" => isset( $_POST['rid1'] ) && intval( $_POST['rid1'] ) == 4 ? intval( $_POST['m1'] ) : 0 );
  619. $resources2 = array( "1" => isset( $_POST['rid2'] ) && intval( $_POST['rid2'] ) == 1 ? intval( $_POST['m2'] ) : 0, "2" => isset( $_POST['rid2'] ) && intval( $_POST['rid2'] ) == 2 ? intval( $_POST['m2'] ) : 0, "3" => isset( $_POST['rid2'] ) && intval( $_POST['rid2'] ) == 3 ? intval( $_POST['m2'] ) : 0, "4" => isset( $_POST['rid2'] ) && intval( $_POST['rid2'] ) == 4 ? intval( $_POST['m2'] ) : 0 );
  620. if ( intval( $_POST['rid1'] ) == intval( $_POST['rid2'] ) || intval( $resources1[1] + $resources1[2] + $resources1[3] + $resources1[4] ) <= 0 || intval( $resources2[1] + $resources2[2] + $resources2[3] + $resources2[4] ) <= 0 )
  621. {
  622. $this->merchantProperty['showError'] = TRUE;
  623. }
  624. else if ( 10 < ceil( ( $resources2[1] + $resources2[2] + $resources2[3] + $resources2[4] ) / ( $resources1[1] + $resources1[2] + $resources1[3] + $resources1[4] ) ) )
  625. {
  626. $this->merchantProperty['showError'] = TRUE;
  627. $this->merchantProperty['showError3'] = TRUE;
  628. }
  629. $this->merchantProperty['available_res'] = $this->isResourcesAvailable( $resources1 );
  630. if ( $this->merchantProperty['available_res'] && !$this->merchantProperty['showError'] )
  631. {
  632. $this->merchantProperty['vRow_merchant_num'] = ceil( ( $resources1[1] + $resources1[2] + $resources1[3] + $resources1[4] ) / $this->merchantProperty['capacity'] );
  633. if ( 0 < $this->merchantProperty['vRow_merchant_num'] && $this->merchantProperty['vRow_merchant_num'] <= $this->merchantProperty['exits_num'] )
  634. {
  635. $this->merchantProperty -= "exits_num";
  636. $this->data += "offer_merchants_count";
  637. $offer = $resources1[1]." ".$resources1[2]." ".$resources1[3]." ".$resources1[4]."|".( $resources2[1]." ".$resources2[2]." ".$resources2[3]." ".$resources2[4] );
  638. $m->addMerchantOffer( $this->player->playerId, $this->data['name'], $this->data['selected_village_id'], $this->data['rel_x'], $this->data['rel_y'], $this->merchantProperty['vRow_merchant_num'], $offer, isset( $_POST['ally'] ), 0 < intval( $_POST['d2'] ) ? intval( $_POST['d2'] ) : 0, $this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity'] * $this->gameMetadata['game_speed'] );
  639. foreach ( $resources1 as $k => $v )
  640. {
  641. $this->resources[$k] -= "current_value";
  642. }
  643. $this->queueModel->_updateVillage( FALSE, FALSE );
  644. }
  645. else
  646. {
  647. $this->merchantProperty['showError'] = TRUE;
  648. }
  649. }
  650. else
  651. {
  652. $this->merchantProperty['showError'] = TRUE;
  653. }
  654. }
  655. else
  656. {
  657. $this->merchantProperty['showError'] = TRUE;
  658. $this->merchantProperty['showError2'] = TRUE;
  659. }
  660. }
  661. else if ( isset( $_GET['d'] ) && 0 < intval( $_GET['d'] ) )
  662. {
  663. $row = $m->getOffer( intval( $_GET['d'] ), $this->player->playerId, $this->data['selected_village_id'] );
  664. if ( $row != NULL )
  665. {
  666. $this->merchantProperty += "exits_num";
  667. $this->data -= "offer_merchants_count";
  668. $resources2 = explode( "|", $row['offer'] );
  669. $resources1 = explode( "|", $row['offer'] );
  670. list( $resources1, $resources2 ) = $resources1;
  671. $resourcesArray1 = explode( " ", $resources1 );
  672. $res = array( );
  673. $i = 0;
  674. $_c = sizeof( $resourcesArray1 );
  675. while ( $i < $_c )
  676. {
  677. $res[$i + 1] = $resourcesArray1[$i];
  678. ++$i;
  679. }
  680. foreach ( $res as $k => $v )
  681. {
  682. $this->resources[$k] += "current_value";
  683. }
  684. $this->queueModel->_updateVillage( FALSE, FALSE );
  685. $m->removeMerchantOffer( intval( $_GET['d'] ), $this->player->playerId, $this->data['selected_village_id'] );
  686. }
  687. }
  688. $this->merchantProperty['offers'] = $m->getOffers( $this->data['selected_village_id'] );
  689. $m->dispose( );
  690. }
  691. else
  692. {
  693. if ( $this->selectedTabIndex == 3 && $this->isPost( ) && isset( $_POST['m2'] ) && is_array( $_POST['m2'] ) && sizeof( $_POST['m2'] ) == 4 && $this->gameMetadata['plusTable'][6]['cost'] <= $this->data['gold_num'] )
  694. {
  695. $resources = array( "1" => intval( $_POST['m2'][0] ), "2" => intval( $_POST['m2'][1] ), "3" => intval( $_POST['m2'][2] ), "4" => intval( $_POST['m2'][3] ) );
  696. $oldSum = $this->resources[1]['current_value'] + $this->resources[2]['current_value'] + $this->resources[3]['current_value'] + $this->resources[4]['current_value'];
  697. $newSum = $resources[1] + $resources[2] + $resources[3] + $resources[4];
  698. if ( $newSum <= $oldSum )
  699. {
  700. foreach ( $resources as $k => $v )
  701. {
  702. $this->resources[$k]['current_value'] = $v;
  703. }
  704. $this->queueModel->_updateVillage( FALSE, FALSE );
  705. $m = new BuildModel( );
  706. $m->decreaseGoldNum( $this->player->playerId, $this->gameMetadata['plusTable'][6]['cost'] );
  707. $m->dispose( );
  708. }
  709. }
  710. }
  711. }
  712. }
  713. public function handleEmbassy( )
  714. {
  715. if ( 0 < intval( $this->data['alliance_id'] ) )
  716. {
  717. }
  718. else
  719. {
  720. $this->embassyProperty = array( "level" => $this->buildings[$this->buildingIndex]['level'], "invites" => NULL, "error" => 0, "ally1" => "", "ally2" => "" );
  721. $maxPlayers = $this->gameMetadata['items'][18]['levels'][$this->embassyProperty['level'] - 1]['value'];
  722. $this->embassyProperty['ally1'] = $ally1 = trim( $_POST['ally1'] );
  723. $this->embassyProperty['ally2'] = $ally2 = trim( $_POST['ally2'] );
  724. if ( $ally1 == "" || $ally2 == "" )
  725. {
  726. $this->embassyProperty['error'] = $ally1 == "" && $ally2 == "" ? 3 : $ally1 == "" ? 1 : 2;
  727. }
  728. else
  729. {
  730. $m = new BuildModel( );
  731. if ( !$m->allianceExists( $this->embassyProperty['ally1'] ) )
  732. {
  733. $this->data['alliance_name'] = $this->embassyProperty['ally1'];
  734. $this->data['alliance_id'] = $m->createAlliance( $this->player->playerId, $this->embassyProperty['ally1'], $this->embassyProperty['ally2'], $maxPlayers );
  735. $m->dispose( );
  736. }
  737. else
  738. {
  739. $this->embassyProperty['error'] = 4;
  740. $m->dispose( );
  741. }
  742. }
  743. $invites_alliance_ids = trim( $this->data['invites_alliance_ids'] );
  744. $this->embassyProperty['invites'] = array( );
  745. if ( $invites_alliance_ids != "" )
  746. {
  747. $_arr = explode( "\n", $invites_alliance_ids );
  748. foreach ( $_arr as $_s )
  749. {
  750. $allianceName = explode( " ", $_s, 2 );
  751. $allianceId = explode( " ", $_s, 2 );
  752. list ( $allianceId, $allianceName ) = $allianceId;
  753. $this->embassyProperty['invites'][$allianceId] = $allianceName;
  754. }
  755. }
  756. if ( !$this->isPost( ) )
  757. {
  758. if ( isset( $_GET['a'] ) && 0 < intval( $_GET['a'] ) )
  759. {
  760. $allianceId = intval( $_GET['a'] );
  761. if ( isset( $this->embassyProperty['invites'][$allianceId] ) )
  762. {
  763. $m = new BuildModel( );
  764. $acceptResult = $m->acceptAllianceJoining( $this->player->playerId, $allianceId );
  765. if ( $acceptResult == 2 )
  766. {
  767. $this->data['alliance_name'] = $this->embassyProperty['invites'][$allianceId];
  768. $this->data['alliance_id'] = $allianceId;
  769. unset( $Var_4320['invites'][$allianceId] );
  770. $m->removeAllianceInvites( $this->player->playerId, $allianceId );
  771. }
  772. else if ( $acceptResult == 1 )
  773. {
  774. $this->embassyProperty['error'] = 15;
  775. }
  776. $m->dispose( );
  777. }
  778. }
  779. else
  780. {
  781. if ( isset( $_GET['d'] ) && 0 < intval( $_GET['d'] ) )
  782. {
  783. $allianceId = intval( $_GET['d'] );
  784. if ( isset( $this->embassyProperty['invites'][$allianceId] ) )
  785. {
  786. unset( $Var_5112['invites'][$allianceId] );
  787. $m = new BuildModel( );
  788. $m->removeAllianceInvites( $this->player->playerId, $allianceId );
  789. $m->dispose( );
  790. }
  791. }
  792. }
  793. }
  794. }
  795. }
  796. public function handleWarrior( )
  797. {
  798. $itemId = $this->buildings[$this->buildingIndex]['item_id'];
  799. $this->troopsUpgrade = array( );
  800. $_arr = explode( ",", $this->data['troops_training'] );
  801. foreach ( $_arr as $troopStr )
  802. {
  803. $attack_level = explode( " ", $troopStr );
  804. $defense_level = explode( " ", $troopStr );
  805. $researches_done = explode( " ", $troopStr );
  806. $troopId = explode( " ", $troopStr );
  807. list( $troopId, $researches_done, $defense_level, $attack_level ) = $troopId;
  808. if ( $researches_done == 1 && 0 < $this->gameMetadata['troops'][$troopId]['gold_needed'] )
  809. {
  810. $this->troopsUpgrade[$troopId] = $troopId;
  811. }
  812. }
  813. $this->warriorMessage = "";
  814. if ( $this->isPost( ) && isset( $_POST['tf'] ) && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  815. {
  816. $cropConsume = 0;
  817. $totalGoldsNeeded = 0;
  818. foreach ( $_POST['tf'] as $troopId => $num )
  819. {
  820. $num = intval( $num );
  821. if ( $num <= 0 || !isset( $this->troopsUpgrade[$troopId] ) )
  822. {
  823. continue;
  824. }
  825. $totalGoldsNeeded += $this->gameMetadata['troops'][$troopId]['gold_needed'] * $num;
  826. $cropConsume += $this->gameMetadata['troops'][$troopId]['crop_consumption'] * $num;
  827. }
  828. if ( $totalGoldsNeeded <= 0 )
  829. {
  830. }
  831. else
  832. {
  833. $canProcess = $totalGoldsNeeded <= $this->data['gold_num'];
  834. $this->warriorMessage = $canProcess ? 1 : 2;
  835. if ( $canProcess )
  836. {
  837. $troopsString = "";
  838. foreach ( $this->troops as $tid => $num )
  839. {
  840. if ( $tid == 99 )
  841. {
  842. continue;
  843. }
  844. $neededNum = isset( $this->troopsUpgrade[$tid], $_POST['tf'][$tid] ) ? $_POST['tf'][$tid] : 0;
  845. if ( $troopsString != "" )
  846. {
  847. $troopsString .= ",";
  848. }
  849. $troopsString .= $tid." ".$neededNum;
  850. }
  851. $m = new BuildModel( );
  852. $m->decreaseGoldNum( $this->player->playerId, $totalGoldsNeeded );
  853. $m->dispose( );
  854. $this->data -= "gold_num";
  855. $procParams = $troopsString."|0||||||1";
  856. $buildingMetadata = $this->gameMetadata['items'][$this->buildProperties['building']['item_id']];
  857. $bLevel = $this->buildings[$this->buildingIndex]['level'];
  858. $needed_time = $buildingMetadata['levels'][$bLevel - 1]['value'] * 3600;
  859. $newTask = new QueueTask( QS_WAR_REINFORCE, 0, $needed_time );
  860. $newTask->villageId = 0;
  861. $newTask->toPlayerId = $this->player->playerId;
  862. $newTask->toVillageId = $this->data['selected_village_id'];
  863. $newTask->procParams = $procParams;
  864. $newTask->tag = array( "troops" => NULL, "hasHero" => FALSE, "resources" => NULL, "troopsCropConsume" => $cropConsume );
  865. $this->queueModel->addTask( $newTask );
  866. }
  867. }
  868. }
  869. }
  870. public function handleTroopBuilding( )
  871. {
  872. $itemId = $this->buildings[$this->buildingIndex]['item_id'];
  873. $this->troopsUpgradeType = QS_TROOP_TRAINING;
  874. $this->troopsUpgrade = array( );
  875. $_arr = explode( ",", $this->data['troops_training'] );
  876. foreach ( $_arr as $troopStr )
  877. {
  878. $attack_level = explode( " ", $troopStr );
  879. $defense_level = explode( " ", $troopStr );
  880. $researches_done = explode( " ", $troopStr );
  881. $troopId = explode( " ", $troopStr );
  882. list( $troopId, $researches_done, $defense_level, $attack_level ) = $troopId;
  883. if ( $researches_done == 1 && $this->_canTrainInBuilding( $troopId, $itemId ) )
  884. {
  885. $this->troopsUpgrade[$troopId] = $troopId;
  886. }
  887. }
  888. if ( $this->isPost( ) && isset( $_POST['tf'] ) && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  889. {
  890. foreach ( $_POST['tf'] as $troopId => $num )
  891. {
  892. $num = intval( $num );
  893. if ( $num <= 0 || !isset( $this->troopsUpgrade[$troopId] ) || $this->_getMaxTrainNumber( $troopId, $itemId ) < $num )
  894. {
  895. continue;
  896. }
  897. $timeFactor = 1;
  898. if ( $this->gameMetadata['troops'][$troopId]['is_cavalry'] == TRUE )
  899. {
  900. $flvl = $this->_getMaxBuildingLevel( 41 );
  901. if ( 0 < $flvl )
  902. {
  903. $timeFactor -= $this->gameMetadata['items'][41]['levels'][$flvl - 1]['value'] / 100;
  904. }
  905. }
  906. $troopMetadata = $this->gameMetadata['troops'][$troopId];
  907. $calcConsume = intval( $troopMetadata['training_time_consume'] / $this->gameSpeed * ( 10 / ( $this->buildProperties['building']['level'] + 9 ) ) * $timeFactor );
  908. $newTask = new QueueTask( $this->troopsUpgradeType, $this->player->playerId, $calcConsume );
  909. $newTask->threads = $num;
  910. $newTask->villageId = $this->data['selected_village_id'];
  911. $newTask->buildingId = $this->buildProperties['building']['item_id'];
  912. $newTask->procParams = $troopId;
  913. $newTask->tag = array( "1" => $troopMetadata['training_resources'][1] * $this->buildingTribeFactor * $num, "2" => $troopMetadata['training_resources'][2] * $this->buildingTribeFactor * $num, "3" => $troopMetadata['training_resources'][3] * $this->buildingTribeFactor * $num, "4" => $troopMetadata['training_resources'][4] * $this->buildingTribeFactor * $num );
  914. $this->queueModel->addTask( $newTask );
  915. }
  916. }
  917. }
  918. public function handleAcademy( )
  919. {
  920. $this->troopsUpgradeType = QS_TROOP_RESEARCH;
  921. $this->troopsUpgrade = array( "available" => array( ), "soon" => array( ) );
  922. $_arr = explode( ",", $this->data['troops_training'] );
  923. foreach ( $_arr as $troopStr )
  924. {
  925. $attack_level = explode( " ", $troopStr );
  926. $defense_level = explode( " ", $troopStr );
  927. $researches_done = explode( " ", $troopStr );
  928. $troopId = explode( " ", $troopStr );
  929. list( $troopId, $researches_done, $defense_level, $attack_level ) = $troopId;
  930. if ( $researches_done == 0 )
  931. {
  932. $this->troopsUpgrade[$this->_canDoResearches( $troopId ) ? "available" : "soon"][] = $troopId;
  933. }
  934. }
  935. if ( $_GET['k'] == $this->data['update_key'] && !isset( $this->queueModel->tasksInQueue[$this->troopsUpgradeType] ) && $this->_canDoResearches( intval( $_GET['a'] ) ) && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  936. {
  937. $troopId = intval( $_GET['a'] );
  938. $buildingMetadata = $this->gameMetadata['troops'][$troopId];
  939. return;
  940. $calcConsume = intval( $buildingMetadata['research_time_consume'] / $this->gameSpeed );
  941. $newTask = new QueueTask( $this->troopsUpgradeType, $this->player->playerId, $calcConsume );
  942. $newTask->villageId = $this->data['selected_village_id'];
  943. $newTask->procParams = $troopId;
  944. $newTask->tag = $buildingMetadata['research_resources'];
  945. $this->queueModel->addTask( $newTask );
  946. }
  947. }
  948. public function handleTownHall( )
  949. {
  950. $buildingMetadata = $this->gameMetadata['items'][$this->buildProperties['building']['item_id']];
  951. $bLevel = $this->buildings[$this->buildingIndex]['level'];
  952. if ( $_GET['k'] == $this->data['update_key'] && !isset( $this->queueModel->tasksInQueue[QS_TOWNHALL_CELEBRATION] ) && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  953. {
  954. if ( intval( $_GET['a'] ) < 1 || 2 < intval( $_GET['a'] ) || intval( $_GET['a'] ) == 1 && $bLevel < $buildingMetadata['celebrations']['small']['level'] || intval( $_GET['a'] ) == 2 && $bLevel < $buildingMetadata['celebrations']['large']['level'] )
  955. {
  956. }
  957. else
  958. {
  959. $key = intval( $_GET['a'] ) == 2 ? "large" : "small";
  960. if ( !$this->isResourcesAvailable( $buildingMetadata['celebrations'][$key]['resources'] ) )
  961. {
  962. }
  963. else
  964. {
  965. $calcConsume = intval( $buildingMetadata['celebrations'][$key]['time_consume'] / $this->gameSpeed * ( 10 / ( $bLevel + 9 ) ) );
  966. $newTask = new QueueTask( QS_TOWNHALL_CELEBRATION, $this->player->playerId, $calcConsume );
  967. $newTask->villageId = $this->data['selected_village_id'];
  968. $newTask->procParams = intval( $_GET['a'] );
  969. $newTask->tag = $buildingMetadata['celebrations'][$key]['resources'];
  970. $this->queueModel->addTask( $newTask );
  971. }
  972. }
  973. }
  974. }
  975. public function handleResidencePalace( )
  976. {
  977. $this->selectedTabIndex = is_numeric( $_GET['t'] ) && 1 <= intval( $_GET['t'] ) && intval( $_GET['t'] ) <= 3 ? intval( $_GET['t'] ) : 0;
  978. $_bid_ = $this->buildings[$this->buildingIndex]['item_id'];
  979. if ( $this->selectedTabIndex == 0 )
  980. {
  981. if ( isset( $_GET['mc'] ) && !$this->data['is_capital'] && !$this->data['is_special_village'] && $_bid_ == 26 )
  982. {
  983. $this->data['is_capital'] = TRUE;
  984. $m = new BuildModel( );
  985. $m->makeVillageAsCapital( $this->player->playerId, $this->data['selected_village_id'] );
  986. $m->dispose( );
  987. }
  988. $this->childVillagesCount = 0;
  989. if ( trim( $this->data['child_villages_id'] ) != "" )
  990. {
  991. $this->childVillagesCount = sizeof( explode( ",", $this->data['child_villages_id'] ) );
  992. }
  993. $itemId = $this->buildings[$this->buildingIndex]['item_id'];
  994. $buildingLevel = $this->buildings[$this->buildingIndex]['level'];
  995. $this->troopsUpgradeType = QS_TROOP_TRAINING;
  996. $this->_getOnlyMyTroops( );
  997. $this->troopsUpgrade = array( );
  998. $_arr = explode( ",", $this->data['troops_training'] );
  999. foreach ( $_arr as $troopStr )
  1000. {
  1001. $attack_level = explode( " ", $troopStr );
  1002. $defense_level = explode( " ", $troopStr );
  1003. $researches_done = explode( " ", $troopStr );
  1004. $troopId = explode( " ", $troopStr );
  1005. list( $troopId, $researches_done, $defense_level, $attack_level ) = $troopId;
  1006. if ( $researches_done == 1 && $this->_canTrainInBuilding( $troopId, $itemId ) )
  1007. {
  1008. $this->troopsUpgrade[] = array( "troopId" => $troopId, "maxNumber" => $this->_getMaxTrainNumber( $troopId, $itemId ), "currentNumber" => $this->_getCurrentNumberFor( $troopId, $itemId ) );
  1009. }
  1010. }
  1011. $this->showBuildingForm = FALSE;
  1012. if ( $buildingLevel < 10 || 2 <= $this->childVillagesCount && $_bid_ == 25 || 3 <= $this->childVillagesCount && $_bid_ == 26 || $this->childVillagesCount == 1 && $buildingLevel < 20 && $_bid_ == 25 || $this->childVillagesCount == 1 && $buildingLevel < 15 && $_bid_ == 26 || $this->childVillagesCount == 2 && $buildingLevel < 20 && $_bid_ == 26 )
  1013. {
  1014. $this->troopsUpgrade = array( );
  1015. }
  1016. else
  1017. {
  1018. if ( 1 < sizeof( $this->troopsUpgrade ) )
  1019. {
  1020. if ( 1 <= $this->troopsUpgrade[0]['currentNumber'] || 3 <= $this->troopsUpgrade[1]['currentNumber'] )
  1021. {
  1022. $this->troopsUpgrade = array( );
  1023. }
  1024. else
  1025. {
  1026. if ( 0 < $this->troopsUpgrade[1]['currentNumber'] )
  1027. {
  1028. unset( $Var_4224[0] );
  1029. }
  1030. }
  1031. }
  1032. else if ( 3 <= $this->troopsUpgrade[0]['currentNumber'] )
  1033. {
  1034. $this->troopsUpgrade = array( );
  1035. }
  1036. $this->showBuildingForm = 0 < sizeof( $this->troopsUpgrade );
  1037. }
  1038. if ( $this->isPost( ) && isset( $_POST['tf'] ) && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  1039. {
  1040. foreach ( $_POST['tf'] as $troopId => $num )
  1041. {
  1042. $num = intval( $num );
  1043. $existsTroop = FALSE;
  1044. foreach ( $this->troopsUpgrade as $troop )
  1045. {
  1046. if ( $troop['troopId'] == $troopId )
  1047. {
  1048. $existsTroop = TRUE;
  1049. break;
  1050. break;
  1051. }
  1052. }
  1053. if ( $num <= 0 || !$existsTroop || $this->_getMaxTrainNumber( $troopId, $itemId ) < $num )
  1054. {
  1055. continue;
  1056. }
  1057. $troopMetadata = $this->gameMetadata['troops'][$troopId];
  1058. $calcConsume = intval( $troopMetadata['training_time_consume'] / $this->gameSpeed * ( 10 / ( $this->buildProperties['building']['level'] + 9 ) ) );
  1059. $newTask = new QueueTask( $this->troopsUpgradeType, $this->player->playerId, $calcConsume );
  1060. $newTask->threads = $num;
  1061. $newTask->villageId = $this->data['selected_village_id'];
  1062. $newTask->buildingId = $this->buildProperties['building']['item_id'];
  1063. $newTask->procParams = $troopId;
  1064. $newTask->tag = $troopMetadata['training_resources'];
  1065. $this->queueModel->addTask( $newTask );
  1066. }
  1067. }
  1068. }
  1069. else
  1070. {
  1071. if ( $this->selectedTabIndex == 1 )
  1072. {
  1073. $this->neededCpValue = $this->totalCpRate = $this->totalCpValue = 0;
  1074. $m = new BuildModel( );
  1075. $result = $m->getVillagesCp( $this->data['villages_id'] );
  1076. while ( $result->next( ) )
  1077. {
  1078. //fix2 part f culture points calculations
  1079. $cpRate = explode( " ", $result->row['cp'] );
  1080. $this->cpValue = explode( " ", $result->row['cp'] );
  1081. list ( $this->cpValue, $cpRate ) = $this->cpValue;
  1082. $_39643752 += "cpValue";
  1083. $_39643848 += "totalCpRate";
  1084. $_39643880 += "totalCpValue";
  1085. $_39643880 += "neededCpValue";
  1086. }
  1087. $this->totalCpValue = floor( $this->totalCpValue );
  1088. $m->dispose( );
  1089. }
  1090. else
  1091. {
  1092. if ( $this->selectedTabIndex == 3 )
  1093. {
  1094. $this->childVillages = array( );
  1095. $m = new BuildModel( );
  1096. $result = $m->getChildVillagesFor( trim( $this->data['child_villages_id'] ) );
  1097. while ( $result != NULL && $result->next( ) )
  1098. {
  1099. $this->childVillages[$result->row['id']] = array( "id" => $result->row['id'], "rel_x" => $result->row['rel_x'], "rel_y" => $result->row['rel_y'], "village_name" => $result->row['village_name'], "people_count" => $result->row['people_count'], "creation_date" => $result->row['creation_date'] );
  1100. }
  1101. $m->dispose( );
  1102. }
  1103. }
  1104. }
  1105. }
  1106. public function handleHerosMansion( )
  1107. {
  1108. $this->selectedTabIndex = isset( $_GET['t'] ) && is_numeric( $_GET['t'] ) && intval( $_GET['t'] ) == 1 ? intval( $_GET['t'] ) : 0;
  1109. if ( $this->selectedTabIndex == 0 )
  1110. {
  1111. $this->hasHero = 0 < intval( $this->data['hero_troop_id'] );
  1112. $this->troopsUpgradeType = QS_TROOP_TRAINING_HERO;
  1113. if ( !$this->hasHero )
  1114. {
  1115. $this->_getOnlyMyTroops( TRUE );
  1116. if ( $_GET['k'] == $this->data['update_key'] && !isset( $this->queueModel->tasksInQueue[$this->troopsUpgradeType] ) && isset( $this->troops[intval( $_GET['a'] )] ) && 0 < $this->troops[intval( $_GET['a'] )] && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  1117. {
  1118. $troopId = intval( $_GET['a'] );
  1119. $troopMetadata = $this->gameMetadata['troops'][$troopId];
  1120. $nResources = array( "1" => $troopMetadata['training_resources'][1] * 2, "2" => $troopMetadata['training_resources'][2] * 2, "3" => $troopMetadata['training_resources'][3] * 2, "4" => $troopMetadata['training_resources'][4] * 2 );
  1121. if ( !$this->isResourcesAvailable( $nResources ) )
  1122. {
  1123. }
  1124. else
  1125. {
  1126. $calcConsume = intval( $troopMetadata['training_time_consume'] / $this->gameSpeed * ( 10 / ( $this->buildProperties['building']['level'] + 9 ) ) ) * 12;
  1127. $newTask = new QueueTask( $this->troopsUpgradeType, $this->player->playerId, $calcConsume );
  1128. $newTask->procParams = $troopId." ".$this->data['selected_village_id'];
  1129. $newTask->tag = $nResources;
  1130. $this->queueModel->addTask( $newTask );
  1131. }
  1132. }
  1133. }
  1134. else
  1135. {
  1136. if ( $this->isPost( ) && isset( $_POST['hname'] ) && trim( $_POST['hname'] ) != "" )
  1137. {
  1138. $this->data['hero_name'] = trim( $_POST['hname'] );
  1139. $m = new BuildModel( );
  1140. $m->changeHeroName( $this->player->playerId, $this->data['hero_name'] );
  1141. $m->dispose( );
  1142. }
  1143. }
  1144. }
  1145. else
  1146. {
  1147. if ( $this->selectedTabIndex == 1 )
  1148. {
  1149. $this->villageOases = array( );
  1150. $m = new BuildModel( );
  1151. $result = $m->getVillageOases( trim( $this->data['village_oases_id'] ) );
  1152. while ( $result != NULL && $result->next( ) )
  1153. {
  1154. $this->villageOases[$result->row['id']] = array( "id" => $result->row['id'], "rel_x" => $result->row['rel_x'], "rel_y" => $result->row['rel_y'], "image_num" => $result->row['image_num'], "allegiance_percent" => $result->row['allegiance_percent'] );
  1155. }
  1156. $m->dispose( );
  1157. if ( !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  1158. {
  1159. $oasisId = intval( $_GET['a'] );
  1160. $newTask = new QueueTask( QS_LEAVEOASIS, $this->player->playerId, floor( 21600 / $this->gameSpeed ) );
  1161. $newTask->villageId = $this->data['selected_village_id'];
  1162. $newTask->buildingId = $oasisId;
  1163. $newTask->procParams = $this->villageOases[$oasisId]['rel_x']." ".$this->villageOases[$oasisId]['rel_y'];
  1164. $this->queueModel->addTask( $newTask );
  1165. }
  1166. else
  1167. {
  1168. if ( isset( $_GET['qid'] ) && 0 < intval( $_GET['qid'] ) )
  1169. {
  1170. $this->queueModel->cancelTask( $this->player->playerId, intval( $_GET['qid'] ) );
  1171. }
  1172. }
  1173. }
  1174. }
  1175. }
  1176. public function preRender( )
  1177. {
  1178. parent::prerender( );
  1179. /* if ( isset( $_GET['p'] ) )
  1180. {
  1181. $_41448384 .= "villagesLinkPostfix";
  1182. }
  1183. if ( isset( $_GET['vid2'] ) )
  1184. {
  1185. $this->villagesLinkPostfix .= "&t=".$this->selectedTabIndex;
  1186. $_41448784 .= "villagesLinkPostfix";
  1187. }
  1188. if ( 0 < $this->selectedTabIndex )
  1189. {
  1190. $this->villagesLinkPostfix .= "&t=".$this->selectedTabIndex;
  1191. // $ && _41448984 .= "villagesLinkPostfix";
  1192. } */
  1193. }
  1194. public function __getCoordInRange( $map_size, $x )
  1195. {
  1196. if ( $map_size <= $x )
  1197. {
  1198. $x -= $map_size;
  1199. }
  1200. else if ( $x < 0 )
  1201. {
  1202. $x = $map_size + $x;
  1203. }
  1204. return $x;
  1205. }
  1206. public function __getVillageId( $map_size, $x, $y )
  1207. {
  1208. return $x * $map_size + ( $y + 1 );
  1209. }
  1210. public function _getOnlyMyOuterTroops( )
  1211. {
  1212. $returnTroops = array( );
  1213. if ( trim( $this->data['troops_out_num'] ) != "" )
  1214. {
  1215. $t_arr = explode( "|", $this->data['troops_out_num'] );
  1216. foreach ( $t_arr as $t_str )
  1217. {
  1218. $t2_arr = explode( ":", $t_str );
  1219. $t2_arr = explode( ",", $t2_arr[1] );
  1220. foreach ( $t2_arr as $t2_str )
  1221. {
  1222. $t = explode( " ", $t2_str );
  1223. if ( $t[1] == 0 - 1 )
  1224. {
  1225. continue;
  1226. }
  1227. if ( isset( $returnTroops[$t[0]] ) )
  1228. {
  1229. $returnTroops += $t[0];
  1230. }
  1231. else
  1232. {
  1233. $returnTroops[$t[0]] = $t[1];
  1234. }
  1235. }
  1236. }
  1237. }
  1238. if ( trim( $this->data['troops_out_intrap_num'] ) != "" )
  1239. {
  1240. $t_arr = explode( "|", $this->data['troops_out_intrap_num'] );
  1241. foreach ( $t_arr as $t_str )
  1242. {
  1243. $t2_arr = explode( ":", $t_str );
  1244. $t2_arr = explode( ",", $t2_arr[1] );
  1245. foreach ( $t2_arr as $t2_str )
  1246. {
  1247. $t = explode( " ", $t2_str );
  1248. if ( $t[1] == 0 - 1 )
  1249. {
  1250. continue;
  1251. }
  1252. if ( isset( $returnTroops[$t[0]] ) )
  1253. {
  1254. $returnTroops += $t[0];
  1255. }
  1256. else
  1257. {
  1258. $returnTroops[$t[0]] = $t[1];
  1259. }
  1260. }
  1261. }
  1262. }
  1263. return $returnTroops;
  1264. }
  1265. public function _getOnlyMyTroops( $toBeHero = FALSE )
  1266. {
  1267. $t_arr = explode( "|", $this->data['troops_num'] );
  1268. foreach ( $t_arr as $t_str )
  1269. {
  1270. $t2_arr = explode( ":", $t_str );
  1271. if ( $t2_arr[0] == 0 - 1 )
  1272. {
  1273. $t2_arr = explode( ",", $t2_arr[1] );
  1274. foreach ( $t2_arr as $t2_str )
  1275. {
  1276. $t = explode( " ", $t2_str );
  1277. if ( $toBeHero && ( $t[0] == 99 || $t[0] == 7 || $t[0] == 8 || $t[0] == 9 || $t[0] == 10 || $t[0] == 17 || $t[0] == 18 || $t[0] == 19 || $t[0] == 20 || $t[0] == 27 || $t[0] == 28 || $t[0] == 29 || $t[0] == 30 || $t[0] == 106 || $t[0] == 107 || $t[0] == 108 || $t[0] == 109 || $t[0] == 57 || $t[0] == 58 || $t[0] == 59 || $t[0] == 60 ) )
  1278. {
  1279. continue;
  1280. }
  1281. if ( isset( $this->troops[$t[0]] ) )
  1282. {
  1283. $this->troops += $t[0];
  1284. }
  1285. else
  1286. {
  1287. $this->troops[$t[0]] = $t[1];
  1288. }
  1289. }
  1290. }
  1291. }
  1292. if ( !$toBeHero && !isset( $this->troops[99] ) )
  1293. {
  1294. $this->troops[99] = 0;
  1295. }
  1296. }
  1297. public function _getMaxBuildingLevel( $itemId )
  1298. {
  1299. $result = 0;
  1300. foreach ( $this->buildings as $villageBuild )
  1301. {
  1302. if ( $villageBuild['item_id'] == $itemId && $result < $villageBuild['level'] )
  1303. {
  1304. $result = $villageBuild['level'];
  1305. }
  1306. }
  1307. return $result;
  1308. }
  1309. public function _getCurrentNumberFor( $troopId, $item )
  1310. {
  1311. $num = 0;
  1312. if ( isset( $this->troops[$troopId] ) )
  1313. {
  1314. $num += $this->troops[$troopId];
  1315. }
  1316. if ( isset( $this->queueModel->tasksInQueue[$this->troopsUpgradeType][$item], $this->queueModel->tasksInQueue[$this->troopsUpgradeType][$item] ) )
  1317. {
  1318. $qts = $this->queueModel->tasksInQueue[$this->troopsUpgradeType][$item];
  1319. foreach ( $qts as $qt )
  1320. {
  1321. if ( $qt['proc_params'] == $troopId )
  1322. {
  1323. $num += $qt['threads'];
  1324. }
  1325. }
  1326. }
  1327. $num += $this->_getTroopCountInTransfer( $troopId, QS_WAR_REINFORCE );
  1328. $num += $this->_getTroopCountInTransfer( $troopId, QS_WAR_ATTACK );
  1329. $num += $this->_getTroopCountInTransfer( $troopId, QS_WAR_ATTACK_PLUNDER );
  1330. $num += $this->_getTroopCountInTransfer( $troopId, QS_WAR_ATTACK_SPY );
  1331. $num += $this->_getTroopCountInTransfer( $troopId, QS_CREATEVILLAGE );
  1332. $ts = $this->_getOnlyMyOuterTroops( );
  1333. if ( isset( $ts[$troopId] ) )
  1334. {
  1335. $num += $ts[$troopId];
  1336. }
  1337. return $num;
  1338. }
  1339. public function _getTroopCountInTransfer( $troopId, $type )
  1340. {
  1341. $num = 0;
  1342. if ( isset( $this->queueModel->tasksInQueue[$type] ) )
  1343. {
  1344. $qts = $this->queueModel->tasksInQueue[$type];
  1345. foreach ( $qts as $qt )
  1346. {
  1347. $arr = explode( "|", $qt['proc_params'] );
  1348. $arr = explode( ",", $arr[0] );
  1349. foreach ( $arr as $arrStr )
  1350. {
  1351. $tnum = explode( " ", $arrStr );
  1352. $tid = explode( " ", $arrStr );
  1353. list ( $tid, $tnum ) = $tid;
  1354. if ( $tid == $troopId )
  1355. {
  1356. $num += $tnum;
  1357. }
  1358. }
  1359. }
  1360. }
  1361. return $num;
  1362. }
  1363. public function _getMaxTrainNumber( $troopId, $item )
  1364. {
  1365. $max = 0;
  1366. $_f = TRUE;
  1367. foreach ( $this->gameMetadata['troops'][$troopId]['training_resources'] as $k => $v )
  1368. {
  1369. $num = floor( $this->resources[$k]['current_value'] / ( $v * $this->buildingTribeFactor ) );
  1370. if ( $num < $max || $_f )
  1371. {
  1372. $_f = FALSE;
  1373. $max = $num;
  1374. }
  1375. }
  1376. if ( $troopId == 99 )
  1377. {
  1378. $buildingMetadata = $this->gameMetadata['items'][$this->buildings[$this->buildingIndex]['item_id']]['levels'][$this->buildProperties['building']['level'] - 1];
  1379. $_maxValue = $buildingMetadata['value'] - $this->troops[$troopId];
  1380. if ( isset( $this->queueModel->tasksInQueue[$this->troopsUpgradeType][$this->buildProperties['building']['item_id']], $this->queueModel->tasksInQueue[$this->troopsUpgradeType][$this->buildProperties['building']['item_id']] ) )
  1381. {
  1382. $qts = $this->queueModel->tasksInQueue[$this->troopsUpgradeType][$this->buildProperties['building']['item_id']];
  1383. foreach ( $qts as $qt )
  1384. {
  1385. if ( $qt['proc_params'] == $troopId )
  1386. {
  1387. $_maxValue -= $qt['threads'];
  1388. }
  1389. }
  1390. }
  1391. if ( $_maxValue < $max )
  1392. {
  1393. $max = $_maxValue;
  1394. }
  1395. }
  1396. else if ( $item == 25 || $item == 26 )
  1397. {
  1398. $_maxValue = $troopId == 9 || $troopId == 19 || $troopId == 29 || $troopId == 108 || $troopId == 59 ? 1 : 3;
  1399. $_maxValue -= $this->_getCurrentNumberFor( $troopId, $item );
  1400. if ( $_maxValue < $max )
  1401. {
  1402. $max = $_maxValue;
  1403. }
  1404. }
  1405. return $max < 0 ? 0 : $max;
  1406. }
  1407. public function _canTrainInBuilding( $troopId, $itemId )
  1408. {
  1409. foreach ( $this->gameMetadata['troops'][$troopId]['trainer_building'] as $buildingId )
  1410. {
  1411. if ( $buildingId == $itemId )
  1412. {
  1413. return TRUE;
  1414. break;
  1415. }
  1416. }
  1417. return FALSE;
  1418. }
  1419. public function _canDoResearches( $troopId )
  1420. {
  1421. foreach ( $this->gameMetadata['troops'][$troopId]['pre_requests'] as $req_item_id => $level )
  1422. {
  1423. $result = FALSE;
  1424. foreach ( $this->buildings as $villageBuild )
  1425. {
  1426. if ( $villageBuild['item_id'] == $req_item_id && $level <= $villageBuild['level'] )
  1427. {
  1428. $result = TRUE;
  1429. break;
  1430. }
  1431. }
  1432. if ( !$result )
  1433. {
  1434. return FALSE;
  1435. break;
  1436. }
  1437. }
  1438. return TRUE;
  1439. }
  1440. public function getNeededTime( $neededResources )
  1441. {
  1442. $timeInSeconds = 0;
  1443. foreach ( $neededResources as $k => $v )
  1444. {
  1445. if ( !( $this->resources[$k]['current_value'] < $v ) )
  1446. {
  1447. continue;
  1448. }
  1449. else if ( $this->resources[$k]['calc_prod_rate'] <= 0 )
  1450. {
  1451. return 0 - 1;
  1452. }
  1453. else
  1454. {
  1455. $time = ( $v - $this->resources[$k]['current_value'] ) / $this->resources[$k]['calc_prod_rate'];
  1456. if ( $timeInSeconds < $time )
  1457. {
  1458. $timeInSeconds = $time;
  1459. }
  1460. }
  1461. }
  1462. return ceil( $timeInSeconds * 3600 );
  1463. }
  1464. public function getActionText4( $neededResources, $url, $text, $queueTaskType, $buildLevel, $troopLevel )
  1465. {
  1466. if ( isset( $this->queueModel->tasksInQueue[$queueTaskType] ) )
  1467. {
  1468. return "<span class=\"none\">".buildings_p_plwait."</span>";
  1469. }
  1470. if ( $buildLevel <= $troopLevel )
  1471. {
  1472. return "<span class=\"none\">".buildings_p_needmorecapacity."</span>";
  1473. }
  1474. return !$this->isResourcesAvailable( $neededResources ) ? "<span class=\"none\">".buildings_p_notenoughres."</span>" : "<a class=\"build\" href=\"build.php?id=".$this->buildingIndex."&".$url."&k=".$this->data['update_key']."\">".$text."</a>";
  1475. }
  1476. public function getActionText3( $neededResources, $url, $text, $queueTaskType )
  1477. {
  1478. if ( isset( $this->queueModel->tasksInQueue[$queueTaskType] ) )
  1479. {
  1480. return "<span class=\"none\">".buildings_p_plwait."</span>";
  1481. }
  1482. return !$this->isResourcesAvailable( $neededResources ) ? "<span class=\"none\">".buildings_p_notenoughres."</span>" : "<a class=\"build\" href=\"build.php?id=".$this->buildingIndex."&".$url."&k=".$this->data['update_key']."\">".$text."</a>";
  1483. }
  1484. public function getActionText2( $neededResources )
  1485. {
  1486. $needUpgradeType = $this->needMoreUpgrades( $neededResources );
  1487. if ( 0 < $needUpgradeType )
  1488. {
  1489. switch ( $needUpgradeType )
  1490. {
  1491. case 2 :
  1492. return "<span class=\"none\">".buildings_p_upg1."</span>";
  1493. case 3 :
  1494. return "<span class=\"none\">".buildings_p_upg2."</span>";
  1495. case 4 :
  1496. return "<span class=\"none\">".buildings_p_upg3."</span>";
  1497. }
  1498. }
  1499. if ( !$this->isResourcesAvailable( $neededResources ) )
  1500. {
  1501. $neededTime = $this->getNeededTime( $neededResources );
  1502. return "<span class=\"none\">".( 0 < $neededTime ? buildings_p_willenoughresat." ".( $neededTime )." ".time_hour_lang : buildings_p_notenoughres2 )."</span>";
  1503. }
  1504. return "";
  1505. }
  1506. public function getActionText( $neededResources, $isField, $upgrade, $item_id )
  1507. {
  1508. $needUpgradeType = $this->needMoreUpgrades( $neededResources, $item_id );
  1509. if ( 0 < $needUpgradeType )
  1510. {
  1511. switch ( $needUpgradeType )
  1512. {
  1513. case 1 :
  1514. return "<span class=\"none\">".buildings_p_upg0."</span>";
  1515. case 2 :
  1516. return "<span class=\"none\">".buildings_p_upg1."</span>";
  1517. case 3 :
  1518. return "<span class=\"none\">".buildings_p_upg2."</span>";
  1519. case 4 :
  1520. return "<span class=\"none\">".buildings_p_upg3."</span>";
  1521. }
  1522. }
  1523. else
  1524. {
  1525. $pageNamePostfix = "2";
  1526. $link = "<a class=\"build\" href=\"village2.php?id=".$this->buildingIndex."&b=".$item_id."&k=".$this->data['update_key']."\">".buildings_p_create_newbuild."</a>";
  1527. $workerResult = $this->isWorkerBusy( $isField );
  1528. return $link.( "" );
  1529. }
  1530. $neededTime = $this->getNeededTime( $neededResources );
  1531. return "<span class=\"none\">".( buildings_p_notenoughres2 )."</span>";
  1532. }
  1533. public function _canAcceptOffer( $needResources, $giveResources, $villageId, $onlyForAlliance, $allianceId, $maxTime, $distance )
  1534. {
  1535. if ( $villageId == $this->data['selected_village_id'] )
  1536. {
  1537. return 0;
  1538. }
  1539. if ( !$this->isResourcesAvailable( $giveResources ) )
  1540. {
  1541. return 1;
  1542. }
  1543. $needMerchantCount = ceil( ( $giveResources[1] + $giveResources[2] + $giveResources[3] + $giveResources[4] ) / $this->merchantProperty['capacity'] );
  1544. if ( $needMerchantCount == 0 || $this->merchantProperty['exits_num'] < $needMerchantCount )
  1545. {
  1546. return 2;
  1547. }
  1548. if ( $onlyForAlliance && ( intval( $this->data['alliance_id'] ) == 0 || $allianceId != intval( $this->data['alliance_id'] ) ) )
  1549. {
  1550. return 3;
  1551. }
  1552. if ( 0 < $maxTime && $maxTime < $distance / $this->merchantProperty['speed'] )
  1553. {
  1554. return 4;
  1555. }
  1556. return 5;
  1557. }
  1558. public function getNextLink( )
  1559. {
  1560. $text = "Âť";
  1561. if ( $this->pageIndex + 1 == $this->pageCount )
  1562. {
  1563. return $text;
  1564. }
  1565. $link = "";
  1566. if ( 0 < $this->selectedTabIndex )
  1567. {
  1568. $link .= "t=".$this->selectedTabIndex;
  1569. }
  1570. if ( $link != "" )
  1571. {
  1572. $link .= "&";
  1573. }
  1574. $link .= "p=".( $this->pageIndex + 1 );
  1575. $link = "build.php?id=".$this->buildingIndex."&".$link;
  1576. return "<a href=\"".$link."\">".$text."</a>";
  1577. }
  1578. public function getPreviousLink( )
  1579. {
  1580. $text = "ÂŤ";
  1581. if ( $this->pageIndex == 0 )
  1582. {
  1583. return $text;
  1584. }
  1585. $link = "";
  1586. if ( 0 < $this->selectedTabIndex )
  1587. {
  1588. $link .= "t=".$this->selectedTabIndex;
  1589. }
  1590. if ( 1 < $this->pageIndex )
  1591. {
  1592. if ( $link != "" )
  1593. {
  1594. $link .= "&";
  1595. }
  1596. $link .= "p=".( $this->pageIndex - 1 );
  1597. }
  1598. $link = "build.php?id=".$this->buildingIndex."&".$link;
  1599. return "<a href=\"".$link."\">".$text."</a>";
  1600. }
  1601. public function getResourceGoldExchange( $neededResources, $itemId, $buildingIndex, $multiple = FALSE )
  1602. {
  1603. if ( $this->data['gold_num'] < $this->gameMetadata['plusTable'][6]['cost'] || 0 < $this->needMoreUpgrades( $neededResources, $itemId ) || $this->isResourcesAvailable( $neededResources ) && !$multiple )
  1604. {
  1605. return "";
  1606. }
  1607. $s1 = 0;
  1608. $s2 = 0;
  1609. $exchangeResource = "";
  1610. foreach ( $neededResources as $k => $v )
  1611. {
  1612. $s1 += $v;
  1613. $s2 += $this->resources[$k]['current_value'];
  1614. if ( $exchangeResource != "" )
  1615. {
  1616. $exchangeResource .= "&";
  1617. }
  1618. $exchangeResource .= "r".$k."=".$v;
  1619. }
  1620. $canExchange = $s1 <= $s2;
  1621. if ( $multiple && $canExchange )
  1622. {
  1623. $num = floor( $s2 / $s1 );
  1624. $exchangeResource = "";
  1625. foreach ( $neededResources as $k => $v )
  1626. {
  1627. if ( $exchangeResource != "" )
  1628. {
  1629. $exchangeResource .= "&";
  1630. }
  1631. $exchangeResource .= "r".$k."=".$v * $num;
  1632. }
  1633. }
  1634. return " | <a href=\"build.php?bid=17&t=3&rid=".$buildingIndex."&".$exchangeResource."\" title=\"".buildings_p_m2m."\"><img class=\"npc".( $canExchange ? "" : "_inactive" )."\" src=\"assets/x.gif\" alt=\"".buildings_p_m2m."\" title=\"".buildings_p_m2m."\"></a>";
  1635. }
  1636. }
  1637. $p = new GPage( );
  1638. $p->run( );
  1639. print_r($GLOBALS);
  1640. ?>