refactor: last of foreach loops optimized

#313
This commit is contained in:
Martin Ambrus
2017-11-19 14:32:00 +01:00
parent 67d9a42506
commit 2a68b07704
4 changed files with 76 additions and 58 deletions
+11 -8
View File
@@ -928,6 +928,8 @@ class Building {
$buildcount = ($this->buildArray ? count($this->buildArray) : 0);
// will be true if the job was successfully finished
$jobFinishSuccess = false;
// IDs of successful jobs to delete
$deletIDs = [];
foreach ($this->buildArray as $jobs) {
if ($jobs['wid']==$village->wid) {
@@ -959,18 +961,16 @@ class Building {
// we need to subtract resources for this, if another 2 jobs are active,
// as we'd never subtract those otherwise
if ( $buildcount > 2 ) {
$database->setVillageField( $jobs['wid'], 'wood', ( $villwood - $buildwood ) );
$database->setVillageField( $jobs['wid'], 'clay', ( $villclay - $buildclay ) );
$database->setVillageField( $jobs['wid'], 'iron', ( $villiron - $buildiron ) );
$database->setVillageField( $jobs['wid'], 'crop', ( $villcrop - $buildcrop ) );
$database->setVillageField( $jobs['wid'],
['wood', 'clay', 'iron', 'crop'],
[( $villwood - $buildwood ), ( $villclay - $buildclay ), ( $villiron - $buildiron ), ( $villcrop - $buildcrop )]);
}
}
} else {
// if we only have 2 gold, we need to cancel this job, as there will never
// be enough gold now in our account to finish this up
$exclude_master = true;
$q = "DELETE FROM " . TB_PREFIX . "bdata WHERE id = " . (int) $jobs['id'];
$database->query( $q );
$deletIDs[] = (int) $jobs['id'];
}
}
} else {
@@ -987,8 +987,7 @@ class Building {
if (!isset($exclude_master) && $database->query($q) && ($enought_res == 1 or $jobs['master'] == 0)) {
$database->modifyPop($jobs['wid'],$resource['pop'],0);
$database->addCP($jobs['wid'],$resource['cp']);
$q = "DELETE FROM ".TB_PREFIX."bdata where id = ".(int) $jobs['id'];
$database->query($q);
$deletIDs[] = (int) $jobs['id'];
if($jobs['type'] == 18) {
$owner = $database->getVillageField($jobs['wid'],"owner");
$max = $bid18[$level]['attri'];
@@ -1008,6 +1007,10 @@ class Building {
$jobFinishSuccess = false;
}
if (count($deletIDs)) {
$database->query("DELETE FROM " . TB_PREFIX . "bdata WHERE id IN(" . implode(', ', $deletIDs) . ")");
}
$demolition = $database->finishDemolition($village->wid);
$tech = $technology->finishTech();
if ($demolition > 0 || $tech > 0) {
+18 -7
View File
@@ -2427,18 +2427,26 @@ class MYSQLi_DB implements IDbConnection {
$result = mysqli_query($this->dblink,$q2);
if (!empty($result)) {
$array=$this->mysqli_fetch_all($result);
$toDelete = [];
foreach($array as $ss) {
$this->DeleteSurvey($ss['id']);
$toDelete[] = $ss['id'];
}
$this->DeleteSurvey($toDelete);
}
mysqli_query($this->dblink,$qs);
return mysqli_query($this->dblink,$q);
}
function DeleteSurvey($id) {
list($id) = $this->escape_input($id);
if (!is_array($id)) {
$id = [$id];
}
$qs = "DELETE from " . TB_PREFIX . "forum_survey where topic = '$id'";
foreach ($id as $index => $idValue) {
$id[$index] = (int) $idValue;
}
$qs = "DELETE from " . TB_PREFIX . "forum_survey where topic IN(".implode(', ', $id).")";
return mysqli_query($this->dblink,$qs);
}
@@ -4306,9 +4314,10 @@ class MYSQLi_DB implements IDbConnection {
// already checks if there are no more people other than the owner
// present before the demolition is allowed.
if ($demolition) {
$evicts = [];
foreach ($members as $member) {
// evict the player from the alliance
mysqli_query($this->dblink, 'UPDATE '.TB_PREFIX.'users SET alliance = 0 WHERE id = '.$member['id']);
$evicts[] = $member['id'];
// notify them via in-game messaging
$this->sendMessage(
@@ -4329,6 +4338,8 @@ class MYSQLi_DB implements IDbConnection {
0,
true);
}
mysqli_query($this->dblink, 'UPDATE '.TB_PREFIX.'users SET alliance = 0 WHERE id IN('.implode(',', $evicts).")");
} else {
// we come from a battle result, therefore we need to check
// for the first player in the alliance who has a sufficient
@@ -6517,9 +6528,9 @@ class MYSQLi_DB implements IDbConnection {
$result = $this->query_return($q);
$attack = 0;
foreach($result as $general) {
if(date("j. M",$time) == date("j. M",$general['time'])){
$attack += 1;
}
if(date("j. M",$time) == date("j. M",$general['time'])){
$attack += 1;
}
}
return $attack;
}
+32 -32
View File
@@ -251,38 +251,38 @@ class Village {
$crop = $clay = $wood = $iron = 0;
if (!empty($this->oasisowned)) {
foreach ($this->oasisowned as $oasis) {
switch($oasis['type']) {
case 1:
case 2:
$wood += 1;
break;
case 3:
$wood += 1;
$crop += 1;
break;
case 4:
case 5:
$clay += 1;
break;
case 6:
$clay += 1;
$crop += 1;
break;
case 7:
case 8:
$iron += 1;
break;
case 9:
$iron += 1;
$crop += 1;
break;
case 10:
case 11:
$crop += 1;
break;
case 12:
$crop += 2;
break;
switch ( $oasis['type'] ) {
case 1:
case 2:
$wood += 1;
break;
case 3:
$wood += 1;
$crop += 1;
break;
case 4:
case 5:
$clay += 1;
break;
case 6:
$clay += 1;
$crop += 1;
break;
case 7:
case 8:
$iron += 1;
break;
case 9:
$iron += 1;
$crop += 1;
break;
case 10:
case 11:
$crop += 1;
break;
case 12:
$crop += 2;
break;
}
}
}
+15 -11
View File
@@ -80,18 +80,22 @@
$abdata = $database->getABTech($getFLData['wref']);
$reference = $database->addAttack(($getFLData['wref']),$data['u1'],$data['u2'],$data['u3'],$data['u4'],$data['u5'],$data['u6'],$data['u7'],$data['u8'],$data['u9'],$data['u10'],$data['u11'],$data['type'],$ctar1,$ctar2,0,$abdata['b1'],$abdata['b2'],$abdata['b3'],$abdata['b4'],$abdata['b5'],$abdata['b6'],$abdata['b7'],$abdata['b8']);
$totalunits = $data['u1']+$data['u2']+$data['u3']+$data['u4']+$data['u5']+$data['u6']+$data['u7']+$data['u8']+$data['u9']+$data['u10']+$data['u11'];
$database->modifyUnit($getFLData['wref'], array($uname2.'1'), array($data['u1']), array(0));
$database->modifyUnit($getFLData['wref'], array($uname2.'2'), array($data['u2']), array(0));
$database->modifyUnit($getFLData['wref'], array($uname2.'3'), array($data['u3']), array(0));
$database->modifyUnit($getFLData['wref'], array($uname2.'4'), array($data['u4']), array(0));
$database->modifyUnit($getFLData['wref'], array($uname2.'5'), array($data['u5']), array(0));
$database->modifyUnit($getFLData['wref'], array($uname2.'6'), array($data['u6']), array(0));
$database->modifyUnit($getFLData['wref'], array($uname2.'7'), array($data['u7']), array(0));
$database->modifyUnit($getFLData['wref'], array($uname2.'8'), array($data['u8']), array(0));
$database->modifyUnit($getFLData['wref'], array($uname2.'9'), array($data['u9']), array(0));
$database->modifyUnit($getFLData['wref'], array($uname2.'10'), array($data['u10']), array(0));
$database->modifyUnit($getFLData['wref'], array('hero'), array($data['u11']), array(0));
$units = [];
$amounts = [];
$modes = [];
for ($u = 1; $u <= 10; $u++) {
$units[] = $uname2.$u;
$amounts[] = $data['u'.$u];
$modes[] = 0;
}
$units[] = 'hero';
$amounts[] = $data['u11'];
$modes[] = 0;
$database->modifyUnit($getFLData['wref'], $uname2, $amounts, $modes);
$database->addMovement(3,$getFLData['wref'],$data['to_vid'],$reference,time(),($time+time()));
}
}