Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
-Removed the "sort_type" 6 from the movement table, looted resources
will now be saved in the "sort_type" 4 returing attack.
This reduces a lot the spam in the tables "send" and "movement" of the
database
-Removed some useless methods in Database.php and Message.php
+Optimized report loading, own report will be loaded only once (and not
twice at random) and allied reports aren't loaded anymore
+Minor improovements and better indentation
  • Loading branch information
iopietro committed May 5, 2018
1 parent cf74514 commit f7e05c9
Show file tree
Hide file tree
Showing 14 changed files with 438 additions and 625 deletions.
24 changes: 10 additions & 14 deletions Admin/Templates/report.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
include_once("../GameEngine/Generator.php");
include_once("../GameEngine/Technology.php");
include_once("../GameEngine/Message.php");
if ($_GET['bid']){
$rep = $database->getNotice4($_GET['bid']);
}else
$sql = "SELECT * FROM ".TB_PREFIX."ndata ORDER BY time DESC ";
$result = mysqli_query($GLOBALS["link"], $sql);
$rep1 = $database->mysqli_fetch_all($result);
if ($_GET['bid']) $rep = $database->getNotice2($_GET['bid']);
else
{
$sql = "SELECT * FROM " . TB_PREFIX . "ndata ORDER BY time DESC ";
$result = mysqli_query($GLOBALS["link"], $sql);
$rep1 = $database->mysqli_fetch_all($result);
}
if($rep1)
{
//$att = $database->getUserArray($rep1['uid'],1);
Expand Down Expand Up @@ -47,13 +48,8 @@ if($rep)
<div id="content" class="reports">
<h1>Reporte</h1>
<?php
$type = $rep[0]['ntype'];
include("Notice/".$type.".tpl");
$type = $rep[0]['ntype'];
include ("Notice/" . $type . ".tpl");
}
else
{
echo "Report ID ".$_GET['bid']." doesn't exist!";
}

else echo "Report ID ".$_GET['bid']." doesn't exist!";
?>
11 changes: 5 additions & 6 deletions GameEngine/Admin/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,12 @@ function DelVillage($wref, $mode=0){

mysqli_query($this->connection, $q);

$getmovement = $database->getMovement(3,$wref,1);
foreach($getmovement as $movedata) {
$getmovement = $database->getMovement(3, $wref, 1);
foreach($getmovement as $movedata){
$time = microtime(true);
$time2 = $time - $movedata['starttime'];
$database->setMovementProc($movedata['moveid']);
$database->addMovement(4,$movedata['to'],$movedata['from'],$movedata['ref'],$time,$time+$time2);
//$database->setMovementProc($movedata['moveid']);
$database->addMovement(4, $movedata['to'], $movedata['from'], $movedata['ref'], $time, $time + $time2);
}

//check return enforcement from del village
Expand Down Expand Up @@ -652,8 +651,8 @@ public function returnTroops($wref) {
$troopsTime = $this->procDistanceTime($fromCor, $toCor, min($speeds), $enforce['from']);
$time = $database->getArtifactsValueInfluence($from['owner'], $enforce['from'], 2, $troopsTime);

$reference = $database->addAttack($enforce['from'],$enforce['u'.$start],$enforce['u'.($start+1)],$enforce['u'.($start+2)],$enforce['u'.($start+3)],$enforce['u'.($start+4)],$enforce['u'.($start+5)],$enforce['u'.($start+6)],$enforce['u'.($start+7)],$enforce['u'.($start+8)],$enforce['u'.($start+9)],$enforce['hero'],2,0,0,0,0);
$database->addMovement(4,$wref,$enforce['from'],$reference,time(),($time+time()));
$reference = $database->addAttack($enforce['from'], $enforce['u' . $start], $enforce['u' . ($start + 1)], $enforce['u' . ($start + 2)], $enforce['u' . ($start + 3)], $enforce['u' . ($start + 4)], $enforce['u' . ($start + 5)], $enforce['u' . ($start + 6)], $enforce['u' . ($start + 7)], $enforce['u' . ($start + 8)], $enforce['u' . ($start + 9)], $enforce['hero'], 2, 0, 0, 0, 0);
$database->addMovement(4, $wref, $enforce['from'], $reference, time(), ($time + time()));
$database->deleteReinf($enforce['id']);
}
}
Expand Down
266 changes: 111 additions & 155 deletions GameEngine/Automation.php

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions GameEngine/BBCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,9 @@ function($matches) {
function($matches) {
global $database;

$report = count($database->getNotice4((int) $matches[1]));
if (count($report)) {
return "<a href=berichte.php?id=".$matches[1].">".$matches[2]."</a>";
} else {
return $matches[2];
}
$report = count($database->getNotice2((int) $matches[1]));
if (count($report)) return "<a href=berichte.php?id=".$matches[1].">".$matches[2]."</a>";
else return $matches[2];
},
$input);

Expand Down
29 changes: 2 additions & 27 deletions GameEngine/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,6 @@ class MYSQLi_DB implements IDbConnection {
*/
$noticesCacheById = [],

/**
* @var array Cache of notices by user ID.
*/
$noticesCacheByUId = [],

/**
* @var array Cache of merchants used count.
*/
Expand Down Expand Up @@ -4257,7 +4252,7 @@ function getNotice($uid) {
return $this->mysqli_fetch_all($result);
}

function getNotice2($id, $field, $use_cache = true) {
function getNotice2($id, $field = null, $use_cache = true) {
list($id, $field) = $this->escape_input((int) $id, $field);

// first of all, check if we should be using cache and whether the field
Expand All @@ -4271,27 +4266,7 @@ function getNotice2($id, $field, $use_cache = true) {
$dbarray = mysqli_fetch_array($result);

self::$noticesCacheById[$id] = $dbarray;
return self::$noticesCacheById[$id][$field];
}

function getNotice3($uid, $alliance, $use_cache = true) {
list($uid) = $this->escape_input((int) $uid);

// first of all, check if we should be using cache and whether the field
// required is already cached
if ($use_cache && ($cachedValue = self::returnCachedContent(self::$noticesCacheByUId, $uid)) && !is_null($cachedValue)) {
return $cachedValue;
}

$q = "SELECT * FROM " . TB_PREFIX . "ndata where uid = $uid ".($alliance > 0 ? 'OR ally = '.$alliance.'' : '')." ORDER BY time ".(isset($_GET['o']) && $_GET['o'] == 1 ? 'ASC' : 'DESC');
$result = mysqli_query($this->dblink,$q);

$noticesCacheByUId[$uid] = $this->mysqli_fetch_all($result);
return $noticesCacheByUId[$uid];
}

function getNotice4($id, $use_cache = true) {
return $this->getNotice2($id, $use_cache);
return is_null($field) ? self::$noticesCacheById[$id] : self::$noticesCacheById[$id][$field];
}

function getUnViewNotice($uid) {
Expand Down
121 changes: 49 additions & 72 deletions GameEngine/Market.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

class Market
{
public $onsale,$onmarket,$sending,$recieving,$return = array();
public $maxcarry,$merchant,$used;
public $onsale, $onmarket, $sending, $recieving, $return = [];
public $maxcarry, $merchant, $used;

public function procMarket($post)
{
Expand Down Expand Up @@ -114,8 +114,8 @@ private function sendResource($post)
elseif($post['send3'] < 1 || $post['send3'] > 3 || ($post['send3'] > 1 && !$session->goldclub)) $form->addError("error", INVALID_MERCHANTS_REPETITION);
elseif($availableWood >= $post['r1'] && $availableClay >= $post['r2'] && $availableIron >= $post['r3'] && $availableCrop >= $post['r4'])
{
$resource = array($wtrans,$ctrans,$itrans,$crtrans);
$reqMerc = ceil((array_sum($resource)-0.1)/$this->maxcarry);
$resource = [$wtrans, $ctrans, $itrans, $crtrans];
$reqMerc = ceil((array_sum($resource) - 0.1) / $this->maxcarry);

if($this->merchantAvail() > 0 && $reqMerc <= $this->merchantAvail())
{
Expand All @@ -124,14 +124,13 @@ private function sendResource($post)
if($database->getVillageState($id))
{
$timetaken = $generator->procDistanceTime($coor, $village->coor, $session->tribe, 0);
$res = $resource[0]+$resource[1]+$resource[2]+$resource[3];
if($res!=0)
{
$reference = $database->sendResource($resource[0],$resource[1],$resource[2],$resource[3],$reqMerc,0);
$database->modifyResource($village->wid,$resource[0],$resource[1],$resource[2],$resource[3],0);
$database->addMovement(0,$village->wid,$id,$reference,time(),time()+$timetaken,$post['send3']);
$logging->addMarketLog($village->wid,1,array($resource[0],$resource[1],$resource[2],$resource[3],$id));
}
$res = $resource[0] + $resource[1] + $resource[2] + $resource[3];
if($res != 0){
$reference = $database->sendResource($resource[0], $resource[1], $resource[2], $resource[3], $reqMerc, 0);
$database->modifyResource($village->wid, $resource[0], $resource[1], $resource[2], $resource[3], 0);
$database->addMovement(0, $village->wid, $id, $reference, time(), time() + $timetaken, $post['send3']);
$logging->addMarketLog($village->wid, 1, [$resource[0], $resource[1], $resource[2], $resource[3], $id]);
}
}
header("Location: build.php?id=".$post['id']);
exit;
Expand Down Expand Up @@ -251,7 +250,6 @@ private function acceptOffer($get)
}
}


// We don't have enough resources
if($infoarray['wamt'] > ([$village->awood, $village->aclay, $village->airon, $village->acrop])[$infoarray['wtype']])
{
Expand All @@ -271,7 +269,7 @@ private function acceptOffer($get)
exit;
}

$myresource = $hisresource = array(1=>0,0,0,0);
$myresource = $hisresource = [ 1=> 0, 0, 0, 0];
$myresource[$infoarray['wtype']] = $infoarray['wamt'];
$mysendid = $database->sendResource($myresource[1],$myresource[2],$myresource[3],$myresource[4],$reqMerc,0);
$hisresource[$infoarray['gtype']] = $infoarray['gamt'];
Expand All @@ -289,13 +287,13 @@ private function acceptOffer($get)
[$timestamp, $timestamp],
[$mytime + $timestamp, $histime + $timestamp]
);
$resource = array(1=>0,0,0,0);
$resource[$infoarray['wtype']] = $infoarray['wamt'];
$database->modifyResource($village->wid, $resource[1], $resource[2], $resource[3], $resource[4] , 0);
$database->setMarketAcc($get['g']);
$database->removeAcceptedOffer($get['g']);
$logging->addMarketLog($village->wid,2,array($infoarray['vref'],$get['g']));
header("Location: build.php?id=".$get['id']);
$resource = [1 => 0, 0, 0, 0];
$resource[$infoarray['wtype']] = $infoarray['wamt'];
$database->modifyResource($village->wid, $resource[1], $resource[2], $resource[3], $resource[4], 0);
$database->setMarketAcc($get['g']);
$database->removeAcceptedOffer($get['g']);
$logging->addMarketLog($village->wid, 2, [$infoarray['vref'], $get['g']]);
header("Location: build.php?id=" . $get['id']);
exit;
}

Expand All @@ -304,7 +302,7 @@ private function loadOnsale()
global $database,$village,$session,$multisort,$generator;

$displayarray = $database->getMarket($village->wid,1);
$holderarray = array();
$holderarray = [];
foreach($displayarray as $value)
{
$targetcoor = $database->getCoor($value['vref']);
Expand All @@ -320,57 +318,36 @@ private function loadOnsale()

private function filterNeed($get)
{
if(isset($get['v']) || isset($get['s']) || isset($get['b']))
{
$holder = $holder2 = array();
if(isset($get['v']) && $get['v'] == "1:1")
{
foreach($this->onsale as $equal)
{
if($equal['wamt'] <= $equal['gamt'])
{
array_push($holder,$equal);
}
}
}
else
{
$holder = $this->onsale;
}
foreach($holder as $sale)
{
if(isset($get['s']) && isset($get['b']))
{
if($sale['gtype'] == $get['s'] && $sale['wtype'] == $get['b'])
{
array_push($holder2,$sale);
}
}
else if(isset($get['s']) && !isset($get['b']))
{
if($sale['gtype'] == $get['s'])
{
array_push($holder2,$sale);
}
}
else if(isset($get['b']) && !isset($get['s']))
{
if($sale['wtype'] == $get['b'])
{
array_push($holder2,$sale);
}
}
else
{
$holder2 = $holder;
}
}
$this->onsale = $holder2;
}
else
{
$this->loadOnsale();
if(isset($get['v']) || isset($get['s']) || isset($get['b'])){
$holder = $holder2 = [];
if(isset($get['v']) && $get['v'] == "1:1"){
foreach($this->onsale as $equal){
if($equal['wamt'] <= $equal['gamt']){
array_push($holder, $equal);
}
}
}
else $holder = $this->onsale;

foreach($holder as $sale){
if(isset($get['s']) && isset($get['b'])){
if($sale['gtype'] == $get['s'] && $sale['wtype'] == $get['b']){
array_push($holder2, $sale);
}
}else if(isset($get['s']) && !isset($get['b'])){
if($sale['gtype'] == $get['s']){
array_push($holder2, $sale);
}
}else if(isset($get['b']) && !isset($get['s'])){
if($sale['wtype'] == $get['b']){
array_push($holder2, $sale);
}
}
else $holder2 = $holder;
}
$this->onsale = $holder2;
}
else $this->loadOnsale();
}

private function tradeResource($post)
Expand Down
Loading

0 comments on commit f7e05c9

Please sign in to comment.