Advertisement
Guest User

build.php

a guest
Jun 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 82.75 KB | None | 0 0
  1. <?php
  2.  
  3. ###############################################################################
  4.  
  5. ## This particular work may not be copied or edited, powered by Ratovi Games ##
  6.  
  7. ###############################################################################
  8.  
  9. require '.' . DIRECTORY_SEPARATOR . 'app_ndc' . DIRECTORY_SEPARATOR . 'boot.php';
  10.  
  11. require_once MODEL_PATH . 'build.php';
  12.  
  13. require_once MODEL_PATH . 'village3.php';
  14.  
  15. class GPage extends VillagePage
  16.  
  17. {
  18.  
  19. var $productionPane = TRUE;
  20.  
  21. var $buildingView = '';
  22.  
  23. var $buildingIndex = -1;
  24.  
  25. var $buildProperties = NULL;
  26.  
  27. var $newBuilds = NULL;
  28.  
  29. var $troopsUpgrade = null;
  30.  
  31. var $troopsUpgradeType = null;
  32.  
  33. var $buildingTribeFactor = null;
  34.  
  35. var $troops = array ();
  36.  
  37. var $selectedTabIndex = 0;
  38.  
  39. var $villageOases = null;
  40.  
  41. var $childVillages = null;
  42.  
  43. var $hasHero = FALSE;
  44.  
  45. var $totalCpRate = null;
  46.  
  47. var $totalCpValue = null;
  48.  
  49. var $neededCpValue = null;
  50.  
  51. var $childVillagesCount = null;
  52.  
  53. var $showBuildingForm = null;
  54.  
  55. var $embassyProperty = null;
  56.  
  57. var $merchantProperty = null;
  58.  
  59. var $rallyPointProperty = null;
  60.  
  61. var $crannyProperty = array ('buildingCount' => 0, 'totalSize' => 0);
  62.  
  63. var $warriorMessage = null;
  64.  
  65. var $dataList = null;
  66.  
  67. var $pageSize = 20;
  68.  
  69. var $pageCount = null;
  70.  
  71. var $pageIndex = null;
  72.  
  73. function GPage ()
  74.  
  75. {
  76.  
  77. parent::villagepage ();
  78.  
  79. $this->viewFile = 'build.phtml';
  80.  
  81. $this->contentCssClass = 'build';
  82.  
  83. }
  84.  
  85. function onLoadBuildings ($building)
  86.  
  87. {
  88.  
  89. $GameMetadata = $GLOBALS['GameMetadata'];
  90.  
  91. if (((($this->buildingIndex == 0 - 1 AND isset ($_GET['bid'])) AND is_numeric ($_GET['bid'])) AND $_GET['bid'] == $building['item_id']))
  92.  
  93. {
  94.  
  95. $this->buildingIndex = $building['index'];
  96.  
  97. }
  98.  
  99. if (($building['item_id'] == 23 AND 0 < $building['level']))
  100.  
  101. {
  102.  
  103. ++$this->crannyProperty['buildingCount'];
  104.  
  105. $this->crannyProperty['totalSize'] += $GameMetadata['items'][$building['item_id']]['levels'][$building['level'] - 1]['value'] * $GameMetadata['items'][$building['item_id']]['for_tribe_id'][$this->tribeId];
  106.  
  107. }
  108.  
  109. }
  110.  
  111. function load ()
  112.  
  113. {
  114.  
  115. parent::load ();
  116.  
  117. if ($this->data['is_special_village'] == 1){
  118.  
  119. if ($_GET['id'] == 26 || $_GET['id'] == 33 || $_GET['id'] == 29 || $_GET['id'] == 30) {
  120.  
  121. $this->buildingIndex = 25;
  122.  
  123. //$this->redirect ('build?id=25');
  124.  
  125. //return null;
  126.  
  127. }
  128.  
  129. }
  130.  
  131. if ( $this->dataGame['blocked_time'] > time() ){
  132.  
  133. $this->redirect ('banned');
  134.  
  135. return null;
  136.  
  137. }
  138.  
  139. if (((($this->buildingIndex == 0 - 1 AND isset ($_GET['id'])) AND is_numeric ($_GET['id'])) AND isset ($this->buildings[$_GET['id']])))
  140.  
  141. {
  142.  
  143. $this->buildingIndex = intval ($_GET['id']);
  144.  
  145. }
  146.  
  147. $this->buildProperties = $this->getBuildingProperties ($this->buildingIndex);
  148.  
  149. if ($this->buildProperties == NULL)
  150.  
  151. {
  152.  
  153. $this->redirect ('village1');
  154.  
  155. return null;
  156.  
  157. }
  158.  
  159. if ($this->buildProperties['emptyPlace'])
  160.  
  161. {
  162.  
  163. $this->villagesLinkPostfix .= '&id=' . $this->buildingIndex;
  164.  
  165. $this->newBuilds = array ('available' => array (), 'soon' => array ());
  166.  
  167. foreach ($this->gameMetadata['items'] as $item_id => $build)
  168.  
  169. {
  170.  
  171. if (($item_id <= 4 OR !isset ($build['for_tribe_id'][$this->tribeId])))
  172.  
  173. {
  174.  
  175. if ($this->tribeId != 5) {
  176.  
  177. continue;
  178.  
  179. }
  180.  
  181. }
  182.  
  183. $canBuild = $this->canCreateNewBuild ($item_id);
  184.  
  185. if ($canBuild != 0 - 1)
  186.  
  187. {
  188.  
  189. if ($canBuild)
  190.  
  191. {
  192.  
  193. if (!isset ($this->newBuilds['available'][$build['levels'][0]['time_consume']]))
  194.  
  195. {
  196.  
  197. $this->newBuilds['available'][$build['levels'][0]['time_consume']] = array ();
  198.  
  199. }
  200.  
  201. $this->newBuilds['available'][$build['levels'][0]['time_consume']][$item_id] = $build;
  202.  
  203. continue;
  204.  
  205. }
  206.  
  207. else
  208.  
  209. {
  210.  
  211. $dependencyCount = 0;
  212.  
  213. foreach ($build['pre_requests'] as $reqId => $reqValue)
  214.  
  215. {
  216.  
  217. if ($reqValue != NULL)
  218.  
  219. {
  220.  
  221. $build['pre_requests_dependencyCount'][$reqId] = $reqValue - $this->_getMaxBuildingLevel ($reqId);
  222.  
  223. $dependencyCount += $build['pre_requests_dependencyCount'][$reqId];
  224.  
  225. continue;
  226.  
  227. }
  228.  
  229. }
  230.  
  231. if (!isset ($this->newBuilds['soon'][$dependencyCount]))
  232.  
  233. {
  234.  
  235. $this->newBuilds['soon'][$dependencyCount] = array ();
  236.  
  237. }
  238.  
  239. $this->newBuilds['soon'][$dependencyCount][$item_id] = $build;
  240.  
  241. continue;
  242.  
  243. }
  244.  
  245. continue;
  246.  
  247. }
  248.  
  249. }
  250.  
  251. ksort ($this->newBuilds['available'], SORT_NUMERIC);
  252.  
  253. ksort ($this->newBuilds['soon'], SORT_NUMERIC);
  254.  
  255. return null;
  256.  
  257. }
  258.  
  259. $bitemId = $this->buildProperties['building']['item_id'];
  260.  
  261. $this->villagesLinkPostfix .= '&id=' . $this->buildingIndex;
  262.  
  263. if (4 < $bitemId)
  264.  
  265. {
  266.  
  267. $this->villagesLinkPostfix .= '&bid=' . $bitemId;
  268.  
  269. }
  270.  
  271. $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);
  272.  
  273. if ($this->buildings[$this->buildingIndex]['level'] == 0)
  274.  
  275. {
  276.  
  277. return null;
  278.  
  279. }
  280.  
  281. switch ($bitemId)
  282.  
  283. {
  284.  
  285. case 12:
  286.  
  287. {
  288.  
  289. }
  290.  
  291. case 13:
  292.  
  293. {
  294.  
  295. $this->productionPane = FALSE;
  296.  
  297. $this->buildingView = 'Blacksmith_Armoury';
  298.  
  299. $this->handleBlacksmithArmoury ();
  300.  
  301. break;
  302.  
  303. }
  304.  
  305. case 15:
  306.  
  307. {
  308.  
  309. if (10 <= $this->buildings[$this->buildingIndex]['level'])
  310.  
  311. {
  312.  
  313. $this->buildingView = 'MainBuilding';
  314.  
  315. $this->handleMainBuilding ();
  316.  
  317. }
  318.  
  319. break;
  320.  
  321. }
  322.  
  323. case 16:
  324.  
  325. {
  326.  
  327. $this->productionPane = FALSE;
  328.  
  329. $this->buildingView = 'RallyPoint';
  330.  
  331. $this->handleRallyPoint ();
  332.  
  333. break;
  334.  
  335. }
  336.  
  337. case 17:
  338.  
  339. {
  340.  
  341. $this->productionPane = FALSE;
  342.  
  343. $this->_getOnlyMyTroops( );
  344.  
  345. $this->buildingView = 'Marketplace';
  346.  
  347. $this->handleMarketplace ();
  348.  
  349. $this->handleWarrior( );
  350.  
  351. break;
  352.  
  353. }
  354.  
  355. case 18:
  356.  
  357. {
  358.  
  359. $this->productionPane = FALSE;
  360.  
  361. $this->buildingView = 'Embassy';
  362.  
  363. $this->handleEmbassy ();
  364.  
  365. break;
  366.  
  367. }
  368.  
  369. case 19:
  370.  
  371. {
  372.  
  373. }
  374.  
  375. case 20:
  376.  
  377. {
  378.  
  379. }
  380.  
  381. case 21:
  382.  
  383. {
  384.  
  385. }
  386.  
  387. case 29:
  388.  
  389. {
  390.  
  391. }
  392.  
  393. case 30:
  394.  
  395. {
  396.  
  397. }
  398.  
  399. case 36:
  400.  
  401. {
  402.  
  403. $this->_getOnlyMyTroops ();
  404.  
  405. $this->productionPane = $bitemId == 36;
  406.  
  407. $this->buildingView = 'TroopBuilding';
  408.  
  409. $this->handleTroopBuilding ();
  410.  
  411. break;
  412.  
  413. }
  414.  
  415. case 22:
  416.  
  417. {
  418.  
  419. $this->productionPane = FALSE;
  420.  
  421. $this->buildingView = 'Academy';
  422.  
  423. $this->handleAcademy ();
  424.  
  425. break;
  426.  
  427. }
  428.  
  429. case 23:
  430.  
  431. {
  432.  
  433. $this->productionPane = TRUE;
  434.  
  435. $this->buildingView = 'Cranny';
  436.  
  437. break;
  438.  
  439. }
  440.  
  441. case 24:
  442.  
  443. {
  444.  
  445. $this->productionPane = FALSE;
  446.  
  447. $this->buildingView = 'TownHall';
  448.  
  449. $this->handleTownHall ();
  450.  
  451. break;
  452.  
  453. }
  454.  
  455. case 25:
  456.  
  457. {
  458.  
  459. }
  460.  
  461. case 26:
  462.  
  463. {
  464.  
  465. $this->productionPane = FALSE;
  466.  
  467. $this->buildingView = 'Residence_Palace';
  468.  
  469. $this->handleResidencePalace ();
  470.  
  471. break;
  472.  
  473. }
  474.  
  475. case 27:
  476.  
  477. {
  478.  
  479. $this->productionPane = FALSE;
  480.  
  481. $this->buildingView   = 'Treasury';
  482.  
  483. break;
  484.  
  485. }
  486.  
  487. case 37:
  488.  
  489. {
  490.  
  491. $this->productionPane = FALSE;
  492.  
  493. $this->buildingView = 'HerosMansion';
  494.  
  495. $this->handleHerosMansion ();
  496.  
  497. break;
  498.  
  499. }
  500.  
  501. case 40:
  502.  
  503. {
  504.  
  505. $this->productionPane = FALSE;
  506.  
  507. }
  508.  
  509. case 42:
  510.  
  511. {
  512.  
  513. $this->_getOnlyMyTroops( );
  514.  
  515. $this->productionPane = TRUE;
  516.  
  517. $this->buildingView = "Warrior";
  518.  
  519. $this->handleWarrior( );
  520.  
  521. break;
  522.  
  523. }
  524.  
  525. }
  526.  
  527. }
  528.  
  529. function handleBlacksmithArmoury ()
  530.  
  531. {
  532.  
  533. $this->troopsUpgradeType = ($this->buildings[$this->buildingIndex]['item_id'] == 12 ? QS_TROOP_UPGRADE_ATTACK : QS_TROOP_UPGRADE_DEFENSE);
  534.  
  535. $this->troopsUpgrade = array ();
  536.  
  537. $_arr = explode (',', $this->data['troops_training']);
  538.  
  539. $_c = 0;
  540.  
  541. foreach ($_arr as $troopStr)
  542.  
  543. {
  544.  
  545. ++$_c;
  546.  
  547. list ($troopId, $researches_done, $defense_level, $attack_level) = explode (' ', $troopStr);
  548.  
  549. $tlevel = ($this->troopsUpgradeType == QS_TROOP_UPGRADE_ATTACK ? $attack_level : $defense_level);
  550.  
  551. if (((($troopId != 99 AND $_c <= 8)) AND $researches_done == 1))
  552.  
  553. {
  554.  
  555. $this->troopsUpgrade[$troopId] = $tlevel;
  556.  
  557. continue;
  558.  
  559. }
  560.  
  561. }
  562.  
  563.  
  564.  
  565. if(isset ($_GET['a']) AND !isset($_GET['k']) AND !isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType]) AND isset ($this->troopsUpgrade[intval ($_GET['a'])]) AND !$this->isGameTransientStopped () AND !$this->isGameOver ())
  566.  
  567. {
  568.  
  569.      $troopId = intval ($_GET['a']);
  570.  
  571.      $level = $this->troopsUpgrade[$troopId];
  572.  
  573.      $levelbuildings = $this->buildings[$this->buildingIndex]['level'];
  574.  
  575.      $buildingMetadata = $this->gameMetadata['items'][$this->buildProperties['building']['item_id']]['troop_upgrades'][$troopId][$level];
  576.  
  577.      if($this->isGameOver ())
  578.  
  579.      {
  580.  
  581.           $this->redirect ('over');
  582.  
  583.      }
  584.  
  585.      if($this->isGameTransientStopped ())
  586.  
  587.      {
  588.  
  589.          return null;
  590.  
  591.      }
  592.  
  593.      if($levelbuildings <= $level)
  594.  
  595.      {
  596.  
  597.           return null;
  598.  
  599.      }
  600.  
  601.      elseif($this->data['gold_num'] < $this->appConfig['Game']['dev_troop_to_20'])
  602.  
  603.      {
  604.  
  605.           return null;
  606.  
  607.      }
  608.  
  609.  
  610.  
  611.      $for_level = 20-$level;
  612.  
  613.  
  614.  
  615.      if($for_level > 0)
  616.  
  617.      {
  618.  
  619.           $mq = new QueueJobModel( );
  620.  
  621.           if($this->buildings[$this->buildingIndex]['item_id'] == 12)
  622.  
  623.           {
  624.  
  625.                $mq->provider->executeQuery2("INSERT INTO p_queue SET player_id='%s', village_id='%s', to_player_id='', to_village_id='', proc_type=5, building_id='', proc_params='%s', threads=1, end_date='2012-08-27 02:46:24', execution_time='500'", array($this->player->playerId, $this->data['selected_village_id'], "$troopId $levelbuildings"));
  626.  
  627.           }
  628.  
  629.           else
  630.  
  631.           {
  632.  
  633.                $mq->provider->executeQuery2("INSERT INTO p_queue SET player_id='%s', village_id='%s', to_player_id='', to_village_id='', proc_type=6, building_id='', proc_params='%s', threads=1, end_date='2012-08-27 02:46:24', execution_time='500'", array($this->player->playerId, $this->data['selected_village_id'], "$troopId $levelbuildings"));
  634.  
  635.           }
  636.  
  637.           $mq->provider->executeQuery2 ("UPDATE `p_players` SET `gold_num` = `gold_num` - %s WHERE id=%s", array($this->appConfig['Game']['dev_troop_to_20'], $this->player->playerId));
  638.  
  639. //start pgold
  640.  
  641. $tatarzx = new QueueModel();
  642.  
  643. $d = date('Y/m/d H:i:s');
  644.  
  645. $n = $this->data['name'];
  646.  
  647. $tatarzx->provider->executeQuery("INSERT INTO `p_plus` (`pid`, `date`, `gold`, `where`) VALUES ('".$n."', '".$d."', '".$this->appConfig['Game']['dev_troop_to_20']."', 'تحسين جندي الى المستوى 20');");
  648.  
  649. //end pgold
  650.  
  651. $this->redirect ('build?id='.$_GET['id'].'');
  652.  
  653.      }
  654.  
  655. }
  656.  
  657. elseif (((((((isset ($_GET['a']) AND isset ($_GET['k'])) AND $_GET['k'] == $this->data['update_key']) AND !isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType])) AND isset ($this->troopsUpgrade[intval ($_GET['a'])])) AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  658.  
  659. {
  660.  
  661. $troopId = intval ($_GET['a']);
  662.  
  663. $level = $this->troopsUpgrade[$troopId];
  664.  
  665. $levelbuildings = $this->buildings[$this->buildingIndex]['level'];
  666.  
  667. $buildingMetadata = $this->gameMetadata['items'][$this->buildProperties['building']['item_id']]['troop_upgrades'][$troopId][$level];
  668.  
  669. if (!$this->isResourcesAvailable ($buildingMetadata['resources']))
  670.  
  671. {
  672.  
  673. return null;
  674.  
  675. }elseif($levelbuildings <= $level)
  676.  
  677. {
  678.  
  679. return null;
  680.  
  681. }
  682.  
  683. $artefact= 10;
  684.  
  685. $q = new QueueModel();
  686.  
  687. $calcConsume = intval ($buildingMetadata['time_consume'] / $this->gameSpeed * ($artefact / ($this->buildProperties['building']['level'] + 9)));
  688.  
  689. $newTask = new QueueTask ($this->troopsUpgradeType, $this->player->playerId, $calcConsume);
  690.  
  691. $newTask->villageId = $this->data['selected_village_id'];
  692.  
  693. $newTask->procParams = $troopId . ' ' . ($level + 1);
  694.  
  695. $newTask->tag = $buildingMetadata['resources'];
  696.  
  697. $this->queueModel->addTask ($newTask);
  698.  
  699. }
  700.  
  701. }
  702.  
  703. function handleMainBuilding ()
  704.  
  705. {
  706.  
  707. if ((((((((($this->isPost () AND isset ($_POST['drbid'])) AND 19 <= intval ($_POST['drbid'])) AND intval ($_POST['drbid']) <= sizeof ($this->buildings)) AND isset ($this->buildings[$_POST['drbid']])) AND 0 < $this->buildings[$_POST['drbid']]['level']) AND !isset ($this->queueModel->tasksInQueue[QS_BUILD_DROP])) AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  708.  
  709. {
  710.  
  711. if (isset($_POST['full']) && $this->data['gold_num'] > 5) {
  712.  
  713. $qj = new QueueModel();
  714.  
  715. $gold = 5;
  716.  
  717. $qj->provider->executeQuery2("UPDATE p_players SET gold_num =gold_num-".$gold." WHERE id = '".$this->player->playerId."'");
  718.  
  719.  
  720.  
  721. $buildingArr = explode( ",", $this->data['buildings'] );
  722.  
  723.         $c = 0;
  724.  
  725.         foreach ( $buildingArr as $buildingItem )
  726.  
  727.         {
  728.  
  729.             ++$c;
  730.  
  731.             list($item_id, $level , $update_state) = explode( " ",$buildingItem );
  732.  
  733. if ($c == $this->buildings[$_POST['drbid']]['index']) {
  734.  
  735.             $dropLevels = $level + $update_state;
  736.  
  737.             while ( 0 < $dropLevels-- )
  738.  
  739.             {
  740.  
  741.                 $mq = new QueueJobModel( );
  742.  
  743.                 $mq->upgradeBuilding( $this->data['selected_village_id'], $c, $item_id, TRUE );
  744.  
  745.             }
  746.  
  747. }
  748.  
  749. }
  750.  
  751. $this->redirect ('build?bid=15');
  752.  
  753. }else {
  754.  
  755. $item_id = $this->buildings[$_POST['drbid']]['item_id'];
  756.  
  757. $calcConsume = intval ($this->gameMetadata['items'][$item_id]['levels'][$this->buildings[$_POST['drbid']]['level'] - 1]['time_consume'] / $this->gameSpeed * ($this->data['time_consume_percent'] / 400));
  758.  
  759. $newTask = new QueueTask (QS_BUILD_DROP, $this->player->playerId, $calcConsume);
  760.  
  761. $newTask->villageId = $this->data['selected_village_id'];
  762.  
  763. $newTask->buildingId = $item_id;
  764.  
  765. $newTask->procParams = $this->buildings[$_POST['drbid']]['index'];
  766.  
  767. $this->queueModel->addTask ($newTask);
  768.  
  769. }
  770.  
  771. return null;
  772.  
  773. }
  774.  
  775. if ((((((((isset ($_GET['qid']) AND is_numeric ($_GET['qid'])) AND isset ($_GET['k'])) AND $_GET['k'] == $this->data['update_key']) AND isset ($_GET['d'])) AND isset ($this->queueModel->tasksInQueue[QS_BUILD_DROP])) AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  776.  
  777. {
  778.  
  779. $this->queueModel->cancelTask ($this->player->playerId, intval ($_GET['qid']));
  780.  
  781. }
  782.  
  783. }
  784.  
  785. function handleRallyPoint ()
  786.  
  787. {
  788.  
  789.  
  790.  
  791. $m = new BuildModel ();
  792.  
  793. $this->hidetroop = $m->hidetroop($this->player->playerId);
  794.  
  795. if($this->isPost () AND $this->data['active_plus_account']){
  796.  
  797. if(isset ($_POST['troop_escape_active'])){ $hidetroop = 1; }else{ $hidetroop = 0; }
  798.  
  799. $m->uphidetroop($hidetroop, $this->player->playerId);
  800.  
  801. $this->redirect ('build?id=39&t=4');
  802.  
  803. }
  804.  
  805. if (isset ($_GET['d']))
  806.  
  807. {
  808.  
  809. $this->queueModel->cancelTask ($this->player->playerId, intval ($_GET['d']));
  810.  
  811. }
  812.  
  813.  $this->rallyPointProperty = array(
  814.  
  815.             'troops_in_village' => array(
  816.  
  817.                 'troopsTable' => $this->_getTroopsList('troops_num'),
  818.  
  819.                 'troopsIntrapTable' => $this->_getTroopsList('troops_intrap_num')
  820.  
  821.             ),
  822.  
  823.             'troops_out_village' => array(
  824.  
  825.                 'troopsTable' => $this->_getTroopsList('troops_out_num'),
  826.  
  827.                 'troopsIntrapTable' => $this->_getTroopsList('troops_out_intrap_num')
  828.  
  829.             ),
  830.  
  831.             'troops_in_oases' => array(),
  832.  
  833.             'war_to_village' => $this->queueModel->tasksInQueue['war_troops']['to_village'],
  834.  
  835.             'war_from_village' => $this->queueModel->tasksInQueue['war_troops']['from_village'],
  836.  
  837.             'war_to_oasis' => $this->queueModel->tasksInQueue['war_troops']['to_oasis']
  838.  
  839.         );
  840.  
  841.         $village_oases_id         = trim($this->data['village_oases_id']);if ($village_oases_id != '')
  842.  
  843. {
  844.  
  845. $m = new BuildModel ();
  846.  
  847. $result = $m->getOasesDataById ($village_oases_id);
  848.  
  849. while ($result->next ())
  850.  
  851. {
  852.  
  853. $this->rallyPointProperty['troops_in_oases'][$result->row['id']] = array ('oasisRow' => $result->row, 'troopsTable' => $this->_getOasisTroopsList ($result->row['troops_num']), 'war_to' => (isset ($this->rallyPointProperty['war_to_oasis'][$result->row['id']]) ? $this->rallyPointProperty['war_to_oasis'][$result->row['id']] : NULL));
  854.  
  855. }
  856.  
  857. $m->dispose ();
  858.  
  859. }
  860.  
  861. }
  862.  
  863. function _canCancelWarTask ($taskType, $taskId)
  864.  
  865. {
  866.  
  867. if (!QueueTask::iscancelabletask ($taskType))
  868.  
  869. {
  870.  
  871. return FALSE;
  872.  
  873. }
  874.  
  875. $timeout = QueueTask::getmaxcanceltimeout ($taskType);
  876.  
  877. if (0 - 1 < $timeout)
  878.  
  879. {
  880.  
  881. $_task = NULL;
  882.  
  883. foreach ($this->queueModel->tasksInQueue[$taskType] as $t)
  884.  
  885. {
  886.  
  887. if ($t['id'] == $taskId)
  888.  
  889. {
  890.  
  891. $_task = $t;
  892.  
  893. break;
  894.  
  895. }
  896.  
  897. }
  898.  
  899. if ($_task == NULL)
  900.  
  901. {
  902.  
  903. return FALSE;
  904.  
  905. }
  906.  
  907. $elapsedTime = $t['elapsedTime'];
  908.  
  909. if ($timeout < $elapsedTime)
  910.  
  911. {
  912.  
  913. return FALSE;
  914.  
  915. }
  916.  
  917. }
  918.  
  919. return TRUE;
  920.  
  921. }
  922.  
  923. function _getOasisTroopsList ($troops_num)
  924.  
  925. {
  926.  
  927. $GameMetadata = $GLOBALS['GameMetadata'];
  928.  
  929. $m = new BuildModel ();
  930.  
  931. $returnTroops = array ();
  932.  
  933. if (trim ($troops_num) != '')
  934.  
  935. {
  936.  
  937. $t_arr = explode ('|', $troops_num);
  938.  
  939. foreach ($t_arr as $t_str)
  940.  
  941. {
  942.  
  943. $t2_arr = explode (':', $t_str);
  944.  
  945. $vid = $t2_arr[0];
  946.  
  947. $villageData = $m->getVillageData2ById ($vid);
  948.  
  949. $returnTroops[$vid] = array ('villageData' => $villageData, 'cropConsumption' => 0, 'hasHero' => FALSE, 'troops' => array ());
  950.  
  951. $t2_arr = explode (',', $t2_arr[1]);
  952.  
  953. foreach ($t2_arr as $t2_str)
  954.  
  955. {
  956.  
  957. list ($tid, $tnum) = explode (' ', $t2_str);
  958.  
  959. if ($tid == 99)
  960.  
  961. {
  962.  
  963. continue;
  964.  
  965. }
  966.  
  967. if ($tnum == 0 - 1)
  968.  
  969. {
  970.  
  971. $tnum = 1;
  972.  
  973. $returnTroops[$vid]['hasHero'] = TRUE;
  974.  
  975. }
  976.  
  977. else
  978.  
  979. {
  980.  
  981. $returnTroops[$vid]['troops'][$tid] = $tnum;
  982.  
  983. }
  984.  
  985. $returnTroops[$vid]['cropConsumption'] += $GameMetadata['troops'][$tid]['crop_consumption'] * $tnum;
  986.  
  987. }
  988.  
  989. }
  990.  
  991. }
  992.  
  993. $m->dispose ();
  994.  
  995. return $returnTroops;
  996.  
  997. }
  998.  
  999. function _getTroopsList ($key)
  1000.  
  1001. {
  1002.  
  1003. $GameMetadata = $GLOBALS['GameMetadata'];
  1004.  
  1005. $m = new BuildModel ();
  1006.  
  1007. $returnTroops = array ();
  1008.  
  1009. if (trim ($this->data[$key]) != '')
  1010.  
  1011. {
  1012.  
  1013. $t_arr = explode ('|', $this->data[$key]);
  1014.  
  1015. foreach ($t_arr as $t_str)
  1016.  
  1017. {
  1018.  
  1019. $t2_arr = explode (':', $t_str);
  1020.  
  1021. $vid = intval ($t2_arr[0]);
  1022.  
  1023. $villageData = NULL;
  1024.  
  1025. if ($vid == 0 - 1)
  1026.  
  1027. {
  1028.  
  1029. $vid = $this->data['selected_village_id'];
  1030.  
  1031. $villageData = array ('id' => $vid, 'village_name' => $this->data['village_name'], 'player_id' => $this->player->playerId, 'player_name' => buildings_p_thisvillage);
  1032.  
  1033. }
  1034.  
  1035. else
  1036.  
  1037. {
  1038.  
  1039. $villageData = $m->getVillageData2ById ($vid);
  1040.  
  1041. }
  1042.  
  1043. $returnTroops[$vid] = array ('villageData' => $villageData, 'cropConsumption' => 0, 'hasHero' => FALSE, 'troops' => array ());
  1044.  
  1045. if ($vid == $this->data['selected_village_id'])
  1046.  
  1047. {
  1048.  
  1049. $returnTroops[$vid]['hasHero'] = intval ($this->data['hero_in_village_id']) == intval ($this->data['selected_village_id']);
  1050.  
  1051. if ($returnTroops[$vid]['hasHero'])
  1052.  
  1053. {
  1054.  
  1055. $returnTroops[$vid]['cropConsumption'] += $GameMetadata['troops'][$this->data['hero_troop_id']]['crop_consumption'];
  1056.  
  1057. }
  1058.  
  1059. }
  1060.  
  1061. $t2_arr = explode (',', $t2_arr[1]);
  1062.  
  1063. foreach ($t2_arr as $t2_str)
  1064.  
  1065. {
  1066.  
  1067. list ($tid, $tnum) = explode (' ', $t2_str);
  1068.  
  1069. if ($tid == 99)
  1070.  
  1071. {
  1072.  
  1073. continue;
  1074.  
  1075. }
  1076.  
  1077. if ($tnum == 0 - 1)
  1078.  
  1079. {
  1080.  
  1081. $tnum = 1;
  1082.  
  1083. $returnTroops[$vid]['hasHero'] = TRUE;
  1084.  
  1085. }
  1086.  
  1087. else
  1088.  
  1089. {
  1090.  
  1091. $returnTroops[$vid]['troops'][$tid] = $tnum;
  1092.  
  1093. }
  1094.  
  1095. $returnTroops[$vid]['cropConsumption'] += $GameMetadata['troops'][$tid]['crop_consumption'] * $tnum;
  1096.  
  1097. }
  1098.  
  1099. }
  1100.  
  1101. }
  1102.  
  1103. $m->dispose ();
  1104.  
  1105. return $returnTroops;
  1106.  
  1107. }
  1108.  
  1109. function handleMarketplace ()
  1110.  
  1111. {
  1112.  
  1113. $this->selectedTabIndex = ((((isset ($_GET['t']) AND is_numeric ($_GET['t'])) AND 1 <= intval ($_GET['t'])) AND intval ($_GET['t']) <= 4) ? intval ($_GET['t']) : 0);
  1114.  
  1115. $itemId = $this->buildings[$this->buildingIndex]['item_id'];
  1116.  
  1117. $itemLevel = $this->buildings[$this->buildingIndex]['level'];
  1118.  
  1119. $tribeMetadata = $this->gameMetadata['tribes'][$this->data['tribe_id']];
  1120.  
  1121. $tradeOfficeLevel = $this->_getMaxBuildingLevel (28);
  1122.  
  1123. $capacityFactor = ($tradeOfficeLevel == 0 ? 1 : $this->gameMetadata['items'][28]['levels'][$tradeOfficeLevel - 1]['value'] / 100);
  1124.  
  1125. $capacityFactor *= $this->gameMetadata['game_speed'];
  1126.  
  1127. $total_merchants_num = $this->gameMetadata['items'][$itemId]['levels'][$itemLevel - 1]['value'];
  1128.  
  1129. $exist_num = $total_merchants_num - $this->queueModel->tasksInQueue['out_merchants_num'] - $this->data['offer_merchants_count'];
  1130.  
  1131. if ($exist_num < 0)
  1132.  
  1133. {
  1134.  
  1135. $exist_num = 0;
  1136.  
  1137. }
  1138.  
  1139. $this->merchantProperty = array(
  1140.  
  1141. "speed" => $tribeMetadata['merchants_velocity'],
  1142.  
  1143. "capacity" => floor( $tribeMetadata['merchants_capacity'] * $capacityFactor ),
  1144.  
  1145. "total_num" => $total_merchants_num,
  1146.  
  1147. "exits_num" => $exist_num,
  1148.  
  1149. "confirm_snd" => FALSE,
  1150.  
  1151. "same_village" => FALSE,
  1152.  
  1153. "vRow" => NULL
  1154.  
  1155. );
  1156.  
  1157. if ( $this->selectedTabIndex == 0 )
  1158.  
  1159. {
  1160.  
  1161. $m = new BuildModel( );
  1162.  
  1163. if ( $this->isPost( ) || isset( $_GET['vid2'] ) )
  1164.  
  1165. {
  1166.  
  1167. $resources = array(
  1168.  
  1169. "1" => isset( $_POST['r1'] ) ? intval( $_POST['r1'] ) : 0,
  1170.  
  1171. "2" => isset( $_POST['r2'] ) ? intval( $_POST['r2'] ) : 0,
  1172.  
  1173. "3" => isset( $_POST['r3'] ) ? intval( $_POST['r3'] ) : 0,
  1174.  
  1175. "4" => isset( $_POST['r4'] ) ? intval( $_POST['r4'] ) : 0
  1176.  
  1177. );
  1178.  
  1179. $this->merchantProperty['confirm_snd'] = $this->isPost( ) ? isset( $_POST['act'] ) && $_POST['act'] == 1 : isset( $_GET['vid2'] );
  1180.  
  1181. $map_size = $this->setupMetadata['map_size'];
  1182.  
  1183. $doSend = FALSE;
  1184.  
  1185. if ( $this->merchantProperty['confirm_snd'] )
  1186.  
  1187. {
  1188.  
  1189. $vRow = NULL;
  1190.  
  1191. if ( isset( $_POST['x'], $_POST['y'] ) && trim( $_POST['x'] ) != "" && trim( $_POST['y'] ) != "" )
  1192.  
  1193. {
  1194.  
  1195. $vid = $this->__getVillageId( $map_size, $this->__getCoordInRange( $map_size, intval( $_POST['x'] ) ), $this->__getCoordInRange( $map_size, intval( $_POST['y'] ) ) );
  1196.  
  1197. $vRow = $m->getVillageDataById( $vid );
  1198.  
  1199. }
  1200.  
  1201. else if ( isset( $_POST['vname'] ) && trim( $_POST['vname'] ) != "" )
  1202.  
  1203. {
  1204.  
  1205. $vRow = $m->getVillageDataByName( trim( $_POST['vname'] ) );
  1206.  
  1207. }
  1208.  
  1209. else if ( isset( $_GET['vid2'] ) )
  1210.  
  1211. {
  1212.  
  1213. $vRow = $m->getVillageDataById( intval( $_GET['vid2'] ) );
  1214.  
  1215. if ( $vRow != NULL )
  1216.  
  1217. {
  1218.  
  1219. $_POST['x'] = $vRow['rel_x'];
  1220.  
  1221. $_POST['y'] = $vRow['rel_y'];
  1222.  
  1223. }
  1224.  
  1225. }
  1226.  
  1227. }
  1228.  
  1229. else
  1230.  
  1231. {
  1232.  
  1233. $doSend = TRUE;
  1234.  
  1235. $vRow = $m->getVillageDataById( intval( $_POST['vid2'] ) );
  1236.  
  1237. $this->merchantProperty['showError'] = FALSE;
  1238.  
  1239. $_POST['r1'] = $_POST['r2'] = $_POST['r3'] = $_POST['r4'] = "";
  1240.  
  1241. }
  1242.  
  1243. if ( 0 < intval( $vRow['player_id'] ) && $m->getPlayType( intval( $vRow['player_id'] ) ) == PLAYERTYPE_ADMIN )
  1244.  
  1245. {
  1246.  
  1247. $this->merchantProperty['showError'] = FALSE;
  1248.  
  1249. $this->merchantProperty['confirm_snd'] = FALSE;
  1250.  
  1251. }
  1252.  
  1253. else
  1254.  
  1255. {
  1256.  
  1257. $this->merchantProperty['vRow'] = $vRow;
  1258.  
  1259. $vid = $this->merchantProperty['to_vid'] = $vRow != NULL ? $vRow['id'] : 0;
  1260.  
  1261. $rel_x = $vRow['rel_x'];
  1262.  
  1263. $rel_y = $vRow['rel_y'];
  1264.  
  1265. $this->merchantProperty['same_village'] = $vid == $this->data['selected_village_id'];
  1266.  
  1267. if( $this->player->isAgent == 1 AND substr($this->player->actions, 2, 1) == 0) { $this->merchantProperty['actions'] = 0; }else{ $this->merchantProperty['actions'] = 1; }
  1268.  
  1269. if($pRow['last_ip'] == $this->data['last_ip'] AND $vRow['player_id']  != $this->player->playerId ){ $this->merchantProperty['send_alien'] = 0; }else{ $this->merchantProperty['send_alien'] = 1; }
  1270.  
  1271.  
  1272.  
  1273.  
  1274.  
  1275. $this->merchantProperty['available_res'] = $this->isResourcesAvailable( $resources );
  1276.  
  1277. $this->merchantProperty['vRow_merchant_num'] = ceil( ( $resources[1] + $resources[2] + $resources[3] + $resources[4] ) / $this->merchantProperty['capacity'] );
  1278.  
  1279. $this->merchantProperty['confirm_snd'] = 0 < $vid && $pRow['last_ip'] != $this->data['last_ip'] && $this->merchantProperty['available_res'] && 0 < $this->merchantProperty['vRow_merchant_num'] && $this->merchantProperty['vRow_merchant_num'] <= $this->merchantProperty['exits_num'] && !$this->merchantProperty['same_village'] && $this->merchantProperty['actions'] && $this->merchantProperty['send_alien'];
  1280.  
  1281.  
  1282.  
  1283. $pvid = $vRow['player_id'];
  1284.  
  1285. $test_kings = new QueueModel();
  1286.  
  1287. $test_black = $test_kings->provider->fetchRow("SELECT name FROM p_players WHERE id = '".$pvid."'");
  1288.  
  1289. $black_list = $this->data['black_list'];
  1290.  
  1291. $ex_black_list = explode(',', $black_list);
  1292.  
  1293. foreach ($ex_black_list as $test)
  1294.  
  1295. {
  1296.  
  1297. if ($test_black['name'] == $test && $vRow['player_id']  != $this->player->playerId) {
  1298.  
  1299. return exit("<h1>Error 505 !</h1>");
  1300.  
  1301. }
  1302.  
  1303. }
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309. $this->merchantProperty['showError'] = !$this->merchantProperty['confirm_snd'];
  1310.  
  1311. $distance = WebHelper::getdistance( $this->data['rel_x'], $this->data['rel_y'], $rel_x, $rel_y, $this->setupMetadata['map_size'] / 2 );
  1312.  
  1313. $this->merchantProperty['vRow_time'] = intval( $distance / $this->merchantProperty['speed'] * 3600 );
  1314.  
  1315. if ( !$this->merchantProperty['showError'] && $doSend && !$this->isGameTransientStopped( ) && !$this->isGameOver( )  )
  1316.  
  1317. {
  1318.  
  1319. if(intval($_POST['send_bell']) >= 1 AND intval($_POST['send_bell']) <= 20 AND $this->data['active_plus_account']){ $send_bell = intval($_POST['send_bell']); }else{ $send_bell = 1; }
  1320.  
  1321.  
  1322.  
  1323. $this->merchantProperty['confirm_snd'] = FALSE;
  1324.  
  1325. $this->merchantProperty['exits_num'] -= $this->merchantProperty['vRow_merchant_num'];
  1326.  
  1327. $newTask = new QueueTask( QS_MERCHANT_GO, $this->player->playerId, $this->merchantProperty['vRow_time'] );
  1328.  
  1329. $newTask->villageId = $this->data['selected_village_id'];
  1330.  
  1331. $newTask->toPlayerId = $vRow['player_id'];
  1332.  
  1333. $newTask->toVillageId = $vid;
  1334.  
  1335. $newTask->procParams = $this->merchantProperty['vRow_merchant_num']."|".( $resources[1]." ".$resources[2]." ".$resources[3]." ".$resources[4] )."|".$send_bell;
  1336.  
  1337. $newTask->tag = $resources;
  1338.  
  1339. $this->queueModel->addTask( $newTask );
  1340.  
  1341. }
  1342.  
  1343. }
  1344.  
  1345. }
  1346.  
  1347. $m->dispose( );
  1348.  
  1349. }
  1350.  
  1351. if ($this->selectedTabIndex == 1)
  1352.  
  1353. {
  1354.  
  1355. $m = new BuildModel ();
  1356.  
  1357. $showOfferList = TRUE;
  1358.  
  1359. if ((isset ($_GET['oid']) AND 0 < intval ($_GET['oid'])))
  1360.  
  1361. {
  1362.  
  1363. $oRow = $m->getOffer2 (intval ($_GET['oid']), $this->data['rel_x'], $this->data['rel_y'], $this->setupMetadata['map_size'] / 2);
  1364.  
  1365. if ($oRow != NULL)
  1366.  
  1367. {
  1368.  
  1369. $aid = 0;
  1370.  
  1371. if ($oRow['alliance_only'])
  1372.  
  1373. {
  1374.  
  1375. if (0 < intval ($this->data['alliance_id']))
  1376.  
  1377. {
  1378.  
  1379. $aid = $m->getPlayerAllianceId ($oRow['player_id']);
  1380.  
  1381. }
  1382.  
  1383. }
  1384.  
  1385. list ($res1, $res2) = explode ('|', $oRow['offer']);
  1386.  
  1387. $resArr1 = explode (' ', $res1);
  1388.  
  1389. $needResources = array ('1' => $resArr1[0], '2' => $resArr1[1], '3' => $resArr1[2], '4' => $resArr1[3]);
  1390.  
  1391. $res1_item_id = 0;
  1392.  
  1393. $res1_value = 0;
  1394.  
  1395. $i = 0;
  1396.  
  1397. $_c = sizeof ($resArr1);
  1398.  
  1399. while ($i < $_c)
  1400.  
  1401. {
  1402.  
  1403. if (0 < $resArr1[$i])
  1404.  
  1405. {
  1406.  
  1407. $res1_item_id = $i + 1;
  1408.  
  1409. $res1_value = $resArr1[$i];
  1410.  
  1411. break;
  1412.  
  1413. }
  1414.  
  1415. ++$i;
  1416.  
  1417. }
  1418.  
  1419. $resArr1 = explode (' ', $res2);
  1420.  
  1421. $giveResources = array ('1' => $resArr1[0], '2' => $resArr1[1], '3' => $resArr1[2], '4' => $resArr1[3]);
  1422.  
  1423. $res2_item_id = 0;
  1424.  
  1425. $res2_value = 0;
  1426.  
  1427. $i = 0;
  1428.  
  1429. $_c = sizeof ($resArr1);
  1430.  
  1431. while ($i < $_c)
  1432.  
  1433. {
  1434.  
  1435. if (0 < $resArr1[$i])
  1436.  
  1437. {
  1438.  
  1439. $res2_item_id = $i + 1;
  1440.  
  1441. $res2_value = $resArr1[$i];
  1442.  
  1443. break;
  1444.  
  1445. }
  1446.  
  1447. ++$i;
  1448.  
  1449. }
  1450.  
  1451. $distance = $oRow['timeInSeconds'] / 3600 * $oRow['merchants_speed'];
  1452.  
  1453. $acceptResult = $this->_canAcceptOffer ($needResources, $giveResources, $oRow['village_id'], $oRow['alliance_only'], $aid, $oRow['max_time'], $distance);
  1454.  
  1455. if ((($acceptResult == 5 AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  1456.  
  1457. {
  1458.  
  1459. $showOfferList = FALSE;
  1460.  
  1461. $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);
  1462.  
  1463. $merchantNum = ceil (($giveResources[1] + $giveResources[2] + $giveResources[3] + $giveResources[4]) / $this->merchantProperty['capacity']);
  1464.  
  1465. $newTask = new QueueTask (QS_MERCHANT_GO, $this->player->playerId, $distance / ($this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity']) * 3600);
  1466.  
  1467. $newTask->villageId = $this->data['selected_village_id'];
  1468.  
  1469. $newTask->toPlayerId = $oRow['player_id'];
  1470.  
  1471. $newTask->toVillageId = $oRow['village_id'];
  1472.  
  1473. $newTask->procParams = $merchantNum . '|' . ($giveResources[1] . ' ' . $giveResources[2] . ' ' . $giveResources[3] . ' ' . $giveResources[4]);
  1474.  
  1475. $newTask->tag = $giveResources;
  1476.  
  1477. $this->queueModel->addTask ($newTask);
  1478.  
  1479. $newTask = new QueueTask (QS_MERCHANT_GO, $oRow['player_id'], $oRow['timeInSeconds']);
  1480.  
  1481. $newTask->villageId = $oRow['village_id'];
  1482.  
  1483. $newTask->toPlayerId = $this->player->playerId;
  1484.  
  1485. $newTask->toVillageId = $this->data['selected_village_id'];
  1486.  
  1487. $newTask->procParams = $oRow['merchants_num'] . '|' . ($needResources[1] . ' ' . $needResources[2] . ' ' . $needResources[3] . ' ' . $needResources[4]);
  1488.  
  1489. $newTask->tag = array ('1' => 0, '2' => 0, '3' => 0, '4' => 0);
  1490.  
  1491. $this->queueModel->addTask ($newTask);
  1492.  
  1493. $m->removeMerchantOffer (intval ($_GET['oid']), $oRow['player_id'], $oRow['village_id']);
  1494.  
  1495. }
  1496.  
  1497. }
  1498.  
  1499. }
  1500.  
  1501. $this->merchantProperty['showOfferList'] = $showOfferList;
  1502.  
  1503. if ($showOfferList)
  1504.  
  1505. {
  1506.  
  1507. $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']);
  1508.  
  1509. $this->pageCount = (0 < $rowsCount ? ceil ($rowsCount / $this->pageSize) : 1);
  1510.  
  1511. $this->pageIndex = (((isset ($_GET['p']) AND is_numeric ($_GET['p'])) AND intval ($_GET['p']) < $this->pageCount) ? intval ($_GET['p']) : 0);
  1512.  
  1513. $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->pageIndex, $this->pageSize);
  1514.  
  1515. }
  1516.  
  1517. $m->dispose ();
  1518.  
  1519. return null;
  1520.  
  1521. }
  1522.  
  1523. if ($this->selectedTabIndex == 2)
  1524.  
  1525. {
  1526.  
  1527. $m = new BuildModel ();
  1528.  
  1529. $this->merchantProperty['showError'] = FALSE;
  1530.  
  1531. $this->merchantProperty['showError2'] = FALSE;
  1532.  
  1533. $this->merchantProperty['showError3'] = FALSE;
  1534.  
  1535. if ($this->isPost ())
  1536.  
  1537. {
  1538.  
  1539. if ((((((((isset ($_POST['m1']) AND 0 < intval ($_POST['m1'])) AND isset ($_POST['m2'])) AND 0 < intval ($_POST['m2'])) AND isset ($_POST['rid1'])) AND 0 < intval ($_POST['rid1'])) AND isset ($_POST['rid2'])) AND 0 < intval ($_POST['rid2'])))
  1540.  
  1541. {
  1542.  
  1543. $resources1 = array ('1' => ((isset ($_POST['rid1']) AND intval ($_POST['rid1']) == 1) ? intval ($_POST['m1']) : 0), '2' => ((isset ($_POST['rid1']) AND intval ($_POST['rid1']) == 2) ? intval ($_POST['m1']) : 0), '3' => ((isset ($_POST['rid1']) AND intval ($_POST['rid1']) == 3) ? intval ($_POST['m1']) : 0), '4' => ((isset ($_POST['rid1']) AND intval ($_POST['rid1']) == 4) ? intval ($_POST['m1']) : 0));
  1544.  
  1545. $resources2 = array ('1' => ((isset ($_POST['rid2']) AND intval ($_POST['rid2']) == 1) ? intval ($_POST['m2']) : 0), '2' => ((isset ($_POST['rid2']) AND intval ($_POST['rid2']) == 2) ? intval ($_POST['m2']) : 0), '3' => ((isset ($_POST['rid2']) AND intval ($_POST['rid2']) == 3) ? intval ($_POST['m2']) : 0), '4' => ((isset ($_POST['rid2']) AND intval ($_POST['rid2']) == 4) ? intval ($_POST['m2']) : 0));
  1546.  
  1547. if (((intval ($_POST['rid1']) == intval ($_POST['rid2']) OR intval ($resources1[1] + $resources1[2] + $resources1[3] + $resources1[4]) <= 0) OR intval ($resources2[1] + $resources2[2] + $resources2[3] + $resources2[4]) <= 0))
  1548.  
  1549. {
  1550.  
  1551. $this->merchantProperty['showError'] = TRUE;
  1552.  
  1553. }
  1554.  
  1555. else
  1556.  
  1557. {
  1558.  
  1559. if (10 < ceil (($resources2[1] + $resources2[2] + $resources2[3] + $resources2[4]) / ($resources1[1] + $resources1[2] + $resources1[3] + $resources1[4])))
  1560.  
  1561. {
  1562.  
  1563. $this->merchantProperty['showError'] = TRUE;
  1564.  
  1565. $this->merchantProperty['showError3'] = TRUE;
  1566.  
  1567. }
  1568.  
  1569. }
  1570.  
  1571. $this->merchantProperty['available_res'] = $this->isResourcesAvailable ($resources1);
  1572.  
  1573. if (($this->merchantProperty['available_res'] AND !$this->merchantProperty['showError']))
  1574.  
  1575. {
  1576.  
  1577. $this->merchantProperty['vRow_merchant_num'] = ceil (($resources1[1] + $resources1[2] + $resources1[3] + $resources1[4]) / $this->merchantProperty['capacity']);
  1578.  
  1579. if ((0 < $this->merchantProperty['vRow_merchant_num'] AND $this->merchantProperty['vRow_merchant_num'] <= $this->merchantProperty['exits_num']))
  1580.  
  1581. {
  1582.  
  1583. $this->merchantProperty['exits_num'] -= $this->merchantProperty['vRow_merchant_num'];
  1584.  
  1585. $this->data['offer_merchants_count'] += $this->merchantProperty['vRow_merchant_num'];
  1586.  
  1587. $offer = $resources1[1] . ' ' . $resources1[2] . ' ' . $resources1[3] . ' ' . $resources1[4] . '|' . ($resources2[1] . ' ' . $resources2[2] . ' ' . $resources2[3] . ' ' . $resources2[4]);
  1588.  
  1589. $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']), (((isset ($_POST['d1']) AND isset ($_POST['d2'])) AND 0 < intval ($_POST['d2'])) ? intval ($_POST['d2']) : 0), $this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity']);
  1590.  
  1591. foreach ($resources1 as $k => $v)
  1592.  
  1593. {
  1594.  
  1595. $this->resources[$k]['current_value'] -= $v;
  1596.  
  1597. }
  1598.  
  1599. $this->queueModel->_updateVillage (FALSE, FALSE);
  1600.  
  1601. }
  1602.  
  1603. else
  1604.  
  1605. {
  1606.  
  1607. $this->merchantProperty['showError'] = TRUE;
  1608.  
  1609. }
  1610.  
  1611. }
  1612.  
  1613. else
  1614.  
  1615. {
  1616.  
  1617. $this->merchantProperty['showError'] = TRUE;
  1618.  
  1619. }
  1620.  
  1621. }
  1622.  
  1623. else
  1624.  
  1625. {
  1626.  
  1627. $this->merchantProperty['showError'] = TRUE;
  1628.  
  1629. $this->merchantProperty['showError2'] = TRUE;
  1630.  
  1631. }
  1632.  
  1633. }
  1634.  
  1635. else
  1636.  
  1637. {
  1638.  
  1639. if ((isset ($_GET['d']) AND 0 < intval ($_GET['d'])))
  1640.  
  1641. {
  1642.  
  1643. $row = $m->getOffer (intval ($_GET['d']), $this->player->playerId, $this->data['selected_village_id']);
  1644.  
  1645. if ($row != NULL)
  1646.  
  1647. {
  1648.  
  1649. $this->merchantProperty['exits_num'] += $row['merchants_num'];
  1650.  
  1651. $this->data['offer_merchants_count'] -= $row['merchants_num'];
  1652.  
  1653. list ($resources1, $resources2) = explode ('|', $row['offer']);
  1654.  
  1655. $resourcesArray1 = explode (' ', $resources1);
  1656.  
  1657. $res = array ();
  1658.  
  1659. $i = 0;
  1660.  
  1661. $_c = sizeof ($resourcesArray1);
  1662.  
  1663. while ($i < $_c)
  1664.  
  1665. {
  1666.  
  1667. $res[$i + 1] = $resourcesArray1[$i];
  1668.  
  1669. ++$i;
  1670.  
  1671. }
  1672.  
  1673. foreach ($res as $k => $v)
  1674.  
  1675. {
  1676.  
  1677. $this->resources[$k]['current_value'] += $v;
  1678.  
  1679. }
  1680.  
  1681. $this->queueModel->_updateVillage (FALSE, FALSE);
  1682.  
  1683. $m->removeMerchantOffer (intval ($_GET['d']), $this->player->playerId, $this->data['selected_village_id']);
  1684.  
  1685. }
  1686.  
  1687. }
  1688.  
  1689. }
  1690.  
  1691. $this->merchantProperty['offers'] = $m->getOffers ($this->data['selected_village_id']);
  1692.  
  1693. $m->dispose ();
  1694.  
  1695. return null;
  1696.  
  1697. }
  1698.  
  1699. if ($this->selectedTabIndex == 3)
  1700.  
  1701. {
  1702.  
  1703. if ((((($this->isPost () AND isset ($_POST['m2'])) AND is_array ($_POST['m2'])) AND sizeof ($_POST['m2']) == 4) AND $this->gameMetadata['plusTable'][6]['cost'] <= $this->data['gold_num']))
  1704.  
  1705. {
  1706.  
  1707. $resources = array ('1' => intval ($_POST['m2'][0]), '2' => intval ($_POST['m2'][1]), '3' => intval ($_POST['m2'][2]), '4' => intval ($_POST['m2'][3]));
  1708.  
  1709. $oldSum = $this->resources[1]['current_value'] + $this->resources[2]['current_value'] + $this->resources[3]['current_value'] + $this->resources[4]['current_value'];
  1710.  
  1711. $newSum = $resources[1] + $resources[2] + $resources[3] + $resources[4];
  1712.  
  1713. //start pgold
  1714.  
  1715. $tatarzx = new QueueModel();
  1716.  
  1717. $d = date('Y/m/d H:i:s');
  1718.  
  1719. $n = $this->data['name'];
  1720.  
  1721. $tatarzx->provider->executeQuery("INSERT INTO `p_plus` (`pid`, `date`, `gold`, `where`) VALUES ('".$n."', '".$d."', '".$this->gameMetadata['plusTable'][6]['cost']."', 'تاجر المبادله');");
  1722.  
  1723. $tst = 0;
  1724.  
  1725. if (($newSum-$tst) <= $oldSum)
  1726.  
  1727. {
  1728.  
  1729. foreach ($resources as $k => $v)
  1730.  
  1731. {
  1732.  
  1733. $this->resources[$k]['current_value'] = $v;
  1734.  
  1735. }
  1736.  
  1737. $this->queueModel->_updateVillage (FALSE, FALSE);
  1738.  
  1739. $m = new BuildModel ();
  1740.  
  1741. $m->decreaseGoldNum ($this->player->playerId, $this->gameMetadata['plusTable'][6]['cost']);
  1742.  
  1743. $m->dispose ();
  1744.  
  1745. }
  1746.  
  1747. }
  1748.  
  1749. }
  1750.  
  1751. }
  1752.  
  1753. function handleEmbassy ()
  1754.  
  1755. {
  1756.  
  1757. if (0 < intval ($this->data['alliance_id']))
  1758.  
  1759. {
  1760.  
  1761. return null;
  1762.  
  1763. }
  1764.  
  1765. $this->embassyProperty = array ('level' => $this->buildings[$this->buildingIndex]['level'], 'invites' => NULL, 'error' => 0, 'ally1' => '', 'ally2' => '');
  1766.  
  1767. $maxPlayers = $this->gameMetadata['items'][18]['levels'][$this->embassyProperty['level'] - 1]['value'];
  1768.  
  1769. if (((($this->isPost () AND 3 <= $this->embassyProperty['level']) AND isset ($_POST['ally1'])) AND isset ($_POST['ally2'])))
  1770.  
  1771. {
  1772.  
  1773. $this->embassyProperty['ally1'] = $ally1 = trim ($_POST['ally1']);
  1774.  
  1775. $this->embassyProperty['ally2'] = $ally2 = trim ($_POST['ally2']);
  1776.  
  1777. if (($ally1 == '' OR $ally2 == ''))
  1778.  
  1779. {
  1780.  
  1781. $this->embassyProperty['error'] = (($ally1 == '' AND $ally2 == '') ? 3 : ($ally1 == '' ? 1 : 2));
  1782.  
  1783. }
  1784.  
  1785. else
  1786.  
  1787. {
  1788.  
  1789. $m = new BuildModel ();
  1790.  
  1791. if (!$m->allianceExists ($this->embassyProperty['ally1']))
  1792.  
  1793. {
  1794.  
  1795. $this->data['alliance_name'] = $this->embassyProperty['ally1'];
  1796.  
  1797. $this->data['alliance_id'] = $m->createAlliance ($this->player->playerId, $this->embassyProperty['ally1'], $this->embassyProperty['ally2'], $maxPlayers);
  1798.  
  1799. $global = new GlobalModel();
  1800.  
  1801. $tatarzx = new QueueModel();
  1802.  
  1803. $show = $tatarzx->provider->fetchScalar( "SELECT * FROM alince WHERE ida='".$this->data['alliance_id']."' AND idn='7' AND mname='".$this->data['name']."'" );
  1804.  
  1805. if ($show < 1) {
  1806.  
  1807. $global->inserttones($this->data['alliance_id'] , 7 , $this->data['name'] , "", $this->player->playerId , "");
  1808.  
  1809. }
  1810.  
  1811. $m->dispose ();
  1812.  
  1813. return null;
  1814.  
  1815. }
  1816.  
  1817. $this->embassyProperty['error'] = 4;
  1818.  
  1819. $m->dispose ();
  1820.  
  1821. }
  1822.  
  1823. }
  1824.  
  1825. $invites_alliance_ids = trim ($this->data['invites_alliance_ids']);
  1826.  
  1827. $this->embassyProperty['invites'] = array ();
  1828.  
  1829. if ($invites_alliance_ids != '')
  1830.  
  1831. {
  1832.  
  1833. $_arr = explode ("\n", $invites_alliance_ids);
  1834.  
  1835. foreach ($_arr as $_s)
  1836.  
  1837. {
  1838.  
  1839. list ($allianceId, $allianceName) = explode (' ', $_s, 2);
  1840.  
  1841. $this->embassyProperty['invites'][$allianceId] = $allianceName;
  1842.  
  1843. }
  1844.  
  1845. }
  1846.  
  1847. if (!$this->isPost ())
  1848.  
  1849. {
  1850.  
  1851. if ((isset ($_GET['a']) AND 0 < intval ($_GET['a'])))
  1852.  
  1853. {
  1854.  
  1855. $allianceId = intval ($_GET['a']);
  1856.  
  1857. if (isset ($this->embassyProperty['invites'][$allianceId]))
  1858.  
  1859. {
  1860.  
  1861. $m = new BuildModel ();
  1862.  
  1863. $acceptResult = $m->acceptAllianceJoining ($this->player->playerId, $allianceId);
  1864.  
  1865.                                         $global = new GlobalModel();
  1866.  
  1867. $tatarzx = new QueueModel();
  1868.  
  1869. $show = $tatarzx->provider->fetchScalar( "SELECT * FROM alince WHERE ida='".$allianceId."' AND idn='1' AND mname='".$this->data['name']."'" );
  1870.  
  1871. if ($show < 1) {
  1872.  
  1873. $global->inserttones($allianceId , 1 , $this->data['name'] , "", $this->player->playerId , "");
  1874.  
  1875. }
  1876.  
  1877. if ($acceptResult == 2)
  1878.  
  1879. {
  1880.  
  1881. $this->data['alliance_name'] = $this->embassyProperty['invites'][$allianceId];
  1882.  
  1883. $this->data['alliance_id'] = $allianceId;
  1884.  
  1885. unset ($this->embassyProperty['invites'][$allianceId]);
  1886.  
  1887. $m->removeAllianceInvites ($this->player->playerId, $allianceId);
  1888.  
  1889. }
  1890.  
  1891. else
  1892.  
  1893. {
  1894.  
  1895. if ($acceptResult == 1)
  1896.  
  1897. {
  1898.  
  1899. $this->embassyProperty['error'] = 15;
  1900.  
  1901. }
  1902.  
  1903. }
  1904.  
  1905. $m->dispose ();
  1906.  
  1907. return null;
  1908.  
  1909. }
  1910.  
  1911. }
  1912.  
  1913. else
  1914.  
  1915. {
  1916.  
  1917. if ((isset ($_GET['d']) AND 0 < intval ($_GET['d'])))
  1918.  
  1919. {
  1920.  
  1921. $allianceId = intval ($_GET['d']);
  1922.  
  1923. if (isset ($this->embassyProperty['invites'][$allianceId]))
  1924.  
  1925. {
  1926.  
  1927. unset ($this->embassyProperty['invites'][$allianceId]);
  1928.  
  1929. $m = new BuildModel ();
  1930.  
  1931. $m->removeAllianceInvites ($this->player->playerId, $allianceId);
  1932.  
  1933. $m->dispose ();
  1934.  
  1935. }
  1936.  
  1937. }
  1938.  
  1939. }
  1940.  
  1941. }
  1942.  
  1943. }
  1944.  
  1945. public function handleWarrior( )
  1946.  
  1947. {
  1948.  
  1949. $itemId = $this->buildings[$this->buildingIndex]['item_id'];
  1950.  
  1951. $this->troopsUpgrade = array( );
  1952.  
  1953. $_arr = explode( ",", $this->data['troops_training'] );
  1954.  
  1955. foreach ( $_arr as $troopStr )
  1956.  
  1957. {
  1958.  
  1959. $tempdata = explode( " ", $troopStr );
  1960.  
  1961. $attack_level = $tempdata[3];
  1962.  
  1963. $defense_level = $tempdata[2];
  1964.  
  1965. $researches_done = $tempdata[1];
  1966.  
  1967. $troopId = $tempdata[0];
  1968.  
  1969. if ( $researches_done == 1 && 0 < $this->gameMetadata['troops'][$troopId]['gold_needed'] )
  1970.  
  1971. {
  1972.  
  1973. $this->troopsUpgrade[$troopId] = $troopId;
  1974.  
  1975. }
  1976.  
  1977. }
  1978.  
  1979. $this->warriorMessage = "";
  1980.  
  1981. if ( $this->isPost( ) && isset( $_POST['tf'] ) && !$this->isGameTransientStopped( ) && !$this->isGameOver( ) )
  1982.  
  1983. {
  1984.  
  1985. $cropConsume = 0;
  1986.  
  1987. $totalGoldsNeeded = 0;
  1988.  
  1989. foreach ( $_POST['tf'] as $troopId => $num )
  1990.  
  1991. {
  1992.  
  1993. if(preg_match('/^[+-]?[0-9]+$/', $_POST['tf'][$troopId]) == 0) {
  1994.  
  1995. return null;
  1996.  
  1997. }
  1998.  
  1999. $num = intval( $num );
  2000.  
  2001. if ( $num <= 0 || !isset( $this->troopsUpgrade[$troopId] ) )
  2002.  
  2003. {
  2004.  
  2005. continue;
  2006.  
  2007. }
  2008.  
  2009. $totalGoldsNeeded += ceil($this->gameMetadata['troops'][$troopId]['gold_needed'] * $num);
  2010.  
  2011.  
  2012.  
  2013. $cropConsume += $this->gameMetadata['troops'][$troopId]['crop_consumption'] * $num;
  2014.  
  2015. }
  2016.  
  2017. $newgold = ($this->data['gold_num']-$totalGoldsNeeded);
  2018.  
  2019. if ($newgold<$GLOBALS['AppConfig']['Game']['freegold']) {
  2020.  
  2021. $this->warriorMessage = 3;
  2022.  
  2023. }
  2024.  
  2025. else
  2026.  
  2027. if ( $totalGoldsNeeded <= 0 )
  2028.  
  2029. {
  2030.  
  2031. }
  2032.  
  2033. else
  2034.  
  2035. {
  2036.  
  2037. $canProcess = $totalGoldsNeeded <= $this->data['gold_num'];
  2038.  
  2039. $this->warriorMessage = $canProcess ? 1 : 2;
  2040.  
  2041. if ( $canProcess )
  2042.  
  2043. {
  2044.  
  2045. $troopsString = "";
  2046.  
  2047. foreach ( $this->troops as $tid => $num )
  2048.  
  2049. {
  2050.  
  2051. if ( $tid == 99 )
  2052.  
  2053. {
  2054.  
  2055. continue;
  2056.  
  2057. }
  2058.  
  2059. $neededNum[$tid] = isset( $this->troopsUpgrade[$tid], $_POST['tf'] ) ? $_POST['tf'][$tid] : 0;
  2060.  
  2061. if ( $troopsString != "" )
  2062.  
  2063. {
  2064.  
  2065. $troopsString .= ",";
  2066.  
  2067. }
  2068.  
  2069. $troopsString .= $tid." ".$neededNum[$tid];
  2070.  
  2071. }
  2072.  
  2073. $m = new BuildModel( );
  2074.  
  2075. $m->decreaseGoldNum( $this->player->playerId, $totalGoldsNeeded );
  2076.  
  2077. //start pgold
  2078.  
  2079. $tatarzx = new QueueModel();
  2080.  
  2081. $d = date('Y/m/d H:i:s');
  2082.  
  2083. $n = $this->data['name'];
  2084.  
  2085. $tatarzx->provider->executeQuery("INSERT INTO `p_plus` (`pid`, `date`, `gold`, `where`) VALUES ('".$n."', '".$d."', '".$totalGoldsNeeded."', 'شراء جيش');");
  2086.  
  2087. //end pgold
  2088.  
  2089.  
  2090.  
  2091. $m->dispose( );
  2092.  
  2093. $this->data['gold_num'] -= $totalGoldsNeeded;
  2094.  
  2095. $procParams = $troopsString."|0||||||1";
  2096.  
  2097.                     $buildingMetadata = $this->gameMetadata['items'][$this->buildProperties['building']['item_id']];
  2098.  
  2099.                     $bLevel = $this->buildings[$this->buildingIndex]['level'];
  2100.  
  2101.                     $needed_time = $buildingMetadata['levels'][$bLevel - 1]['value'] * 60;
  2102.  
  2103. $newTask = new QueueTask( QS_WAR_REINFORCE, 0, $needed_time );
  2104.  
  2105. $newTask->villageId = 0;
  2106.  
  2107. $newTask->toPlayerId = $this->player->playerId;
  2108.  
  2109. $newTask->toVillageId = $this->data['selected_village_id'];
  2110.  
  2111. $newTask->procParams = $procParams;
  2112.  
  2113. $newTask->tag = array( "troops" => NULL, "hasHero" => FALSE, "resources" => NULL, "troopsCropConsume" => $cropConsume );
  2114.  
  2115. $this->queueModel->addTask( $newTask );
  2116.  
  2117. }
  2118.  
  2119. }
  2120.  
  2121. }
  2122.  
  2123. }
  2124.  
  2125. function handleTroopBuilding ()
  2126.  
  2127. {
  2128.  
  2129. $itemId = $this->buildings[$this->buildingIndex]['item_id'];
  2130.  
  2131. $this->troopsUpgradeType = QS_TROOP_TRAINING;
  2132.  
  2133. $this->troopsUpgrade = array ();
  2134.  
  2135. $_arr = explode (',', $this->data['troops_training']);
  2136.  
  2137. foreach ($_arr as $troopStr)
  2138.  
  2139. {
  2140.  
  2141. list ($troopId, $researches_done, $defense_level, $attack_level) = explode (' ', $troopStr);
  2142.  
  2143. if (($researches_done == 1 AND $this->_canTrainInBuilding ($troopId, $itemId)))
  2144.  
  2145. {
  2146.  
  2147. $this->troopsUpgrade[$troopId] = $troopId;
  2148.  
  2149. continue;
  2150.  
  2151. }
  2152.  
  2153. }
  2154.  
  2155. if (((($this->isPost () AND isset ($_POST['tf'])) AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  2156.  
  2157. {
  2158.  
  2159. foreach ($_POST['tf'] as $troopId => $num)
  2160.  
  2161. {
  2162.  
  2163. if(preg_match('/^[+-]?[0-9]+$/', $_POST['tf'][$troopId]) == 0) {
  2164.  
  2165. return null;
  2166.  
  2167. }
  2168.  
  2169. $num = intval ($num);
  2170.  
  2171. if ((($num <= 0 OR !isset ($this->troopsUpgrade[$troopId])) OR $this->_getMaxTrainNumber ($troopId, $itemId) < $num))
  2172.  
  2173. {
  2174.  
  2175. continue;
  2176.  
  2177. }
  2178.  
  2179. $timeFactor = 1;
  2180.  
  2181. if ($this->gameMetadata['troops'][$troopId]['is_cavalry'] == TRUE)
  2182.  
  2183. {
  2184.  
  2185. $flvl = $this->_getMaxBuildingLevel (41);
  2186.  
  2187. if (0 < $flvl)
  2188.  
  2189. {
  2190.  
  2191. $timeFactor -= $this->gameMetadata['items'][41]['levels'][$flvl - 1]['value'] / 100;
  2192.  
  2193. }
  2194.  
  2195. }
  2196.  
  2197. ///Start Artefect
  2198.  
  2199. $efect = 3;
  2200.  
  2201. $pid = $this->player->playerId;
  2202.  
  2203. $vid = $this->data['selected_village_id'];
  2204.  
  2205. $tatarzx = new QueueModel();
  2206.  
  2207. $this->BigArt = $tatarzx->provider->fetchScalar( "SELECT COUNT(*) FROM `p_villages` WHERE type=4 AND artefacts='".$efect."' AND player_id='".$pid."'" );
  2208.  
  2209. $this->SeCArt = $tatarzx->provider->fetchScalar( "SELECT COUNT(*) FROM `p_villages` WHERE type=2 AND artefacts='".$efect."' AND player_id='".$pid."'" );
  2210.  
  2211. $this->SmallArt = $tatarzx->provider->fetchScalar( "SELECT COUNT(*) FROM `p_villages` WHERE type=3 AND id='".$vid."' AND artefacts='".$efect."'" );
  2212.  
  2213. $Tfsh = 1;
  2214.  
  2215. if ($this->BigArt) {
  2216.  
  2217. $Tfsh = 2;
  2218.  
  2219. }else if ($this->SeCArt) {
  2220.  
  2221. $Tfsh = 1.5;
  2222.  
  2223. }else if ($this->SmallArt) {
  2224.  
  2225. $Tfsh = 2;
  2226.  
  2227. }
  2228.  
  2229. ///End Artefect
  2230.  
  2231.  
  2232.  
  2233. $troopMetadata = $this->gameMetadata['troops'][$troopId];
  2234.  
  2235. $calcConsume = intval ($troopMetadata['training_time_consume'] / $GLOBALS['AppConfig']['Game']['speed_t'] * (10 / ($this->buildProperties['building']['level'] + 9)) * $timeFactor);
  2236.  
  2237. $newTask = new QueueTask ($this->troopsUpgradeType, $this->player->playerId, $calcConsume);
  2238.  
  2239. $newTask->threads = $num;
  2240.  
  2241. $newTask->villageId = $this->data['selected_village_id'];
  2242.  
  2243. $newTask->buildingId = $this->buildProperties['building']['item_id'];
  2244.  
  2245. $newTask->procParams = $troopId;
  2246.  
  2247. $newTask->tag = array ('1' => ceil($troopMetadata['training_resources'][1] * $this->buildingTribeFactor * $num/$Tfsh), '2' => ceil($troopMetadata['training_resources'][2] * $this->buildingTribeFactor * $num/$Tfsh), '3' => ceil($troopMetadata['training_resources'][3] * $this->buildingTribeFactor * $num/$Tfsh), '4' => ceil($troopMetadata['training_resources'][4] * $this->buildingTribeFactor * $num/$Tfsh));
  2248.  
  2249.  
  2250.  
  2251. //here is kook
  2252.  
  2253. $tatarzx = new QueueModel();
  2254.  
  2255.  
  2256.  
  2257. $date = date('Y/m/d H:i:s');
  2258.  
  2259.                 $tatarzx->provider->executeQuery("INSERT admin_troops SET n_p='%s', n_n='%s', n_t=%s , n_d='%s'", array(
  2260.  
  2261.                 $this->data['name'],
  2262.  
  2263.                 $num,
  2264.  
  2265.                 $troopId,
  2266.  
  2267.                 $date
  2268.  
  2269.                 ));
  2270.  
  2271.  
  2272.  
  2273. //here is end kook
  2274.  
  2275. $this->queueModel->addTask ($newTask);
  2276.  
  2277. }
  2278.  
  2279. }
  2280.  
  2281. }
  2282.  
  2283. function handleAcademy ()
  2284.  
  2285. {
  2286.  
  2287. $this->troopsUpgradeType = QS_TROOP_RESEARCH;
  2288.  
  2289. $this->troopsUpgrade = array ('available' => array (), 'soon' => array ());
  2290.  
  2291. $_arr = explode (',', $this->data['troops_training']);
  2292.  
  2293. foreach ($_arr as $troopStr)
  2294.  
  2295. {
  2296.  
  2297. list ($troopId, $researches_done, $defense_level, $attack_level) = explode (' ', $troopStr);
  2298.  
  2299. if ($researches_done == 0)
  2300.  
  2301. {
  2302.  
  2303. $this->troopsUpgrade[($this->_canDoResearches ($troopId) ? 'available' : 'soon')][] = $troopId;
  2304.  
  2305. continue;
  2306.  
  2307. }
  2308.  
  2309. }
  2310.  
  2311. if (((((((isset ($_GET['a']) AND isset ($_GET['k'])) AND $_GET['k'] == $this->data['update_key']) AND !isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType])) AND $this->_canDoResearches (intval ($_GET['a']))) AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  2312.  
  2313. {
  2314.  
  2315. $troopId = intval ($_GET['a']);
  2316.  
  2317. $buildingMetadata = $this->gameMetadata['troops'][$troopId];
  2318.  
  2319. if (!$this->isResourcesAvailable ($buildingMetadata['research_resources']))
  2320.  
  2321. {
  2322.  
  2323. return null;
  2324.  
  2325. }
  2326.  
  2327. $calcConsume = intval ($buildingMetadata['research_time_consume'] / $this->gameSpeed);
  2328.  
  2329. $newTask = new QueueTask ($this->troopsUpgradeType, $this->player->playerId, $calcConsume);
  2330.  
  2331. $newTask->villageId = $this->data['selected_village_id'];
  2332.  
  2333. $newTask->procParams = $troopId;
  2334.  
  2335. $newTask->tag = $buildingMetadata['research_resources'];
  2336.  
  2337. $this->queueModel->addTask ($newTask);
  2338.  
  2339. }
  2340.  
  2341. }
  2342.  
  2343. function handleTownHall ()
  2344.  
  2345. {
  2346.  
  2347. $buildingMetadata = $this->gameMetadata['items'][$this->buildProperties['building']['item_id']];
  2348.  
  2349. $bLevel = $this->buildings[$this->buildingIndex]['level'];
  2350.  
  2351. if ((((((isset ($_GET['a']) AND isset ($_GET['k'])) AND $_GET['k'] == $this->data['update_key']) AND !isset ($this->queueModel->tasksInQueue[QS_TOWNHALL_CELEBRATION])) AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  2352.  
  2353. {
  2354.  
  2355. if ((((intval ($_GET['a']) < 1 OR 2 < intval ($_GET['a'])) OR (intval ($_GET['a']) == 1 AND $bLevel < $buildingMetadata['celebrations']['small']['level'])) OR (intval ($_GET['a']) == 2 AND $bLevel < $buildingMetadata['celebrations']['large']['level'])))
  2356.  
  2357. {
  2358.  
  2359. return null;
  2360.  
  2361. }
  2362.  
  2363. $key = (intval ($_GET['a']) == 2 ? 'large' : 'small');
  2364.  
  2365. if (!$this->isResourcesAvailable ($buildingMetadata['celebrations'][$key]['resources']))
  2366.  
  2367. {
  2368.  
  2369. return null;
  2370.  
  2371. }
  2372.  
  2373. $calcConsume = intval ($buildingMetadata['celebrations'][$key]['time_consume'] / $this->gameSpeed * (10 / ($bLevel + 9)));
  2374.  
  2375. $newTask = new QueueTask (QS_TOWNHALL_CELEBRATION, $this->player->playerId, $calcConsume);
  2376.  
  2377. $newTask->villageId = $this->data['selected_village_id'];
  2378.  
  2379. $newTask->procParams = intval ($_GET['a']);
  2380.  
  2381. $newTask->tag = $buildingMetadata['celebrations'][$key]['resources'];
  2382.  
  2383. $this->queueModel->addTask ($newTask);
  2384.  
  2385. }
  2386.  
  2387. }
  2388.  
  2389. function handleResidencePalace ()
  2390.  
  2391. {
  2392.  
  2393. $this->selectedTabIndex = ((((isset ($_GET['t']) AND is_numeric ($_GET['t'])) AND 1 <= intval ($_GET['t'])) AND intval ($_GET['t']) <= 4) ? intval ($_GET['t']) : 0);
  2394.  
  2395. if ($this->selectedTabIndex == 4)
  2396.  
  2397. {
  2398.  
  2399. if($this->isPost ()){
  2400.  
  2401. $abodR = new QueueModel();
  2402.  
  2403. $mdist = $this->setupMetadata['field_maps_summary'][$this->data['field_maps_id']];
  2404.  
  2405. $gold = ceil($this->data['people_count']*3.5);
  2406.  
  2407. $time = ceil($this->data['people_count']/5);
  2408.  
  2409. $x = $_POST['x'];
  2410.  
  2411. $y = $_POST['y'];
  2412.  
  2413. $this->warriorMessage = "";
  2414.  
  2415. if ($this->data['gold_num'] <= $gold) {
  2416.  
  2417. $this->warriorMessage = "الذهب غير كافي";
  2418.  
  2419. }
  2420.  
  2421. if ($this->data['gold_num'] >= $gold && is_numeric($y) && is_numeric($x)) {
  2422.  
  2423. $r_row = $abodR->provider->fetchRow("SELECT * FROM p_villages where rel_x='".$x."' and rel_y='".$y."' and ISNULL(player_id)");
  2424.  
  2425. $v_row = $abodR->provider->fetchRow("SELECT * FROM p_villages where id = '".$this->data['selected_village_id']."'");
  2426.  
  2427. if (!$r_row) {
  2428.  
  2429. $this->warriorMessage = "يجب ان تكون الاحداثيات تابعه لوادي مهجور";
  2430.  
  2431. }
  2432.  
  2433. if ($r_row){
  2434.  
  2435. $vid = $r_row['id'];
  2436.  
  2437. $fmaps = $r_row['field_maps_id'];
  2438.  
  2439. $troops_training = $r_row['troops_training'];
  2440.  
  2441. $mdistmaps = $this->setupMetadata['field_maps_summary'][$fmaps];
  2442.  
  2443. if ($mdistmaps != $mdist) {
  2444.  
  2445. $this->warriorMessage = "يجب ان يكون النوع ".$mdist;
  2446.  
  2447. }
  2448.  
  2449. if ($r_row['is_oasis']) {
  2450.  
  2451. $this->warriorMessage = "لايمكن نسخ واحه";
  2452.  
  2453. }
  2454.  
  2455. if ($this->data['create_nvil'] >= 15) {
  2456.  
  2457. $this->warriorMessage = "لايمكن نسخ اكثر من 15 قريه";
  2458.  
  2459. }
  2460.  
  2461. if ($mdistmaps == $mdist && !$r_row['is_oasis'] && $this->data['create_nvil'] < 15) {
  2462.  
  2463. //start pgold
  2464.  
  2465. $tatarzx = new QueueModel();
  2466.  
  2467. $d = date('Y/m/d H:i:s');
  2468.  
  2469. $n = $this->data['name'];
  2470.  
  2471. $tatarzx->provider->executeQuery("INSERT INTO `p_plus` (`pid`, `date`, `gold`, `where`) VALUES ('".$n."', '".$d."', '".$gold."', 'نسخ قريه ');");
  2472.  
  2473. //end pgold
  2474.  
  2475.  
  2476.  
  2477.         $toVillageRow = $r_row;
  2478.  
  2479.         $fromVillageRow = $v_row;
  2480.  
  2481.         $abodR->provider->executeQuery2("UPDATE p_players SET gold_num =gold_num-".$gold.",create_nvil=create_nvil+1 WHERE id = '".$fromVillageRow['player_id']."'");
  2482.  
  2483.         $cropConsumption = 0;
  2484.  
  2485.         $GameMetadata = $GLOBALS['GameMetadata'];
  2486.  
  2487.         $SetupMetadata = $GLOBALS['SetupMetadata'];
  2488.  
  2489.         $villageName = new_village_name;
  2490.  
  2491.         $update_key = substr( md5( $fromVillageRow['player_id'].$fromVillageRow['tribe_id'].$toVillageRow['id'].$fromVillageRow['player_name'].$villageName ), 2, 5 );
  2492.  
  2493.         $field_map_id = $toVillageRow['field_maps_id'];
  2494.  
  2495.         $troops_training2 = $fromVillageRow['troops_training'];
  2496.  
  2497.         $buildings = $fromVillageRow['buildings'];
  2498.  
  2499.         $resources = $fromVillageRow['resources'];
  2500.  
  2501.         $troops_training = "";
  2502.  
  2503.         $troops_num = "";
  2504.  
  2505.         foreach ( $GameMetadata['troops'] as $k => $v )
  2506.  
  2507.         {
  2508.  
  2509.             if ( $v['for_tribe_id'] == 0 - 1 || $v['for_tribe_id'] == $fromVillageRow['tribe_id'] )
  2510.  
  2511.             {
  2512.  
  2513.                 if ( $troops_training != "" )
  2514.  
  2515.                 {
  2516.  
  2517.                     $troops_training .= ",";
  2518.  
  2519.                 }
  2520.  
  2521.                 $researching_done = $v['research_time_consume'] == 0 ? 1 : 0;
  2522.  
  2523.                 $troops_training .= $k." ".$researching_done." 0 0";
  2524.  
  2525.                 if ( $troops_num != "" )
  2526.  
  2527.                 {
  2528.  
  2529.                     $troops_num .= ",";
  2530.  
  2531.                 }
  2532.  
  2533.                 $troops_num .= $k." 0";
  2534.  
  2535.             }
  2536.  
  2537.         }
  2538.  
  2539.         $troops_num = "-1:".$troops_num;
  2540.  
  2541.         $abodR->provider->executeQuery( "UPDATE p_villages v\r\n\t\t\tSET\r\n\t\t\t\tv.parent_id=%s,\r\n\t\t\t\tv.tribe_id=%s,\r\n\t\t\t\tv.player_id=%s,\r\n\t\t\t\tv.alliance_id=%s,\r\n\t\t\t\tv.player_name='%s',\r\n\t\t\t\tv.village_name='%s',\r\n\t\t\t\tv.alliance_name='%s',\r\n\t\t\t\tv.is_capital=0,\r\n\t\t\t\tv.buildings='%s',\r\n\t\t\t\tv.resources='%s',\r\n\t\t\t\tv.cp='0 2',\r\n\t\t\t\tv.troops_training='%s',\r\n\t\t\t\tv.troops_num='%s',\r\n\t\t\t\tv.update_key='%s',troops_training='%s',\r\n\t\t\t\tv.creation_date=NOW(),\r\n\t\t\t\tv.last_update_date=NOW()\r\n\t\t\tWHERE v.id=%s", array(
  2542.  
  2543.             intval( $fromVillageRow['id'] ),
  2544.  
  2545.             intval( $fromVillageRow['tribe_id'] ),
  2546.  
  2547.             intval( $fromVillageRow['player_id'] ),
  2548.  
  2549.             0 < intval( $fromVillageRow['alliance_id'] ) ? intval( $fromVillageRow['alliance_id'] ) : "NULL",
  2550.  
  2551.             $fromVillageRow['player_name'],
  2552.  
  2553.             $villageName,
  2554.  
  2555.             $fromVillageRow['alliance_name'],
  2556.  
  2557.             $buildings,
  2558.  
  2559.             $resources,
  2560.  
  2561.             $troops_training2,
  2562.  
  2563.             $troops_num,
  2564.  
  2565.             $update_key,
  2566.  
  2567.             $troops_training2,
  2568.  
  2569.             intval( $toVillageRow['id'] )
  2570.  
  2571.         ) );
  2572.  
  2573.         $child_villages_id = trim( $fromVillageRow['child_villages_id'] );
  2574.  
  2575.         if ( $child_villages_id != "" )
  2576.  
  2577.         {
  2578.  
  2579.             $child_villages_id .= ",";
  2580.  
  2581.         }
  2582.  
  2583.         $child_villages_id .= $toVillageRow['id'];
  2584.  
  2585.         $abodR->provider->executeQuery( "UPDATE p_villages v\r\n\t\t\tSET\r\n\t\t\t\tv.crop_consumption=v.crop_consumption-%s,\r\n\t\t\t\tv.child_villages_id='%s'\r\n\t\t\tWHERE v.id=%s", array(
  2586.  
  2587.             $cropConsumption,
  2588.  
  2589.             $child_villages_id,
  2590.  
  2591.             intval( $fromVillageRow['id'] )
  2592.  
  2593.         ) );
  2594.  
  2595.         $prow = $abodR->provider->fetchRow( "SELECT p.villages_id, p.villages_data FROM p_players p WHERE p.id=%s", array(
  2596.  
  2597.             intval( $fromVillageRow['player_id'] )
  2598.  
  2599.         ) );
  2600.  
  2601.         $villages_id = trim( $prow['villages_id'] );
  2602.  
  2603.         if ( $villages_id != "" )
  2604.  
  2605.         {
  2606.  
  2607.             $villages_id .= ",";
  2608.  
  2609.         }
  2610.  
  2611.         $villages_id .= $toVillageRow['id'];
  2612.  
  2613.         $villages_data = trim( $prow['villages_data'] );
  2614.  
  2615.         if ( $villages_data != "" )
  2616.  
  2617.         {
  2618.  
  2619.             $villages_data .= "\n";
  2620.  
  2621.         }
  2622.  
  2623.         $villages_data .= $toVillageRow['id']." ".$toVillageRow['rel_x']." ".$toVillageRow['rel_y']." ".$villageName;
  2624.  
  2625.         $abodR->provider->executeQuery( "UPDATE p_players p\r\n\t\t\tSET\r\n\t\t\t\tp.total_people_count=p.total_people_count+%s,\r\n\t\t\t\tp.villages_count=p.villages_count+1,\r\n\t\t\t\tp.selected_village_id=%s,\r\n\t\t\t\tp.villages_id='%s',\r\n\t\t\t\tp.villages_data='%s'\r\n\t\t\tWHERE\r\n\t\t\t\tp.id=%s", array(
  2626.  
  2627.             intval( $fromVillageRow['people_count'] ),
  2628.  
  2629.             intval( $toVillageRow['id'] ),
  2630.  
  2631.             $villages_id,
  2632.  
  2633.             $villages_data,
  2634.  
  2635.             intval( $fromVillageRow['player_id'] )
  2636.  
  2637.         ) );
  2638.  
  2639.         $abodR->provider->executeQuery2("UPDATE p_villages SET people_count='".$fromVillageRow['people_count']."' WHERE id = '".$toVillageRow['id']."'");
  2640.  
  2641. require_once(MODEL_PATH."plus.php");
  2642.  
  2643. $msl = new Puls();
  2644.  
  2645. $msl->plusress($toVillageRow['id'],1);
  2646.  
  2647. $msl->plusress($toVillageRow['id'],2);
  2648.  
  2649. $msl->plusress($toVillageRow['id'],3);
  2650.  
  2651. $msl->plusress($toVillageRow['id'],4);
  2652.  
  2653. $this->redirect ('village1');
  2654.  
  2655.  
  2656.  
  2657.  
  2658.  
  2659.  
  2660.  
  2661. }
  2662.  
  2663. }
  2664.  
  2665. }
  2666.  
  2667. }
  2668.  
  2669. }
  2670.  
  2671.  
  2672.  
  2673.  
  2674.  
  2675. if ($this->selectedTabIndex == 0)
  2676.  
  2677. {
  2678.  
  2679. if ((((isset ($_GET['mc']) AND !$this->data['is_capital']) AND !$this->data['is_special_village']) AND $this->buildings[$this->buildingIndex]['item_id'] == 26))
  2680.  
  2681. {
  2682.  
  2683. if($this->isPost ()){
  2684.  
  2685. if(md5($_POST['pwd']) != $this->data['pwd']){ $this->datapass = 1; }
  2686.  
  2687. if($this->datapass != 1){
  2688.  
  2689. $m = new BuildModel ();
  2690.  
  2691. $this->makeVillageAsCapital = $m->makeVillageAsCapital ($this->player->playerId, $this->data['selected_village_id']);
  2692.  
  2693.  
  2694.  
  2695. if(!$this->makeVillageAsCapital AND $this->datapass != 1){
  2696.  
  2697. $this->data['is_capital'] = TRUE;
  2698.  
  2699. }
  2700.  
  2701. $m->dispose ();
  2702.  
  2703. }
  2704.  
  2705. }
  2706.  
  2707. }
  2708.  
  2709. $this->childVillagesCount = 0;
  2710.  
  2711. if (trim ($this->data['child_villages_id']) != '')
  2712.  
  2713. {
  2714.  
  2715. $this->childVillagesCount = sizeof (explode (',', $this->data['child_villages_id']));
  2716.  
  2717. }
  2718.  
  2719. $itemId = $this->buildings[$this->buildingIndex]['item_id'];
  2720.  
  2721. $buildingLevel = $this->buildings[$this->buildingIndex]['level'];
  2722.  
  2723. $this->troopsUpgradeType = QS_TROOP_TRAINING;
  2724.  
  2725. $this->_getOnlyMyTroops ();
  2726.  
  2727. $this->troopsUpgrade = array ();
  2728.  
  2729. $_arr = explode (',', $this->data['troops_training']);
  2730.  
  2731. foreach ($_arr as $troopStr)
  2732.  
  2733. {
  2734.  
  2735. list ($troopId, $researches_done, $defense_level, $attack_level) = explode (' ', $troopStr);
  2736.  
  2737. if (($researches_done == 1 AND $this->_canTrainInBuilding ($troopId, $itemId)))
  2738.  
  2739. {
  2740.  
  2741. $this->troopsUpgrade[] = array ('troopId' => $troopId, 'maxNumber' => $this->_getMaxTrainNumber ($troopId, $itemId), 'currentNumber' => $this->_getCurrentNumberFor ($troopId, $itemId));
  2742.  
  2743. continue;
  2744.  
  2745. }
  2746.  
  2747. }
  2748.  
  2749. $maxvillagetomake = 0;
  2750.  
  2751. if($this->buildings[$this->buildingIndex]['item_id'] == 25) {
  2752.  
  2753. if($buildingLevel >= 10) {
  2754.  
  2755. $maxvillagetomake += 1;
  2756.  
  2757. }
  2758.  
  2759. if($buildingLevel >= 20) {
  2760.  
  2761. $maxvillagetomake += 1;
  2762.  
  2763. }
  2764.  
  2765. }
  2766.  
  2767. if($this->buildings[$this->buildingIndex]['item_id'] == 26) {
  2768.  
  2769. if($buildingLevel >= 10) {
  2770.  
  2771. $maxvillagetomake += 1;
  2772.  
  2773. }
  2774.  
  2775. if($buildingLevel >= 15) {
  2776.  
  2777. $maxvillagetomake += 1;
  2778.  
  2779. }
  2780.  
  2781. if($buildingLevel >= 20) {
  2782.  
  2783. $maxvillagetomake += 1;
  2784.  
  2785. }
  2786.  
  2787. }
  2788.  
  2789. $maxvillagetomake = $maxvillagetomake-$this->childVillagesCount;
  2790.  
  2791. if($maxvillagetomake <= 0) {
  2792.  
  2793. $maxvillagetomake = 0;
  2794.  
  2795. }
  2796.  
  2797. if(count($this->troopsUpgrade) == 1) {
  2798.  
  2799. $maxnumtocal = $this->troopsUpgrade[0]['currentNumber'];
  2800.  
  2801. $this->troopsUpgrade[0]['maxNumber'] = (($maxvillagetomake*3)-$maxnumtocal) < 0 ? 0 : (($maxvillagetomake*3)-$maxnumtocal);
  2802.  
  2803. } elseif (count($this->troopsUpgrade) == 2) {
  2804.  
  2805. $maxnumtocal = ($this->troopsUpgrade[0]['currentNumber']*3)+$this->troopsUpgrade[1]['currentNumber'];
  2806.  
  2807. $this->troopsUpgrade[0]['maxNumber'] = (($maxvillagetomake*3)-$maxnumtocal) >= 3 ? 1 : 0;
  2808.  
  2809. $this->troopsUpgrade[1]['maxNumber'] = (($maxvillagetomake*3)-$maxnumtocal) < 0 ? 0 : (($maxvillagetomake*3)-$maxnumtocal);
  2810.  
  2811. }
  2812.  
  2813. $this->showBuildingForm = FALSE;
  2814.  
  2815. //if ((($buildingLevel < 10 OR $this->childVillagesCount == 2) OR ($this->childVillagesCount == 1 AND $buildingLevel < 20)))
  2816.  
  2817. if($maxvillagetomake == 0)
  2818.  
  2819. {
  2820.  
  2821. $this->troopsUpgrade = array ();
  2822.  
  2823. }
  2824.  
  2825. else
  2826.  
  2827. {
  2828.  
  2829. if (1 < sizeof ($this->troopsUpgrade))
  2830.  
  2831. {
  2832.  
  2833. if (($this->troopsUpgrade[0]['currentNumber'] == 1 OR $this->troopsUpgrade[1]['currentNumber'] == 3))
  2834.  
  2835. {
  2836.  
  2837. //$this->troopsUpgrade = array ();
  2838.  
  2839. }
  2840.  
  2841. else
  2842.  
  2843. {
  2844.  
  2845. if (0 < $this->troopsUpgrade[1]['currentNumber'])
  2846.  
  2847. {
  2848.  
  2849. //unset ($this->troopsUpgrade[0]);
  2850.  
  2851. }
  2852.  
  2853. }
  2854.  
  2855. }
  2856.  
  2857. else
  2858.  
  2859. {
  2860.  
  2861. if ($this->troopsUpgrade[0]['currentNumber'] == 3)
  2862.  
  2863. {
  2864.  
  2865. $this->troopsUpgrade = array ();
  2866.  
  2867. }
  2868.  
  2869. }
  2870.  
  2871. $this->showBuildingForm = 0 < sizeof ($this->troopsUpgrade);
  2872.  
  2873. }
  2874.  
  2875. if (((($this->isPost () AND isset ($_POST['tf'])) AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  2876.  
  2877. {
  2878.  
  2879. $postednow = 0;
  2880.  
  2881. $postednext = 0;
  2882.  
  2883. foreach ($_POST['tf'] as $troopId => $num)
  2884.  
  2885. {
  2886.  
  2887. if(preg_match('/^[+-]?[0-9]+$/', $_POST['tf'][$troopId]) == 0) {
  2888.  
  2889. return null;
  2890.  
  2891. }
  2892.  
  2893. $postednext += 1;
  2894.  
  2895. $num = intval ($num);
  2896.  
  2897. if($postednow >= 1) {
  2898.  
  2899. return ;
  2900.  
  2901. }
  2902.  
  2903. $existsTroop = FALSE;
  2904.  
  2905. foreach ($this->troopsUpgrade as $troop)
  2906.  
  2907. {
  2908.  
  2909. if ($troop['troopId'] == $troopId)
  2910.  
  2911. {
  2912.  
  2913. $existsTroop = TRUE;
  2914.  
  2915. break;
  2916.  
  2917. }
  2918.  
  2919. }
  2920.  
  2921. if ((($num <= 0 OR !$existsTroop) OR $this->troopsUpgrade[$postednext-1]['maxNumber'] < $num))
  2922.  
  2923. {
  2924.  
  2925. continue;
  2926.  
  2927. }
  2928.  
  2929. $artefact= 10;
  2930.  
  2931. $q = new QueueModel();
  2932.  
  2933.  
  2934.  
  2935.  
  2936.  
  2937.  
  2938.  
  2939. $troopMetadata = $this->gameMetadata['troops'][$troopId];
  2940.  
  2941. foreach ($troopMetadata['training_resources'] as $key => $value) {
  2942.  
  2943.   $troopMetadata['training_resources'][$key] = $value * $num;
  2944.  
  2945. }
  2946.  
  2947. $calcConsume = intval ($troopMetadata['training_time_consume'] / $this->gameSpeed * ($artefact / ($this->buildProperties['building']['level'] + 9)));
  2948.  
  2949. $newTask = new QueueTask ($this->troopsUpgradeType, $this->player->playerId, $calcConsume);
  2950.  
  2951. $newTask->threads = $num;
  2952.  
  2953. $newTask->villageId = $this->data['selected_village_id'];
  2954.  
  2955. $newTask->buildingId = $this->buildProperties['building']['item_id'];
  2956.  
  2957. $newTask->procParams = $troopId;
  2958.  
  2959. $newTask->tag = $troopMetadata['training_resources'];
  2960.  
  2961. $this->queueModel->addTask ($newTask);
  2962.  
  2963. if($num >= 1) {
  2964.  
  2965. $postednow += 1;
  2966.  
  2967. }
  2968.  
  2969. }
  2970.  
  2971. return null;
  2972.  
  2973. }
  2974.  
  2975. }
  2976.  
  2977. else
  2978.  
  2979. {
  2980.  
  2981. if ($this->selectedTabIndex == 1)
  2982.  
  2983. {
  2984.  
  2985. $this->neededCpValue = $this->totalCpRate = $this->totalCpValue = 0;
  2986.  
  2987. $m = new BuildModel ();
  2988.  
  2989. $result = $m->getVillagesCp ($this->data['villages_id']);
  2990.  
  2991. while ($result->next ())
  2992.  
  2993. {
  2994.  
  2995. $tempdata = explode(' ', $result->row['cp']);
  2996.  
  2997. $this->cpValue = $tempdata[0];
  2998.  
  2999. $cpRate = $tempdata[1];
  3000.  
  3001. $this->neededCpValue = ceil(($this->data['villages_count']*$this->data['villages_count'])*255+500);
  3002.  
  3003. }
  3004.  
  3005. $this->vcplist = 0;
  3006.  
  3007. $this->totalCpValue = 0;
  3008.  
  3009. foreach ( $this->playerVillages as $vid => $pvillage )
  3010.  
  3011. {
  3012.  
  3013. $result = $m->getVillagesCp ($vid);
  3014.  
  3015. while ($result->next ())
  3016.  
  3017. {
  3018.  
  3019. $tempdata = explode(' ', $result->row['cp']);
  3020.  
  3021. $this->totalCpValue += $tempdata[0];
  3022.  
  3023. $this->vcplist += $tempdata[1];
  3024.  
  3025. }
  3026.  
  3027. }
  3028.  
  3029. $this->totalCpRate = floor($this->vcplist);
  3030.  
  3031. $this->totalCpValue = floor($this->totalCpValue);
  3032.  
  3033. $m->dispose ();
  3034.  
  3035. return null;
  3036.  
  3037. }
  3038.  
  3039. if ($this->selectedTabIndex == 3)
  3040.  
  3041. {
  3042.  
  3043. $this->childVillages = array ();
  3044.  
  3045. $m = new BuildModel ();
  3046.  
  3047. $result = $m->getChildVillagesFor (trim ($this->data['child_villages_id']));
  3048.  
  3049. while (($result != NULL AND $result->next ()))
  3050.  
  3051. {
  3052.  
  3053. $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']);
  3054.  
  3055. }
  3056.  
  3057. $m->dispose ();
  3058.  
  3059. }
  3060.  
  3061. }
  3062.  
  3063. }
  3064.  
  3065. function handleHerosMansion ()
  3066.  
  3067. {
  3068.  
  3069. $this->selectedTabIndex = (((isset ($_GET['t']) AND is_numeric ($_GET['t'])) AND intval ($_GET['t']) == 1) ? intval ($_GET['t']) : 0);
  3070.  
  3071. if ($this->selectedTabIndex == 0)
  3072.  
  3073. {
  3074.  
  3075. $this->hasHero = 0 < intval ($this->data['hero_troop_id']);
  3076.  
  3077. $this->troopsUpgradeType = QS_TROOP_TRAINING_HERO;
  3078.  
  3079. if (!$this->hasHero)
  3080.  
  3081. {
  3082.  
  3083. $this->_getOnlyMyTroops (TRUE);
  3084.  
  3085. if ((((((((isset ($_GET['a']) AND isset ($_GET['k'])) AND $_GET['k'] == $this->data['update_key']) AND !isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType])) AND isset ($this->troops[intval ($_GET['a'])])) AND 0 < $this->troops[intval ($_GET['a'])]) AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  3086.  
  3087. {
  3088.  
  3089. $troopId = intval ($_GET['a']);
  3090.  
  3091. $troopMetadata = $this->gameMetadata['troops'][$troopId];
  3092.  
  3093. $nResources = array ('1' => $troopMetadata['training_resources'][1] * ($this->data['hero_level']+5), '2' => $troopMetadata['training_resources'][2] * ($this->data['hero_level']+5), '3' => $troopMetadata['training_resources'][3] * ($this->data['hero_level']+5), '4' => $troopMetadata['training_resources'][4] * ($this->data['hero_level']+5));
  3094.  
  3095. if (!$this->isResourcesAvailable ($nResources))
  3096.  
  3097. {
  3098.  
  3099. return null;
  3100.  
  3101. }
  3102.  
  3103. $artefact= 10;
  3104.  
  3105. $q = new QueueModel();
  3106.  
  3107. $calcConsume = intval($this->data['hero_level']/5000)+($troopId*3);
  3108.  
  3109. $newTask = new QueueTask ($this->troopsUpgradeType, $this->player->playerId, $calcConsume);
  3110.  
  3111. $newTask->procParams = $troopId . ' ' . $this->data['selected_village_id'];
  3112.  
  3113. $newTask->tag = $nResources;
  3114.  
  3115. $this->queueModel->addTask ($newTask);
  3116.  
  3117. return null;
  3118.  
  3119. }
  3120.  
  3121. }
  3122.  
  3123. else
  3124.  
  3125. {
  3126.  
  3127. if (isset($_GET['addatt'])) {
  3128.  
  3129. $m = new BuildModel ();
  3130.  
  3131. if ($this->data['h2ero_points'] >= 9) {
  3132.  
  3133. $m->changeHeropointatt ($this->player->playerId);
  3134.  
  3135. }
  3136.  
  3137. $m->dispose ();
  3138.  
  3139. $this->redirect ('build?id='.$this->buildingIndex.'');
  3140.  
  3141. return null;
  3142.  
  3143. }
  3144.  
  3145.  
  3146.  
  3147. if (isset($_GET['addattp'])) {
  3148.  
  3149. $m = new BuildModel ();
  3150.  
  3151. $rr = $this->data['h2ero_points']-1;
  3152.  
  3153. if ($this->data['h2ero_points'] >= $rr) {
  3154.  
  3155. $m->changeHeropointattX ($this->player->playerId);
  3156.  
  3157. }
  3158.  
  3159. $m->dispose ();
  3160.  
  3161. $this->redirect ('build?id='.$this->buildingIndex.'');
  3162.  
  3163. return null;
  3164.  
  3165. }
  3166.  
  3167.  
  3168.  
  3169. if (isset($_GET['adddeff'])) {
  3170.  
  3171. $m = new BuildModel ();
  3172.  
  3173. if ($this->data['h2ero_points'] >= 9) {
  3174.  
  3175. $m->changeHeropointdeff ($this->player->playerId);
  3176.  
  3177. }
  3178.  
  3179. $this->redirect ('build?id='.$this->buildingIndex.'');
  3180.  
  3181. $m->dispose ();
  3182.  
  3183. return null;
  3184.  
  3185. }
  3186.  
  3187.  
  3188.  
  3189. if (isset($_GET['adddeffp'])) {
  3190.  
  3191. $m = new BuildModel ();
  3192.  
  3193. $rr = $this->data['h2ero_points']-1;
  3194.  
  3195. if ($this->data['h2ero_points'] >= $rr) {
  3196.  
  3197. $m->changeHeropointdeffX ($this->player->playerId);
  3198.  
  3199. }
  3200.  
  3201. $this->redirect ('build?id='.$this->buildingIndex.'');
  3202.  
  3203. $m->dispose ();
  3204.  
  3205. return null;
  3206.  
  3207. }
  3208.  
  3209. if ((($this->isPost () AND isset ($_POST['hname'])) AND trim ($_POST['hname']) != ''))
  3210.  
  3211. {
  3212.  
  3213. $this->data['hero_name'] = trim ($_POST['hname']);
  3214.  
  3215. $m = new BuildModel ();
  3216.  
  3217. $m->changeHeroName ($this->player->playerId, $this->data['hero_name']);
  3218.  
  3219. $m->dispose ();
  3220.  
  3221. return null;
  3222.  
  3223. }
  3224.  
  3225. }
  3226.  
  3227. }
  3228.  
  3229. else
  3230.  
  3231. {
  3232.  
  3233. if ($this->selectedTabIndex == 1)
  3234.  
  3235. {
  3236.  
  3237. $this->villageOases = array ();
  3238.  
  3239. $m = new BuildModel ();
  3240.  
  3241. $result = $m->getVillageOases (trim ($this->data['village_oases_id']));
  3242.  
  3243. while (($result != NULL AND $result->next ()))
  3244.  
  3245. {
  3246.  
  3247. $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']);
  3248.  
  3249. }
  3250.  
  3251. $m->dispose ();
  3252.  
  3253. if (((((((isset ($_GET['a']) AND isset ($_GET['k'])) AND $_GET['k'] == $this->data['update_key']) AND isset ($this->villageOases[intval ($_GET['a'])])) AND !isset ($this->queueModel->tasksInQueue[QS_LEAVEOASIS][intval ($_GET['a'])])) AND !$this->isGameTransientStopped ()) AND !$this->isGameOver ()))
  3254.  
  3255. {
  3256.  
  3257. $oasisId = intval ($_GET['a']);
  3258.  
  3259. $newTask = new QueueTask (QS_LEAVEOASIS, $this->player->playerId, floor (21600 / $this->gameSpeed));
  3260.  
  3261. $newTask->villageId = $this->data['selected_village_id'];
  3262.  
  3263. $newTask->buildingId = $oasisId;
  3264.  
  3265. $newTask->procParams = $this->villageOases[$oasisId]['rel_x'] . ' ' . $this->villageOases[$oasisId]['rel_y'];
  3266.  
  3267. $this->queueModel->addTask ($newTask);
  3268.  
  3269. return null;
  3270.  
  3271. }
  3272.  
  3273. if ((isset ($_GET['qid']) AND 0 < intval ($_GET['qid'])))
  3274.  
  3275. {
  3276.  
  3277. $this->queueModel->cancelTask ($this->player->playerId, intval ($_GET['qid']));
  3278.  
  3279. }
  3280.  
  3281. }
  3282.  
  3283. }
  3284.  
  3285. }
  3286.  
  3287. function preRender ()
  3288.  
  3289. {
  3290.  
  3291. parent::prerender ();
  3292.  
  3293. if (isset ($_GET['p']))
  3294.  
  3295. {
  3296.  
  3297. $this->villagesLinkPostfix .= '&p=' . intval ($_GET['p']);
  3298.  
  3299. }
  3300.  
  3301. if (isset ($_GET['vid2']))
  3302.  
  3303. {
  3304.  
  3305. $this->villagesLinkPostfix .= '&vid2=' . intval ($_GET['vid2']);
  3306.  
  3307. }
  3308.  
  3309. if (0 < $this->selectedTabIndex)
  3310.  
  3311. {
  3312.  
  3313. $this->villagesLinkPostfix .= '&t=' . $this->selectedTabIndex;
  3314.  
  3315. }
  3316.  
  3317. }
  3318.  
  3319. function __getCoordInRange ($map_size, $x)
  3320.  
  3321. {
  3322.  
  3323. if ($map_size <= $x)
  3324.  
  3325. {
  3326.  
  3327. $x -= $map_size;
  3328.  
  3329. }
  3330.  
  3331. else
  3332.  
  3333. {
  3334.  
  3335. if ($x < 0)
  3336.  
  3337. {
  3338.  
  3339. $x = $map_size + $x;
  3340.  
  3341. }
  3342.  
  3343. }
  3344.  
  3345. return $x;
  3346.  
  3347. }
  3348.  
  3349. function __getVillageId ($map_size, $x, $y)
  3350.  
  3351. {
  3352.  
  3353. return $x * $map_size + ($y + 1);
  3354.  
  3355. }
  3356.  
  3357. function _getOnlyMyOuterTroops ()
  3358.  
  3359. {
  3360.  
  3361. $returnTroops = array ();
  3362.  
  3363. if (trim ($this->data['troops_out_num']) != '')
  3364.  
  3365. {
  3366.  
  3367. $t_arr = explode ('|', $this->data['troops_out_num']);
  3368.  
  3369. foreach ($t_arr as $t_str)
  3370.  
  3371. {
  3372.  
  3373. $t2_arr = explode (':', $t_str);
  3374.  
  3375. $t2_arr = explode (',', $t2_arr[1]);
  3376.  
  3377. foreach ($t2_arr as $t2_str)
  3378.  
  3379. {
  3380.  
  3381. $t = explode (' ', $t2_str);
  3382.  
  3383. if ($t[1] == 0 - 1)
  3384.  
  3385. {
  3386.  
  3387. continue;
  3388.  
  3389. }
  3390.  
  3391. if (isset ($returnTroops[$t[0]]))
  3392.  
  3393. {
  3394.  
  3395. $returnTroops[$t[0]] += $t[1];
  3396.  
  3397. continue;
  3398.  
  3399. }
  3400.  
  3401. else
  3402.  
  3403. {
  3404.  
  3405. $returnTroops[$t[0]] = $t[1];
  3406.  
  3407. continue;
  3408.  
  3409. }
  3410.  
  3411. }
  3412.  
  3413. }
  3414.  
  3415. }
  3416.  
  3417. if (trim ($this->data['troops_out_intrap_num']) != '')
  3418.  
  3419. {
  3420.  
  3421. $t_arr = explode ('|', $this->data['troops_out_intrap_num']);
  3422.  
  3423. foreach ($t_arr as $t_str)
  3424.  
  3425. {
  3426.  
  3427. $t2_arr = explode (':', $t_str);
  3428.  
  3429. $t2_arr = explode (',', $t2_arr[1]);
  3430.  
  3431. foreach ($t2_arr as $t2_str)
  3432.  
  3433. {
  3434.  
  3435. $t = explode (' ', $t2_str);
  3436.  
  3437. if ($t[1] == 0 - 1)
  3438.  
  3439. {
  3440.  
  3441. continue;
  3442.  
  3443. }
  3444.  
  3445. if (isset ($returnTroops[$t[0]]))
  3446.  
  3447. {
  3448.  
  3449. $returnTroops += $t[0] = $t[1];
  3450.  
  3451. continue;
  3452.  
  3453. }
  3454.  
  3455. else
  3456.  
  3457. {
  3458.  
  3459. $returnTroops[$t[0]] = $t[1];
  3460.  
  3461. continue;
  3462.  
  3463. }
  3464.  
  3465. }
  3466.  
  3467. }
  3468.  
  3469. }
  3470.  
  3471. return $returnTroops;
  3472.  
  3473. }
  3474.  
  3475. function _getOnlyMyTroops ($toBeHero = FALSE)
  3476.  
  3477. {
  3478.  
  3479. $t_arr = explode ('|', $this->data['troops_num']);
  3480.  
  3481. foreach ($t_arr as $t_str)
  3482.  
  3483. {
  3484.  
  3485. $t2_arr = explode (':', $t_str);
  3486.  
  3487. if ($t2_arr[0] == 0 - 1)
  3488.  
  3489. {
  3490.  
  3491. $t2_arr = explode (',', $t2_arr[1]);
  3492.  
  3493. foreach ($t2_arr as $t2_str)
  3494.  
  3495. {
  3496.  
  3497. $t = explode (' ', $t2_str);
  3498.  
  3499. if (($toBeHero AND (((((((((((((((((((($t[0] == 99 OR $t[0] == 7) OR $t[0] == 8) OR $t[0] == 9) OR $t[0] == 10) OR $t[0] == 17) OR $t[0] == 18) OR $t[0] == 19) OR $t[0] == 20) OR $t[0] == 27) OR $t[0] == 28) OR $t[0] == 29) OR $t[0] == 30) OR $t[0] == 106) OR $t[0] == 107) OR $t[0] == 108) OR $t[0] == 109) OR $t[0] == 57) OR $t[0] == 58) OR $t[0] == 59) OR $t[0] == 60)))
  3500.  
  3501. {
  3502.  
  3503. continue;
  3504.  
  3505. }
  3506.  
  3507. if (isset ($this->troops[$t[0]]))
  3508.  
  3509. {
  3510.  
  3511. $this->troops += $t[0] = $t[1];
  3512.  
  3513. continue;
  3514.  
  3515. }
  3516.  
  3517. else
  3518.  
  3519. {
  3520.  
  3521. $this->troops[$t[0]] = $t[1];
  3522.  
  3523. continue;
  3524.  
  3525. }
  3526.  
  3527. }
  3528.  
  3529. continue;
  3530.  
  3531. }
  3532.  
  3533. }
  3534.  
  3535. if ((!$toBeHero AND !isset ($this->troops[99])))
  3536.  
  3537. {
  3538.  
  3539. $this->troops[99] = 0;
  3540.  
  3541. }
  3542.  
  3543. }
  3544.  
  3545. function _getMaxBuildingLevel ($itemId)
  3546.  
  3547. {
  3548.  
  3549. $result = 0;
  3550.  
  3551. foreach ($this->buildings as $villageBuild)
  3552.  
  3553. {
  3554.  
  3555. if (($villageBuild['item_id'] == $itemId AND $result < $villageBuild['level']))
  3556.  
  3557. {
  3558.  
  3559. $result = $villageBuild['level'];
  3560.  
  3561. continue;
  3562.  
  3563. }
  3564.  
  3565. }
  3566.  
  3567. return $result;
  3568.  
  3569. }
  3570.  
  3571. function _getCurrentNumberFor ($troopId, $item)
  3572.  
  3573. {
  3574.  
  3575. $num = 0;
  3576.  
  3577. if (isset ($this->troops[$troopId]))
  3578.  
  3579. {
  3580.  
  3581. $num += $this->troops[$troopId];
  3582.  
  3583. }
  3584.  
  3585. if ((isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType]) AND isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType][$item])))
  3586.  
  3587. {
  3588.  
  3589. $qts = $this->queueModel->tasksInQueue[$this->troopsUpgradeType][$item];
  3590.  
  3591. foreach ($qts as $qt)
  3592.  
  3593. {
  3594.  
  3595. if ($qt['proc_params'] == $troopId)
  3596.  
  3597. {
  3598.  
  3599. $num += $qt['threads'];
  3600.  
  3601. continue;
  3602.  
  3603. }
  3604.  
  3605. }
  3606.  
  3607. }
  3608.  
  3609. $num += $this->_getTroopCountInTransfer ($troopId, QS_WAR_REINFORCE);
  3610.  
  3611. $num += $this->_getTroopCountInTransfer ($troopId, QS_WAR_ATTACK);
  3612.  
  3613. $num += $this->_getTroopCountInTransfer ($troopId, QS_WAR_ATTACK_PLUNDER);
  3614.  
  3615. $num += $this->_getTroopCountInTransfer ($troopId, QS_WAR_ATTACK_SPY);
  3616.  
  3617. $num += $this->_getTroopCountInTransfer ($troopId, QS_CREATEVILLAGE);
  3618.  
  3619. $ts = $this->_getOnlyMyOuterTroops ();
  3620.  
  3621. if (isset ($ts[$troopId]))
  3622.  
  3623. {
  3624.  
  3625. $num += $ts[$troopId];
  3626.  
  3627. }
  3628.  
  3629. return $num;
  3630.  
  3631. }
  3632.  
  3633. function _getTroopCountInTransfer ($troopId, $type)
  3634.  
  3635. {
  3636.  
  3637. $num = 0;
  3638.  
  3639. if (isset ($this->queueModel->tasksInQueue[$type]))
  3640.  
  3641. {
  3642.  
  3643. $qts = $this->queueModel->tasksInQueue[$type];
  3644.  
  3645. foreach ($qts as $qt)
  3646.  
  3647. {
  3648.  
  3649. $arr = explode ('|', $qt['proc_params']);
  3650.  
  3651. $arr = explode (',', $arr[0]);
  3652.  
  3653. foreach ($arr as $arrStr)
  3654.  
  3655. {
  3656.  
  3657. list ($tid, $tnum) = explode (' ', $arrStr);
  3658.  
  3659. if ($tid == $troopId)
  3660.  
  3661. {
  3662.  
  3663. $num += $tnum;
  3664.  
  3665. continue;
  3666.  
  3667. }
  3668.  
  3669. }
  3670.  
  3671. }
  3672.  
  3673. }
  3674.  
  3675. return $num;
  3676.  
  3677. }
  3678.  
  3679. function _getMaxTrainNumber ($troopId, $item)
  3680.  
  3681. {
  3682.  
  3683. ///Start Artefect
  3684.  
  3685. $efect = 3;
  3686.  
  3687. $pid = $this->player->playerId;
  3688.  
  3689. $vid = $this->data['selected_village_id'];
  3690.  
  3691. $tatarzx = new QueueModel();
  3692.  
  3693. $this->BigArt = $tatarzx->provider->fetchScalar( "SELECT COUNT(*) FROM `p_villages` WHERE type=4 AND artefacts='".$efect."' AND player_id='".$pid."'" );
  3694.  
  3695. $this->SeCArt = $tatarzx->provider->fetchScalar( "SELECT COUNT(*) FROM `p_villages` WHERE type=2 AND artefacts='".$efect."' AND player_id='".$pid."'" );
  3696.  
  3697. $this->SmallArt = $tatarzx->provider->fetchScalar( "SELECT COUNT(*) FROM `p_villages` WHERE type=3 AND id='".$vid."' AND artefacts='".$efect."'" );
  3698.  
  3699. $Tfsh = 1;
  3700.  
  3701. if ($this->BigArt) {
  3702.  
  3703. $Tfsh = 2;
  3704.  
  3705. }else if ($this->SeCArt) {
  3706.  
  3707. $Tfsh = 1.5;
  3708.  
  3709. }else if ($this->SmallArt) {
  3710.  
  3711. $Tfsh = 2;
  3712.  
  3713. }
  3714.  
  3715. ///End Artefect
  3716.  
  3717.  
  3718.  
  3719. $max = 0;
  3720.  
  3721. $_f = TRUE;
  3722.  
  3723. foreach ($this->gameMetadata['troops'][$troopId]['training_resources'] as $k => $v)
  3724.  
  3725. {
  3726.  
  3727. $num = floor ($this->resources[$k]['current_value'] / ($v * $this->buildingTribeFactor/$Tfsh));
  3728.  
  3729. if (($num < $max OR $_f))
  3730.  
  3731. {
  3732.  
  3733. $_f = FALSE;
  3734.  
  3735. $max = $num;
  3736.  
  3737. continue;
  3738.  
  3739. }
  3740.  
  3741. }
  3742.  
  3743. if ($troopId == 99)
  3744.  
  3745. {
  3746.  
  3747. $buildingMetadata = $this->gameMetadata['items'][$this->buildings[$this->buildingIndex]['item_id']]['levels'][$this->buildProperties['building']['level'] - 1];
  3748.  
  3749. $_maxValue = $buildingMetadata['value'] - $this->troops[$troopId];
  3750.  
  3751. if ((isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType]) AND isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType][$this->buildProperties['building']['item_id']])))
  3752.  
  3753. {
  3754.  
  3755. $qts = $this->queueModel->tasksInQueue[$this->troopsUpgradeType][$this->buildProperties['building']['item_id']];
  3756.  
  3757. foreach ($qts as $qt)
  3758.  
  3759. {
  3760.  
  3761. if ($qt['proc_params'] == $troopId)
  3762.  
  3763. {
  3764.  
  3765. $_maxValue -= $qt['threads'];
  3766.  
  3767. continue;
  3768.  
  3769. }
  3770.  
  3771. }
  3772.  
  3773. }
  3774.  
  3775. if ($_maxValue < $max)
  3776.  
  3777. {
  3778.  
  3779. $max = $_maxValue;
  3780.  
  3781. }
  3782.  
  3783. }
  3784.  
  3785. else
  3786.  
  3787. {
  3788.  
  3789. if (($item == 25 OR $item == 26))
  3790.  
  3791. {
  3792.  
  3793. $_maxValue = ((((($troopId == 9 OR $troopId == 19) OR $troopId == 29) OR $troopId == 108) OR $troopId == 59) ? 1 : 3);
  3794.  
  3795. if ((isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType]) AND isset ($this->queueModel->tasksInQueue[$this->troopsUpgradeType][$item])))
  3796.  
  3797. {
  3798.  
  3799. $qts = $this->queueModel->tasksInQueue[$this->troopsUpgradeType][$item];
  3800.  
  3801. foreach ($qts as $qt)
  3802.  
  3803. {
  3804.  
  3805. if ($qt['proc_params'] == $troopId)
  3806.  
  3807. {
  3808.  
  3809. $_maxValue -= $qt['threads'];
  3810.  
  3811. continue;
  3812.  
  3813. }
  3814.  
  3815. }
  3816.  
  3817. }
  3818.  
  3819. if ($_maxValue < $max)
  3820.  
  3821. {
  3822.  
  3823. $max = $_maxValue;
  3824.  
  3825. }
  3826.  
  3827. }
  3828.  
  3829. }
  3830.  
  3831. return ($max < 0 ? 0 : $max);
  3832.  
  3833. }
  3834.  
  3835. function _canTrainInBuilding ($troopId, $itemId)
  3836.  
  3837. {
  3838.  
  3839. foreach ($this->gameMetadata['troops'][$troopId]['trainer_building'] as $buildingId)
  3840.  
  3841. {
  3842.  
  3843. if ($buildingId == $itemId)
  3844.  
  3845. {
  3846.  
  3847. return TRUE;
  3848.  
  3849. }
  3850.  
  3851. }
  3852.  
  3853. return FALSE;
  3854.  
  3855. }
  3856.  
  3857. function _canDoResearches ($troopId)
  3858.  
  3859. {
  3860.  
  3861. foreach ($this->gameMetadata['troops'][$troopId]['pre_requests'] as $req_item_id => $level)
  3862.  
  3863. {
  3864.  
  3865. $result = FALSE;
  3866.  
  3867. foreach ($this->buildings as $villageBuild)
  3868.  
  3869. {
  3870.  
  3871. if (($villageBuild['item_id'] == $req_item_id AND $level <= $villageBuild['level']))
  3872.  
  3873. {
  3874.  
  3875. $result = TRUE;
  3876.  
  3877. break;
  3878.  
  3879. continue;
  3880.  
  3881. }
  3882.  
  3883. }
  3884.  
  3885. if (!$result)
  3886.  
  3887. {
  3888.  
  3889. return FALSE;
  3890.  
  3891. }
  3892.  
  3893. }
  3894.  
  3895. return TRUE;
  3896.  
  3897. }
  3898.  
  3899. function txt ($n)
  3900.  
  3901. {
  3902.  
  3903. if ($n >= 35) {
  3904.  
  3905. $txt = "<b><font color='green'>".$n."</font></b>";
  3906.  
  3907. }else {
  3908.  
  3909. $txt = "<b><font color='red'>".$n."</font></b>";
  3910.  
  3911. }
  3912.  
  3913. return $txt."%";
  3914.  
  3915. }
  3916.  
  3917.  
  3918.  
  3919. function getNeededTime3 ($k,$v)
  3920.  
  3921. {
  3922.  
  3923. $timeInSeconds = 0;
  3924.  
  3925. $time = ($this->resources[$k]['current_value'] - $v) / $this->resources[$k]['calc_prod_rate'];
  3926.  
  3927. if ($timeInSeconds < $time)
  3928.  
  3929. {
  3930.  
  3931. $timeInSeconds = $time;
  3932.  
  3933.  
  3934.  
  3935. }
  3936.  
  3937. return ceil ($timeInSeconds * 3600);
  3938.  
  3939. }
  3940.  
  3941.  
  3942.  
  3943.  
  3944.  
  3945.  
  3946.  
  3947. function getNeededTime2 ($k,$v)
  3948.  
  3949. {
  3950.  
  3951. $timeInSeconds = 0;
  3952.  
  3953. if ($this->resources[$k]['current_value'] < $v)
  3954.  
  3955. {
  3956.  
  3957. $time = ($v - $this->resources[$k]['current_value']) / $this->resources[$k]['calc_prod_rate'];
  3958.  
  3959. if ($timeInSeconds < $time)
  3960.  
  3961. {
  3962.  
  3963. $timeInSeconds = $time;
  3964.  
  3965. }
  3966.  
  3967. }
  3968.  
  3969. return ceil ($timeInSeconds * 3600);
  3970.  
  3971. }
  3972.  
  3973. function getNeededTime ($neededResources)
  3974.  
  3975. {
  3976.  
  3977. $timeInSeconds = 0;
  3978.  
  3979. foreach ($neededResources as $k => $v)
  3980.  
  3981. {
  3982.  
  3983. if ($this->resources[$k]['current_value'] < $v)
  3984.  
  3985. {
  3986.  
  3987. if ($this->resources[$k]['calc_prod_rate'] <= 0)
  3988.  
  3989. {
  3990.  
  3991. return 0 - 1;
  3992.  
  3993. }
  3994.  
  3995. $time = ($v - $this->resources[$k]['current_value']) / $this->resources[$k]['calc_prod_rate'];
  3996.  
  3997. if ($timeInSeconds < $time)
  3998.  
  3999. {
  4000.  
  4001. $timeInSeconds = $time;
  4002.  
  4003. continue;
  4004.  
  4005. }
  4006.  
  4007. continue;
  4008.  
  4009. }
  4010.  
  4011. }
  4012.  
  4013. return ceil ($timeInSeconds * 3600);
  4014.  
  4015. }
  4016.  
  4017. function getActionText5 ($neededResources, $url, $text, $queueTaskType, $buildLevel, $troopLevel)
  4018.  
  4019. {
  4020.  
  4021. if (isset ($this->queueModel->tasksInQueue[$queueTaskType]))
  4022.  
  4023. {
  4024.  
  4025. return '';
  4026.  
  4027. }
  4028.  
  4029. if ($buildLevel <= $troopLevel && $troopLevel != 20)
  4030.  
  4031. {
  4032.  
  4033. return '';
  4034.  
  4035. }
  4036.  
  4037. if ($troopLevel >= 20)
  4038.  
  4039. {
  4040.  
  4041. return '';
  4042.  
  4043. }
  4044.  
  4045.  
  4046.  
  4047. if($this->data['gold_num'] > $this->appConfig['Game']['dev_troop_to_20'])
  4048.  
  4049. {
  4050.  
  4051.      $return_k = '<a href="build?id=' . $this->buildingIndex . '&' . $url . '" title="التحديث للمستوى '.$buildLevel.' بالكامل مقابل '.$this->appConfig['Game']['dev_troop_to_20'].' ذهبة  مرة واحدة">'.$this->appConfig['Game']['dev_troop_to_20'].'  <img src="'.$GLOBALS['AppConfig']['system']['linksite'].'x.gif" class="gold"></a>';
  4052.  
  4053. }
  4054.  
  4055. else
  4056.  
  4057. {
  4058.  
  4059.      $return_k = '<span class="none">ليس لديك ذهب كافي</span>';
  4060.  
  4061. }
  4062.  
  4063. return $return_k;
  4064.  
  4065. }
  4066.  
  4067.  
  4068.  
  4069. function getActionText4 ($neededResources, $url, $text, $queueTaskType, $buildLevel, $troopLevel)
  4070.  
  4071. {
  4072.  
  4073. if (isset ($this->queueModel->tasksInQueue[$queueTaskType]))
  4074.  
  4075. {
  4076.  
  4077. return '<span class="none">' . buildings_p_plwait . '</span>';
  4078.  
  4079. }
  4080.  
  4081. if ($buildLevel <= $troopLevel && $troopLevel != 20)
  4082.  
  4083. {
  4084.  
  4085. return '<span class="none">' . buildings_p_needmorecapacity . '</span>';
  4086.  
  4087. }
  4088.  
  4089. if ($troopLevel >= 20)
  4090.  
  4091. {
  4092.  
  4093. return '<span class="none">تم التحديث بالكامل</span>';
  4094.  
  4095. }
  4096.  
  4097. return (!$this->isResourcesAvailable ($neededResources) ? '<span class="none">' . buildings_p_notenoughres . '</span>' : '<a class="build" href="build?id=' . $this->buildingIndex . '&' . $url . '&k=' . $this->data['update_key'] . '">' . $text . '</a>');
  4098.  
  4099. }
  4100.  
  4101. function getActionText3 ($neededResources, $url, $text, $queueTaskType)
  4102.  
  4103. {
  4104.  
  4105. if (isset ($this->queueModel->tasksInQueue[$queueTaskType]))
  4106.  
  4107. {
  4108.  
  4109. return '<span class="none">' . buildings_p_plwait . '</span>';
  4110.  
  4111. }
  4112.  
  4113. return (!$this->isResourcesAvailable ($neededResources) ? '<span class="none">' . buildings_p_notenoughres . '</span>' : '<a class="build" href="build?id=' . $this->buildingIndex . '&' . $url . '&k=' . $this->data['update_key'] . '">' . $text . '</a>');
  4114.  
  4115. }
  4116.  
  4117. function getActionText2 ($neededResources)
  4118.  
  4119. {
  4120.  
  4121. $needUpgradeType = $this->needMoreUpgrades ($neededResources);
  4122.  
  4123. if (0 < $needUpgradeType)
  4124.  
  4125. {
  4126.  
  4127. switch ($needUpgradeType)
  4128.  
  4129. {
  4130.  
  4131. case 2:
  4132.  
  4133. {
  4134.  
  4135. '<span class="none">' . buildings_p_upg1 . '</span>';
  4136.  
  4137. }
  4138.  
  4139. case 3:
  4140.  
  4141. {
  4142.  
  4143. '<span class="none">' . buildings_p_upg2 . '</span>';
  4144.  
  4145. }
  4146.  
  4147. case 4:
  4148.  
  4149. {
  4150.  
  4151. '<span class="none">' . buildings_p_upg3 . '</span>';
  4152.  
  4153. }
  4154.  
  4155. }
  4156.  
  4157. return ;
  4158.  
  4159. }
  4160.  
  4161. if (!$this->isResourcesAvailable ($neededResources))
  4162.  
  4163. {
  4164.  
  4165. $neededTime = $this->getNeededTime ($neededResources);
  4166.  
  4167. return '<span class="none">' . (0 < $neededTime ? buildings_p_willenoughresat . ' ' . WebHelper::secondstostring ($neededTime) . ' ' . time_hour_lang : buildings_p_notenoughres2) . '</span>';
  4168.  
  4169. }
  4170.  
  4171. return '';
  4172.  
  4173. }
  4174.  
  4175. function getActionText ($neededResources, $isField, $upgrade, $item_id)
  4176.  
  4177. {
  4178.  
  4179. $needUpgradeType = $this->needMoreUpgrades ($neededResources, $item_id);
  4180.  
  4181. if($needUpgradeType == 1) $needUpgradeType = 0;
  4182.  
  4183. if (0 < $needUpgradeType)
  4184.  
  4185. {
  4186.  
  4187. switch ($needUpgradeType)
  4188.  
  4189. {
  4190.  
  4191. case 1:
  4192.  
  4193. {
  4194.  
  4195. return '<span class="none">' . buildings_p_upg0 . '</span>';
  4196.  
  4197. }
  4198.  
  4199. case 2:
  4200.  
  4201. {
  4202.  
  4203. return '<span class="none">' . buildings_p_upg1 . '</span>';
  4204.  
  4205. }
  4206.  
  4207. case 3:
  4208.  
  4209. {
  4210.  
  4211. return '<span class="none">' . buildings_p_upg2 . '</span>';
  4212.  
  4213. }
  4214.  
  4215. case 4:
  4216.  
  4217. {
  4218.  
  4219. return '<span class="none">' . buildings_p_upg3 . '</span>';
  4220.  
  4221. }
  4222.  
  4223. }
  4224.  
  4225. return ;
  4226.  
  4227. }
  4228.  
  4229. else
  4230.  
  4231. {
  4232.  
  4233. if (!$this->isResourcesAvailable ($neededResources) && $_GET['id'] == 39 && !$this->buildProperties['nextLevel'])
  4234.  
  4235. {
  4236.  
  4237. $pageNamePostfix = ($isField ? '1' : '2');
  4238.  
  4239. $total_up_lvl = $this->buildProperties['nextLevel']-$this->buildProperties['building']['level'];
  4240.  
  4241.  
  4242.  
  4243. // SET GOLD :
  4244. $gold = 3 * $total_up_lvl;
  4245.  
  4246.  
  4247. $lg = ' | <a href="village' . $pageNamePostfix . '?id=' . $this->buildingIndex . '&up">الارتقاء حالاً '.$gold.'<img alt="ذهب" src="'.$GLOBALS['AppConfig']['system']['linksite'].'default/img/a/gold.gif" class="tooltip2" title="ذهب"></a>';
  4248.  
  4249. $link = ($upgrade ? '<a class="build" href="village' . $pageNamePostfix . '?id=' . $this->buildingIndex . '&upz&k=' . $this->data['update_key'] . '">' . buildings_p_upg_tolevel . ' ' . $this->buildProperties['nextLevel'] . '</a>' : '<a class="build" href="village2?id=' . $this->buildingIndex . '&b=' . $item_id . '&upz&k=' . $this->data['update_key'] . '">' . buildings_p_create_newbuild . '</a>');
  4250.  
  4251. $workerResult = $this->isWorkerBusy ($isField);
  4252.  
  4253. return ($workerResult['isBusy'] ? '<span class="none">' . buildings_p_workersbusy . '</span>' : $link . ($workerResult['isPlusUsed'] ? ' <span class="none">(' . buildings_p_wait_buildqueue . ')</span>' : ''));
  4254.  
  4255. }else
  4256.  
  4257.  
  4258.  
  4259. if ($this->isResourcesAvailable ($neededResources))
  4260.  
  4261. {
  4262.  
  4263. $gold = 0;
  4264.  
  4265. $pageNamePostfix = ($isField ? '1' : '2');
  4266.  
  4267. //$gold = $this->buildProperties['nextLevel'];
  4268.  
  4269. $lg = ' | <a href="village' . $pageNamePostfix . '?id=' . $this->buildingIndex . '&up">الارتقاء حالاً '.$gold.'<img alt="ذهب" src="'.$GLOBALS['AppConfig']['system']['linksite'].'default/img/a/gold.gif" class="tooltip2" title="ذهب"></a>';
  4270.  
  4271. $link = ($upgrade ? '<a class="build" href="village' . $pageNamePostfix . '?id=' . $this->buildingIndex . '&k=' . $this->data['update_key'] . '">' . buildings_p_upg_tolevel . ' ' . $this->buildProperties['nextLevel'] . '</a>' : '<a class="build" href="village2?id=' . $this->buildingIndex . '&b=' . $item_id . '&k=' . $this->data['update_key'] . '">' . buildings_p_create_newbuild . '</a>');
  4272.  
  4273. $workerResult = $this->isWorkerBusy ($isField);
  4274.  
  4275. return ($workerResult['isBusy'] ? '<span class="none">' . buildings_p_workersbusy . '</span>' : $link . ($workerResult['isPlusUsed'] ? ' <span class="none">(' . buildings_p_wait_buildqueue . ')</span>' : ''));
  4276.  
  4277. }
  4278.  
  4279. }
  4280.  
  4281. $neededTime = $this->getNeededTime ($neededResources);
  4282.  
  4283. return '<span class="none">' . (0 < $neededTime ? buildings_p_willenoughresat . ' ' . WebHelper::secondstostring ($neededTime) . ' ' . time_hour_lang : buildings_p_notenoughres2) . '</span>';
  4284.  
  4285. }
  4286.  
  4287. function _canAcceptOffer ($needResources, $giveResources, $villageId, $onlyForAlliance, $allianceId, $maxTime, $distance)
  4288.  
  4289. {
  4290.  
  4291. if ($villageId == $this->data['selected_village_id'])
  4292.  
  4293. {
  4294.  
  4295. return 0;
  4296.  
  4297. }
  4298.  
  4299. if (!$this->isResourcesAvailable ($giveResources))
  4300.  
  4301. {
  4302.  
  4303. return 1;
  4304.  
  4305. }
  4306.  
  4307. $needMerchantCount = ceil (($giveResources[1] + $giveResources[2] + $giveResources[3] + $giveResources[4]) / $this->merchantProperty['capacity']);
  4308.  
  4309. if (($needMerchantCount == 0 OR $this->merchantProperty['exits_num'] < $needMerchantCount))
  4310.  
  4311. {
  4312.  
  4313. return 2;
  4314.  
  4315. }
  4316.  
  4317. if (($onlyForAlliance AND (intval ($this->data['alliance_id']) == 0 OR $allianceId != intval ($this->data['alliance_id']))))
  4318.  
  4319. {
  4320.  
  4321. return 3;
  4322.  
  4323. }
  4324.  
  4325. if ((0 < $maxTime AND $maxTime < $distance / $this->merchantProperty['speed']))
  4326.  
  4327. {
  4328.  
  4329. return 4;
  4330.  
  4331. }
  4332.  
  4333. return 5;
  4334.  
  4335. }
  4336.  
  4337.     public function getNextLink( )
  4338.  
  4339.     {
  4340.  
  4341.         $text = "»";
  4342.  
  4343.         if ( $this->pageIndex + 1 == $this->pageCount )
  4344.  
  4345.         {
  4346.  
  4347.             return $text;
  4348.  
  4349.         }
  4350.  
  4351.         $link = "";
  4352.  
  4353.         if ( 0 < $this->selectedTabIndex )
  4354.  
  4355.         {
  4356.  
  4357.             $link .= "t=".$this->selectedTabIndex;
  4358.  
  4359.         }
  4360.  
  4361.         if ( $link != "" )
  4362.  
  4363.         {
  4364.  
  4365.             $link .= "&";
  4366.  
  4367.         }
  4368.  
  4369.         $link .= "p=".( $this->pageIndex + 1 );
  4370.  
  4371.         $link = "build?id=".$this->buildingIndex."&".$link;
  4372.  
  4373.         return "<a href=\"".$link."\">".$text."</a>";
  4374.  
  4375.     }
  4376.  
  4377.  
  4378.  
  4379.     public function getPreviousLink( )
  4380.  
  4381.     {
  4382.  
  4383.         $text = "«";
  4384.  
  4385.         if ( $this->pageIndex == 0 )
  4386.  
  4387.         {
  4388.  
  4389.             return $text;
  4390.  
  4391.         }
  4392.  
  4393.         $link = "";
  4394.  
  4395.         if ( 0 < $this->selectedTabIndex )
  4396.  
  4397.         {
  4398.  
  4399.             $link .= "t=".$this->selectedTabIndex;
  4400.  
  4401.         }
  4402.  
  4403.         if ( 1 < $this->pageIndex )
  4404.  
  4405.         {
  4406.  
  4407.             if ( $link != "" )
  4408.  
  4409.             {
  4410.  
  4411.                 $link .= "&";
  4412.  
  4413.             }
  4414.  
  4415.             $link .= "p=".( $this->pageIndex - 1 );
  4416.  
  4417.         }
  4418.  
  4419.         $link = "build?id=".$this->buildingIndex."&".$link;
  4420.  
  4421.         return "<a href=\"".$link."\">".$text."</a>";
  4422.  
  4423.     }
  4424.  
  4425.  
  4426.  
  4427.  
  4428.  
  4429. function getResourceGoldExchange ($neededResources, $itemId, $buildingIndex, $multiple = FALSE)
  4430.  
  4431. {
  4432.  
  4433. if ( $itemId != 0 )
  4434.  
  4435. {
  4436.  
  4437. if ((($this->data['gold_num'] < $this->gameMetadata['plusTable'][6]['cost'] OR 0 < $this->needMoreUpgrades ($neededResources, $itemId)) OR ($this->isResourcesAvailable ($neededResources) AND !$multiple)))
  4438.  
  4439. {
  4440.  
  4441. return '';
  4442.  
  4443. }
  4444.  
  4445. }
  4446.  
  4447. $s1 = 0;
  4448.  
  4449. $s2 = 0;
  4450.  
  4451. $exchangeResource = '';
  4452.  
  4453. foreach ($neededResources as $k => $v)
  4454.  
  4455. {
  4456.  
  4457. $s1 += $v;
  4458.  
  4459. $s2 += $this->resources[$k]['current_value'];
  4460.  
  4461. if ($exchangeResource != '')
  4462.  
  4463. {
  4464.  
  4465. $exchangeResource .= '&';
  4466.  
  4467. }
  4468.  
  4469. $exchangeResource .= 'r' . $k . '=' . $v;
  4470.  
  4471. }
  4472.  
  4473. $canExchange = $s1 <= $s2;
  4474.  
  4475. if (($multiple AND $canExchange))
  4476.  
  4477. {
  4478.  
  4479. $num = floor ($s2 / $s1);
  4480.  
  4481. $exchangeResource = '';
  4482.  
  4483. foreach ($neededResources as $k => $v)
  4484.  
  4485. {
  4486.  
  4487. if ($exchangeResource != '')
  4488.  
  4489. {
  4490.  
  4491. $exchangeResource .= '&';
  4492.  
  4493. }
  4494.  
  4495. $exchangeResource .= 'r' . $k . '=' . $v * $num;
  4496.  
  4497. }
  4498.  
  4499. }
  4500.  
  4501. return ' | <a href="build?bid=17&t=3&rid=' . $buildingIndex . '&' . $exchangeResource . '" title="' . buildings_p_m2m . '"><img class="npc' . ($canExchange ? '' : '_inactive') . '" src="'.$GLOBALS['AppConfig']['system']['linksite'].'x.gif" alt="' . buildings_p_m2m . '" title="' . buildings_p_m2m . '"></a>';
  4502.  
  4503. }
  4504.  
  4505. }
  4506.  
  4507. $p = new GPage ();
  4508.  
  4509. $p->run ();
  4510.  
  4511. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement