Advertisement
Guest User

Untitled

a guest
Jun 8th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 54.98 KB | None | 0 0
  1. <?php
  2. class battlemodel {
  3. function getwarresult($taskRow) {
  4. global $GameMetadata;
  5.  
  6. $toVillageRow = $this->_getVillageInfo( $taskRow['to_village_id'] );
  7. $fromVillageRow = $this->_getVillageInfo( $taskRow['village_id'] );
  8. $paramsArray = explode( '|', $taskRow['proc_params'] );
  9. $troopsArrStr = explode( ',', $paramsArray[0] );
  10. $troopsArray = array( 'troops' => array( ), 'onlyHero' => FALSE, 'heroTroopId' => 0, 'hasHero' => FALSE, 'hasKing' => FALSE, 'cropConsumption' => 0 );
  11. $_onlyHero = TRUE;
  12.  
  13. foreach ($troopsArrStr as $_t) {
  14. list( $tid, $tnum ) = explode( ' ', $_t );
  15.  
  16. if ($tnum == 0 - 1) {
  17. $troopsArray['hasHero'] = TRUE;
  18. $troopsArray['heroTroopId'] = $tid;
  19. $tnum = 1;
  20. } else {
  21. $troopsArray['troops'][$tid] = $tnum;
  22.  
  23. if (0 < $tnum) {
  24. $_onlyHero = FALSE;
  25. }
  26.  
  27. if (( ( ( $tid == 9 || $tid == 19 ) || $tid == 29 ) || $tid == 108 )) {
  28. $troopsArray['hasKing'] = TRUE;
  29. }
  30. }
  31.  
  32. $troopsArray['cropConsumption'] += $GameMetadata['troops'][$tid]['crop_consumption'] * $tnum;
  33. }
  34.  
  35.  
  36. if (( $_onlyHero && $troopsArray['hasHero'] )) {
  37. $troopsArray['onlyHero'] = TRUE;
  38. }
  39.  
  40. $procInfo = array( 'troopsArray' => $troopsArray, 'hasHero' => $paramsArray[1] == 1, 'spyAction' => $paramsArray[2], 'catapultTarget' => $paramsArray[3], 'harvestResources' => $paramsArray[4], 'spyInfo' => $paramsArray[5], 'catapultResult' => $paramsArray[6], 'troopBack' => $paramsArray[7] == 1 );
  41.  
  42. if ($taskRow['proc_type'] == QS_CREATEVILLAGE) {
  43. if (( $toVillageRow['is_oasis'] || 0 < intval( $toVillageRow['player_id'] ) )) {
  44. $taskRow['proc_type'] = QS_WAR_ATTACK_PLUNDER;
  45. }
  46. }
  47.  
  48. switch ($taskRow['proc_type']) {
  49. case QS_WAR_REINFORCE: {
  50. return $this->_handleReInforcement( $taskRow, $toVillageRow, $fromVillageRow, $procInfo, $paramsArray[0] );
  51. }
  52.  
  53. case QS_WAR_ATTACK: {
  54. }
  55.  
  56. case QS_WAR_ATTACK_PLUNDER: {
  57. return $this->_handleWarAttack( $taskRow, $toVillageRow, $fromVillageRow, $procInfo );
  58. }
  59.  
  60. case QS_WAR_ATTACK_SPY: {
  61. return $this->_handleWarSpy( $taskRow, $toVillageRow, $fromVillageRow, $procInfo );
  62. }
  63.  
  64. case QS_CREATEVILLAGE: {
  65. $this->_handleCreateNewVillage( $taskRow, $toVillageRow, $fromVillageRow, $troopsArray['cropConsumption'] );
  66. return FALSE;
  67. }
  68. }
  69.  
  70. return TRUE;
  71. }
  72.  
  73. function _handlewarspy($taskRow, $toVillageRow, $fromVillageRow, $procInfo) {
  74. global $GameMetadata;
  75.  
  76. if (( !$toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 )) {
  77. $paramsArray = explode( '|', $taskRow['proc_params'] );
  78. $paramsArray[sizeof( $paramsArray ) - 1] = 1;
  79. $newParams = implode( '|', $paramsArray );
  80. $this->provider->executeQuery( 'UPDATE p_queue q
  81. SET
  82. q.player_id=%s,
  83. q.village_id=%s,
  84. q.to_player_id=%s,
  85. q.to_village_id=%s,
  86. q.proc_type=%s,
  87. q.proc_params=\'%s\',
  88. q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
  89. q.locked=0
  90. WHERE q.id=%s', array( intval( $taskRow['to_player_id'] ), intval( $taskRow['to_village_id'] ), intval( $taskRow['player_id'] ), intval( $taskRow['village_id'] ), QS_WAR_REINFORCE, $newParams, $taskRow['id'] ) );
  91. return TRUE;
  92. }
  93.  
  94. $attackTroops = $this->_getAttackTroopsForVillage( '', $procInfo['troopsArray']['troops'], 0, 0, 0, TRUE );
  95. $defenseTroops = array( );
  96. $totalDefensePower = 0;
  97. $troops_num = trim( $toVillageRow['troops_num'] );
  98.  
  99. if ($troops_num != '') {
  100. $vtroopsArr = explode( '|', $troops_num );
  101.  
  102. foreach ($vtroopsArr as $vtroopsStr) {
  103. list( $tvid, $tvtroopsStr ) = explode( ':', $vtroopsStr );
  104. $incFactor = (( ( $toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 ) && $tvid == 0 - 1 ) ? floor( $toVillageRow['oasisElapsedTimeInSeconds'] / 86400 ) : 0);
  105. $_hasHero = FALSE;
  106. $vtroops = array( );
  107. $_arr = explode( ',', $tvtroopsStr );
  108.  
  109. foreach ($_arr as $_arrStr) {
  110. list( $_tid, $_tnum ) = explode( ' ', $_arrStr );
  111.  
  112. if ($_tnum == 0 - 1) {
  113. $_hasHero = TRUE;
  114. continue;
  115. }
  116.  
  117. $vtroops[$_tid] = $_tnum + $incFactor;
  118. }
  119.  
  120.  
  121. if ($tvid == 0 - 1) {
  122. $hero_in_village_id = intval( $this->provider->fetchScalar( 'SELECT p.hero_in_village_id FROM p_players p WHERE p.id=%s', array( intval( $toVillageRow['player_id'] ) ) ) );
  123.  
  124. if (( 0 < $hero_in_village_id && $hero_in_village_id == $toVillageRow['id'] )) {
  125. $_hasHero = TRUE;
  126. }
  127. }
  128.  
  129. $defenseTroops[$tvid] = $this->_getDefenseTroopsForVillage( ($tvid == 0 - 1 ? $toVillageRow['id'] : $tvid), $vtroops, $_hasHero, 0, 0, TRUE );
  130. $totalDefensePower += $defenseTroops[$tvid]['total_power'];
  131. }
  132. }
  133.  
  134. $warResult = $this->_getSpyResult( $attackTroops, $defenseTroops, $totalDefensePower );
  135. $reduceConsumption = $warResult['attackTroops']['total_dead_consumption'];
  136.  
  137. if (0 < $reduceConsumption) {
  138. $this->_updateVillage( $fromVillageRow, $reduceConsumption, FALSE );
  139. }
  140.  
  141. $defenseTroopsStr = '';
  142. $defenseReduceConsumption = 0;
  143. $reportTroopTable = array( );
  144. $tribeId = 0;
  145.  
  146. foreach ($warResult['defenseTroops'] as $vid => $troopsTable) {
  147. $defenseReduceConsumption += $troopsTable['total_dead_consumption'];
  148. $newTroops = '';
  149. $thisInforcementDied = TRUE;
  150.  
  151. foreach ($troopsTable['troops'] as $tid => $tprop) {
  152. if ($newTroops != '') {
  153. $newTroops .= ',';
  154. }
  155.  
  156. $newTroops .= $tid . ' ' . $tprop['live_number'];
  157.  
  158. if (0 < $tprop['live_number']) {
  159. $thisInforcementDied = FALSE;
  160. }
  161.  
  162. $tribeId = $GameMetadata['troops'][$tid]['for_tribe_id'];
  163.  
  164. if (!isset( $reportTroopTable[$tribeId] )) {
  165. $reportTroopTable[$tribeId] = array( 'troops' => array( ), 'hero' => array( 'number' => 0, 'dead_number' => 0 ) );
  166. }
  167.  
  168. if ($tid != 99) {
  169. if (!isset( $reportTroopTable[$tribeId]['troops'][$tid] )) {
  170. $reportTroopTable[$tribeId]['troops'][$tid] = array( 'number' => $tprop['number'], 'dead_number' => $tprop['number'] - $tprop['live_number'] );
  171. continue;
  172. }
  173.  
  174. $reportTroopTable[$tribeId]['troops'][$tid]['number'] += $tprop['number'];
  175. $reportTroopTable[$tribeId]['troops'][$tid]['dead_number'] += $tprop['number'] - $tprop['live_number'];
  176. continue;
  177. }
  178. }
  179.  
  180.  
  181. if ($troopsTable['hasHero']) {
  182. ++$reportTroopTable[$tribeId]['hero']['number'];
  183.  
  184. if ($newTroops != '') {
  185. $newTroops .= ',';
  186. }
  187.  
  188. $newTroops .= $troopsTable['heroTroopId'] . ' -1';
  189. $thisInforcementDied = FALSE;
  190. }
  191.  
  192. $this->_updateVillageOutTroops( $vid, $toVillageRow['id'], $newTroops, ( $troopsTable['hasHero'] && $troopsTable['total_live_number'] <= 0 ), $thisInforcementDied, intval( $toVillageRow['player_id'] ) );
  193.  
  194. if (( $vid == 0 - 1 && $toVillageRow['is_oasis'] )) {
  195. $this->provider->executeQuery( 'UPDATE p_villages v SET v.creation_date=NOW() WHERE v.id=%s', array( $toVillageRow['id'] ) );
  196. }
  197.  
  198. if (( !$thisInforcementDied || $vid == 0 - 1 )) {
  199. if ($defenseTroopsStr != '') {
  200. $defenseTroopsStr .= '|';
  201. }
  202.  
  203. $defenseTroopsStr .= $vid . ':' . $newTroops;
  204. continue;
  205. }
  206. }
  207.  
  208.  
  209. if (( ( $toVillageRow['is_oasis'] && 0 < intval( $toVillageRow['player_id'] ) ) && isset( $reportTroopTable[4] ) )) {
  210. unset( $reportTroopTable[4] );
  211. }
  212.  
  213. $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_num=\'%s\' WHERE v.id=%s', array( $defenseTroopsStr, $toVillageRow['id'] ) );
  214.  
  215. if (!( $toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 )) {
  216. $_tovid = ($toVillageRow['is_oasis'] ? intval( $toVillageRow['parent_id'] ) : $toVillageRow['id']);
  217. $this->provider->executeQuery( 'UPDATE p_villages v SET v.crop_consumption=v.crop_consumption-%s WHERE v.id=%s', array( $defenseReduceConsumption, $_tovid ) );
  218. }
  219.  
  220. $newTroops = '';
  221.  
  222. foreach ($warResult['attackTroops']['troops'] as $tid => $tprop) {
  223. if ($newTroops != '') {
  224. $newTroops .= ',';
  225. }
  226.  
  227. $newTroops .= $tid . ' ' . $tprop['number'] . ' ' . ( $tprop['number'] - $tprop['live_number'] );
  228. }
  229.  
  230.  
  231. if ($procInfo['troopsArray']['hasHero']) {
  232. if ($newTroops != '') {
  233. $newTroops .= ',';
  234. }
  235.  
  236. $newTroops .= 0 - 1 . ' ' . 1 . ' ' . ($warResult['all_attack_killed'] ? 1 : 0);
  237. }
  238.  
  239. $attackReportTroops = $newTroops;
  240. $defenseReportTroops = '';
  241.  
  242. foreach ($reportTroopTable as $tribeId => $defTroops) {
  243. $defenseReportTroops1 = '';
  244.  
  245. foreach ($defTroops['troops'] as $tid => $tArr) {
  246. if ($defenseReportTroops1 != '') {
  247. $defenseReportTroops1 .= ',';
  248. }
  249.  
  250. $defenseReportTroops1 .= $tid . ' ' . $tArr['number'] . ' ' . $tArr['dead_number'];
  251. }
  252.  
  253.  
  254. if (0 < $defTroops['hero']['number']) {
  255. if ($defenseReportTroops1 != '') {
  256. $defenseReportTroops1 .= ',';
  257. }
  258.  
  259. $defenseReportTroops1 .= 0 - 1 . ' ' . $defTroops['hero']['number'] . ' ' . $defTroops['hero']['dead_number'];
  260. }
  261.  
  262. if ($defenseReportTroops1 != '') {
  263. if ($defenseReportTroops != '') {
  264. $defenseReportTroops .= '#';
  265. }
  266.  
  267. $defenseReportTroops .= $defenseReportTroops1;
  268. continue;
  269. }
  270. }
  271.  
  272. $harvestInfo = '';
  273. $harvestResources = '';
  274. $spyType = $procInfo['spyAction'];
  275.  
  276. if (!$warResult['all_spy_killed']) {
  277. if ($spyType == 1) {
  278. $harvestResources = '0 0 0 0';
  279.  
  280. if (!$toVillageRow['is_oasis']) {
  281. $resources_info = array( );
  282. $r_arr = explode( ',', $toVillageRow['resources'] );
  283.  
  284. foreach ($r_arr as $r_str) {
  285. $r2 = explode( ' ', $r_str );
  286. $prate = floor( $r2[4] * ( 1 + $r2[5] / 100 ) ) - ($r2[0] == 4 ? $toVillageRow['crop_consumption'] : 0);
  287. $current_value = floor( $r2[1] + $toVillageRow['elapsedTimeInSeconds'] * ( $prate / 3600 ) );
  288.  
  289. if ($r2[2] < $current_value) {
  290. $current_value = $r2[2];
  291. }
  292.  
  293. $resources_info[] = $current_value;
  294. }
  295.  
  296. $harvestResources = implode( ' ', $resources_info );
  297. }
  298. }
  299.  
  300. if ($spyType == 2) {
  301. if (!$toVillageRow['is_oasis']) {
  302. $buildingsInfo = array( );
  303. $bStr = trim( $toVillageRow['buildings'] );
  304.  
  305. if ($bStr != '') {
  306. $bStrArr = explode( ',', $bStr );
  307. $_i = 0;
  308.  
  309. foreach ($bStrArr as $b2Str) {
  310. ++$_i;
  311.  
  312. if ($_i < 19) {
  313. continue;
  314. }
  315.  
  316. list( $item_id, $level, $update_state ) = explode( ' ', $b2Str );
  317.  
  318. if (0 < $level) {
  319. $buildingsInfo[] = $item_id . ' ' . $level;
  320. continue;
  321. }
  322. }
  323. }
  324.  
  325. if (0 < sizeof( $buildingsInfo )) {
  326. $_randIndex = mt_rand( 0, sizeof( $buildingsInfo ) - 1 );
  327. $harvestInfo = $buildingsInfo[$_randIndex];
  328. }
  329. }
  330. }
  331. } else {
  332. $spyType = 3;
  333. }
  334.  
  335. $timeInSeconds = $taskRow['remainingTimeInSeconds'];
  336.  
  337. if (!$warResult['defense_has_spytroops']) {
  338. $reportResult = 100;
  339. } else {
  340. $reportResult = ($warResult['all_spy_killed'] ? 9 : 10);
  341. }
  342.  
  343. $reportCategory = 4;
  344. $reportBody = $attackReportTroops . '|' . $defenseReportTroops . '|' . $harvestResources . '|' . $harvestInfo . '|' . $spyType;
  345. $r = new ReportModel( );
  346. $reportId = $r->createReport( intval( $fromVillageRow['player_id'] ), intval( $toVillageRow['player_id'] ), intval( $fromVillageRow['id'] ), intval( $toVillageRow['id'] ), $reportCategory, $reportResult, $reportBody, $timeInSeconds );
  347.  
  348. if (( !$warResult['defense_has_spytroops'] && $toVillageRow['player_id'] != $fromVillageRow['player_id'] )) {
  349. $r->deleteReport( intval( $taskRow['to_player_id'] ), $reportId );
  350. }
  351.  
  352. if (!$warResult['all_attack_killed']) {
  353. $paramsArray = explode( '|', $taskRow['proc_params'] );
  354. $paramsArray[sizeof( $paramsArray ) - 1] = 1;
  355. $newTroops = '';
  356.  
  357. foreach ($warResult['attackTroops']['troops'] as $tid => $tprop) {
  358. if ($newTroops != '') {
  359. $newTroops .= ',';
  360. }
  361.  
  362. $newTroops .= $tid . ' ' . $tprop['live_number'];
  363. }
  364.  
  365.  
  366. if (( !$warResult['all_attack_killed'] && $procInfo['troopsArray']['hasHero'] )) {
  367. if ($newTroops != '') {
  368. $newTroops .= ',';
  369. }
  370.  
  371. $newTroops .= $procInfo['troopsArray']['heroTroopId'] . ' -1';
  372. }
  373.  
  374. $paramsArray[0] = $newTroops;
  375. $newParams = implode( '|', $paramsArray );
  376. $this->provider->executeQuery( 'UPDATE p_queue q
  377. SET
  378. q.player_id=%s,
  379. q.village_id=%s,
  380. q.to_player_id=%s,
  381. q.to_village_id=%s,
  382. q.proc_type=%s,
  383. q.proc_params=\'%s\',
  384. q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
  385. q.locked=0
  386. WHERE q.id=%s', array( intval( $taskRow['to_player_id'] ), intval( $taskRow['to_village_id'] ), intval( $taskRow['player_id'] ), intval( $taskRow['village_id'] ), QS_WAR_REINFORCE, $newParams, $taskRow['id'] ) );
  387. return TRUE;
  388. }
  389.  
  390. return FALSE;
  391. }
  392.  
  393. function _handlewarattack($taskRow, $toVillageRow, $fromVillageRow, $procInfo) {
  394. global $GameMetadata;
  395.  
  396. if (( !$toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 )) {
  397. $paramsArray = explode( '|', $taskRow['proc_params'] );
  398. $paramsArray[sizeof( $paramsArray ) - 1] = 1;
  399. $newParams = implode( '|', $paramsArray );
  400. $this->provider->executeQuery( 'UPDATE p_queue q
  401. SET
  402. q.player_id=%s,
  403. q.village_id=%s,
  404. q.to_player_id=%s,
  405. q.to_village_id=%s,
  406. q.proc_type=%s,
  407. q.proc_params=\'%s\',
  408. q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
  409. q.locked=0
  410. WHERE q.id=%s', array( intval( $taskRow['to_player_id'] ), intval( $taskRow['to_village_id'] ), intval( $taskRow['player_id'] ), intval( $taskRow['village_id'] ), QS_WAR_REINFORCE, $newParams, $taskRow['id'] ) );
  411. return TRUE;
  412. }
  413.  
  414. $heroLevel = 0;
  415.  
  416. if ($procInfo['troopsArray']['hasHero']) {
  417. $heroLevel = intval( $this->provider->fetchScalar( 'SELECT p.hero_level FROM p_players p WHERE p.id=%s', array( intval( $fromVillageRow['player_id'] ) ) ) );
  418. }
  419.  
  420. $wringerLevel = 0;
  421. $buildings = array( );
  422. $bStr = trim( $fromVillageRow['buildings'] );
  423.  
  424. if ($bStr != '') {
  425. $bStrArr = explode( ',', $bStr );
  426.  
  427. foreach ($bStrArr as $b2Str) {
  428. list( $item_id, $level, $update_state ) = explode( ' ', $b2Str );
  429.  
  430. if ($item_id == 35) {
  431. $wringerLevel = $level;
  432. break;
  433. }
  434. }
  435. }
  436.  
  437. $attackTroops = $this->_getAttackTroopsForVillage( $fromVillageRow['troops_training'], $procInfo['troopsArray']['troops'], $heroLevel, $fromVillageRow['people_count'], $wringerLevel, FALSE );
  438. $crannyTotalSize = 0;
  439. $wallPower = 0;
  440. $buildings = array( );
  441. $bStr = trim( $toVillageRow['buildings'] );
  442.  
  443. if ($bStr != '') {
  444. $bStrArr = explode( ',', $bStr );
  445.  
  446. foreach ($bStrArr as $b2Str) {
  447. list( $item_id, $level, $update_state ) = explode( ' ', $b2Str );
  448.  
  449. if (( ( $item_id == 31 || $item_id == 32 ) || $item_id == 33 )) {
  450. $wallPower = (0 < $level ? $GameMetadata['items'][$item_id]['levels'][$level - 1]['value'] : 0);
  451. continue;
  452. }
  453.  
  454. if (( $item_id == 23 && 0 < $level )) {
  455. $crannyTotalSize += $GameMetadata['items'][$item_id]['levels'][$level - 1]['value'];
  456. continue;
  457. }
  458. }
  459. }
  460.  
  461. $defenseTroops = array( );
  462. $totalDefensePower = 0;
  463. $troops_num = trim( $toVillageRow['troops_num'] );
  464.  
  465. if ($troops_num != '') {
  466. $vtroopsArr = explode( '|', $troops_num );
  467.  
  468. foreach ($vtroopsArr as $vtroopsStr) {
  469. list( $tvid, $tvtroopsStr ) = explode( ':', $vtroopsStr );
  470. $incFactor = (( ( $toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 ) && $tvid == 0 - 1 ) ? floor( $toVillageRow['oasisElapsedTimeInSeconds'] / 86400 ) : 0);
  471. $_hasHero = FALSE;
  472. $vtroops = array( );
  473. $_arr = explode( ',', $tvtroopsStr );
  474.  
  475. foreach ($_arr as $_arrStr) {
  476. list( $_tid, $_tnum ) = explode( ' ', $_arrStr );
  477.  
  478. if ($_tnum == 0 - 1) {
  479. $_hasHero = TRUE;
  480. continue;
  481. }
  482.  
  483. $vtroops[$_tid] = $_tnum + $incFactor;
  484. }
  485.  
  486.  
  487. if ($tvid == 0 - 1) {
  488. $hero_in_village_id = intval( $this->provider->fetchScalar( 'SELECT p.hero_in_village_id FROM p_players p WHERE p.id=%s', array( intval( $toVillageRow['player_id'] ) ) ) );
  489.  
  490. if (( 0 < $hero_in_village_id && $hero_in_village_id == $toVillageRow['id'] )) {
  491. $_hasHero = TRUE;
  492. }
  493. }
  494.  
  495. $defenseTroops[$tvid] = $this->_getDefenseTroopsForVillage( ($tvid == 0 - 1 ? $toVillageRow['id'] : $tvid), $vtroops, $_hasHero, $toVillageRow['people_count'], $wallPower, FALSE );
  496. $totalDefensePower += $defenseTroops[$tvid]['total_power'];
  497. }
  498. }
  499.  
  500. $warResult = $this->_getWarResult( $attackTroops, $defenseTroops, $totalDefensePower, $taskRow['proc_type'] == QS_WAR_ATTACK_PLUNDER );
  501. $harvestResources = '0 0 0 0';
  502. $harvestInfoStruct = array( 'string' => $harvestResources, 'sum' => 0 );
  503.  
  504. if (!$warResult['all_attack_killed']) {
  505. $harvestInfoStruct = $this->_harvestTroopsFrom( $toVillageRow, $warResult['attackTroops']['total_carry_load'], $crannyTotalSize );
  506. $harvestResources = $harvestInfoStruct['string'];
  507. }
  508.  
  509. $reduceConsumption = $warResult['attackTroops']['total_dead_consumption'];
  510.  
  511. if (( $warResult['all_attack_killed'] && $procInfo['troopsArray']['hasHero'] )) {
  512. $reduceConsumption += $GameMetadata['troops'][$procInfo['troopsArray']['heroTroopId']]['crop_consumption'];
  513. }
  514.  
  515. if (0 < $reduceConsumption) {
  516. $this->_updateVillage( $fromVillageRow, $reduceConsumption, ( $warResult['all_attack_killed'] && $procInfo['troopsArray']['hasHero'] ) );
  517. }
  518.  
  519. if (( $procInfo['troopsArray']['hasHero'] && !$warResult['all_attack_killed'] )) {
  520. $heroStatisticPoint = 1;
  521. $this->provider->executeQuery( 'UPDATE p_players p SET p.hero_points=p.hero_points+%s, p.hero_level=floor(p.hero_points/30) WHERE p.id=%s', array( $heroStatisticPoint, intval( $fromVillageRow['player_id'] ) ) );
  522. }
  523.  
  524. $defenseTroopsStr = '';
  525. $defenseReduceConsumption = 0;
  526. $reportTroopTable = array( );
  527. $tribeId = 0;
  528.  
  529. foreach ($warResult['defenseTroops'] as $vid => $troopsTable) {
  530. $defenseReduceConsumption += $troopsTable['total_dead_consumption'];
  531. $newTroops = '';
  532. $thisInforcementDied = TRUE;
  533.  
  534. foreach ($troopsTable['troops'] as $tid => $tprop) {
  535. if ($newTroops != '') {
  536. $newTroops .= ',';
  537. }
  538.  
  539. $newTroops .= $tid . ' ' . $tprop['live_number'];
  540.  
  541. if (0 < $tprop['live_number']) {
  542. $thisInforcementDied = FALSE;
  543. }
  544.  
  545. $tribeId = $GameMetadata['troops'][$tid]['for_tribe_id'];
  546.  
  547. if (!isset( $reportTroopTable[$tribeId] )) {
  548. $reportTroopTable[$tribeId] = array( 'troops' => array( ), 'hero' => array( 'number' => 0, 'dead_number' => 0 ) );
  549. }
  550.  
  551. if ($tid != 99) {
  552. if (!isset( $reportTroopTable[$tribeId]['troops'][$tid] )) {
  553. $reportTroopTable[$tribeId]['troops'][$tid] = array( 'number' => $tprop['number'], 'dead_number' => $tprop['number'] - $tprop['live_number'] );
  554. continue;
  555. }
  556.  
  557. $reportTroopTable[$tribeId]['troops'][$tid]['number'] += $tprop['number'];
  558. $reportTroopTable[$tribeId]['troops'][$tid]['dead_number'] += $tprop['number'] - $tprop['live_number'];
  559. continue;
  560. }
  561. }
  562.  
  563.  
  564. if ($troopsTable['hasHero']) {
  565. ++$reportTroopTable[$tribeId]['hero']['number'];
  566. }
  567.  
  568. if (( 0 < $troopsTable['total_live_number'] && $troopsTable['hasHero'] )) {
  569. if ($newTroops != '') {
  570. $newTroops .= ',';
  571. }
  572.  
  573. $newTroops .= $troopsTable['heroTroopId'] . ' -1';
  574.  
  575. if (( $vid == 0 - 1 && !$toVillageRow['is_oasis'] )) {
  576. $heroStatisticPoint = 1;
  577. $this->provider->executeQuery( 'UPDATE p_players p SET p.hero_points=p.hero_points+%s, p.hero_level=floor(p.hero_points/30) WHERE p.id=%s', array( $heroStatisticPoint, intval( $toVillageRow['player_id'] ) ) );
  578. }
  579.  
  580. $thisInforcementDied = FALSE;
  581. }
  582.  
  583. if (( $troopsTable['hasHero'] && $troopsTable['total_live_number'] <= 0 )) {
  584. ++$reportTroopTable[$tribeId]['hero']['dead_number'];
  585. $defenseReduceConsumption += $GameMetadata['troops'][$troopsTable['heroTroopId']]['crop_consumption'];
  586. }
  587.  
  588. $this->_updateVillageOutTroops( $vid, $toVillageRow['id'], $newTroops, ( $troopsTable['hasHero'] && $troopsTable['total_live_number'] <= 0 ), $thisInforcementDied, intval( $toVillageRow['player_id'] ) );
  589.  
  590. if (( $vid == 0 - 1 && $toVillageRow['is_oasis'] )) {
  591. $this->provider->executeQuery( 'UPDATE p_villages v SET v.creation_date=NOW() WHERE v.id=%s', array( $toVillageRow['id'] ) );
  592. }
  593.  
  594. if (( !$thisInforcementDied || $vid == 0 - 1 )) {
  595. if ($defenseTroopsStr != '') {
  596. $defenseTroopsStr .= '|';
  597. }
  598.  
  599. $defenseTroopsStr .= $vid . ':' . $newTroops;
  600. continue;
  601. }
  602. }
  603.  
  604.  
  605. if (( ( $toVillageRow['is_oasis'] && 0 < intval( $toVillageRow['player_id'] ) ) && isset( $reportTroopTable[4] ) )) {
  606. unset( $reportTroopTable[4] );
  607. }
  608.  
  609. $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_num=\'%s\' WHERE v.id=%s', array( $defenseTroopsStr, $toVillageRow['id'] ) );
  610.  
  611. if (!( $toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 )) {
  612. $_tovid = ($toVillageRow['is_oasis'] ? intval( $toVillageRow['parent_id'] ) : $toVillageRow['id']);
  613. $this->provider->executeQuery( 'UPDATE p_villages v SET v.crop_consumption=v.crop_consumption-%s WHERE v.id=%s', array( $defenseReduceConsumption, $_tovid ) );
  614. }
  615.  
  616. $newTroops = '';
  617.  
  618. foreach ($warResult['attackTroops']['troops'] as $tid => $tprop) {
  619. if ($newTroops != '') {
  620. $newTroops .= ',';
  621. }
  622.  
  623. $newTroops .= $tid . ' ' . $tprop['number'] . ' ' . ( $tprop['number'] - $tprop['live_number'] );
  624. }
  625.  
  626.  
  627. if ($procInfo['troopsArray']['hasHero']) {
  628. if ($newTroops != '') {
  629. $newTroops .= ',';
  630. }
  631.  
  632. $newTroops .= 0 - 1 . ' ' . 1 . ' ' . ($warResult['all_attack_killed'] ? 1 : 0);
  633. }
  634.  
  635. $attackReportTroops = $newTroops;
  636. $defenseReportTroops = '';
  637.  
  638. foreach ($reportTroopTable as $tribeId => $defTroops) {
  639. $defenseReportTroops1 = '';
  640.  
  641. foreach ($defTroops['troops'] as $tid => $tArr) {
  642. if ($defenseReportTroops1 != '') {
  643. $defenseReportTroops1 .= ',';
  644. }
  645.  
  646. $defenseReportTroops1 .= $tid . ' ' . $tArr['number'] . ' ' . $tArr['dead_number'];
  647. }
  648.  
  649.  
  650. if (0 < $defTroops['hero']['number']) {
  651. if ($defenseReportTroops1 != '') {
  652. $defenseReportTroops1 .= ',';
  653. }
  654.  
  655. $defenseReportTroops1 .= 0 - 1 . ' ' . $defTroops['hero']['number'] . ' ' . $defTroops['hero']['dead_number'];
  656. }
  657.  
  658. if ($defenseReportTroops1 != '') {
  659. if ($defenseReportTroops != '') {
  660. $defenseReportTroops .= '#';
  661. }
  662.  
  663. $defenseReportTroops .= $defenseReportTroops1;
  664. continue;
  665. }
  666. }
  667.  
  668. $timeInSeconds = $taskRow['remainingTimeInSeconds'];
  669. $reportResult = 1;
  670. $reportCategory = 3;
  671. $reportBody = $attackReportTroops . '|' . $defenseReportTroops . '|' . $warResult['attackTroops']['total_carry_load'] . '|' . $harvestResources;
  672. $r = new ReportModel( );
  673. $r->createReport( intval( $fromVillageRow['player_id'] ), intval( $toVillageRow['player_id'] ), intval( $fromVillageRow['id'] ), intval( $toVillageRow['id'] ), $reportCategory, $reportResult, $reportBody, $timeInSeconds );
  674.  
  675. if (intval( $toVillageRow['player_id'] ) != intval( $fromVillageRow['player_id'] )) {
  676. $statisticPoint = 0;
  677. $harvestPoint = $harvestInfoStruct['sum'];
  678.  
  679. if (0 < intval( $toVillageRow['player_id'] )) {
  680. $statisticPoint = ($warResult['all_attack_killed'] ? $warResult['attackTroops']['total_dead_consumption'] : 0);
  681. $this->provider->executeQuery( 'UPDATE p_players p SET p.defense_points=p.defense_points+%s, p.week_defense_points=p.week_defense_points+%s WHERE p.id=%s', array( $statisticPoint, $statisticPoint, intval( $toVillageRow['player_id'] ) ) );
  682. }
  683.  
  684. if (0 < intval( $fromVillageRow['player_id'] )) {
  685. $statisticPoint = ($warResult['all_attack_killed'] ? 0 : $warResult['attackTroops']['total_power']);
  686. $this->provider->executeQuery( 'UPDATE p_players p SET p.attack_points=p.attack_points+%s, p.week_attack_points=p.week_attack_points+%s, p.week_thief_points=p.week_thief_points+%s WHERE p.id=%s', array( $statisticPoint, $statisticPoint, $harvestPoint, intval( $fromVillageRow['player_id'] ) ) );
  687. }
  688.  
  689. if (0 < intval( $toVillageRow['alliance_id'] )) {
  690. $statisticPoint = ($warResult['all_attack_killed'] ? $warResult['attackTroops']['total_dead_consumption'] : 0);
  691. $this->provider->executeQuery( 'UPDATE p_alliances p SET p.rating=p.rating+%s, p.defense_points=p.defense_points+%s, p.week_defense_points=p.week_defense_points+%s WHERE p.id=%s', array( $statisticPoint, $statisticPoint, $statisticPoint, intval( $toVillageRow['alliance_id'] ) ) );
  692. }
  693.  
  694. if (0 < intval( $fromVillageRow['alliance_id'] )) {
  695. $statisticPoint = ($warResult['all_attack_killed'] ? 0 : $warResult['attackTroops']['total_power']);
  696. $this->provider->executeQuery( 'UPDATE p_alliances p SET p.rating=p.rating+%s, p.attack_points=p.attack_points+%s, p.week_attack_points=p.week_attack_points+%s, p.week_thief_points=p.week_thief_points+%s WHERE p.id=%s', array( $statisticPoint, $statisticPoint, $statisticPoint, $harvestPoint, intval( $fromVillageRow['alliance_id'] ) ) );
  697. }
  698. }
  699.  
  700. if (!$warResult['all_attack_killed']) {
  701. $paramsArray = explode( '|', $taskRow['proc_params'] );
  702. $paramsArray[sizeof( $paramsArray ) - 1] = 1;
  703. $newTroops = '';
  704.  
  705. foreach ($warResult['attackTroops']['troops'] as $tid => $tprop) {
  706. if ($newTroops != '') {
  707. $newTroops .= ',';
  708. }
  709.  
  710. $newTroops .= $tid . ' ' . $tprop['live_number'];
  711. }
  712.  
  713.  
  714. if (( !$warResult['all_attack_killed'] && $procInfo['troopsArray']['hasHero'] )) {
  715. if ($newTroops != '') {
  716. $newTroops .= ',';
  717. }
  718.  
  719. $newTroops .= $procInfo['troopsArray']['heroTroopId'] . ' -1';
  720. }
  721.  
  722. $paramsArray[4] = $harvestResources;
  723. $paramsArray[0] = $newTroops;
  724. $newParams = implode( '|', $paramsArray );
  725. $this->provider->executeQuery( 'UPDATE p_queue q
  726. SET
  727. q.player_id=%s,
  728. q.village_id=%s,
  729. q.to_player_id=%s,
  730. q.to_village_id=%s,
  731. q.proc_type=%s,
  732. q.proc_params=\'%s\',
  733. q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
  734. q.locked=0
  735. WHERE q.id=%s', array( intval( $taskRow['to_player_id'] ), intval( $taskRow['to_village_id'] ), intval( $taskRow['player_id'] ), intval( $taskRow['village_id'] ), QS_WAR_REINFORCE, $newParams, $taskRow['id'] ) );
  736. return TRUE;
  737. }
  738.  
  739. return FALSE;
  740. }
  741.  
  742. function _handlecreatenewvillage($taskRow, $toVillageRow, $fromVillageRow, $cropConsumption) {
  743. global $GameMetadata;
  744. global $SetupMetadata;
  745.  
  746. $row = $this->provider->fetchRow( 'SELECT v.player_id,v.field_maps_id FROM p_villages v WHERE v.id=%s', array( $toVillageRow['id'] ) );
  747.  
  748. if (0 < intval( $row['player_id'] )) {
  749. return FALSE;
  750. }
  751.  
  752. if (intval( $this->provider->fetchScalar( 'SELECT p.id FROM p_players p WHERE p.id=%s', array( intval( $fromVillageRow['player_id'] ) ) ) ) == 0) {
  753. return FALSE;
  754. }
  755.  
  756. $villageName = 'قرية جديدة';
  757. $update_key = substr( md5( $fromVillageRow['player_id'] . $fromVillageRow['tribe_id'] . $toVillageRow['id'] . $fromVillageRow['player_name'] . $villageName ), 2, 5 );
  758. $field_map_id = $row['field_maps_id'];
  759. $buildings = '';
  760.  
  761. foreach ($SetupMetadata['field_maps'][$field_map_id] as $v) {
  762. if ($buildings != '') {
  763. $buildings .= ',';
  764. }
  765.  
  766. $buildings .= sprintf( '%s 0 0', $v );
  767. }
  768.  
  769. $k = 19;
  770.  
  771. while ($k <= 40) {
  772. $buildings .= ($k == 26 ? ',15 1 0' : ',0 0 0');
  773. ++$k;
  774. }
  775.  
  776. $resources = '';
  777. $farr = explode( '-', $SetupMetadata['field_maps_summary'][$field_map_id] );
  778. $i = 1;
  779. $_c = sizeof( $farr );
  780.  
  781. while ($i <= $_c) {
  782. if ($resources != '') {
  783. $resources .= ',';
  784. }
  785.  
  786. $resources .= sprintf( '%s 650 800 800 %s 0', $i, $farr[$i - 1] * 2 );
  787. ++$i;
  788. }
  789.  
  790. $troops_training = '';
  791. $troops_num = '';
  792.  
  793. foreach ($GameMetadata['troops'] as $k => $v) {
  794. if (( $v['for_tribe_id'] == 0 - 1 || $v['for_tribe_id'] == $fromVillageRow['tribe_id'] )) {
  795. if ($troops_training != '') {
  796. $troops_training .= ',';
  797. }
  798.  
  799. $researching_done = ($v['research_time_consume'] == 0 ? 1 : 0);
  800. $troops_training .= $k . ' ' . $researching_done . ' 0 0';
  801.  
  802. if ($troops_num != '') {
  803. $troops_num .= ',';
  804. }
  805.  
  806. $troops_num .= $k . ' 0';
  807. continue;
  808. }
  809. }
  810.  
  811. $troops_num = '-1:' . $troops_num;
  812. $this->provider->executeQuery( 'UPDATE p_villages v
  813. SET
  814. v.parent_id=%s,
  815. v.tribe_id=%s,
  816. v.player_id=%s,
  817. v.alliance_id=%s,
  818. v.player_name=\'%s\',
  819. v.village_name=\'%s\',
  820. v.alliance_name=\'%s\',
  821. v.is_capital=0,
  822. v.buildings=\'%s\',
  823. v.resources=\'%s\',
  824. v.cp=\'0 2\',
  825. v.troops_training=\'%s\',
  826. v.troops_num=\'%s\',
  827. v.update_key=\'%s\',
  828. v.creation_date=NOW(),
  829. v.last_update_date=NOW()
  830. WHERE v.id=%s', array( $fromVillageRow['id'], $fromVillageRow['tribe_id'], $fromVillageRow['player_id'], (0 < intval( $fromVillageRow['alliance_id'] ) ? intval( $fromVillageRow['alliance_id'] ) : 'NULL'), $fromVillageRow['player_name'], $villageName, $fromVillageRow['alliance_name'], $buildings, $resources, $troops_training, $troops_num, $update_key, $toVillageRow['id'] ) );
  831. $child_villages_id = trim( $fromVillageRow['child_villages_id'] );
  832.  
  833. if ($child_villages_id != '') {
  834. $child_villages_id .= ',';
  835. }
  836.  
  837. $child_villages_id .= $toVillageRow['id'];
  838. $this->provider->executeQuery( 'UPDATE p_villages v
  839. SET
  840. v.crop_consumption=v.crop_consumption-%s,
  841. v.child_villages_id=\'%s\'
  842. WHERE v.id=%s', array( $cropConsumption, $child_villages_id, $fromVillageRow['id'] ) );
  843. $prow = $this->provider->fetchRow( 'SELECT p.villages_id, p.villages_data FROM p_players p WHERE p.id=%s', array( $fromVillageRow['player_id'] ) );
  844. $villages_id = trim( $prow['villages_id'] );
  845.  
  846. if ($villages_id != '') {
  847. $villages_id .= ',';
  848. }
  849.  
  850. $villages_id .= $toVillageRow['id'];
  851. $villages_data = trim( $prow['villages_data'] );
  852.  
  853. if ($villages_data != '') {
  854. $villages_data .= '
  855. ';
  856. }
  857.  
  858. $villages_data .= $toVillageRow['id'] . ' ' . $toVillageRow['rel_x'] . ' ' . $toVillageRow['rel_y'] . ' ' . $villageName;
  859. $this->provider->executeQuery( 'UPDATE p_players p
  860. SET
  861. p.total_people_count=p.total_people_count+2,
  862. p.villages_count=p.villages_count+1,
  863. p.selected_village_id=%s,
  864. p.villages_id=\'%s\',
  865. p.villages_data=\'%s\'
  866. WHERE
  867. p.id=%s', array( $toVillageRow['id'], $villages_id, $villages_data, $fromVillageRow['player_id'] ) );
  868. }
  869.  
  870. function _handlereinforcement($taskRow, $toVillageRow, $fromVillageRow, $procInfo, $troopsArrStr) {
  871. if ($procInfo['troopBack']) {
  872. if (( 0 < intval( $toVillageRow['player_id'] ) && $taskRow['to_player_id'] == intval( $toVillageRow['player_id'] ) )) {
  873. $paramsArray = explode( '|', $taskRow['proc_params'] );
  874. $res = array( 0, 0, 0, 0 );
  875.  
  876. if (trim( $paramsArray[4] ) != '') {
  877. $res = explode( ' ', $paramsArray[4] );
  878. }
  879.  
  880. $k = 0;
  881. $r_arr = explode( ',', $toVillageRow['resources'] );
  882.  
  883. foreach ($r_arr as $r_str) {
  884. $r2 = explode( ' ', $r_str );
  885. $resources[$r2[0]] = array( 'current_value' => ($r2[2] < $r2[1] + $res[$k] ? $r2[2] : $r2[1] + $res[$k]), 'store_max_limit' => $r2[2], 'store_init_limit' => $r2[3], 'prod_rate' => $r2[4], 'prod_rate_percentage' => $r2[5] );
  886. ++$k;
  887. }
  888.  
  889. $resourcesStr = '';
  890.  
  891. foreach ($resources as $k => $v) {
  892. if ($resourcesStr != '') {
  893. $resourcesStr .= ',';
  894. }
  895.  
  896. $resourcesStr .= sprintf( '%s %s %s %s %s %s', $k, $v['current_value'], $v['store_max_limit'], $v['store_init_limit'], $v['prod_rate'], $v['prod_rate_percentage'] );
  897. }
  898.  
  899. $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_num=\'%s\', v.resources=\'%s\' WHERE v.id=%s', array( $this->_getNewTroops( $toVillageRow['troops_num'], $procInfo['troopsArray'], 0 - 1, ( $toVillageRow['player_id'] == $fromVillageRow['player_id'] && !$toVillageRow['is_oasis'] ) ), $resourcesStr, $toVillageRow['id'] ) );
  900.  
  901. if ($procInfo['troopsArray']['hasHero']) {
  902. $this->provider->executeQuery( 'UPDATE p_players p SET p.hero_in_village_id=%s WHERE p.id=%s', array( $toVillageRow['id'], intval( $toVillageRow['player_id'] ) ) );
  903. }
  904. } else {
  905. if ($procInfo['troopsArray']['hasHero']) {
  906. $this->provider->executeQuery( 'UPDATE p_players p SET p.hero_in_village_id=NULL, hero_troop_id=NULL WHERE p.id=%s', array( intval( $toVillageRow['player_id'] ) ) );
  907. }
  908. }
  909.  
  910. return FALSE;
  911. }
  912.  
  913. if (0 < intval( $toVillageRow['player_id'] )) {
  914. $affectCropConsumption = TRUE;
  915.  
  916. if (( $toVillageRow['is_oasis'] && trim( $fromVillageRow['village_oases_id'] ) != '' )) {
  917. $oArr = explode( ',', trim( $fromVillageRow['village_oases_id'] ) );
  918.  
  919. foreach ($oArr as $oid) {
  920. if ($oid == $taskRow['to_village_id']) {
  921. $affectCropConsumption = FALSE;
  922. break;
  923. }
  924. }
  925. }
  926.  
  927. $this->_addTroopsToVillage( $toVillageRow, $fromVillageRow, $procInfo['troopsArray'], $affectCropConsumption );
  928.  
  929. if (( ( $procInfo['troopsArray']['hasHero'] && $toVillageRow['player_id'] == $fromVillageRow['player_id'] ) && !$toVillageRow['is_oasis'] )) {
  930. $this->provider->executeQuery( 'UPDATE p_players p SET p.hero_in_village_id=%s WHERE p.id=%s', array( $toVillageRow['id'], intval( $toVillageRow['player_id'] ) ) );
  931. }
  932.  
  933. $timeInSeconds = $taskRow['remainingTimeInSeconds'];
  934. $reportResult = 8;
  935. $reportCategory = 2;
  936. $troopsCropConsumption = $procInfo['troopsArray']['cropConsumption'];
  937. $reportBody = $troopsArrStr . '|' . $troopsCropConsumption;
  938. $r = new ReportModel( );
  939. $r->createReport( intval( $taskRow['player_id'] ), intval( $taskRow['to_player_id'] ), intval( $taskRow['village_id'] ), intval( $taskRow['to_village_id'] ), $reportCategory, $reportResult, $reportBody, $timeInSeconds );
  940. return FALSE;
  941. }
  942.  
  943. $paramsArray = explode( '|', $taskRow['proc_params'] );
  944. $paramsArray[sizeof( $paramsArray ) - 1] = 1;
  945. $newParams = implode( '|', $paramsArray );
  946. $this->provider->executeQuery( 'UPDATE p_queue q
  947. SET
  948. q.player_id=%s,
  949. q.village_id=%s,
  950. q.to_player_id=%s,
  951. q.to_village_id=%s,
  952. q.proc_type=%s,
  953. q.proc_params=\'%s\',
  954. q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
  955. q.locked=0
  956. WHERE q.id=%s', array( intval( $taskRow['to_player_id'] ), intval( $taskRow['to_village_id'] ), intval( $taskRow['player_id'] ), intval( $taskRow['village_id'] ), QS_WAR_REINFORCE, $newParams, $taskRow['id'] ) );
  957. return TRUE;
  958. }
  959.  
  960. function _getspyresult($attackTroops, $defenseTroops, $totalDefensePower) {
  961. $warResult = array( 'all_attack_killed' => FALSE, 'all_spy_killed' => FALSE, 'defense_has_spytroops' => FALSE );
  962. $attackFactor = 1;
  963. $totalAttackPower = $attackTroops['total_power'] * $attackFactor;
  964. $divisionFactor = 10;
  965. $i = 1;
  966.  
  967. while ($i <= $divisionFactor) {
  968. $curPower = floor( $totalDefensePower * $i / $divisionFactor );
  969.  
  970. if (0 < $curPower) {
  971. foreach ($attackTroops['troops'] as $tid => $tProp) {
  972. if ($warResult['all_attack_killed']) {
  973. break;
  974. }
  975.  
  976. if (( $tid == 99 || ( ( ( $tid != 103 && $tid != 4 ) && $tid != 14 ) && $tid != 23 ) )) {
  977. continue;
  978. }
  979.  
  980. $sPower = ($tProp['single_power'] <= 0 ? 1 : $tProp['single_power']);
  981. $deadNum = floor( $curPower / $sPower );
  982.  
  983. if ($tProp['live_number'] < $deadNum) {
  984. $deadNum = $tProp['live_number'];
  985. }
  986.  
  987. $curPower -= $deadNum * $tProp['single_power'];
  988. $totalDefensePower -= $deadNum * $tProp['single_power'];
  989. $attackTroops['total_power'] -= $deadNum * $tProp['single_power'];
  990. $attackTroops['total_carry_load'] -= $deadNum * $tProp['single_carry_load'];
  991. $attackTroops['total_dead_consumption'] += $deadNum * $tProp['single_consumption'];
  992. $attackTroops['total_live_number'] -= $deadNum;
  993. $attackTroops['total_spy_live_number'] -= $deadNum;
  994.  
  995. if ($attackTroops['total_live_number'] <= 0) {
  996. $warResult['all_attack_killed'] = TRUE;
  997. }
  998.  
  999. if ($attackTroops['total_spy_live_number'] <= 0) {
  1000. $warResult['all_spy_killed'] = TRUE;
  1001. }
  1002.  
  1003. $attackTroops['troops'][$tid]['live_number'] -= $deadNum;
  1004.  
  1005. if ($curPower <= 0) {
  1006. break;
  1007. }
  1008. }
  1009. }
  1010.  
  1011.  
  1012. foreach ($defenseTroops as $vid => $troopsTable) {
  1013. if ($defenseTroops[$vid]['total_power'] <= 0) {
  1014. continue;
  1015. }
  1016.  
  1017. $curPower = floor( $totalAttackPower * $i / $divisionFactor );
  1018.  
  1019. if (0 < $curPower) {
  1020. foreach ($troopsTable['troops'] as $tid => $tProp) {
  1021. if (( $tid == 99 || ( ( ( $tid != 103 && $tid != 4 ) && $tid != 14 ) && $tid != 23 ) )) {
  1022. continue;
  1023. }
  1024.  
  1025. if (0 < $tProp['live_number']) {
  1026. $warResult['defense_has_spytroops'] = TRUE;
  1027. }
  1028.  
  1029. $sPower = ($tProp['single_power'] <= 0 ? 1 : $tProp['single_power']);
  1030. $deadNum = floor( $curPower / $sPower );
  1031.  
  1032. if ($tProp['live_number'] < $deadNum) {
  1033. $deadNum = $tProp['live_number'];
  1034. }
  1035.  
  1036. $curPower -= $deadNum * $tProp['single_power'];
  1037. $totalAttackPower -= $deadNum * $tProp['single_power'];
  1038. $defenseTroops[$vid]['total_power'] -= $deadNum * $tProp['single_power'];
  1039. $defenseTroops[$vid]['total_dead_consumption'] += $deadNum * $tProp['single_consumption'];
  1040. $defenseTroops[$vid]['total_live_number'] -= $deadNum;
  1041. $defenseTroops[$vid]['total_spy_live_number'] -= $deadNum;
  1042. $defenseTroops[$vid]['troops'][$tid]['live_number'] -= $deadNum;
  1043.  
  1044. if ($curPower <= 0) {
  1045. break;
  1046. }
  1047. }
  1048.  
  1049. continue;
  1050. }
  1051. }
  1052.  
  1053. ++$i;
  1054. }
  1055.  
  1056. $warResult['attackTroops'] = $attackTroops;
  1057. $warResult['defenseTroops'] = $defenseTroops;
  1058. return $warResult;
  1059. }
  1060.  
  1061. function _getwarresult($attackTroops, $defenseTroops, $totalDefensePower, $isPlunderAttack) {
  1062. $warResult = array( 'all_attack_killed' => FALSE );
  1063. $attackFactor = ($isPlunderAttack ? 0.75 : 1);
  1064. $totalAttackPower = $attackTroops['total_power'] * $attackFactor;
  1065. $divisionFactor = 10;
  1066. $i = 1;
  1067.  
  1068. while ($i <= $divisionFactor) {
  1069. $curPower = floor( $totalDefensePower * $i / $divisionFactor );
  1070.  
  1071. if (0 < $curPower) {
  1072. foreach ($attackTroops['troops'] as $tid => $tProp) {
  1073. if ($warResult['all_attack_killed']) {
  1074. break;
  1075. }
  1076.  
  1077. if ($tid == 99) {
  1078. continue;
  1079. }
  1080.  
  1081. $sPower = ($tProp['single_power'] <= 0 ? 1 : $tProp['single_power']);
  1082. $deadNum = floor( $curPower / $sPower );
  1083.  
  1084. if ($tProp['live_number'] < $deadNum) {
  1085. $deadNum = $tProp['live_number'];
  1086. }
  1087.  
  1088. $curPower -= $deadNum * $tProp['single_power'];
  1089. $totalDefensePower -= $deadNum * $tProp['single_power'];
  1090. $attackTroops['total_power'] -= $deadNum * $tProp['single_power'];
  1091. $attackTroops['total_carry_load'] -= $deadNum * $tProp['single_carry_load'];
  1092. $attackTroops['total_dead_consumption'] += $deadNum * $tProp['single_consumption'];
  1093. $attackTroops['total_live_number'] -= $deadNum;
  1094.  
  1095. if ($attackTroops['total_live_number'] <= 0) {
  1096. $warResult['all_attack_killed'] = TRUE;
  1097. }
  1098.  
  1099. $attackTroops['troops'][$tid]['live_number'] -= $deadNum;
  1100.  
  1101. if ($curPower <= 0) {
  1102. break;
  1103. }
  1104. }
  1105. }
  1106.  
  1107.  
  1108. foreach ($defenseTroops as $vid => $troopsTable) {
  1109. if ($defenseTroops[$vid]['total_power'] <= 0) {
  1110. continue;
  1111. }
  1112.  
  1113. $curPower = floor( $totalAttackPower * $i / $divisionFactor );
  1114.  
  1115. if (0 < $curPower) {
  1116. foreach ($troopsTable['troops'] as $tid => $tProp) {
  1117. if ($tid == 99) {
  1118. continue;
  1119. }
  1120.  
  1121. $sPower = ($tProp['single_power'] <= 0 ? 1 : $tProp['single_power']);
  1122. $deadNum = floor( $curPower / $sPower );
  1123.  
  1124. if ($tProp['live_number'] < $deadNum) {
  1125. $deadNum = $tProp['live_number'];
  1126. }
  1127.  
  1128. $curPower -= $deadNum * $tProp['single_power'];
  1129. $totalAttackPower -= $deadNum * $tProp['single_power'];
  1130. $defenseTroops[$vid]['total_power'] -= $deadNum * $tProp['single_power'];
  1131. $defenseTroops[$vid]['total_dead_consumption'] += $deadNum * $tProp['single_consumption'];
  1132. $defenseTroops[$vid]['total_live_number'] -= $deadNum;
  1133. $defenseTroops[$vid]['troops'][$tid]['live_number'] -= $deadNum;
  1134.  
  1135. if ($curPower <= 0) {
  1136. break;
  1137. }
  1138. }
  1139.  
  1140. continue;
  1141. }
  1142. }
  1143.  
  1144. ++$i;
  1145. }
  1146.  
  1147. $warResult['attackTroops'] = $attackTroops;
  1148. $warResult['defenseTroops'] = $defenseTroops;
  1149. return $warResult;
  1150. }
  1151.  
  1152. function _gettroopwithpower($troops, $troopsPower, $isAttacking, $heroLevel, $peopleCount, $wringerPower = 0, $wallPower = 0, $heroId = -1, $spyAction = FALSE) {
  1153. global $GameMetadata;
  1154.  
  1155. $returnTroops = array( 'troops' => array( ), 'total_live_number' => 0, 'total_spy_live_number' => 0, 'total_power' => 0, 'total_carry_load' => 0, 'total_dead_consumption' => 0, 'hasHero' => 0 - 1 < $heroId, 'heroTroopId' => $heroId );
  1156. $powerFactor = (!$spyAction ? floor( $peopleCount / 250 ) + $wringerPower + $heroLevel : 0);
  1157.  
  1158. foreach ($troops as $tid => $tnum) {
  1159. $tpower = ($isAttacking ? $GameMetadata['troops'][$tid]['attack_value'] : $GameMetadata['troops'][$tid]['defense_infantry']);
  1160. $tpower = floor( ( $tpower + (isset( $troopsPower[$tid] ) ? $troopsPower[$tid] : 0) + $powerFactor ) * ( 1 + $wallPower / 100 ) );
  1161.  
  1162. if ($spyAction) {
  1163. $tpower = (( ( ( $tid == 103 || $tid == 4 ) || $tid == 14 ) || $tid == 23 ) ? 1 : 0);
  1164. }
  1165.  
  1166. $num = (0 < intval( $tnum ) ? intval( $tnum ) : 0);
  1167.  
  1168. if ($tid != 99) {
  1169. $returnTroops['total_live_number'] += $num;
  1170. $returnTroops['total_power'] += $num * $tpower;
  1171. $returnTroops['total_carry_load'] += $num * $GameMetadata['troops'][$tid]['carry_load'];
  1172.  
  1173. if ($spyAction) {
  1174. if (( ( ( $tid == 103 || $tid == 4 ) || $tid == 14 ) || $tid == 23 )) {
  1175. $returnTroops['total_spy_live_number'] += $num;
  1176. }
  1177. }
  1178. }
  1179.  
  1180. $returnTroops['troops'][$tid] = array( 'number' => $num, 'live_number' => $num, 'single_consumption' => $GameMetadata['troops'][$tid]['crop_consumption'], 'single_carry_load' => $GameMetadata['troops'][$tid]['carry_load'], 'single_power' => $tpower );
  1181. }
  1182.  
  1183. return $returnTroops;
  1184. }
  1185.  
  1186. function _getattacktroopsforvillage($troopsTrainingStr, $troopsArray, $heroLevel, $peopleCount, $wringerLevel, $spyAction) {
  1187. $troopsPower = array( );
  1188.  
  1189. if (!$spyAction) {
  1190. $_c = 0;
  1191. $troopsTrainingStr = trim( $troopsTrainingStr );
  1192.  
  1193. if ($troopsTrainingStr != '') {
  1194. $_arr = explode( ',', $troopsTrainingStr );
  1195.  
  1196. foreach ($_arr as $troopStr) {
  1197. ++$_c;
  1198. list( $troopId, $researches_done, $defense_level, $attack_level ) = explode( ' ', $troopStr );
  1199.  
  1200. if (( $troopId != 99 && $_c <= 8 )) {
  1201. $troopsPower[$troopId] = $attack_level;
  1202. continue;
  1203. }
  1204. }
  1205. }
  1206. }
  1207.  
  1208. return $this->_getTroopWithPower( $troopsArray, $troopsPower, TRUE, $heroLevel, $peopleCount, $wringerLevel, 0, 0 - 1, $spyAction );
  1209. }
  1210.  
  1211. function _getdefensetroopsforvillage($vid, $troopsArray, $hasHero, $peopleCount, $wallPower, $spyAction) {
  1212. $vrow = $this->provider->fetchRow( 'SELECT v.player_id, v.troops_training FROM p_villages v WHERE v.id=%s', array( $vid ) );
  1213. $heroLevel = 0;
  1214. $heroId = 0 - 1;
  1215. $troopsPower = array( );
  1216.  
  1217. if ($vrow != NULL) {
  1218. if ($hasHero) {
  1219. $_row = $this->provider->fetchRow( 'SELECT p.hero_level, p.hero_troop_id FROM p_players p WHERE p.id=%s', array( intval( $vrow['player_id'] ) ) );
  1220.  
  1221. if ($_row != NULL) {
  1222. $heroLevel = intval( $_row['hero_level'] );
  1223. $heroId = intval( $_row['hero_troop_id'] );
  1224. }
  1225. }
  1226.  
  1227. if (!$spyAction) {
  1228. $_c = 0;
  1229. $troopsTrainingStr = trim( $vrow['troops_training'] );
  1230.  
  1231. if ($troopsTrainingStr != '') {
  1232. $_arr = explode( ',', $troopsTrainingStr );
  1233.  
  1234. foreach ($_arr as $troopStr) {
  1235. ++$_c;
  1236. list( $troopId, $researches_done, $defense_level, $attack_level ) = explode( ' ', $troopStr );
  1237.  
  1238. if (( $troopId != 99 && $_c <= 8 )) {
  1239. $troopsPower[$troopId] = $defense_level;
  1240. continue;
  1241. }
  1242. }
  1243. }
  1244. }
  1245. }
  1246.  
  1247. return $this->_getTroopWithPower( $troopsArray, $troopsPower, FALSE, $heroLevel, $peopleCount, 0, $wallPower, $heroId, $spyAction );
  1248. }
  1249.  
  1250. function _addtroopstovillage($toVillageRow, $fromVillageRow, $addTroopsArray, $affectCropConsumption) {
  1251. $troopsCropConsume = ($affectCropConsumption ? $addTroopsArray['cropConsumption'] : 0);
  1252. $t = (( ( $toVillageRow['player_id'] == $fromVillageRow['player_id'] && !$toVillageRow['is_oasis'] ) && $addTroopsArray['onlyHero'] ) ? $toVillageRow['troops_num'] : $this->_getNewTroops( $toVillageRow['troops_num'], $addTroopsArray, $fromVillageRow['id'], ( $toVillageRow['player_id'] == $fromVillageRow['player_id'] && !$toVillageRow['is_oasis'] ) ));
  1253.  
  1254. if (!$toVillageRow['is_oasis']) {
  1255. $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_num=\'%s\', v.crop_consumption=v.crop_consumption+%s WHERE v.id=%s', array( $t, $troopsCropConsume, $toVillageRow['id'] ) );
  1256. } else {
  1257. $this->provider->executeQuery( 'UPDATE p_villages v SET v.crop_consumption=v.crop_consumption+%s WHERE v.id=%s', array( $troopsCropConsume, $toVillageRow['parent_id'] ) );
  1258. $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_num=\'%s\' WHERE v.id=%s', array( $t, $toVillageRow['id'] ) );
  1259. }
  1260.  
  1261. $t = (( ( $toVillageRow['player_id'] == $fromVillageRow['player_id'] && !$toVillageRow['is_oasis'] ) && $addTroopsArray['onlyHero'] ) ? $fromVillageRow['troops_out_num'] : $this->_getNewTroops( $fromVillageRow['troops_out_num'], $addTroopsArray, $toVillageRow['id'], ( $toVillageRow['player_id'] == $fromVillageRow['player_id'] && !$toVillageRow['is_oasis'] ) ));
  1262. $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_out_num=\'%s\', v.crop_consumption=v.crop_consumption-%s WHERE v.id=%s', array( $t, $troopsCropConsume, $fromVillageRow['id'] ) );
  1263. }
  1264.  
  1265. function _getnewtroops($troopsStr, $addTroopsArray, $fromVillageId, $isSamePlayer) {
  1266. $newTroopsStr = '';
  1267. $troopsStr = trim( $troopsStr );
  1268.  
  1269. if ($troopsStr == '') {
  1270. foreach ($addTroopsArray['troops'] as $tid => $tnum) {
  1271. if ($newTroopsStr != '') {
  1272. $newTroopsStr .= ',';
  1273. }
  1274.  
  1275. $newTroopsStr .= $tid . ' ' . $tnum;
  1276. }
  1277.  
  1278.  
  1279. if (( ( $addTroopsArray['hasHero'] && 0 - 1 < $fromVillageId ) && !$isSamePlayer )) {
  1280. if ($newTroopsStr != '') {
  1281. $newTroopsStr .= ',';
  1282. }
  1283.  
  1284. $newTroopsStr .= $addTroopsArray['heroTroopId'] . ' -1';
  1285. }
  1286.  
  1287. $newTroopsStr = $fromVillageId . ':' . $newTroopsStr;
  1288. } else {
  1289. $hasTroopsIn = FALSE;
  1290. $troopsStrArr = explode( '|', $troopsStr );
  1291.  
  1292. foreach ($troopsStrArr as $tvStr) {
  1293. if ($newTroopsStr != '') {
  1294. $newTroopsStr .= '|';
  1295. }
  1296.  
  1297. list( $vid, $vtroopsStr ) = explode( ':', $tvStr );
  1298.  
  1299. if ($vid == $fromVillageId) {
  1300. $hasTroopsIn = TRUE;
  1301. $curTroopsStr = explode( ',', $vtroopsStr );
  1302. $curTroops = array( );
  1303.  
  1304. foreach ($curTroopsStr as $curTroopsStrItem) {
  1305. list( $_tid, $_tnum ) = explode( ' ', $curTroopsStrItem );
  1306.  
  1307. if ($_tnum == 0 - 1) {
  1308. $curTroops[0 - 1] = $_tid;
  1309. continue;
  1310. }
  1311.  
  1312. $curTroops[$_tid] = $_tnum;
  1313. }
  1314.  
  1315. $newtvStr = '';
  1316.  
  1317. foreach ($addTroopsArray['troops'] as $tid => $tnum) {
  1318. if ($newtvStr != '') {
  1319. $newtvStr .= ',';
  1320. }
  1321.  
  1322. if (isset( $curTroops[$tid] )) {
  1323. $tnum += $curTroops[$tid];
  1324. }
  1325.  
  1326. $newtvStr .= $tid . ' ' . $tnum;
  1327. }
  1328.  
  1329.  
  1330. if (isset( $curTroops[0 - 1] )) {
  1331. if ($newtvStr != '') {
  1332. $newtvStr .= ',';
  1333. }
  1334.  
  1335. $newtvStr .= $curTroops[0 - 1] . ' -1';
  1336. } else {
  1337. if (( 0 - 1 < $fromVillageId && !$isSamePlayer )) {
  1338. if ($addTroopsArray['hasHero']) {
  1339. if ($newtvStr != '') {
  1340. $newtvStr .= ',';
  1341. }
  1342.  
  1343. $newtvStr .= $addTroopsArray['heroTroopId'] . ' -1';
  1344. }
  1345. }
  1346. }
  1347.  
  1348. $newTroopsStr .= $vid . ':' . $newtvStr;
  1349. continue;
  1350. }
  1351.  
  1352. $newTroopsStr .= $tvStr;
  1353. }
  1354.  
  1355.  
  1356. if (!$hasTroopsIn) {
  1357. $newTroopsStr = '';
  1358.  
  1359. foreach ($addTroopsArray['troops'] as $tid => $tnum) {
  1360. if ($newTroopsStr != '') {
  1361. $newTroopsStr .= ',';
  1362. }
  1363.  
  1364. $newTroopsStr .= $tid . ' ' . $tnum;
  1365. }
  1366.  
  1367.  
  1368. if (( ( $addTroopsArray['hasHero'] && 0 - 1 < $fromVillageId ) && !$isSamePlayer )) {
  1369. if ($newTroopsStr != '') {
  1370. $newTroopsStr .= ',';
  1371. }
  1372.  
  1373. $newTroopsStr .= $addTroopsArray['heroTroopId'] . ' -1';
  1374. }
  1375.  
  1376. $newTroopsStr = $fromVillageId . ':' . $newTroopsStr;
  1377.  
  1378. if ($troopsStr != '') {
  1379. $newTroopsStr = $troopsStr . '|' . $newTroopsStr;
  1380. }
  1381. }
  1382. }
  1383.  
  1384. return $newTroopsStr;
  1385. }
  1386.  
  1387. function _harvesttroopsfrom($villageRow, $maxCarryLoad, $crannyTotalSize) {
  1388. if (( $maxCarryLoad <= 0 || $villageRow['is_oasis'] )) {
  1389. return array( 'string' => '0 0 0 0', 'sum' => 0 );
  1390. }
  1391.  
  1392. $resources = array( );
  1393. $r_arr = explode( ',', $villageRow['resources'] );
  1394.  
  1395. foreach ($r_arr as $r_str) {
  1396. $r2 = explode( ' ', $r_str );
  1397. $prate = floor( $r2[4] * ( 1 + $r2[5] / 100 ) ) - ($r2[0] == 4 ? $villageRow['crop_consumption'] : 0);
  1398. $current_value = floor( $r2[1] + $villageRow['elapsedTimeInSeconds'] * ( $prate / 3600 ) );
  1399.  
  1400. if ($r2[2] < $current_value) {
  1401. $current_value = $r2[2];
  1402. }
  1403.  
  1404. $resources[$r2[0]] = array( 'current_value' => $current_value - $crannyTotalSize, 'store_max_limit' => $r2[2], 'store_init_limit' => $r2[3], 'prod_rate' => $r2[4], 'prod_rate_percentage' => $r2[5] );
  1405. }
  1406.  
  1407. $divFactor = 4;
  1408. $harvest = array( 0, 0, 0, 0 );
  1409. $sum = 0;
  1410.  
  1411. while (0 < $maxCarryLoad) {
  1412. $curTotalRes = 0;
  1413. $m = 0;
  1414.  
  1415. foreach ($resources as $k => $rdata) {
  1416. $v = $rdata['current_value'];
  1417. $take = floor( $maxCarryLoad / $divFactor );
  1418.  
  1419. if (0 < $v) {
  1420. if ($v < $take) {
  1421. $take = $v;
  1422. }
  1423.  
  1424. $maxCarryLoad -= $take;
  1425. $resources[$k]['current_value'] -= $take;
  1426. $harvest[$m] += $take;
  1427. $sum += $take;
  1428. $curTotalRes += $resources[$k]['current_value'];
  1429. }
  1430.  
  1431. ++$m;
  1432. }
  1433.  
  1434.  
  1435. if (( $curTotalRes <= 0 && $divFactor == 1 )) {
  1436. break;
  1437. }
  1438.  
  1439. $divFactor = 1;
  1440. }
  1441.  
  1442. $resourcesStr = '';
  1443.  
  1444. foreach ($resources as $k => $v) {
  1445. if ($resourcesStr != '') {
  1446. $resourcesStr .= ',';
  1447. }
  1448.  
  1449. $resourcesStr .= sprintf( '%s %s %s %s %s %s', $k, $v['current_value'] + $crannyTotalSize, $v['store_max_limit'], $v['store_init_limit'], $v['prod_rate'], $v['prod_rate_percentage'] );
  1450. }
  1451.  
  1452. $elapsedTimeInSeconds = $villageRow['elapsedTimeInSeconds'];
  1453. list( $cpValue, $cpRate ) = explode( ' ', $villageRow['cp'] );
  1454. $cpValue = floor( $cpValue + $elapsedTimeInSeconds * ( $cpRate / 86400 ) );
  1455. $cp = $cpValue . ' ' . $cpRate;
  1456. $this->provider->executeQuery( 'UPDATE p_villages v
  1457. SET
  1458. v.resources=\'%s\',
  1459. v.cp=\'%s\',
  1460. v.last_update_date=NOW()
  1461. WHERE
  1462. v.id=%s', array( $resourcesStr, $cp, $villageRow['id'] ) );
  1463. return array( 'string' => implode( ' ', $harvest ), 'sum' => $sum );
  1464. }
  1465.  
  1466. function _updatevillageouttroops($vid, $invid, $newTroopsStr, $heroKilled, $thisInforcementDied, $uid) {
  1467. $pid = $uid;
  1468.  
  1469. if (0 - 1 < $vid) {
  1470. $row = $this->provider->fetchRow( 'SELECT v.player_id, v.troops_out_num FROM p_villages v WHERE v.id=%s', array( $vid ) );
  1471.  
  1472. if ($row == NULL) {
  1473. return null;
  1474. }
  1475.  
  1476. $pid = $row['player_id'];
  1477. $troops_out_num = '';
  1478. $ts = trim( $row['troops_out_num'] );
  1479.  
  1480. if ($ts != '') {
  1481. $tsArr = explode( '|', $ts );
  1482.  
  1483. foreach ($tsArr as $tsArrStr) {
  1484. list( $_vid, $_troops ) = explode( ':', $tsArrStr );
  1485.  
  1486. if ($_vid == $invid) {
  1487. if (!$thisInforcementDied) {
  1488. if ($troops_out_num != '') {
  1489. $troops_out_num .= '|';
  1490. }
  1491.  
  1492. $troops_out_num .= $invid . ':' . $newTroopsStr;
  1493. continue;
  1494. }
  1495.  
  1496. continue;
  1497. }
  1498.  
  1499. if ($troops_out_num != '') {
  1500. $troops_out_num .= '|';
  1501. }
  1502.  
  1503. $troops_out_num .= $tsArrStr;
  1504. }
  1505. }
  1506.  
  1507. $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_out_num=\'%s\' WHERE v.id=%s', array( $troops_out_num, $vid ) );
  1508. }
  1509.  
  1510. if ($heroKilled) {
  1511. $this->provider->executeQuery( 'UPDATE p_players p SET p.hero_troop_id=NULL, p.hero_in_village_id=NULL WHERE p.id=%s', array( intval( $pid ) ) );
  1512. }
  1513.  
  1514. }
  1515.  
  1516. function _updatevillage($villageRow, $reduceCropConsumption, $heroKilled) {
  1517. $elapsedTimeInSeconds = $villageRow['elapsedTimeInSeconds'];
  1518. $resources = array( );
  1519. $r_arr = explode( ',', $villageRow['resources'] );
  1520.  
  1521. foreach ($r_arr as $r_str) {
  1522. $r2 = explode( ' ', $r_str );
  1523. $prate = floor( $r2[4] * ( 1 + $r2[5] / 100 ) ) - ($r2[0] == 4 ? $villageRow['crop_consumption'] : 0);
  1524. $current_value = floor( $r2[1] + $elapsedTimeInSeconds * ( $prate / 3600 ) );
  1525.  
  1526. if ($r2[2] < $current_value) {
  1527. $current_value = $r2[2];
  1528. }
  1529.  
  1530. $resources[$r2[0]] = array( 'current_value' => $current_value, 'store_max_limit' => $r2[2], 'store_init_limit' => $r2[3], 'prod_rate' => $r2[4], 'prod_rate_percentage' => $r2[5], 'calc_prod_rate' => $prate );
  1531. }
  1532.  
  1533. list( $cpValue, $cpRate ) = explode( ' ', $villageRow['cp'] );
  1534. $cpValue = floor( $cpValue + $elapsedTimeInSeconds * ( $cpRate / 86400 ) );
  1535. $resourcesStr = '';
  1536.  
  1537. foreach ($resources as $k => $v) {
  1538. if ($resourcesStr != '') {
  1539. $resourcesStr .= ',';
  1540. }
  1541.  
  1542. $resourcesStr .= sprintf( '%s %s %s %s %s %s', $k, $v['current_value'], $v['store_max_limit'], $v['store_init_limit'], $v['prod_rate'], $v['prod_rate_percentage'] );
  1543. }
  1544.  
  1545. $cp = $cpValue . ' ' . $cpRate;
  1546. $this->provider->executeQuery( 'UPDATE p_villages v
  1547. SET
  1548. v.resources=\'%s\',
  1549. v.cp=\'%s\',
  1550. v.crop_consumption=v.crop_consumption-%s,
  1551. v.last_update_date=NOW()
  1552. WHERE
  1553. v.id=%s', array( $resourcesStr, $cp, $reduceCropConsumption, $villageRow['id'] ) );
  1554.  
  1555. if ($heroKilled) {
  1556. $this->provider->executeQuery( 'UPDATE p_players p SET p.hero_troop_id=NULL, p.hero_in_village_id=NULL WHERE p.id=%s', array( intval( $villageRow['player_id'] ) ) );
  1557. }
  1558.  
  1559. }
  1560.  
  1561. function _capturevillage() {
  1562. }
  1563.  
  1564. function _captureoasis() {
  1565. }
  1566.  
  1567. function _getvillageinfo($villageId) {
  1568. return $this->provider->fetchRow( 'SELECT
  1569. v.id, v.parent_id, v.tribe_id,
  1570. v.rel_x, v.rel_y, v.crop_consumption,
  1571. v.player_id, v.alliance_id, v.village_oases_id,
  1572. v.player_name, v.alliance_name,
  1573. v.is_capital, v.is_oasis, v.people_count,
  1574. v.resources, v.buildings, v.cp,
  1575. v.troops_training,
  1576. v.troops_num, v.troops_out_num, v.troops_intrap_num, v.troops_out_intrap_num,
  1577. v.allegiance_percent,
  1578. v.child_villages_id,
  1579. TIME_TO_SEC(TIMEDIFF(NOW(), v.last_update_date)) elapsedTimeInSeconds,
  1580. TIME_TO_SEC(TIMEDIFF(NOW(), v.creation_date)) oasisElapsedTimeInSeconds
  1581. FROM p_villages v
  1582. WHERE v.id=%s', array( $villageId ) );
  1583. }
  1584. }
  1585.  
  1586. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement