Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class battlemodel {
- function getwarresult($taskRow) {
- global $GameMetadata;
- $toVillageRow = $this->_getVillageInfo( $taskRow['to_village_id'] );
- $fromVillageRow = $this->_getVillageInfo( $taskRow['village_id'] );
- $paramsArray = explode( '|', $taskRow['proc_params'] );
- $troopsArrStr = explode( ',', $paramsArray[0] );
- $troopsArray = array( 'troops' => array( ), 'onlyHero' => FALSE, 'heroTroopId' => 0, 'hasHero' => FALSE, 'hasKing' => FALSE, 'cropConsumption' => 0 );
- $_onlyHero = TRUE;
- foreach ($troopsArrStr as $_t) {
- list( $tid, $tnum ) = explode( ' ', $_t );
- if ($tnum == 0 - 1) {
- $troopsArray['hasHero'] = TRUE;
- $troopsArray['heroTroopId'] = $tid;
- $tnum = 1;
- } else {
- $troopsArray['troops'][$tid] = $tnum;
- if (0 < $tnum) {
- $_onlyHero = FALSE;
- }
- if (( ( ( $tid == 9 || $tid == 19 ) || $tid == 29 ) || $tid == 108 )) {
- $troopsArray['hasKing'] = TRUE;
- }
- }
- $troopsArray['cropConsumption'] += $GameMetadata['troops'][$tid]['crop_consumption'] * $tnum;
- }
- if (( $_onlyHero && $troopsArray['hasHero'] )) {
- $troopsArray['onlyHero'] = TRUE;
- }
- $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 );
- if ($taskRow['proc_type'] == QS_CREATEVILLAGE) {
- if (( $toVillageRow['is_oasis'] || 0 < intval( $toVillageRow['player_id'] ) )) {
- $taskRow['proc_type'] = QS_WAR_ATTACK_PLUNDER;
- }
- }
- switch ($taskRow['proc_type']) {
- case QS_WAR_REINFORCE: {
- return $this->_handleReInforcement( $taskRow, $toVillageRow, $fromVillageRow, $procInfo, $paramsArray[0] );
- }
- case QS_WAR_ATTACK: {
- }
- case QS_WAR_ATTACK_PLUNDER: {
- return $this->_handleWarAttack( $taskRow, $toVillageRow, $fromVillageRow, $procInfo );
- }
- case QS_WAR_ATTACK_SPY: {
- return $this->_handleWarSpy( $taskRow, $toVillageRow, $fromVillageRow, $procInfo );
- }
- case QS_CREATEVILLAGE: {
- $this->_handleCreateNewVillage( $taskRow, $toVillageRow, $fromVillageRow, $troopsArray['cropConsumption'] );
- return FALSE;
- }
- }
- return TRUE;
- }
- function _handlewarspy($taskRow, $toVillageRow, $fromVillageRow, $procInfo) {
- global $GameMetadata;
- if (( !$toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 )) {
- $paramsArray = explode( '|', $taskRow['proc_params'] );
- $paramsArray[sizeof( $paramsArray ) - 1] = 1;
- $newParams = implode( '|', $paramsArray );
- $this->provider->executeQuery( 'UPDATE p_queue q
- SET
- q.player_id=%s,
- q.village_id=%s,
- q.to_player_id=%s,
- q.to_village_id=%s,
- q.proc_type=%s,
- q.proc_params=\'%s\',
- q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
- q.locked=0
- 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'] ) );
- return TRUE;
- }
- $attackTroops = $this->_getAttackTroopsForVillage( '', $procInfo['troopsArray']['troops'], 0, 0, 0, TRUE );
- $defenseTroops = array( );
- $totalDefensePower = 0;
- $troops_num = trim( $toVillageRow['troops_num'] );
- if ($troops_num != '') {
- $vtroopsArr = explode( '|', $troops_num );
- foreach ($vtroopsArr as $vtroopsStr) {
- list( $tvid, $tvtroopsStr ) = explode( ':', $vtroopsStr );
- $incFactor = (( ( $toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 ) && $tvid == 0 - 1 ) ? floor( $toVillageRow['oasisElapsedTimeInSeconds'] / 86400 ) : 0);
- $_hasHero = FALSE;
- $vtroops = array( );
- $_arr = explode( ',', $tvtroopsStr );
- foreach ($_arr as $_arrStr) {
- list( $_tid, $_tnum ) = explode( ' ', $_arrStr );
- if ($_tnum == 0 - 1) {
- $_hasHero = TRUE;
- continue;
- }
- $vtroops[$_tid] = $_tnum + $incFactor;
- }
- if ($tvid == 0 - 1) {
- $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'] ) ) ) );
- if (( 0 < $hero_in_village_id && $hero_in_village_id == $toVillageRow['id'] )) {
- $_hasHero = TRUE;
- }
- }
- $defenseTroops[$tvid] = $this->_getDefenseTroopsForVillage( ($tvid == 0 - 1 ? $toVillageRow['id'] : $tvid), $vtroops, $_hasHero, 0, 0, TRUE );
- $totalDefensePower += $defenseTroops[$tvid]['total_power'];
- }
- }
- $warResult = $this->_getSpyResult( $attackTroops, $defenseTroops, $totalDefensePower );
- $reduceConsumption = $warResult['attackTroops']['total_dead_consumption'];
- if (0 < $reduceConsumption) {
- $this->_updateVillage( $fromVillageRow, $reduceConsumption, FALSE );
- }
- $defenseTroopsStr = '';
- $defenseReduceConsumption = 0;
- $reportTroopTable = array( );
- $tribeId = 0;
- foreach ($warResult['defenseTroops'] as $vid => $troopsTable) {
- $defenseReduceConsumption += $troopsTable['total_dead_consumption'];
- $newTroops = '';
- $thisInforcementDied = TRUE;
- foreach ($troopsTable['troops'] as $tid => $tprop) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $tid . ' ' . $tprop['live_number'];
- if (0 < $tprop['live_number']) {
- $thisInforcementDied = FALSE;
- }
- $tribeId = $GameMetadata['troops'][$tid]['for_tribe_id'];
- if (!isset( $reportTroopTable[$tribeId] )) {
- $reportTroopTable[$tribeId] = array( 'troops' => array( ), 'hero' => array( 'number' => 0, 'dead_number' => 0 ) );
- }
- if ($tid != 99) {
- if (!isset( $reportTroopTable[$tribeId]['troops'][$tid] )) {
- $reportTroopTable[$tribeId]['troops'][$tid] = array( 'number' => $tprop['number'], 'dead_number' => $tprop['number'] - $tprop['live_number'] );
- continue;
- }
- $reportTroopTable[$tribeId]['troops'][$tid]['number'] += $tprop['number'];
- $reportTroopTable[$tribeId]['troops'][$tid]['dead_number'] += $tprop['number'] - $tprop['live_number'];
- continue;
- }
- }
- if ($troopsTable['hasHero']) {
- ++$reportTroopTable[$tribeId]['hero']['number'];
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $troopsTable['heroTroopId'] . ' -1';
- $thisInforcementDied = FALSE;
- }
- $this->_updateVillageOutTroops( $vid, $toVillageRow['id'], $newTroops, ( $troopsTable['hasHero'] && $troopsTable['total_live_number'] <= 0 ), $thisInforcementDied, intval( $toVillageRow['player_id'] ) );
- if (( $vid == 0 - 1 && $toVillageRow['is_oasis'] )) {
- $this->provider->executeQuery( 'UPDATE p_villages v SET v.creation_date=NOW() WHERE v.id=%s', array( $toVillageRow['id'] ) );
- }
- if (( !$thisInforcementDied || $vid == 0 - 1 )) {
- if ($defenseTroopsStr != '') {
- $defenseTroopsStr .= '|';
- }
- $defenseTroopsStr .= $vid . ':' . $newTroops;
- continue;
- }
- }
- if (( ( $toVillageRow['is_oasis'] && 0 < intval( $toVillageRow['player_id'] ) ) && isset( $reportTroopTable[4] ) )) {
- unset( $reportTroopTable[4] );
- }
- $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_num=\'%s\' WHERE v.id=%s', array( $defenseTroopsStr, $toVillageRow['id'] ) );
- if (!( $toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 )) {
- $_tovid = ($toVillageRow['is_oasis'] ? intval( $toVillageRow['parent_id'] ) : $toVillageRow['id']);
- $this->provider->executeQuery( 'UPDATE p_villages v SET v.crop_consumption=v.crop_consumption-%s WHERE v.id=%s', array( $defenseReduceConsumption, $_tovid ) );
- }
- $newTroops = '';
- foreach ($warResult['attackTroops']['troops'] as $tid => $tprop) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $tid . ' ' . $tprop['number'] . ' ' . ( $tprop['number'] - $tprop['live_number'] );
- }
- if ($procInfo['troopsArray']['hasHero']) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= 0 - 1 . ' ' . 1 . ' ' . ($warResult['all_attack_killed'] ? 1 : 0);
- }
- $attackReportTroops = $newTroops;
- $defenseReportTroops = '';
- foreach ($reportTroopTable as $tribeId => $defTroops) {
- $defenseReportTroops1 = '';
- foreach ($defTroops['troops'] as $tid => $tArr) {
- if ($defenseReportTroops1 != '') {
- $defenseReportTroops1 .= ',';
- }
- $defenseReportTroops1 .= $tid . ' ' . $tArr['number'] . ' ' . $tArr['dead_number'];
- }
- if (0 < $defTroops['hero']['number']) {
- if ($defenseReportTroops1 != '') {
- $defenseReportTroops1 .= ',';
- }
- $defenseReportTroops1 .= 0 - 1 . ' ' . $defTroops['hero']['number'] . ' ' . $defTroops['hero']['dead_number'];
- }
- if ($defenseReportTroops1 != '') {
- if ($defenseReportTroops != '') {
- $defenseReportTroops .= '#';
- }
- $defenseReportTroops .= $defenseReportTroops1;
- continue;
- }
- }
- $harvestInfo = '';
- $harvestResources = '';
- $spyType = $procInfo['spyAction'];
- if (!$warResult['all_spy_killed']) {
- if ($spyType == 1) {
- $harvestResources = '0 0 0 0';
- if (!$toVillageRow['is_oasis']) {
- $resources_info = array( );
- $r_arr = explode( ',', $toVillageRow['resources'] );
- foreach ($r_arr as $r_str) {
- $r2 = explode( ' ', $r_str );
- $prate = floor( $r2[4] * ( 1 + $r2[5] / 100 ) ) - ($r2[0] == 4 ? $toVillageRow['crop_consumption'] : 0);
- $current_value = floor( $r2[1] + $toVillageRow['elapsedTimeInSeconds'] * ( $prate / 3600 ) );
- if ($r2[2] < $current_value) {
- $current_value = $r2[2];
- }
- $resources_info[] = $current_value;
- }
- $harvestResources = implode( ' ', $resources_info );
- }
- }
- if ($spyType == 2) {
- if (!$toVillageRow['is_oasis']) {
- $buildingsInfo = array( );
- $bStr = trim( $toVillageRow['buildings'] );
- if ($bStr != '') {
- $bStrArr = explode( ',', $bStr );
- $_i = 0;
- foreach ($bStrArr as $b2Str) {
- ++$_i;
- if ($_i < 19) {
- continue;
- }
- list( $item_id, $level, $update_state ) = explode( ' ', $b2Str );
- if (0 < $level) {
- $buildingsInfo[] = $item_id . ' ' . $level;
- continue;
- }
- }
- }
- if (0 < sizeof( $buildingsInfo )) {
- $_randIndex = mt_rand( 0, sizeof( $buildingsInfo ) - 1 );
- $harvestInfo = $buildingsInfo[$_randIndex];
- }
- }
- }
- } else {
- $spyType = 3;
- }
- $timeInSeconds = $taskRow['remainingTimeInSeconds'];
- if (!$warResult['defense_has_spytroops']) {
- $reportResult = 100;
- } else {
- $reportResult = ($warResult['all_spy_killed'] ? 9 : 10);
- }
- $reportCategory = 4;
- $reportBody = $attackReportTroops . '|' . $defenseReportTroops . '|' . $harvestResources . '|' . $harvestInfo . '|' . $spyType;
- $r = new ReportModel( );
- $reportId = $r->createReport( intval( $fromVillageRow['player_id'] ), intval( $toVillageRow['player_id'] ), intval( $fromVillageRow['id'] ), intval( $toVillageRow['id'] ), $reportCategory, $reportResult, $reportBody, $timeInSeconds );
- if (( !$warResult['defense_has_spytroops'] && $toVillageRow['player_id'] != $fromVillageRow['player_id'] )) {
- $r->deleteReport( intval( $taskRow['to_player_id'] ), $reportId );
- }
- if (!$warResult['all_attack_killed']) {
- $paramsArray = explode( '|', $taskRow['proc_params'] );
- $paramsArray[sizeof( $paramsArray ) - 1] = 1;
- $newTroops = '';
- foreach ($warResult['attackTroops']['troops'] as $tid => $tprop) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $tid . ' ' . $tprop['live_number'];
- }
- if (( !$warResult['all_attack_killed'] && $procInfo['troopsArray']['hasHero'] )) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $procInfo['troopsArray']['heroTroopId'] . ' -1';
- }
- $paramsArray[0] = $newTroops;
- $newParams = implode( '|', $paramsArray );
- $this->provider->executeQuery( 'UPDATE p_queue q
- SET
- q.player_id=%s,
- q.village_id=%s,
- q.to_player_id=%s,
- q.to_village_id=%s,
- q.proc_type=%s,
- q.proc_params=\'%s\',
- q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
- q.locked=0
- 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'] ) );
- return TRUE;
- }
- return FALSE;
- }
- function _handlewarattack($taskRow, $toVillageRow, $fromVillageRow, $procInfo) {
- global $GameMetadata;
- if (( !$toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 )) {
- $paramsArray = explode( '|', $taskRow['proc_params'] );
- $paramsArray[sizeof( $paramsArray ) - 1] = 1;
- $newParams = implode( '|', $paramsArray );
- $this->provider->executeQuery( 'UPDATE p_queue q
- SET
- q.player_id=%s,
- q.village_id=%s,
- q.to_player_id=%s,
- q.to_village_id=%s,
- q.proc_type=%s,
- q.proc_params=\'%s\',
- q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
- q.locked=0
- 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'] ) );
- return TRUE;
- }
- $heroLevel = 0;
- if ($procInfo['troopsArray']['hasHero']) {
- $heroLevel = intval( $this->provider->fetchScalar( 'SELECT p.hero_level FROM p_players p WHERE p.id=%s', array( intval( $fromVillageRow['player_id'] ) ) ) );
- }
- $wringerLevel = 0;
- $buildings = array( );
- $bStr = trim( $fromVillageRow['buildings'] );
- if ($bStr != '') {
- $bStrArr = explode( ',', $bStr );
- foreach ($bStrArr as $b2Str) {
- list( $item_id, $level, $update_state ) = explode( ' ', $b2Str );
- if ($item_id == 35) {
- $wringerLevel = $level;
- break;
- }
- }
- }
- $attackTroops = $this->_getAttackTroopsForVillage( $fromVillageRow['troops_training'], $procInfo['troopsArray']['troops'], $heroLevel, $fromVillageRow['people_count'], $wringerLevel, FALSE );
- $crannyTotalSize = 0;
- $wallPower = 0;
- $buildings = array( );
- $bStr = trim( $toVillageRow['buildings'] );
- if ($bStr != '') {
- $bStrArr = explode( ',', $bStr );
- foreach ($bStrArr as $b2Str) {
- list( $item_id, $level, $update_state ) = explode( ' ', $b2Str );
- if (( ( $item_id == 31 || $item_id == 32 ) || $item_id == 33 )) {
- $wallPower = (0 < $level ? $GameMetadata['items'][$item_id]['levels'][$level - 1]['value'] : 0);
- continue;
- }
- if (( $item_id == 23 && 0 < $level )) {
- $crannyTotalSize += $GameMetadata['items'][$item_id]['levels'][$level - 1]['value'];
- continue;
- }
- }
- }
- $defenseTroops = array( );
- $totalDefensePower = 0;
- $troops_num = trim( $toVillageRow['troops_num'] );
- if ($troops_num != '') {
- $vtroopsArr = explode( '|', $troops_num );
- foreach ($vtroopsArr as $vtroopsStr) {
- list( $tvid, $tvtroopsStr ) = explode( ':', $vtroopsStr );
- $incFactor = (( ( $toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 ) && $tvid == 0 - 1 ) ? floor( $toVillageRow['oasisElapsedTimeInSeconds'] / 86400 ) : 0);
- $_hasHero = FALSE;
- $vtroops = array( );
- $_arr = explode( ',', $tvtroopsStr );
- foreach ($_arr as $_arrStr) {
- list( $_tid, $_tnum ) = explode( ' ', $_arrStr );
- if ($_tnum == 0 - 1) {
- $_hasHero = TRUE;
- continue;
- }
- $vtroops[$_tid] = $_tnum + $incFactor;
- }
- if ($tvid == 0 - 1) {
- $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'] ) ) ) );
- if (( 0 < $hero_in_village_id && $hero_in_village_id == $toVillageRow['id'] )) {
- $_hasHero = TRUE;
- }
- }
- $defenseTroops[$tvid] = $this->_getDefenseTroopsForVillage( ($tvid == 0 - 1 ? $toVillageRow['id'] : $tvid), $vtroops, $_hasHero, $toVillageRow['people_count'], $wallPower, FALSE );
- $totalDefensePower += $defenseTroops[$tvid]['total_power'];
- }
- }
- $warResult = $this->_getWarResult( $attackTroops, $defenseTroops, $totalDefensePower, $taskRow['proc_type'] == QS_WAR_ATTACK_PLUNDER );
- $harvestResources = '0 0 0 0';
- $harvestInfoStruct = array( 'string' => $harvestResources, 'sum' => 0 );
- if (!$warResult['all_attack_killed']) {
- $harvestInfoStruct = $this->_harvestTroopsFrom( $toVillageRow, $warResult['attackTroops']['total_carry_load'], $crannyTotalSize );
- $harvestResources = $harvestInfoStruct['string'];
- }
- $reduceConsumption = $warResult['attackTroops']['total_dead_consumption'];
- if (( $warResult['all_attack_killed'] && $procInfo['troopsArray']['hasHero'] )) {
- $reduceConsumption += $GameMetadata['troops'][$procInfo['troopsArray']['heroTroopId']]['crop_consumption'];
- }
- if (0 < $reduceConsumption) {
- $this->_updateVillage( $fromVillageRow, $reduceConsumption, ( $warResult['all_attack_killed'] && $procInfo['troopsArray']['hasHero'] ) );
- }
- if (( $procInfo['troopsArray']['hasHero'] && !$warResult['all_attack_killed'] )) {
- $heroStatisticPoint = 1;
- $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'] ) ) );
- }
- $defenseTroopsStr = '';
- $defenseReduceConsumption = 0;
- $reportTroopTable = array( );
- $tribeId = 0;
- foreach ($warResult['defenseTroops'] as $vid => $troopsTable) {
- $defenseReduceConsumption += $troopsTable['total_dead_consumption'];
- $newTroops = '';
- $thisInforcementDied = TRUE;
- foreach ($troopsTable['troops'] as $tid => $tprop) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $tid . ' ' . $tprop['live_number'];
- if (0 < $tprop['live_number']) {
- $thisInforcementDied = FALSE;
- }
- $tribeId = $GameMetadata['troops'][$tid]['for_tribe_id'];
- if (!isset( $reportTroopTable[$tribeId] )) {
- $reportTroopTable[$tribeId] = array( 'troops' => array( ), 'hero' => array( 'number' => 0, 'dead_number' => 0 ) );
- }
- if ($tid != 99) {
- if (!isset( $reportTroopTable[$tribeId]['troops'][$tid] )) {
- $reportTroopTable[$tribeId]['troops'][$tid] = array( 'number' => $tprop['number'], 'dead_number' => $tprop['number'] - $tprop['live_number'] );
- continue;
- }
- $reportTroopTable[$tribeId]['troops'][$tid]['number'] += $tprop['number'];
- $reportTroopTable[$tribeId]['troops'][$tid]['dead_number'] += $tprop['number'] - $tprop['live_number'];
- continue;
- }
- }
- if ($troopsTable['hasHero']) {
- ++$reportTroopTable[$tribeId]['hero']['number'];
- }
- if (( 0 < $troopsTable['total_live_number'] && $troopsTable['hasHero'] )) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $troopsTable['heroTroopId'] . ' -1';
- if (( $vid == 0 - 1 && !$toVillageRow['is_oasis'] )) {
- $heroStatisticPoint = 1;
- $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'] ) ) );
- }
- $thisInforcementDied = FALSE;
- }
- if (( $troopsTable['hasHero'] && $troopsTable['total_live_number'] <= 0 )) {
- ++$reportTroopTable[$tribeId]['hero']['dead_number'];
- $defenseReduceConsumption += $GameMetadata['troops'][$troopsTable['heroTroopId']]['crop_consumption'];
- }
- $this->_updateVillageOutTroops( $vid, $toVillageRow['id'], $newTroops, ( $troopsTable['hasHero'] && $troopsTable['total_live_number'] <= 0 ), $thisInforcementDied, intval( $toVillageRow['player_id'] ) );
- if (( $vid == 0 - 1 && $toVillageRow['is_oasis'] )) {
- $this->provider->executeQuery( 'UPDATE p_villages v SET v.creation_date=NOW() WHERE v.id=%s', array( $toVillageRow['id'] ) );
- }
- if (( !$thisInforcementDied || $vid == 0 - 1 )) {
- if ($defenseTroopsStr != '') {
- $defenseTroopsStr .= '|';
- }
- $defenseTroopsStr .= $vid . ':' . $newTroops;
- continue;
- }
- }
- if (( ( $toVillageRow['is_oasis'] && 0 < intval( $toVillageRow['player_id'] ) ) && isset( $reportTroopTable[4] ) )) {
- unset( $reportTroopTable[4] );
- }
- $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_num=\'%s\' WHERE v.id=%s', array( $defenseTroopsStr, $toVillageRow['id'] ) );
- if (!( $toVillageRow['is_oasis'] && intval( $toVillageRow['player_id'] ) == 0 )) {
- $_tovid = ($toVillageRow['is_oasis'] ? intval( $toVillageRow['parent_id'] ) : $toVillageRow['id']);
- $this->provider->executeQuery( 'UPDATE p_villages v SET v.crop_consumption=v.crop_consumption-%s WHERE v.id=%s', array( $defenseReduceConsumption, $_tovid ) );
- }
- $newTroops = '';
- foreach ($warResult['attackTroops']['troops'] as $tid => $tprop) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $tid . ' ' . $tprop['number'] . ' ' . ( $tprop['number'] - $tprop['live_number'] );
- }
- if ($procInfo['troopsArray']['hasHero']) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= 0 - 1 . ' ' . 1 . ' ' . ($warResult['all_attack_killed'] ? 1 : 0);
- }
- $attackReportTroops = $newTroops;
- $defenseReportTroops = '';
- foreach ($reportTroopTable as $tribeId => $defTroops) {
- $defenseReportTroops1 = '';
- foreach ($defTroops['troops'] as $tid => $tArr) {
- if ($defenseReportTroops1 != '') {
- $defenseReportTroops1 .= ',';
- }
- $defenseReportTroops1 .= $tid . ' ' . $tArr['number'] . ' ' . $tArr['dead_number'];
- }
- if (0 < $defTroops['hero']['number']) {
- if ($defenseReportTroops1 != '') {
- $defenseReportTroops1 .= ',';
- }
- $defenseReportTroops1 .= 0 - 1 . ' ' . $defTroops['hero']['number'] . ' ' . $defTroops['hero']['dead_number'];
- }
- if ($defenseReportTroops1 != '') {
- if ($defenseReportTroops != '') {
- $defenseReportTroops .= '#';
- }
- $defenseReportTroops .= $defenseReportTroops1;
- continue;
- }
- }
- $timeInSeconds = $taskRow['remainingTimeInSeconds'];
- $reportResult = 1;
- $reportCategory = 3;
- $reportBody = $attackReportTroops . '|' . $defenseReportTroops . '|' . $warResult['attackTroops']['total_carry_load'] . '|' . $harvestResources;
- $r = new ReportModel( );
- $r->createReport( intval( $fromVillageRow['player_id'] ), intval( $toVillageRow['player_id'] ), intval( $fromVillageRow['id'] ), intval( $toVillageRow['id'] ), $reportCategory, $reportResult, $reportBody, $timeInSeconds );
- if (intval( $toVillageRow['player_id'] ) != intval( $fromVillageRow['player_id'] )) {
- $statisticPoint = 0;
- $harvestPoint = $harvestInfoStruct['sum'];
- if (0 < intval( $toVillageRow['player_id'] )) {
- $statisticPoint = ($warResult['all_attack_killed'] ? $warResult['attackTroops']['total_dead_consumption'] : 0);
- $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'] ) ) );
- }
- if (0 < intval( $fromVillageRow['player_id'] )) {
- $statisticPoint = ($warResult['all_attack_killed'] ? 0 : $warResult['attackTroops']['total_power']);
- $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'] ) ) );
- }
- if (0 < intval( $toVillageRow['alliance_id'] )) {
- $statisticPoint = ($warResult['all_attack_killed'] ? $warResult['attackTroops']['total_dead_consumption'] : 0);
- $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'] ) ) );
- }
- if (0 < intval( $fromVillageRow['alliance_id'] )) {
- $statisticPoint = ($warResult['all_attack_killed'] ? 0 : $warResult['attackTroops']['total_power']);
- $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'] ) ) );
- }
- }
- if (!$warResult['all_attack_killed']) {
- $paramsArray = explode( '|', $taskRow['proc_params'] );
- $paramsArray[sizeof( $paramsArray ) - 1] = 1;
- $newTroops = '';
- foreach ($warResult['attackTroops']['troops'] as $tid => $tprop) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $tid . ' ' . $tprop['live_number'];
- }
- if (( !$warResult['all_attack_killed'] && $procInfo['troopsArray']['hasHero'] )) {
- if ($newTroops != '') {
- $newTroops .= ',';
- }
- $newTroops .= $procInfo['troopsArray']['heroTroopId'] . ' -1';
- }
- $paramsArray[4] = $harvestResources;
- $paramsArray[0] = $newTroops;
- $newParams = implode( '|', $paramsArray );
- $this->provider->executeQuery( 'UPDATE p_queue q
- SET
- q.player_id=%s,
- q.village_id=%s,
- q.to_player_id=%s,
- q.to_village_id=%s,
- q.proc_type=%s,
- q.proc_params=\'%s\',
- q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
- q.locked=0
- 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'] ) );
- return TRUE;
- }
- return FALSE;
- }
- function _handlecreatenewvillage($taskRow, $toVillageRow, $fromVillageRow, $cropConsumption) {
- global $GameMetadata;
- global $SetupMetadata;
- $row = $this->provider->fetchRow( 'SELECT v.player_id,v.field_maps_id FROM p_villages v WHERE v.id=%s', array( $toVillageRow['id'] ) );
- if (0 < intval( $row['player_id'] )) {
- return FALSE;
- }
- if (intval( $this->provider->fetchScalar( 'SELECT p.id FROM p_players p WHERE p.id=%s', array( intval( $fromVillageRow['player_id'] ) ) ) ) == 0) {
- return FALSE;
- }
- $villageName = 'قرية جديدة';
- $update_key = substr( md5( $fromVillageRow['player_id'] . $fromVillageRow['tribe_id'] . $toVillageRow['id'] . $fromVillageRow['player_name'] . $villageName ), 2, 5 );
- $field_map_id = $row['field_maps_id'];
- $buildings = '';
- foreach ($SetupMetadata['field_maps'][$field_map_id] as $v) {
- if ($buildings != '') {
- $buildings .= ',';
- }
- $buildings .= sprintf( '%s 0 0', $v );
- }
- $k = 19;
- while ($k <= 40) {
- $buildings .= ($k == 26 ? ',15 1 0' : ',0 0 0');
- ++$k;
- }
- $resources = '';
- $farr = explode( '-', $SetupMetadata['field_maps_summary'][$field_map_id] );
- $i = 1;
- $_c = sizeof( $farr );
- while ($i <= $_c) {
- if ($resources != '') {
- $resources .= ',';
- }
- $resources .= sprintf( '%s 650 800 800 %s 0', $i, $farr[$i - 1] * 2 );
- ++$i;
- }
- $troops_training = '';
- $troops_num = '';
- foreach ($GameMetadata['troops'] as $k => $v) {
- if (( $v['for_tribe_id'] == 0 - 1 || $v['for_tribe_id'] == $fromVillageRow['tribe_id'] )) {
- if ($troops_training != '') {
- $troops_training .= ',';
- }
- $researching_done = ($v['research_time_consume'] == 0 ? 1 : 0);
- $troops_training .= $k . ' ' . $researching_done . ' 0 0';
- if ($troops_num != '') {
- $troops_num .= ',';
- }
- $troops_num .= $k . ' 0';
- continue;
- }
- }
- $troops_num = '-1:' . $troops_num;
- $this->provider->executeQuery( 'UPDATE p_villages v
- SET
- v.parent_id=%s,
- v.tribe_id=%s,
- v.player_id=%s,
- v.alliance_id=%s,
- v.player_name=\'%s\',
- v.village_name=\'%s\',
- v.alliance_name=\'%s\',
- v.is_capital=0,
- v.buildings=\'%s\',
- v.resources=\'%s\',
- v.cp=\'0 2\',
- v.troops_training=\'%s\',
- v.troops_num=\'%s\',
- v.update_key=\'%s\',
- v.creation_date=NOW(),
- v.last_update_date=NOW()
- 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'] ) );
- $child_villages_id = trim( $fromVillageRow['child_villages_id'] );
- if ($child_villages_id != '') {
- $child_villages_id .= ',';
- }
- $child_villages_id .= $toVillageRow['id'];
- $this->provider->executeQuery( 'UPDATE p_villages v
- SET
- v.crop_consumption=v.crop_consumption-%s,
- v.child_villages_id=\'%s\'
- WHERE v.id=%s', array( $cropConsumption, $child_villages_id, $fromVillageRow['id'] ) );
- $prow = $this->provider->fetchRow( 'SELECT p.villages_id, p.villages_data FROM p_players p WHERE p.id=%s', array( $fromVillageRow['player_id'] ) );
- $villages_id = trim( $prow['villages_id'] );
- if ($villages_id != '') {
- $villages_id .= ',';
- }
- $villages_id .= $toVillageRow['id'];
- $villages_data = trim( $prow['villages_data'] );
- if ($villages_data != '') {
- $villages_data .= '
- ';
- }
- $villages_data .= $toVillageRow['id'] . ' ' . $toVillageRow['rel_x'] . ' ' . $toVillageRow['rel_y'] . ' ' . $villageName;
- $this->provider->executeQuery( 'UPDATE p_players p
- SET
- p.total_people_count=p.total_people_count+2,
- p.villages_count=p.villages_count+1,
- p.selected_village_id=%s,
- p.villages_id=\'%s\',
- p.villages_data=\'%s\'
- WHERE
- p.id=%s', array( $toVillageRow['id'], $villages_id, $villages_data, $fromVillageRow['player_id'] ) );
- }
- function _handlereinforcement($taskRow, $toVillageRow, $fromVillageRow, $procInfo, $troopsArrStr) {
- if ($procInfo['troopBack']) {
- if (( 0 < intval( $toVillageRow['player_id'] ) && $taskRow['to_player_id'] == intval( $toVillageRow['player_id'] ) )) {
- $paramsArray = explode( '|', $taskRow['proc_params'] );
- $res = array( 0, 0, 0, 0 );
- if (trim( $paramsArray[4] ) != '') {
- $res = explode( ' ', $paramsArray[4] );
- }
- $k = 0;
- $r_arr = explode( ',', $toVillageRow['resources'] );
- foreach ($r_arr as $r_str) {
- $r2 = explode( ' ', $r_str );
- $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] );
- ++$k;
- }
- $resourcesStr = '';
- foreach ($resources as $k => $v) {
- if ($resourcesStr != '') {
- $resourcesStr .= ',';
- }
- $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'] );
- }
- $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'] ) );
- if ($procInfo['troopsArray']['hasHero']) {
- $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'] ) ) );
- }
- } else {
- if ($procInfo['troopsArray']['hasHero']) {
- $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'] ) ) );
- }
- }
- return FALSE;
- }
- if (0 < intval( $toVillageRow['player_id'] )) {
- $affectCropConsumption = TRUE;
- if (( $toVillageRow['is_oasis'] && trim( $fromVillageRow['village_oases_id'] ) != '' )) {
- $oArr = explode( ',', trim( $fromVillageRow['village_oases_id'] ) );
- foreach ($oArr as $oid) {
- if ($oid == $taskRow['to_village_id']) {
- $affectCropConsumption = FALSE;
- break;
- }
- }
- }
- $this->_addTroopsToVillage( $toVillageRow, $fromVillageRow, $procInfo['troopsArray'], $affectCropConsumption );
- if (( ( $procInfo['troopsArray']['hasHero'] && $toVillageRow['player_id'] == $fromVillageRow['player_id'] ) && !$toVillageRow['is_oasis'] )) {
- $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'] ) ) );
- }
- $timeInSeconds = $taskRow['remainingTimeInSeconds'];
- $reportResult = 8;
- $reportCategory = 2;
- $troopsCropConsumption = $procInfo['troopsArray']['cropConsumption'];
- $reportBody = $troopsArrStr . '|' . $troopsCropConsumption;
- $r = new ReportModel( );
- $r->createReport( intval( $taskRow['player_id'] ), intval( $taskRow['to_player_id'] ), intval( $taskRow['village_id'] ), intval( $taskRow['to_village_id'] ), $reportCategory, $reportResult, $reportBody, $timeInSeconds );
- return FALSE;
- }
- $paramsArray = explode( '|', $taskRow['proc_params'] );
- $paramsArray[sizeof( $paramsArray ) - 1] = 1;
- $newParams = implode( '|', $paramsArray );
- $this->provider->executeQuery( 'UPDATE p_queue q
- SET
- q.player_id=%s,
- q.village_id=%s,
- q.to_player_id=%s,
- q.to_village_id=%s,
- q.proc_type=%s,
- q.proc_params=\'%s\',
- q.end_date=(q.end_date + INTERVAL q.execution_time SECOND),
- q.locked=0
- 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'] ) );
- return TRUE;
- }
- function _getspyresult($attackTroops, $defenseTroops, $totalDefensePower) {
- $warResult = array( 'all_attack_killed' => FALSE, 'all_spy_killed' => FALSE, 'defense_has_spytroops' => FALSE );
- $attackFactor = 1;
- $totalAttackPower = $attackTroops['total_power'] * $attackFactor;
- $divisionFactor = 10;
- $i = 1;
- while ($i <= $divisionFactor) {
- $curPower = floor( $totalDefensePower * $i / $divisionFactor );
- if (0 < $curPower) {
- foreach ($attackTroops['troops'] as $tid => $tProp) {
- if ($warResult['all_attack_killed']) {
- break;
- }
- if (( $tid == 99 || ( ( ( $tid != 103 && $tid != 4 ) && $tid != 14 ) && $tid != 23 ) )) {
- continue;
- }
- $sPower = ($tProp['single_power'] <= 0 ? 1 : $tProp['single_power']);
- $deadNum = floor( $curPower / $sPower );
- if ($tProp['live_number'] < $deadNum) {
- $deadNum = $tProp['live_number'];
- }
- $curPower -= $deadNum * $tProp['single_power'];
- $totalDefensePower -= $deadNum * $tProp['single_power'];
- $attackTroops['total_power'] -= $deadNum * $tProp['single_power'];
- $attackTroops['total_carry_load'] -= $deadNum * $tProp['single_carry_load'];
- $attackTroops['total_dead_consumption'] += $deadNum * $tProp['single_consumption'];
- $attackTroops['total_live_number'] -= $deadNum;
- $attackTroops['total_spy_live_number'] -= $deadNum;
- if ($attackTroops['total_live_number'] <= 0) {
- $warResult['all_attack_killed'] = TRUE;
- }
- if ($attackTroops['total_spy_live_number'] <= 0) {
- $warResult['all_spy_killed'] = TRUE;
- }
- $attackTroops['troops'][$tid]['live_number'] -= $deadNum;
- if ($curPower <= 0) {
- break;
- }
- }
- }
- foreach ($defenseTroops as $vid => $troopsTable) {
- if ($defenseTroops[$vid]['total_power'] <= 0) {
- continue;
- }
- $curPower = floor( $totalAttackPower * $i / $divisionFactor );
- if (0 < $curPower) {
- foreach ($troopsTable['troops'] as $tid => $tProp) {
- if (( $tid == 99 || ( ( ( $tid != 103 && $tid != 4 ) && $tid != 14 ) && $tid != 23 ) )) {
- continue;
- }
- if (0 < $tProp['live_number']) {
- $warResult['defense_has_spytroops'] = TRUE;
- }
- $sPower = ($tProp['single_power'] <= 0 ? 1 : $tProp['single_power']);
- $deadNum = floor( $curPower / $sPower );
- if ($tProp['live_number'] < $deadNum) {
- $deadNum = $tProp['live_number'];
- }
- $curPower -= $deadNum * $tProp['single_power'];
- $totalAttackPower -= $deadNum * $tProp['single_power'];
- $defenseTroops[$vid]['total_power'] -= $deadNum * $tProp['single_power'];
- $defenseTroops[$vid]['total_dead_consumption'] += $deadNum * $tProp['single_consumption'];
- $defenseTroops[$vid]['total_live_number'] -= $deadNum;
- $defenseTroops[$vid]['total_spy_live_number'] -= $deadNum;
- $defenseTroops[$vid]['troops'][$tid]['live_number'] -= $deadNum;
- if ($curPower <= 0) {
- break;
- }
- }
- continue;
- }
- }
- ++$i;
- }
- $warResult['attackTroops'] = $attackTroops;
- $warResult['defenseTroops'] = $defenseTroops;
- return $warResult;
- }
- function _getwarresult($attackTroops, $defenseTroops, $totalDefensePower, $isPlunderAttack) {
- $warResult = array( 'all_attack_killed' => FALSE );
- $attackFactor = ($isPlunderAttack ? 0.75 : 1);
- $totalAttackPower = $attackTroops['total_power'] * $attackFactor;
- $divisionFactor = 10;
- $i = 1;
- while ($i <= $divisionFactor) {
- $curPower = floor( $totalDefensePower * $i / $divisionFactor );
- if (0 < $curPower) {
- foreach ($attackTroops['troops'] as $tid => $tProp) {
- if ($warResult['all_attack_killed']) {
- break;
- }
- if ($tid == 99) {
- continue;
- }
- $sPower = ($tProp['single_power'] <= 0 ? 1 : $tProp['single_power']);
- $deadNum = floor( $curPower / $sPower );
- if ($tProp['live_number'] < $deadNum) {
- $deadNum = $tProp['live_number'];
- }
- $curPower -= $deadNum * $tProp['single_power'];
- $totalDefensePower -= $deadNum * $tProp['single_power'];
- $attackTroops['total_power'] -= $deadNum * $tProp['single_power'];
- $attackTroops['total_carry_load'] -= $deadNum * $tProp['single_carry_load'];
- $attackTroops['total_dead_consumption'] += $deadNum * $tProp['single_consumption'];
- $attackTroops['total_live_number'] -= $deadNum;
- if ($attackTroops['total_live_number'] <= 0) {
- $warResult['all_attack_killed'] = TRUE;
- }
- $attackTroops['troops'][$tid]['live_number'] -= $deadNum;
- if ($curPower <= 0) {
- break;
- }
- }
- }
- foreach ($defenseTroops as $vid => $troopsTable) {
- if ($defenseTroops[$vid]['total_power'] <= 0) {
- continue;
- }
- $curPower = floor( $totalAttackPower * $i / $divisionFactor );
- if (0 < $curPower) {
- foreach ($troopsTable['troops'] as $tid => $tProp) {
- if ($tid == 99) {
- continue;
- }
- $sPower = ($tProp['single_power'] <= 0 ? 1 : $tProp['single_power']);
- $deadNum = floor( $curPower / $sPower );
- if ($tProp['live_number'] < $deadNum) {
- $deadNum = $tProp['live_number'];
- }
- $curPower -= $deadNum * $tProp['single_power'];
- $totalAttackPower -= $deadNum * $tProp['single_power'];
- $defenseTroops[$vid]['total_power'] -= $deadNum * $tProp['single_power'];
- $defenseTroops[$vid]['total_dead_consumption'] += $deadNum * $tProp['single_consumption'];
- $defenseTroops[$vid]['total_live_number'] -= $deadNum;
- $defenseTroops[$vid]['troops'][$tid]['live_number'] -= $deadNum;
- if ($curPower <= 0) {
- break;
- }
- }
- continue;
- }
- }
- ++$i;
- }
- $warResult['attackTroops'] = $attackTroops;
- $warResult['defenseTroops'] = $defenseTroops;
- return $warResult;
- }
- function _gettroopwithpower($troops, $troopsPower, $isAttacking, $heroLevel, $peopleCount, $wringerPower = 0, $wallPower = 0, $heroId = -1, $spyAction = FALSE) {
- global $GameMetadata;
- $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 );
- $powerFactor = (!$spyAction ? floor( $peopleCount / 250 ) + $wringerPower + $heroLevel : 0);
- foreach ($troops as $tid => $tnum) {
- $tpower = ($isAttacking ? $GameMetadata['troops'][$tid]['attack_value'] : $GameMetadata['troops'][$tid]['defense_infantry']);
- $tpower = floor( ( $tpower + (isset( $troopsPower[$tid] ) ? $troopsPower[$tid] : 0) + $powerFactor ) * ( 1 + $wallPower / 100 ) );
- if ($spyAction) {
- $tpower = (( ( ( $tid == 103 || $tid == 4 ) || $tid == 14 ) || $tid == 23 ) ? 1 : 0);
- }
- $num = (0 < intval( $tnum ) ? intval( $tnum ) : 0);
- if ($tid != 99) {
- $returnTroops['total_live_number'] += $num;
- $returnTroops['total_power'] += $num * $tpower;
- $returnTroops['total_carry_load'] += $num * $GameMetadata['troops'][$tid]['carry_load'];
- if ($spyAction) {
- if (( ( ( $tid == 103 || $tid == 4 ) || $tid == 14 ) || $tid == 23 )) {
- $returnTroops['total_spy_live_number'] += $num;
- }
- }
- }
- $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 );
- }
- return $returnTroops;
- }
- function _getattacktroopsforvillage($troopsTrainingStr, $troopsArray, $heroLevel, $peopleCount, $wringerLevel, $spyAction) {
- $troopsPower = array( );
- if (!$spyAction) {
- $_c = 0;
- $troopsTrainingStr = trim( $troopsTrainingStr );
- if ($troopsTrainingStr != '') {
- $_arr = explode( ',', $troopsTrainingStr );
- foreach ($_arr as $troopStr) {
- ++$_c;
- list( $troopId, $researches_done, $defense_level, $attack_level ) = explode( ' ', $troopStr );
- if (( $troopId != 99 && $_c <= 8 )) {
- $troopsPower[$troopId] = $attack_level;
- continue;
- }
- }
- }
- }
- return $this->_getTroopWithPower( $troopsArray, $troopsPower, TRUE, $heroLevel, $peopleCount, $wringerLevel, 0, 0 - 1, $spyAction );
- }
- function _getdefensetroopsforvillage($vid, $troopsArray, $hasHero, $peopleCount, $wallPower, $spyAction) {
- $vrow = $this->provider->fetchRow( 'SELECT v.player_id, v.troops_training FROM p_villages v WHERE v.id=%s', array( $vid ) );
- $heroLevel = 0;
- $heroId = 0 - 1;
- $troopsPower = array( );
- if ($vrow != NULL) {
- if ($hasHero) {
- $_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'] ) ) );
- if ($_row != NULL) {
- $heroLevel = intval( $_row['hero_level'] );
- $heroId = intval( $_row['hero_troop_id'] );
- }
- }
- if (!$spyAction) {
- $_c = 0;
- $troopsTrainingStr = trim( $vrow['troops_training'] );
- if ($troopsTrainingStr != '') {
- $_arr = explode( ',', $troopsTrainingStr );
- foreach ($_arr as $troopStr) {
- ++$_c;
- list( $troopId, $researches_done, $defense_level, $attack_level ) = explode( ' ', $troopStr );
- if (( $troopId != 99 && $_c <= 8 )) {
- $troopsPower[$troopId] = $defense_level;
- continue;
- }
- }
- }
- }
- }
- return $this->_getTroopWithPower( $troopsArray, $troopsPower, FALSE, $heroLevel, $peopleCount, 0, $wallPower, $heroId, $spyAction );
- }
- function _addtroopstovillage($toVillageRow, $fromVillageRow, $addTroopsArray, $affectCropConsumption) {
- $troopsCropConsume = ($affectCropConsumption ? $addTroopsArray['cropConsumption'] : 0);
- $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'] ) ));
- if (!$toVillageRow['is_oasis']) {
- $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'] ) );
- } else {
- $this->provider->executeQuery( 'UPDATE p_villages v SET v.crop_consumption=v.crop_consumption+%s WHERE v.id=%s', array( $troopsCropConsume, $toVillageRow['parent_id'] ) );
- $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_num=\'%s\' WHERE v.id=%s', array( $t, $toVillageRow['id'] ) );
- }
- $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'] ) ));
- $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'] ) );
- }
- function _getnewtroops($troopsStr, $addTroopsArray, $fromVillageId, $isSamePlayer) {
- $newTroopsStr = '';
- $troopsStr = trim( $troopsStr );
- if ($troopsStr == '') {
- foreach ($addTroopsArray['troops'] as $tid => $tnum) {
- if ($newTroopsStr != '') {
- $newTroopsStr .= ',';
- }
- $newTroopsStr .= $tid . ' ' . $tnum;
- }
- if (( ( $addTroopsArray['hasHero'] && 0 - 1 < $fromVillageId ) && !$isSamePlayer )) {
- if ($newTroopsStr != '') {
- $newTroopsStr .= ',';
- }
- $newTroopsStr .= $addTroopsArray['heroTroopId'] . ' -1';
- }
- $newTroopsStr = $fromVillageId . ':' . $newTroopsStr;
- } else {
- $hasTroopsIn = FALSE;
- $troopsStrArr = explode( '|', $troopsStr );
- foreach ($troopsStrArr as $tvStr) {
- if ($newTroopsStr != '') {
- $newTroopsStr .= '|';
- }
- list( $vid, $vtroopsStr ) = explode( ':', $tvStr );
- if ($vid == $fromVillageId) {
- $hasTroopsIn = TRUE;
- $curTroopsStr = explode( ',', $vtroopsStr );
- $curTroops = array( );
- foreach ($curTroopsStr as $curTroopsStrItem) {
- list( $_tid, $_tnum ) = explode( ' ', $curTroopsStrItem );
- if ($_tnum == 0 - 1) {
- $curTroops[0 - 1] = $_tid;
- continue;
- }
- $curTroops[$_tid] = $_tnum;
- }
- $newtvStr = '';
- foreach ($addTroopsArray['troops'] as $tid => $tnum) {
- if ($newtvStr != '') {
- $newtvStr .= ',';
- }
- if (isset( $curTroops[$tid] )) {
- $tnum += $curTroops[$tid];
- }
- $newtvStr .= $tid . ' ' . $tnum;
- }
- if (isset( $curTroops[0 - 1] )) {
- if ($newtvStr != '') {
- $newtvStr .= ',';
- }
- $newtvStr .= $curTroops[0 - 1] . ' -1';
- } else {
- if (( 0 - 1 < $fromVillageId && !$isSamePlayer )) {
- if ($addTroopsArray['hasHero']) {
- if ($newtvStr != '') {
- $newtvStr .= ',';
- }
- $newtvStr .= $addTroopsArray['heroTroopId'] . ' -1';
- }
- }
- }
- $newTroopsStr .= $vid . ':' . $newtvStr;
- continue;
- }
- $newTroopsStr .= $tvStr;
- }
- if (!$hasTroopsIn) {
- $newTroopsStr = '';
- foreach ($addTroopsArray['troops'] as $tid => $tnum) {
- if ($newTroopsStr != '') {
- $newTroopsStr .= ',';
- }
- $newTroopsStr .= $tid . ' ' . $tnum;
- }
- if (( ( $addTroopsArray['hasHero'] && 0 - 1 < $fromVillageId ) && !$isSamePlayer )) {
- if ($newTroopsStr != '') {
- $newTroopsStr .= ',';
- }
- $newTroopsStr .= $addTroopsArray['heroTroopId'] . ' -1';
- }
- $newTroopsStr = $fromVillageId . ':' . $newTroopsStr;
- if ($troopsStr != '') {
- $newTroopsStr = $troopsStr . '|' . $newTroopsStr;
- }
- }
- }
- return $newTroopsStr;
- }
- function _harvesttroopsfrom($villageRow, $maxCarryLoad, $crannyTotalSize) {
- if (( $maxCarryLoad <= 0 || $villageRow['is_oasis'] )) {
- return array( 'string' => '0 0 0 0', 'sum' => 0 );
- }
- $resources = array( );
- $r_arr = explode( ',', $villageRow['resources'] );
- foreach ($r_arr as $r_str) {
- $r2 = explode( ' ', $r_str );
- $prate = floor( $r2[4] * ( 1 + $r2[5] / 100 ) ) - ($r2[0] == 4 ? $villageRow['crop_consumption'] : 0);
- $current_value = floor( $r2[1] + $villageRow['elapsedTimeInSeconds'] * ( $prate / 3600 ) );
- if ($r2[2] < $current_value) {
- $current_value = $r2[2];
- }
- $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] );
- }
- $divFactor = 4;
- $harvest = array( 0, 0, 0, 0 );
- $sum = 0;
- while (0 < $maxCarryLoad) {
- $curTotalRes = 0;
- $m = 0;
- foreach ($resources as $k => $rdata) {
- $v = $rdata['current_value'];
- $take = floor( $maxCarryLoad / $divFactor );
- if (0 < $v) {
- if ($v < $take) {
- $take = $v;
- }
- $maxCarryLoad -= $take;
- $resources[$k]['current_value'] -= $take;
- $harvest[$m] += $take;
- $sum += $take;
- $curTotalRes += $resources[$k]['current_value'];
- }
- ++$m;
- }
- if (( $curTotalRes <= 0 && $divFactor == 1 )) {
- break;
- }
- $divFactor = 1;
- }
- $resourcesStr = '';
- foreach ($resources as $k => $v) {
- if ($resourcesStr != '') {
- $resourcesStr .= ',';
- }
- $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'] );
- }
- $elapsedTimeInSeconds = $villageRow['elapsedTimeInSeconds'];
- list( $cpValue, $cpRate ) = explode( ' ', $villageRow['cp'] );
- $cpValue = floor( $cpValue + $elapsedTimeInSeconds * ( $cpRate / 86400 ) );
- $cp = $cpValue . ' ' . $cpRate;
- $this->provider->executeQuery( 'UPDATE p_villages v
- SET
- v.resources=\'%s\',
- v.cp=\'%s\',
- v.last_update_date=NOW()
- WHERE
- v.id=%s', array( $resourcesStr, $cp, $villageRow['id'] ) );
- return array( 'string' => implode( ' ', $harvest ), 'sum' => $sum );
- }
- function _updatevillageouttroops($vid, $invid, $newTroopsStr, $heroKilled, $thisInforcementDied, $uid) {
- $pid = $uid;
- if (0 - 1 < $vid) {
- $row = $this->provider->fetchRow( 'SELECT v.player_id, v.troops_out_num FROM p_villages v WHERE v.id=%s', array( $vid ) );
- if ($row == NULL) {
- return null;
- }
- $pid = $row['player_id'];
- $troops_out_num = '';
- $ts = trim( $row['troops_out_num'] );
- if ($ts != '') {
- $tsArr = explode( '|', $ts );
- foreach ($tsArr as $tsArrStr) {
- list( $_vid, $_troops ) = explode( ':', $tsArrStr );
- if ($_vid == $invid) {
- if (!$thisInforcementDied) {
- if ($troops_out_num != '') {
- $troops_out_num .= '|';
- }
- $troops_out_num .= $invid . ':' . $newTroopsStr;
- continue;
- }
- continue;
- }
- if ($troops_out_num != '') {
- $troops_out_num .= '|';
- }
- $troops_out_num .= $tsArrStr;
- }
- }
- $this->provider->executeQuery( 'UPDATE p_villages v SET v.troops_out_num=\'%s\' WHERE v.id=%s', array( $troops_out_num, $vid ) );
- }
- if ($heroKilled) {
- $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 ) ) );
- }
- }
- function _updatevillage($villageRow, $reduceCropConsumption, $heroKilled) {
- $elapsedTimeInSeconds = $villageRow['elapsedTimeInSeconds'];
- $resources = array( );
- $r_arr = explode( ',', $villageRow['resources'] );
- foreach ($r_arr as $r_str) {
- $r2 = explode( ' ', $r_str );
- $prate = floor( $r2[4] * ( 1 + $r2[5] / 100 ) ) - ($r2[0] == 4 ? $villageRow['crop_consumption'] : 0);
- $current_value = floor( $r2[1] + $elapsedTimeInSeconds * ( $prate / 3600 ) );
- if ($r2[2] < $current_value) {
- $current_value = $r2[2];
- }
- $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 );
- }
- list( $cpValue, $cpRate ) = explode( ' ', $villageRow['cp'] );
- $cpValue = floor( $cpValue + $elapsedTimeInSeconds * ( $cpRate / 86400 ) );
- $resourcesStr = '';
- foreach ($resources as $k => $v) {
- if ($resourcesStr != '') {
- $resourcesStr .= ',';
- }
- $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'] );
- }
- $cp = $cpValue . ' ' . $cpRate;
- $this->provider->executeQuery( 'UPDATE p_villages v
- SET
- v.resources=\'%s\',
- v.cp=\'%s\',
- v.crop_consumption=v.crop_consumption-%s,
- v.last_update_date=NOW()
- WHERE
- v.id=%s', array( $resourcesStr, $cp, $reduceCropConsumption, $villageRow['id'] ) );
- if ($heroKilled) {
- $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'] ) ) );
- }
- }
- function _capturevillage() {
- }
- function _captureoasis() {
- }
- function _getvillageinfo($villageId) {
- return $this->provider->fetchRow( 'SELECT
- v.id, v.parent_id, v.tribe_id,
- v.rel_x, v.rel_y, v.crop_consumption,
- v.player_id, v.alliance_id, v.village_oases_id,
- v.player_name, v.alliance_name,
- v.is_capital, v.is_oasis, v.people_count,
- v.resources, v.buildings, v.cp,
- v.troops_training,
- v.troops_num, v.troops_out_num, v.troops_intrap_num, v.troops_out_intrap_num,
- v.allegiance_percent,
- v.child_villages_id,
- TIME_TO_SEC(TIMEDIFF(NOW(), v.last_update_date)) elapsedTimeInSeconds,
- TIME_TO_SEC(TIMEDIFF(NOW(), v.creation_date)) oasisElapsedTimeInSeconds
- FROM p_villages v
- WHERE v.id=%s', array( $villageId ) );
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement