Skip to content

Commit

Permalink
fix: defender troops do not die during attack
Browse files Browse the repository at this point in the history
  • Loading branch information
martinambrus committed Nov 18, 2017
1 parent dd03085 commit fffedc4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 40 deletions.
69 changes: 44 additions & 25 deletions GameEngine/Automation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ private function resolveCatapultsDestruction(&$bdo, &$battlepart, &$info_cat, &$

// embassy level was changed
if ($tbgid==18){
$info_cat .= $database->checkEmbassiesAfterBattle($data['to']);
$info_cat .= $database->checkEmbassiesAfterBattle($data['to'], false);
}

// oasis cannot be destroyed
Expand Down Expand Up @@ -1110,7 +1110,7 @@ private function resolveCatapultsDestruction(&$bdo, &$battlepart, &$info_cat, &$

// embassy level was changed
if ( $tbgid == 18 ) {
$info_cat .= $database->checkEmbassiesAfterBattle( $data['to'] );
$info_cat .= $database->checkEmbassiesAfterBattle( $data['to'], false );
}

// no need to recalculate population of oasis, as there is none
Expand Down Expand Up @@ -1241,7 +1241,7 @@ private function sendunitsComplete() {
$enforDefender = array();
$rom = $ger = $gal = $nat = $natar = 0;
$Defender = $database->getUnit($data['to'], false);
$enforcementarray = $database->getEnforceVillage($data['to'],0);
$enforcementarray = $database->getEnforceVillage($data['to'], 0, false);
if(count($enforcementarray) > 0) {

foreach($enforcementarray as $enforce) {
Expand All @@ -1261,7 +1261,7 @@ private function sendunitsComplete() {
for($i=1;$i<=50;$i++){
$def_ab[$i]=0;
if(!isset($Defender['u'.$i])){
$Defender['u'.$i] = '0';
$Defender['u'.$i] = 0;
} else {
if($Defender['u'.$i]=='' || $Defender['u'.$i] <= 0){
$Defender['u'.$i] = 0;
Expand Down Expand Up @@ -1310,15 +1310,15 @@ private function sendunitsComplete() {
$Attacker['uhero'] = $dataarray[$data_num]['t11'];
$hero_pic = "hero";
//need to set these variables.
$def_wall = $database->getFieldLevel($data['to'],40);
$def_wall = $database->getFieldLevel($data['to'], 40, false);
$att_tribe = $owntribe;
$def_tribe = $targettribe;
$residence = "0";
$attpop = $fromF['pop'];
$defpop = $toF['pop'];
$def_ab=array();
//get level of palace or residence
$residence = $database->getFieldLevelInVillage($data['to'], '25, 26');
$residence = $database->getFieldLevelInVillage($data['to'], '25, 26', false);

//type of attack
if($dataarray[$data_num]['attack_type'] == 1){
Expand Down Expand Up @@ -1358,7 +1358,7 @@ private function sendunitsComplete() {
//TODO: where did dead7 & traped7 come from??/
if (($data['t7']/*-$dead7-$traped7*/)>0 and $type=='3') {
$basearraywall = $to;
if (($walllevel = $database->getFieldLevel($basearraywall['wref'],40)) > 0){
if (($walllevel = $database->getFieldLevel($basearraywall['wref'],40, false)) > 0){
$wallgid = $database->getFieldLevel($basearraywall['wref'],"40t");
$wallid = 40;
$w='4';
Expand Down Expand Up @@ -1386,8 +1386,8 @@ private function sendunitsComplete() {
//get defence units
$enforDefender = array();
$rom = $ger = $gal = $nat = $natar = 0;
$Defender = $database->getUnit($data['to']);
$enforcementarray = $database->getEnforceVillage($data['to'],0);
$Defender = $database->getUnit($data['to'], false);
$enforcementarray = $database->getEnforceVillage($data['to'],0, false);

if(count($enforcementarray) > 0) {
foreach($enforcementarray as $enforce) {
Expand Down Expand Up @@ -1480,8 +1480,13 @@ private function sendunitsComplete() {
$stonemason = 0;
}

$varray = $database->getProfileVillages($to['owner']);
$varray1 = $database->getProfileVillages($from['owner']);
$varray = $database->getProfileVillages($to['owner'], false);

if ($to['owner'] == $from['owner']) {
$varray1 = $varray;
} else {
$varray1 = $database->getProfileVillages($from['owner'], false);
}

//fix by ronix
for ($i=1;$i<=50;$i++) {
Expand All @@ -1491,6 +1496,7 @@ private function sendunitsComplete() {

$enforDefender['u'.$i] += (isset($Defender['u'.$i]) ? $Defender['u'.$i] : 0);
}

$defspy=($enforDefender['u4']>0 || $enforDefender['u14']>0 || $enforDefender['u23']>0 || $enforDefender['u44']>0)? true:false;

if(PEACE == 0 || $targettribe == 4 || $targettribe == 5){
Expand All @@ -1503,6 +1509,7 @@ private function sendunitsComplete() {
}elseif($targettribe == 5){
$def_spy = $enforDefender['u54'];
}

//impossible to attack or scout NATAR Capital Village
if ($NatarCapital) {
for($i=1;$i<=11;$i++){
Expand Down Expand Up @@ -1534,7 +1541,7 @@ private function sendunitsComplete() {
}
$Attacker['uhero'] -= $traped11;
if($totaltraped_att > 0){
$prisoners2 = $database->getPrisoners2($data['to'],$data['from']);
$prisoners2 = $database->getPrisoners2($data['to'],$data['from'], false);
if(empty($prisoners2)){
$database->addPrisoners($data['to'],$data['from'],$traped1,$traped2,$traped3,$traped4,$traped5,$traped6,$traped7,$traped8,$traped9,$traped10,$traped11);
}else{
Expand Down Expand Up @@ -1613,7 +1620,7 @@ private function sendunitsComplete() {
for($i=1;$i<=11;$i++){
//MUST TO BE FIX : This is only for defender and still not properly coded
if (isset($battlepart['casualties_attacker']) && isset($battlepart['casualties_attacker'][$i]) && $battlepart['casualties_attacker'][$i] <= 0) {
${'dead'.$i} = 0;
${'dead'.$i} = 0;
} else if (isset($data['t'.$i]) && isset($battlepart['casualties_attacker']) && isset($battlepart['casualties_attacker'][$i]) && $battlepart['casualties_attacker'][$i] > $data['t'.$i]) {
${'dead'.$i}=$data['t'.$i];
} else {
Expand Down Expand Up @@ -1650,7 +1657,22 @@ private function sendunitsComplete() {
$start = ($targettribe-1)*10+1;
$end = ($targettribe*10);

if($targettribe == 1){ $u = ""; $rom='1'; } else if($targettribe == 2){ $u = "1"; $ger='1'; } else if($targettribe == 3){$u = "2"; $gal='1'; }else if($targettribe == 4){ $u = "3"; $nat='1'; } else { $u = "4"; $natar='1'; } //FIX
if ( $targettribe == 1 ) {
$u = "";
$rom = '1';
} else if ( $targettribe == 2 ) {
$u = "1";
$ger = '1';
} else if ( $targettribe == 3 ) {
$u = "2";
$gal = '1';
} else if ( $targettribe == 4 ) {
$u = "3";
$nat = '1';
} else {
$u = "4";
$natar = '1';
} //FIX

$unitModifications_units = [];
$unitModifications_amounts = [];
Expand All @@ -1661,7 +1683,7 @@ private function sendunitsComplete() {
}

if($unitlist){
$owndead[$i] = round($battlepart[2] * (isset($unitlist[0]) ? $unitlist[0]['u'.$i] : 0));
$owndead[$i] = round($battlepart[2] * $unitlist['u'.$i]);
$unitModifications_units[] = $i;
$unitModifications_amounts[] = $owndead[$i];
$unitModifications_modes[] = 0;
Expand Down Expand Up @@ -2071,7 +2093,7 @@ private function sendunitsComplete() {
}
}
if ($herosend_att>0){
$hero_unit = $database->getHeroField($from['owner'], 'unit');
$hero_unit = $database->getHeroField($from['owner'], 'unit', false);
$speeds[] = $GLOBALS['u'.$hero_unit]['speed'];
}

Expand Down Expand Up @@ -2251,6 +2273,9 @@ private function sendunitsComplete() {
if (!$catapults2WillNotShoot) {
$this->resolveCatapultsDestruction($bdo, $battlepart, $info_cat, $data, $catapultTarget2, true, true, $catp_pic, $can_destroy, $isoasis, $village_destroyed);
}

// clear resource levels cache, since we might have destroyed buildings/fields by now
$database->clearResourseLevelsCache();
}
}
} elseif (($data['t7']-$traped7)>0) {
Expand Down Expand Up @@ -2486,7 +2511,7 @@ private function sendunitsComplete() {

if ($isoasis != 0) { //oasis fix by ronix
if ($to['owner']!=$from['owner']) {
$troopcount = $database->countOasisTroops($data['to']);
$troopcount = $database->countOasisTroops($data['to'], false);
$canqured=$database->canConquerOasis($data['from'],$data['to']);
if ($canqured==1 && $troopcount==0) {
$database->conquerOasis($data['from'],$data['to']);
Expand Down Expand Up @@ -2678,10 +2703,7 @@ private function sendunitsComplete() {
}

if ($prisoner['t11']>0){
$p_qh = "SELECT unit FROM ".TB_PREFIX."hero WHERE uid = ".(int) $p_owner." AND dead = 0";
$p_resulth = $database->query($p_qh);
$p_hero_f=mysqli_fetch_array($p_resulth);
$p_hero_unit=$p_hero_f['unit'];
$p_hero_unit = $database->getHeroField($p_owner, 'unit', false)['unit'];
$p_speeds[] = $GLOBALS['u'.$p_hero_unit]['speed'];
}

Expand Down Expand Up @@ -2862,10 +2884,7 @@ private function sendunitsComplete() {
}
}
if ($herosend_att>0){
$qh = "SELECT unit FROM ".TB_PREFIX."hero WHERE uid = ".(int) $from['owner']." AND dead = 0";
$resulth = mysqli_query($GLOBALS['link'],$qh);
$hero_f=mysqli_fetch_array($resulth);
$hero_unit=$hero_f['unit'];
$hero_unit = $database->getHeroField($from['owner'], 'unit', false)['unit'];
$speeds[] = $GLOBALS['u'.$hero_unit]['speed'];
}
$artefact = count($database->getOwnUniqueArtefactInfo2($from['owner'],2,3,0));
Expand Down
5 changes: 4 additions & 1 deletion GameEngine/Battle.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ function calculateBattle($Attacker,$Defender,$def_wall,$att_tribe,$def_tribe,$re
foreach($DefendersAll as $defenders) {
for ($i=1;$i<=50;$i++) {$def_ab[$i]=0;}
$fromvillage = $defenders['from'];
$enforcetribe = $database->getUserArray($database->getVillageField($fromvillage,"owner"), 1)["tribe"];

$userdataCache[$fromvillage] = $database->getUserArray($database->getVillageField($fromvillage,"owner"), 1);

$enforcetribe = $userdataCache[$fromvillage]["tribe"];
$ud=($enforcetribe-1)*10;
if($defenders['from']>0) { //don't check nature tribe
$armory = $database->getABTech($defenders['from']); // Armory level every village enforcement
Expand Down
38 changes: 25 additions & 13 deletions GameEngine/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ private static function returnCachedContent(&$arrayVariable, $arrayStructure) {
$arrayVariable[$arrayStructure] = [];
}

if (isset($arrayVariable[$arrayStructure]) && !is_null($arrayVariable[$arrayStructure])) {
if (isset($arrayVariable[$arrayStructure]) && count($arrayVariable[$arrayStructure])) {
return $arrayVariable[$arrayStructure];
} else {
return null;
Expand Down Expand Up @@ -1789,7 +1789,7 @@ function getVillageFields($ref, $fields, $use_cache = true) {
function getOasisField($ref, $field, $use_cache = true) {
// return all data, don't waste time by selecting fields one by one
$oasisArray = $this->getOasisV($ref, $use_cache);
$result = (isset($oasisArray[$field]) ? $oasisArray[$field] : null);
return (isset($oasisArray[$field]) ? $oasisArray[$field] : null);

/*list($ref, $field) = $this->escape_input((int) $ref, $field);
Expand Down Expand Up @@ -1817,7 +1817,13 @@ function setVillageField($ref, $field, $value) {

$pairs = [];
foreach ($field as $index => $fieldValue) {
$pairs[] = $this->escape($fieldValue).' = '.((Math::isInt($value[$index]) || Math::isFloat($value[$index])) ? $value[$index] : '"'.$this->escape($value[$index]).'"');
$newValue = ((Math::isInt($value[$index]) || Math::isFloat($value[$index])) ? $value[$index] : '"'.$this->escape($value[$index]).'"');
$pairs[] = $this->escape($fieldValue).' = '.$newValue;

// update cache
if (isset(self::$villageFieldsCache[$ref])) {
self::$villageFieldsCache[$ref][$fieldValue] = $newValue;
}
}

$q = "UPDATE " . TB_PREFIX . "vdata SET ".implode(', ', $pairs)." WHERE wref = ".(int) $ref;
Expand Down Expand Up @@ -1872,6 +1878,11 @@ function getResourceLevel($vid, $use_cache = true) {
return self::$resourceLevelsCache[$vid];
}

public function clearResourseLevelsCache() {
self::$resourceLevelsCache = [];
self::$fieldLevelsInVillageSearchCache = [];
}

function getAdminLog() {
$q = "SELECT id,user,log,time from " . TB_PREFIX . "admin_log where id != 0 ORDER BY id DESC";
$result = mysqli_query($this->dblink,$q);
Expand Down Expand Up @@ -4216,12 +4227,12 @@ public function getAllianceCapacity($embassyLevel) {
* and 0 when the player was evicted from
* the alliance due to Embassy damage.
*/
public function checkAllianceEmbassiesStatus($userData, $demolition = false) {
public function checkAllianceEmbassiesStatus($userData, $demolition = false, $use_cache = true) {
// TODO: refactor this and break it into more smaler methods
global $session;

// check whether this player is an alliance owner
$isOwner = ($userData['alliance'] && $this->isAllianceOwner($userData['id']) == $userData['alliance']);
$isOwner = ($userData['alliance'] && $this->isAllianceOwner($userData['id'], $use_cache) == $userData['alliance']);

// for demolition, the Embassy was already destroyed, so we need at least 1
// Embassy still standing... for battle, the Embassy is still standing
Expand All @@ -4233,7 +4244,7 @@ public function checkAllianceEmbassiesStatus($userData, $demolition = false) {
// at lvl 1+ standing somewhere
if (!$isOwner) {
// TODO: replace magic numbers by constants (18 = Embassy)
if ($this->getSingleFieldTypeCount($userData['id'], 18, '>=', 1) < $minimumExistingEmbassyRecords) {
if ($this->getSingleFieldTypeCount($userData['id'], 18, '>=', 1, $use_cache) < $minimumExistingEmbassyRecords) {

// the player has no more Embassies, evict them from the alliance
mysqli_query($this->dblink, 'UPDATE '.TB_PREFIX.'users SET alliance = 0 WHERE id = '.$userData['id']);
Expand Down Expand Up @@ -4278,7 +4289,7 @@ public function checkAllianceEmbassiesStatus($userData, $demolition = false) {
}
} else {
// the player IS an alliance owner, check if we need to take any action
$membersCount = $this->countAllianceMembers($userData['alliance']);
$membersCount = $this->countAllianceMembers($userData['alliance'], $use_cache);
$minAllianceEmbassyLevel = $this->getMinEmbassyLevel($membersCount);

// in this case, the minimum Embassy level cannot go below 3,
Expand All @@ -4294,15 +4305,15 @@ public function checkAllianceEmbassiesStatus($userData, $demolition = false) {
&&
// check for standing Embassies with sufficient level
// TODO: replace magic numbers by constants (18 = Embassy)
($this->getSingleFieldTypeCount($userData['id'], 18, '>=', $minAllianceEmbassyLevel) < $minimumExistingEmbassyRecords)
($this->getSingleFieldTypeCount($userData['id'], 18, '>=', $minAllianceEmbassyLevel, false, $use_cache) < $minimumExistingEmbassyRecords)
);

// the Embassy got damaged below a sufficient level and there are no more Embassies
// at that level standing on this player's account, additional actions are needed
if ($takeAction) {

// load all alliance members
$members = $this->getAllMember($userData['alliance']);
$members = $this->getAllMember($userData['alliance'], 0, $use_cache);

// if we come from demolition, we need to evict all new members
// that accepted an invitation while level 3 of the last
Expand Down Expand Up @@ -4410,7 +4421,7 @@ public function checkAllianceEmbassiesStatus($userData, $demolition = false) {
} else {
// let's determine whether to keep currently attacked player
// in the alliance or not
if ($userData['lvl'] > 0 || $this->getSingleFieldTypeCount($member['id'], 18, '>=', 1) >= $minimumExistingEmbassyRecords) {
if ($userData['lvl'] > 0 || $this->getSingleFieldTypeCount($member['id'], 18, '>=', 1, $use_cache) >= $minimumExistingEmbassyRecords) {
$keepCurrentPlayer = true;
} else {
$keepCurrentPlayer = false;
Expand Down Expand Up @@ -4483,7 +4494,7 @@ public function checkAllianceEmbassiesStatus($userData, $demolition = false) {
return true;
}

function checkEmbassiesAfterBattle($vid) {
function checkEmbassiesAfterBattle($vid, $use_cache = true) {
$userData = $this->getUserArray($this->getVillageField($vid,"owner"), 1);

Automation::updateMax($this->getVillageField($vid,"owner"));
Expand All @@ -4492,7 +4503,7 @@ function checkEmbassiesAfterBattle($vid) {
'alliance' => $userData["alliance"],
'username' => $userData["username"],
'lvl' => $totallvl
]);
], false, $use_cache);

if ($allianceStatus === false) {
return ' This player\'s alliance has been dispersed.';
Expand Down Expand Up @@ -5081,6 +5092,7 @@ function getUnit($vid, $use_cache = true) {
}

if (!is_array($vid)) {
$singleVillage = true;
$vid = [$vid];
}

Expand Down Expand Up @@ -5114,7 +5126,7 @@ function getUnit($vid, $use_cache = true) {
}
}

return ($vidCount > 1 ? $returnArray : reset($returnArray));
return (!isset($singleVillage) ? $returnArray : reset($returnArray));
}

// no need to cache this method
Expand Down
Loading

0 comments on commit fffedc4

Please sign in to comment.