refactor: for loops optimized

#313
This commit is contained in:
Martin Ambrus
2017-11-19 15:17:24 +01:00
parent 2a68b07704
commit 699781d130
6 changed files with 166 additions and 69 deletions
+22 -3
View File
@@ -3260,9 +3260,17 @@ class Automation {
$end = ($database->getUserField($to['owner'],'tribe',0)*10);
$j='1';
$units = [];
$amounts = [];
$modes = [];
for($i=$start;$i<=$end;$i++){
$database->modifyEnforce($post['ckey'],$i,$post['t'.$j.''],0); $j++;
$units[] = $i;
$amounts[] = $post['t'.$j.''];
$modes[] = 0;
$j++;
}
$database->modifyEnforce($post['ckey'], $units, $amounts, $modes);
//get cord
$from = $database->getVillage($enforce['from']);
@@ -5635,8 +5643,13 @@ class Automation {
//Put all true dens to 0
$query="SELECT id FROM ".TB_PREFIX."users ORDER BY id+0 DESC";
$result=mysqli_query($GLOBALS['link'],$query);
$userIDs = [];
for ($i=0; $row=mysqli_fetch_row($result); $i++){
mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."users SET ap=0, dp=0,Rc=0,clp=0, RR=0 WHERE id = ".(int) $row[0]);
$userIDs[] = (int) $row[0];
}
if (count($userIDs)) {
mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."users SET ap=0, dp=0,Rc=0,clp=0, RR=0 WHERE id IN(".implode(', ', $userIDs).")");
}
//Start alliance Medals wooot
@@ -5708,8 +5721,14 @@ class Automation {
$query="SELECT id FROM ".TB_PREFIX."alidata ORDER BY id+0 DESC";
$result=mysqli_query($GLOBALS['link'],$query);
$aliIDs = [];
for ($i=0; $row=mysqli_fetch_row($result); $i++){
mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."alidata SET ap=0, dp=0, RR=0, clp=0 WHERE id = ".(int) $row[0]);
$aliIDs[] = (int) $row[0];
}
if (count($aliIDs)) {
mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."alidata SET ap=0, dp=0,RR=0,clp=0 WHERE id IN(".implode(', ', $aliIDs).")");
}
$q = "UPDATE ".TB_PREFIX."config SET lastgavemedal=".$time;
+86 -38
View File
@@ -3594,12 +3594,15 @@ class MYSQLi_DB implements IDbConnection {
}
function clearExpansionSlot($id) {
list($id) = $this->escape_input((int) $id);
$id = (int) $id;
$pairs = [];
for($i = 1; $i <= 3; $i++) {
$q = "UPDATE " . TB_PREFIX . "vdata SET exp" . $i . "=0 WHERE exp" . $i . "=" . $id;
mysqli_query($this->dblink,$q);
$pairs[] = 'exp'.$i.' = 0';
}
$q = "UPDATE " . TB_PREFIX . "vdata SET ".implode(',', $pairs)." WHERE wref = " . $id;
mysqli_query($this->dblink,$q);
}
// no need to cache this method
@@ -3691,16 +3694,28 @@ class MYSQLi_DB implements IDbConnection {
}
function setArchived($id) {
list($id) = $this->escape_input((int) $id);
if (!is_array($id)) {
$id = [$id];
$q = "UPDATE " . TB_PREFIX . "mdata set archived = 1 where id = $id";
foreach ($id as $index => $idValue) {
$id[$index] = (int) $idValue;
}
}
$q = "UPDATE " . TB_PREFIX . "mdata set archived = 1 where id IN(".implode(', ', $id).")";
return mysqli_query($this->dblink,$q);
}
function setNorm($id) {
list($id) = $this->escape_input((int) $id);
if (!is_array($id)) {
$id = [$id];
$q = "UPDATE " . TB_PREFIX . "mdata set archived = 0 where id = $id";
foreach ($id as $index => $idValue) {
$id[$index] = (int) $idValue;
}
}
$q = "UPDATE " . TB_PREFIX . "mdata set archived = 0 where id IN(".implode(',', $id).")";
return mysqli_query($this->dblink,$q);
}
@@ -3718,8 +3733,7 @@ class MYSQLi_DB implements IDbConnection {
function getMessage($id, $mode) {
global $session;
list($id, $mode) = $this->escape_input((int) $id, $mode);
$mode = (int) $mode;
// update $id if we should show Support messages for Admins and we are an admin
if (
($session->access == MULTIHUNTER || $session->access == ADMIN)
@@ -3729,6 +3743,18 @@ class MYSQLi_DB implements IDbConnection {
$id = $id . ', 1';
}
if (in_array($mode, [5,7,8])) {
if (!is_array($id)) {
$id = [$id];
foreach ($id as $index => $idValue) {
$id[$index] = (int) $idValue;
}
}
} else {
$id = (int) $id;
}
global $session;
switch($mode) {
case 1:
@@ -3744,16 +3770,16 @@ class MYSQLi_DB implements IDbConnection {
$q = "UPDATE " . TB_PREFIX . "mdata set viewed = 1 where id = $id AND target IN(".((($session->access == MULTIHUNTER || $session->access == ADMIN) && ADMIN_RECEIVE_SUPPORT_MESSAGES) ? $session->uid.',1' : $session->uid).")";
break;
case 5:
$q = "UPDATE " . TB_PREFIX . "mdata set deltarget = 1,viewed = 1 where id = $id";
$q = "UPDATE " . TB_PREFIX . "mdata set deltarget = 1,viewed = 1 where id IN(".implode(', ', $id).")";
break;
case 6:
$q = "SELECT * FROM " . TB_PREFIX . "mdata where target IN($id) and send = 0 and archived = 1";
break;
case 7:
$q = "UPDATE " . TB_PREFIX . "mdata set delowner = 1 where id = $id";
$q = "UPDATE " . TB_PREFIX . "mdata set delowner = 1 where id IN(".implode(', ', $id).")";
break;
case 8:
$q = "UPDATE " . TB_PREFIX . "mdata set deltarget = 1,delowner = 1,viewed = 1 where id = $id";
$q = "UPDATE " . TB_PREFIX . "mdata set deltarget = 1,delowner = 1,viewed = 1 where IN(".implode(', ', $id).")";
break;
case 9:
$q = "SELECT * FROM " . TB_PREFIX . "mdata WHERE target IN($id) and send = 0 and archived = 0 and deltarget = 0 ORDER BY time DESC";
@@ -3774,23 +3800,41 @@ class MYSQLi_DB implements IDbConnection {
}
function unarchiveNotice($id) {
list($id) = $this->escape_input((int) $id);
if (!is_array($id)) {
$id = [$id];
$q = "UPDATE " . TB_PREFIX . "ndata set ntype = archive, archive = 0 where id = $id";
foreach ($id as $index => $idValue) {
$id[$index] = (int) $idValue;
}
}
$q = "UPDATE " . TB_PREFIX . "ndata set ntype = archive, archive = 0 where id IN(".implode(',', $id).")";
return mysqli_query($this->dblink,$q);
}
function archiveNotice($id) {
list($id) = $this->escape_input((int) $id);
if (!is_array($id)) {
$id = [$id];
$q = "update " . TB_PREFIX . "ndata set archive = ntype, ntype = 9 where id = $id";
foreach ($id as $index => $idValue) {
$id[$index] = (int) $idValue;
}
}
$q = "update " . TB_PREFIX . "ndata set archive = ntype, ntype = 9 where id IN(".implode(',', $id).")";
return mysqli_query($this->dblink,$q);
}
function removeNotice($id) {
list($id) = $this->escape_input((int) $id);
if (!is_array($id)) {
$id = [$id];
$q = "UPDATE " . TB_PREFIX . "ndata set del = 1,viewed = 1 where id = $id";
foreach ($id as $index => $idValue) {
$id[$index] = (int) $idValue;
}
}
$q = "UPDATE " . TB_PREFIX . "ndata set del = 1,viewed = 1 where id IN(".implode(',', $id).")";
return mysqli_query($this->dblink,$q);
}
@@ -5194,27 +5238,31 @@ class MYSQLi_DB implements IDbConnection {
// no need to cache this method
function getUnitsNumber($vid, $use_cache = false) {
list($vid) = $this->escape_input((int) $vid);
list( $vid ) = $this->escape_input( (int) $vid );
$dbarray = $this->getUnit( $vid );
$totalunits = 0;
$movingunits = $this->getVillageMovement( $vid );
for ( $i = 1; $i <= 50; $i ++ ) {
$totalunits += $dbarray[ 'u' . $i ];
}
$totalunits += $dbarray['hero'];
$movingunits = $this->getVillageMovement( $vid );
$reinforcingunits = $this->getEnforceArray( $vid, 1 );
$owner = $this->getVillageField( $vid, "owner" );
$ownertribe = $this->getUserField( $owner, "tribe", 0 );
$start = ( $ownertribe - 1 ) * 10 + 1;
$end = ( $ownertribe * 10 );
for ( $i = $start; $i <= $end; $i ++ ) {
$totalunits += $movingunits[ 'u' . $i ];
$totalunits += $reinforcingunits[ 'u' . $i ];
}
$totalunits += $movingunits['hero'];
$totalunits += $reinforcingunits['hero'];
$dbarray = $this->getUnit($vid);
$totalunits = 0;
$movingunits = $this->getVillageMovement($vid);
for($i=1;$i<=50;$i++){
$totalunits += $dbarray['u'.$i];
}
$totalunits += $dbarray['hero'];
$movingunits = $this->getVillageMovement($vid);
$reinforcingunits = $this->getEnforceArray($vid,1);
$owner = $this->getVillageField($vid,"owner");
$ownertribe = $this->getUserField($owner,"tribe",0);
$start = ($ownertribe-1)*10+1;
$end = ($ownertribe*10);
for($i=$start;$i<=$end;$i++){
$totalunits += $movingunits['u'.$i];
$totalunits += $reinforcingunits['u'.$i];
}
$totalunits += $movingunits['hero'];
$totalunits += $reinforcingunits['hero'];
return $totalunits;
}
+51 -8
View File
@@ -236,75 +236,118 @@ class Message {
private function removeMessage($post) {
global $database,$session;
$post = $database->escape($post);
$mode5updates = [];
$mode7updates = [];
$mode8updates = [];
for($i = 1; $i <= 10; $i++) {
if ( isset( $post[ 'n' . $i ] ) ) {
$message1 = mysqli_query( $GLOBALS['link'], "SELECT target, owner FROM " . TB_PREFIX . "mdata where id = " . (int) $post[ 'n' . $i ] . "" );
$message = mysqli_fetch_array( $message1 );
if ( $message['target'] == $session->uid && $message['owner'] == $session->uid ) {
$database->getMessage( $post[ 'n' . $i ], 8 );
$mode8updates[] = $post[ 'n' . $i ];
} else if ( $message['target'] == $session->uid ) {
$database->getMessage( $post[ 'n' . $i ], 5 );
$mode5updates[] = $post[ 'n' . $i ];
} else if ( $message['owner'] == $session->uid ) {
$database->getMessage( $post[ 'n' . $i ], 7 );
$mode7updates[] = $post[ 'n' . $i ];
}
}
}
if (count($mode5updates)) {
$database->getMessage( $mode5updates, 5 );
}
if (count($mode7updates)) {
$database->getMessage( $mode7updates, 7 );
}
if (count($mode8updates)) {
$database->getMessage( $mode8updates, 8 );
}
header("Location: nachrichten.php");
exit;
}
private function archiveMessage($post) {
global $database;
$archIDs = [];
for($i = 1; $i <= 10; $i++) {
if(isset($post['n' . $i])) {
$database->setArchived($post['n' . $i]);
$archIDs[] = $post['n' . $i];
}
}
$database->setArchived($archIDs);
header("Location: nachrichten.php");
exit;
}
private function unarchiveMessage($post) {
global $database;
$normIDs = [];
for($i = 1; $i <= 10; $i++) {
if(isset($post['n' . $i])) {
$database->setNorm($post['n' . $i]);
$normIDs[] = $post['n' . $i];
}
}
$database->setNorm($normIDs);
header("Location: nachrichten.php");
exit;
}
private function removeNotice($post) {
global $database;
$removeIDs = [];
for($i = 1; $i <= 10; $i++) {
if(isset($post['n' . $i])) {
$database->removeNotice($post['n' . $i], 5);
$removeIDs[] = $post['n' . $i];
}
}
$database->removeNotice($removeIDs);
header("Location: berichte.php");
exit;
}
private function archiveNotice($post) {
global $database;
$archiveIDs = [];
for($i = 1; $i <= 10; $i++) {
if(isset($post['n' . $i])) {
$database->archiveNotice($post['n' . $i]);
$archiveIDs[] = $post['n' . $i];
}
}
$database->archiveNotice($archiveIDs);
header("Location: berichte.php");
exit;
}
private function unarchiveNotice($post) {
global $database;
$unarchIDs = [];
for($i = 1; $i <= 10; $i++) {
if(isset($post['n' . $i])) {
$database->unarchiveNotice($post['n' . $i]);
$unarchIDs[] = $post['n' . $i];
}
}
$database->unarchiveNotice($unarchIDs);
header("Location: berichte.php");
exit;
}
+3 -1
View File
@@ -235,7 +235,9 @@ class Village {
$bakery = $this->resarray['f'.$i];
}
}
for($i=0;$i<=count($cropholder)-1;$i++) { $basecrop+= $bid4[$this->resarray[$cropholder[$i]]]['prod']; }
for ( $i = 0; $i <= count( $cropholder ) - 1; $i ++ ) {
$basecrop += $bid4[ $this->resarray[ $cropholder[ $i ] ] ]['prod'];
}
$crop = $basecrop + $basecrop * 0.25 * $this->ocounter[3];
if($grainmill >= 1 || $bakery >= 1) {
$crop += $basecrop /100 * (isset($bid8[$grainmill]['attri']) ? $bid8[$grainmill]['attri'] : 0) + (isset($bid9[$bakery]['attri']) ? $bid9[$bakery]['attri'] : 0);
+1 -6
View File
@@ -542,12 +542,7 @@ if(isset($_POST['password']) && $_POST['password'] != ""){
$text = preg_replace("'%TEKST%'",ARTEFACT ,$text);
fwrite($fh, $text);
$query="SELECT * FROM ".TB_PREFIX."users ORDER BY id DESC";
$result=mysqli_query($GLOBALS['link'],$query) or die (mysqli_error($database->dblink));
for ($i=0; $row=mysqli_fetch_row($result); $i++) {
$updateattquery = mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."users SET ok = '1' WHERE id = '".$row[0]."'")
or die(mysqli_error($database->dblink));
}
$query="UPDATE ".TB_PREFIX."users SET ok = 1";
echo "Done";
}elseif(User::exists($database,'Natars')) {
+3 -13
View File
@@ -18,12 +18,7 @@ $max_per_pass = 1000;
if (mysqli_num_rows(mysqli_query($GLOBALS['link'],"SELECT id FROM ".TB_PREFIX."users WHERE access = 9 AND id = ".$session->uid)) != '1') die("Hacking attempt!");
if(isset($_GET['del'])){
$query="SELECT * FROM ".TB_PREFIX."users ORDER BY id DESC";
$result=mysqli_query($GLOBALS['link'],$query) or die (mysqli_error($database->dblink));
for ($i=0; $row=mysqli_fetch_row($result); $i++) {
$updateattquery = mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."users SET ok = '0' WHERE id = '".$row[0]."'")
or die(mysqli_error($database->dblink));
}
mysqli_query($GLOBALS['link'], "UPDATE ".TB_PREFIX."users SET ok = 0");
}
if (@$_POST['submit'] == "Send")
@@ -33,7 +28,6 @@ if (@$_POST['submit'] == "Send")
$NextStep = true;
}
if (@isset($_POST['confirm']))
{
if ($_POST['confirm'] == 'No' ) $Interupt = true;
@@ -50,12 +44,8 @@ if (@isset($_POST['confirm']))
// $text = utf8_encode($text);
fwrite($fh, $text);
$query="SELECT * FROM ".TB_PREFIX."users ORDER BY id DESC";
$result=mysqli_query($GLOBALS['link'],$query) or die (mysqli_error($database->dblink));
for ($i=0; $row=mysqli_fetch_row($result); $i++) {
$updateattquery = mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."users SET ok = '1' WHERE id = '".$row[0]."'")
or die(mysqli_error($database->dblink));
}
mysqli_query($GLOBALS['link'], "UPDATE ".TB_PREFIX."users SET ok = 1");
$done = true;
} else { die("<br/><br/><br/>wrong"); }
}}