refactor: village-related methods now all optimized

This commit is contained in:
Martin Ambrus
2017-11-15 23:33:07 +01:00
parent 612661c9f0
commit d3ec209ad8
2 changed files with 20 additions and 6 deletions
+11 -2
View File
@@ -511,10 +511,19 @@ class MYSQLi_DB implements IDbConnection {
mysqli_query($this->dblink,$q);
}
function updateResource($vid, $what, $number) {
list($vid, $what, $number) = $this->escape_input((int) $vid, $what, (int) $number);
$vid = (int) $vid;
if (!is_array($what)) {
$what = [$what];
$number = [$number];
}
$q = "UPDATE " . TB_PREFIX . "vdata set " . $what . "=" . $number . " where wref = $vid";
$pairs = [];
foreach ($what as $index => $whatValue) {
$pairs[] = $this->escape($whatValue) . ' = ' . (Math::isInt($number[$index]) ? $number[$index] : '"'.$this->escape($number[$index]).'"');
}
$q = "UPDATE " . TB_PREFIX . "vdata SET ".implode(', ', $pairs)." WHERE wref = $vid";
$result = mysqli_query($this->dblink,$q);
return mysqli_query($this->dblink,$q);
}
+9 -4
View File
@@ -96,11 +96,16 @@ class Village {
$this->loyalty = $this->infoarray['loyalty'];
$this->master = count($database->getMasterJobs($this->wid));
//de gs in town, zetten op max pakhuisinhoud
if($this->awood>$this->maxstore){ $this->awood=$this->maxstore; $database->updateResource($this->wid,'wood',$this->maxstore); }
if($this->aclay>$this->maxstore){ $this->aclay=$this->maxstore; $database->updateResource($this->wid,'clay',$this->maxstore); }
if($this->airon>$this->maxstore){ $this->airon=$this->maxstore; $database->updateResource($this->wid,'iron',$this->maxstore); }
if($this->acrop>$this->maxcrop){ $this->acrop=$this->maxcrop; $database->updateResource($this->wid,'crop',$this->maxcrop); }
$resourceUpdates = [];
if($this->awood>$this->maxstore){ $this->awood=$this->maxstore; $resourceUpdates['wood'] = $this->maxstore; }
if($this->aclay>$this->maxstore){ $this->aclay=$this->maxstore; $resourceUpdates['clay'] = $this->maxstore; }
if($this->airon>$this->maxstore){ $this->airon=$this->maxstore; $resourceUpdates['iron'] = $this->maxstore; }
if($this->acrop>$this->maxcrop){ $this->acrop=$this->maxcrop; $resourceUpdates['crop'] = $this->maxcrop; }
// update DB values
if (count($resourceUpdates)) {
$database->updateResource( $this->wid, array_keys( $resourceUpdates ), array_values($resourceUpdates) );
}
}
private function calculateProduction() {