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); $buildcount = ($this->buildArray ? count($this->buildArray) : 0);
// will be true if the job was successfully finished // will be true if the job was successfully finished
$jobFinishSuccess = false; $jobFinishSuccess = false;
// IDs of successful jobs to delete
$deletIDs = [];
foreach ($this->buildArray as $jobs) { foreach ($this->buildArray as $jobs) {
if ($jobs['wid']==$village->wid) { if ($jobs['wid']==$village->wid) {
@@ -959,18 +961,16 @@ class Building {
// we need to subtract resources for this, if another 2 jobs are active, // we need to subtract resources for this, if another 2 jobs are active,
// as we'd never subtract those otherwise // as we'd never subtract those otherwise
if ( $buildcount > 2 ) { if ( $buildcount > 2 ) {
$database->setVillageField( $jobs['wid'], 'wood', ( $villwood - $buildwood ) ); $database->setVillageField( $jobs['wid'],
$database->setVillageField( $jobs['wid'], 'clay', ( $villclay - $buildclay ) ); ['wood', 'clay', 'iron', 'crop'],
$database->setVillageField( $jobs['wid'], 'iron', ( $villiron - $buildiron ) ); [( $villwood - $buildwood ), ( $villclay - $buildclay ), ( $villiron - $buildiron ), ( $villcrop - $buildcrop )]);
$database->setVillageField( $jobs['wid'], 'crop', ( $villcrop - $buildcrop ) );
} }
} }
} else { } else {
// if we only have 2 gold, we need to cancel this job, as there will never // 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 // be enough gold now in our account to finish this up
$exclude_master = true; $exclude_master = true;
$q = "DELETE FROM " . TB_PREFIX . "bdata WHERE id = " . (int) $jobs['id']; $deletIDs[] = (int) $jobs['id'];
$database->query( $q );
} }
} }
} else { } else {
@@ -987,8 +987,7 @@ class Building {
if (!isset($exclude_master) && $database->query($q) && ($enought_res == 1 or $jobs['master'] == 0)) { if (!isset($exclude_master) && $database->query($q) && ($enought_res == 1 or $jobs['master'] == 0)) {
$database->modifyPop($jobs['wid'],$resource['pop'],0); $database->modifyPop($jobs['wid'],$resource['pop'],0);
$database->addCP($jobs['wid'],$resource['cp']); $database->addCP($jobs['wid'],$resource['cp']);
$q = "DELETE FROM ".TB_PREFIX."bdata where id = ".(int) $jobs['id']; $deletIDs[] = (int) $jobs['id'];
$database->query($q);
if($jobs['type'] == 18) { if($jobs['type'] == 18) {
$owner = $database->getVillageField($jobs['wid'],"owner"); $owner = $database->getVillageField($jobs['wid'],"owner");
$max = $bid18[$level]['attri']; $max = $bid18[$level]['attri'];
@@ -1008,6 +1007,10 @@ class Building {
$jobFinishSuccess = false; $jobFinishSuccess = false;
} }
if (count($deletIDs)) {
$database->query("DELETE FROM " . TB_PREFIX . "bdata WHERE id IN(" . implode(', ', $deletIDs) . ")");
}
$demolition = $database->finishDemolition($village->wid); $demolition = $database->finishDemolition($village->wid);
$tech = $technology->finishTech(); $tech = $technology->finishTech();
if ($demolition > 0 || $tech > 0) { if ($demolition > 0 || $tech > 0) {
+18 -7
View File
@@ -2427,18 +2427,26 @@ class MYSQLi_DB implements IDbConnection {
$result = mysqli_query($this->dblink,$q2); $result = mysqli_query($this->dblink,$q2);
if (!empty($result)) { if (!empty($result)) {
$array=$this->mysqli_fetch_all($result); $array=$this->mysqli_fetch_all($result);
$toDelete = [];
foreach($array as $ss) { foreach($array as $ss) {
$this->DeleteSurvey($ss['id']); $toDelete[] = $ss['id'];
} }
$this->DeleteSurvey($toDelete);
} }
mysqli_query($this->dblink,$qs); mysqli_query($this->dblink,$qs);
return mysqli_query($this->dblink,$q); return mysqli_query($this->dblink,$q);
} }
function DeleteSurvey($id) { 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); 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 // already checks if there are no more people other than the owner
// present before the demolition is allowed. // present before the demolition is allowed.
if ($demolition) { if ($demolition) {
$evicts = [];
foreach ($members as $member) { foreach ($members as $member) {
// evict the player from the alliance // 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 // notify them via in-game messaging
$this->sendMessage( $this->sendMessage(
@@ -4329,6 +4338,8 @@ class MYSQLi_DB implements IDbConnection {
0, 0,
true); true);
} }
mysqli_query($this->dblink, 'UPDATE '.TB_PREFIX.'users SET alliance = 0 WHERE id IN('.implode(',', $evicts).")");
} else { } else {
// we come from a battle result, therefore we need to check // we come from a battle result, therefore we need to check
// for the first player in the alliance who has a sufficient // 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); $result = $this->query_return($q);
$attack = 0; $attack = 0;
foreach($result as $general) { foreach($result as $general) {
if(date("j. M",$time) == date("j. M",$general['time'])){ if(date("j. M",$time) == date("j. M",$general['time'])){
$attack += 1; $attack += 1;
} }
} }
return $attack; return $attack;
} }
+32 -32
View File
@@ -251,38 +251,38 @@ class Village {
$crop = $clay = $wood = $iron = 0; $crop = $clay = $wood = $iron = 0;
if (!empty($this->oasisowned)) { if (!empty($this->oasisowned)) {
foreach ($this->oasisowned as $oasis) { foreach ($this->oasisowned as $oasis) {
switch($oasis['type']) { switch ( $oasis['type'] ) {
case 1: case 1:
case 2: case 2:
$wood += 1; $wood += 1;
break; break;
case 3: case 3:
$wood += 1; $wood += 1;
$crop += 1; $crop += 1;
break; break;
case 4: case 4:
case 5: case 5:
$clay += 1; $clay += 1;
break; break;
case 6: case 6:
$clay += 1; $clay += 1;
$crop += 1; $crop += 1;
break; break;
case 7: case 7:
case 8: case 8:
$iron += 1; $iron += 1;
break; break;
case 9: case 9:
$iron += 1; $iron += 1;
$crop += 1; $crop += 1;
break; break;
case 10: case 10:
case 11: case 11:
$crop += 1; $crop += 1;
break; break;
case 12: case 12:
$crop += 2; $crop += 2;
break; break;
} }
} }
} }
+15 -11
View File
@@ -80,18 +80,22 @@
$abdata = $database->getABTech($getFLData['wref']); $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']); $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']; $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())); $database->addMovement(3,$getFLData['wref'],$data['to_vid'],$reference,time(),($time+time()));
} }
} }