From d3ec209ad87db5cd939aa7c1dac8de5346838e98 Mon Sep 17 00:00:00 2001 From: Martin Ambrus Date: Wed, 15 Nov 2017 23:33:07 +0100 Subject: [PATCH] refactor: village-related methods now all optimized --- GameEngine/Database.php | 13 +++++++++++-- GameEngine/Village.php | 13 +++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 7ed5e567..f32c9e10 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -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); } diff --git a/GameEngine/Village.php b/GameEngine/Village.php index db39cd5a..e6ff9a94 100755 --- a/GameEngine/Village.php +++ b/GameEngine/Village.php @@ -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() {