refactor: Units class-related caching done

#313
This commit is contained in:
Martin Ambrus
2017-11-17 15:34:08 +01:00
parent 079c830b94
commit 1318f34cba
2 changed files with 470 additions and 365 deletions
+93 -26
View File
@@ -229,11 +229,31 @@ class MYSQLi_DB implements IDbConnection {
*/
$prisonersCacheByVillageAndFromIDs = [],
/**
* @var array Cache of info about whether a village is an oasis.
*/
$isVillageOasisCache = [],
/**
* @var array Cache of resource levels.
*/
$resourceLevelsCache = [],
/**
* @var array Cache of oasis data.
*/
$oasisFieldsCache = [],
/**
* @var array Cache of oasis data by conquered ID.
*/
$oasisFieldsCacheByConqueredID = [],
/**
* @var array Cache of research.
*/
$abTechCache = [],
/**
* @var array Cache of messages to be sent out to players,
* so we can collect them and send them out together
@@ -581,6 +601,7 @@ class MYSQLi_DB implements IDbConnection {
return mysqli_query($this->dblink,$q);
}
// no need to cache this method
public function hasBeginnerProtection($vid) {
list($vid) = $this->escape_input($vid);
@@ -636,6 +657,7 @@ class MYSQLi_DB implements IDbConnection {
return $this->mysqli_fetch_all($result);
}
// no need to cache this method
function getVilWref($x, $y) {
list($x, $y) = $this->escape_input((int) $x, (int) $y);
@@ -798,11 +820,7 @@ class MYSQLi_DB implements IDbConnection {
}
function getVrefField($ref, $field) {
list($ref, $field) = $this->escape_input((int) $ref, $field);
$q = "SELECT $field FROM " . TB_PREFIX . "vdata where wref = $ref";
$result = mysqli_query($this->dblink,$q) or die(mysqli_error($this->dblink));
$dbarray = mysqli_fetch_array($result);
return $dbarray[$field];
return $this->getVillage($ref)[$field];
}
function getVrefCapital($ref) {
@@ -1198,15 +1216,24 @@ class MYSQLi_DB implements IDbConnection {
}
return mysqli_query($this->dblink,$q);
}
function isVillageOases($wref) {
function isVillageOases($wref, $use_cache = true) {
list($wref) = $this->escape_input((int) $wref);
// 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::$isVillageOasisCache, $wref)) && !is_null($cachedValue)) {
return $cachedValue;
}
$q = "SELECT id, oasistype FROM " . TB_PREFIX . "wdata where id = ". $wref;
$result = mysqli_query($this->dblink,$q);
if($result){
$dbarray = mysqli_fetch_array($result);
return $dbarray['oasistype'];
}else return 0;
$result = $dbarray['oasistype'];
}else $result = 0;
self::$isVillageOasisCache[$wref] = $result;
return self::$isVillageOasisCache[$wref];
}
public function VillageOasisCount($vref) {
@@ -1499,12 +1526,20 @@ class MYSQLi_DB implements IDbConnection {
return $dbarray['pop'];
}
function getOasisV($vid) {
function getOasisV($vid, $use_cache = true) {
list($vid) = $this->escape_input((int) $vid);
// 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::$oasisFieldsCache, $vid)) && !is_null($cachedValue)) {
return $cachedValue;
}
$q = "SELECT * FROM " . TB_PREFIX . "odata where wref = $vid";
$result = mysqli_query($this->dblink,$q);
return mysqli_fetch_array($result);
self::$oasisFieldsCache[$vid] = mysqli_fetch_array($result, MYSQLI_ASSOC);
return self::$oasisFieldsCache[$vid];
}
function getMInfo($id) {
@@ -1523,20 +1558,24 @@ class MYSQLi_DB implements IDbConnection {
return mysqli_fetch_array($result);
}
function getOasis($vid) {
function getOasis($vid, $use_cache = true) {
list($vid) = $this->escape_input((int) $vid);
// 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::$oasisFieldsCacheByConqueredID, $vid)) && !is_null($cachedValue)) {
return $cachedValue;
}
$q = "SELECT * FROM " . TB_PREFIX . "odata where conqured = $vid";
$result = mysqli_query($this->dblink,$q);
return $this->mysqli_fetch_all($result);
self::$oasisFieldsCacheByConqueredID[$vid] = $this->mysqli_fetch_all($result);
return self::$oasisFieldsCacheByConqueredID[$vid];
}
function getOasisInfo($wid) {
list($wid) = $this->escape_input((int) $wid);
$q = "SELECT * FROM " . TB_PREFIX . "odata where wref = $wid";
$result = mysqli_query($this->dblink,$q);
return mysqli_fetch_assoc($result);
function getOasisInfo($wid, $use_cache = true) {
return $this->getOasisV($wid, $use_cache);
}
function getVillageField($ref, $field, $use_cache = true) {
@@ -1577,20 +1616,27 @@ class MYSQLi_DB implements IDbConnection {
} else return 0;*/
}
function getOasisField($ref, $field) {
list($ref, $field) = $this->escape_input((int) $ref, $field);
function getOasisField($ref, $field, $use_cache = true) {
// return all data, don't waste time by selecting fields one by one
$oasisArray = $this->getOasisV($ref, $use_cache);
$result = (isset($oasisArray[$field]) ? $oasisArray[$field] : null);
/*list($ref, $field) = $this->escape_input((int) $ref, $field);
$q = "SELECT $field FROM " . TB_PREFIX . "odata where wref = $ref";
$result = mysqli_query($this->dblink,$q);
$dbarray = mysqli_fetch_array($result);
return $dbarray[$field];
return $dbarray[$field];*/
}
function getOasisFields($ref, $fields) {
list($ref, $fields) = $this->escape_input((int) $ref, $fields);
function getOasisFields($ref, $fields, $use_cache = true) {
// return all data, don't waste time by selecting fields one by one
return $this->getOasisV($ref, $use_cache);
/*list($ref, $fields) = $this->escape_input((int) $ref, $fields);
$q = "SELECT $fields FROM " . TB_PREFIX . "odata where wref = $ref";
return mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
return mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);*/
}
function setVillageField($ref, $field, $value) {
@@ -1907,6 +1953,7 @@ class MYSQLi_DB implements IDbConnection {
return mysqli_fetch_array($result);
}
// no need to cache this method
function checkVilExist($wref) {
list($wref) = $this->escape_input((int) $wref);
@@ -1919,6 +1966,7 @@ class MYSQLi_DB implements IDbConnection {
}
}
// no need to cache this method
function checkOasisExist($wref) {
list($wref) = $this->escape_input((int) $wref);
@@ -4266,6 +4314,7 @@ class MYSQLi_DB implements IDbConnection {
return mysqli_query($this->dblink,$q);
}
// no need to cache this method
function getVillageByName($name) {
list($name) = $this->escape_input($name);
@@ -4573,6 +4622,7 @@ class MYSQLi_DB implements IDbConnection {
return mysqli_insert_id($this->dblink);
}
// no need to cache this method
function getA2b($ckey, $check) {
list($ckey, $check) = $this->escape_input($ckey, $check);
@@ -4842,12 +4892,20 @@ class MYSQLi_DB implements IDbConnection {
return mysqli_query($this->dblink,$q);
}
function getABTech($vid) {
function getABTech($vid, $use_cache = true) {
list($vid) = $this->escape_input((int) $vid);
// 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::$abTechCache, $vid)) && !is_null($cachedValue)) {
return $cachedValue;
}
$q = "SELECT * FROM " . TB_PREFIX . "abdata where vref = $vid";
$result = mysqli_query($this->dblink,$q);
return mysqli_fetch_assoc($result);
self::$abTechCache[$vid] = mysqli_fetch_assoc($result);
return self::$abTechCache[$vid];
}
function addResearch($vid, $tech, $time) {
@@ -6304,6 +6362,9 @@ References:
*****************************************/
function setvacmode($uid,$days) {
// TODO: refactor vacation mode
return;
list($uid,$days) = $this->escape_input((int) $uid,(int) $days);
$days1 =60*60*24*$days;
$time =time()+$days1;
@@ -6312,12 +6373,18 @@ References:
}
function removevacationmode($uid) {
// TODO: refactor vacation mode
return;
list($uid) = $this->escape_input((int) $uid);
$q ="UPDATE ".TB_PREFIX."users SET vac_mode = '0' , vac_time='0' WHERE id=".$uid."";
$result =mysqli_query($this->dblink,$q);
}
function getvacmodexy($wref) {
// TODO: refactor vacation mode
return;
list($wref) = $this->escape_input((int) $wref);
$q = "SELECT id,oasistype,occupied FROM " . TB_PREFIX . "wdata where id = $wref";
$result = mysqli_query($this->dblink,$q);
+377 -339
View File
@@ -21,6 +21,10 @@ class Units {
public function procUnits($post) {
if(isset($post['c'])) {
if (!isset($post['disabled'])) {
$post['disabled'] = '';
}
switch($post['c']) {
case "1":
@@ -33,7 +37,7 @@ class Units {
break;
case "2":
if (isset($post['a'])&& $post['a']==533374 && $post['disabledr'] == ""){
if (isset($post['a'])&& $post['a']==533374 && $post['disabled'] == ""){
$this->sendTroops($post);
}else{
$post = $this->loadUnits($post);
@@ -283,11 +287,8 @@ class Units {
}
}
if( intval($enforce['hero']) > 0){
$q = "SELECT unit FROM ".TB_PREFIX."hero WHERE uid = ".(int) $from['owner']." AND dead = 0";
$result = mysqli_query($GLOBALS['link'],$q);
$hero_f=mysqli_fetch_array($result);
$hero_unit=$hero_f['unit'];
$speeds[] = $GLOBALS['u'.$hero_unit]['speed'];
$hero_unit = $database->getHeroField($from['owner'], 'unit')['unit'];
$speeds[] = $GLOBALS['u'.$hero_unit]['speed'];
}else{
$enforce['hero']='0';
}
@@ -325,388 +326,425 @@ class Units {
private function sendTroops($post) {
global $form, $database, $village, $generator, $session;
$data = $database->getA2b($post['timestamp_checksum'], $post['timestamp']);
$data = $database->getA2b( $post['timestamp_checksum'], $post['timestamp'] );
$Gtribe = "";
if ( $session->tribe == '2' ) {
$Gtribe = "1";
} else if ( $session->tribe == '3' ) {
$Gtribe = "2";
} else if ( $session->tribe == '4' ) {
$Gtribe = "3";
} else if ( $session->tribe == '5' ) {
$Gtribe = "4";
}
for ( $i = 1; $i < 10; $i ++ ) {
if ( isset( $data[ 'u' . $i ] ) ) {
$Gtribe = "";
if ($session->tribe == '2'){ $Gtribe = "1"; } else if ($session->tribe == '3'){ $Gtribe = "2"; }else if ($session->tribe == '4'){ $Gtribe = "3"; }else if ($session->tribe == '5'){ $Gtribe = "4"; }
for($i=1; $i<10; $i++){
if(isset($data['u'.$i])){
if ($data['u'.$i] > $village->unitarray['u'.$Gtribe.$i])
{
$form->addError("error","You can't send more units than you have");
break;
}
if($data['u'.$i]<0)
{
$form->addError("error","You can't send negative units.");
break;
}
}
}
if ($data['u11'] > $village->unitarray['hero'])
{
$form->addError("error","You can't send more units than you have");
}
if($data['u11']<0)
{
$form->addError("error","You can't send negative units.");
}
if($form->returnErrors() > 0) {
$_SESSION['errorarray'] = $form->getErrors();
$_SESSION['valuearray'] = $_POST;
header("Location: a2b.php");
exit;
} else {
if($session->access != BANNED){
if($session->tribe == 1){ $u = ""; } elseif($session->tribe == 2){ $u = "1"; } elseif($session->tribe == 3){ $u = "2"; }elseif($session->tribe == 4){ $u = "3"; }else {$u = "4"; }
$database->modifyUnit(
$village->wid,
array($u."1",$u."2",$u."3",$u."4",$u."5",$u."6",$u."7",$u."8",$u."9",$u.$session->tribe."0","hero"),
array($data['u1'],$data['u2'],$data['u3'],$data['u4'],$data['u5'],$data['u6'],$data['u7'],$data['u8'],$data['u9'],$data['u10'],$data['u11']),
array(0,0,0,0,0,0,0,0,0,0,0)
);
$query1 = mysqli_query($GLOBALS['link'],'SELECT owner FROM `' . TB_PREFIX . 'vdata` WHERE `wref` = ' . mysqli_escape_string($GLOBALS['link'],(int) $data['to_vid']));
$data1 = mysqli_fetch_assoc($query1);
$query11 = mysqli_query($GLOBALS['link'],'SELECT owner FROM `' . TB_PREFIX . 'vdata` WHERE `wref` = ' . mysqli_escape_string($GLOBALS['link'],(int) $village->wid));
$data11 = mysqli_fetch_assoc($query11);
$query21 = mysqli_query($GLOBALS['link'],'SELECT tribe FROM `' . TB_PREFIX . 'users` WHERE `id` = ' . (int) $data11['owner']);
$data21 = mysqli_fetch_assoc($query21);
$eigen = $database->getCoor($village->wid);
$from = array('x'=>$eigen['x'], 'y'=>$eigen['y']);
$ander = $database->getCoor($data['to_vid']);
$to = array('x'=>$ander['x'], 'y'=>$ander['y']);
$start = ($data21['tribe']-1)*10+1;
$end = ($data21['tribe']*10);
$speeds = array();
$scout = 1;
//find slowest unit.
for($i=1;$i<=10;$i++){
if (isset($data['u'.$i])){
if($data['u'.$i] != '' && $data['u'.$i] > 0){
if($unitarray) { reset($unitarray); }
$unitarray = $GLOBALS["u".(($session->tribe-1)*10+$i)];
$speeds[] = $unitarray['speed'];
if ( $data[ 'u' . $i ] > $village->unitarray[ 'u' . $Gtribe . $i ] ) {
$form->addError( "error", "You can't send more units than you have" );
break;
}
if ( $data[ 'u' . $i ] < 0 ) {
$form->addError( "error", "You can't send negative units." );
break;
}
}
}
if (isset($data['u11'])) {
if($data['u11'] != '' && $data['u11'] > 0){
$heroarray = $database->getHero($session->uid);
$herodata = $GLOBALS["u".$heroarray[0]['unit']];
$speeds[] = $herodata['speed'];
}
}
$artefact = count($database->getOwnUniqueArtefactInfo2($session->uid,2,3,0));
$artefact1 = count($database->getOwnUniqueArtefactInfo2($village->wid,2,1,1));
$artefact2 = count($database->getOwnUniqueArtefactInfo2($session->uid,2,2,0));
if($artefact > 0){
$fastertroops = 3;
}else if($artefact1 > 0){
$fastertroops = 2;
}else if($artefact2 > 0){
$fastertroops = 1.5;
}else{
$fastertroops = 1;
}
$time = round($generator->procDistanceTime($from,$to,min($speeds),1)/$fastertroops);
$foolartefact = $database->getFoolArtefactInfo(2,$village->wid,$session->uid);
if(count($foolartefact) > 0){
foreach($foolartefact as $arte){
if($arte['bad_effect'] == 1){
$time *= $arte['effect2'];
}else{
$time /= $arte['effect2'];
$time = round($time);
}
}
}
$to_owner = $database->getVillageField($data['to_vid'],"owner");
// Check if have WW owner have artefact Rivals great confusion or Artefact of the unique fool with that effect
// If is a WW village you can target on WW , if is not a WW village catapults will target randomly.
// Like it says : Exceptions are the WW which can always be targeted and the treasure chamber which can always be targeted, except with the unique artifact.
// Fixed by Advocaite and Shadow
$q = mysqli_fetch_array(mysqli_query($GLOBALS['link'],"SELECT Count(*) as Total FROM ".TB_PREFIX."fdata WHERE f99t = '40' AND vref = ".(int) $data['to_vid']), MYSQLI_ASSOC);
$isThere = $q['Total'];
if($isThere > 0)
{
$iswwvilla = 1;
$artefact_2 = count($database->getOwnUniqueArtefactInfo2($to_owner,7,3,0));
$artefact1_2 = count($database->getOwnUniqueArtefactInfo2($data['to_vid'],7,1,1));
$artefact2_2 = count($database->getOwnUniqueArtefactInfo2($to_owner,7,2,0));
$foolartefact2 = $database->getFoolArtefactInfo(7,$data['to_vid'],$to_owner);
$good_artefact = 0;
if(count($foolartefact2) > 0){
foreach($foolartefact2 as $arte){
if($arte['bad_effect'] == 0){
$good_artefact = 1;
}
}
}
}else{
$artefact_2 = count($database->getOwnUniqueArtefactInfo2($to_owner,7,3,0));
$artefact1_2 = count($database->getOwnUniqueArtefactInfo2($data['to_vid'],7,1,1));
$artefact2_2 = count($database->getOwnUniqueArtefactInfo2($to_owner,7,2,0));
$foolartefact2 = $database->getFoolArtefactInfo(7,$data['to_vid'],$to_owner);
$iswwvilla = 0;
$good_artefact = 0;
if(count($foolartefact2) > 0){
foreach($foolartefact2 as $arte){
if($arte['bad_effect'] == 0){
$good_artefact = 1;
}
}
}
}
if (isset($post['ctar1'])){
if($artefact_2 > 0 or $artefact1_2 > 0 or $artefact2_2 > 0 or $good_artefact == 1){
if ($post['ctar1'] != 40 or $post['ctar1'] != 27 and $iswwvilla == 1){
$post['ctar1'] = 99;
}else{
$post['ctar1'] = 99;
}
}else{
$post['ctar1'] = $post['ctar1'];
}
}else{
$post['ctar1'] = 0;
}
if (isset($post['ctar2'])){
if($artefact_2 > 0 or $artefact1_2 > 0 or $artefact2_2 > 0 or $good_artefact == 1){
if ($post['ctar2'] != 40 or $post['ctar2'] != 27 and $iswwvilla == 1){
$post['ctar2'] = 99;
}else{
$post['ctar2'] = 99;
}
}else{
$post['ctar2'] = $post['ctar2'];
}
}else{
$post['ctar2'] = 0;}
if (isset($post['spy'])){
$post['spy'] = $post['spy'];
}else{
$post['spy'] = 0;
}
$abdata = $database->getABTech($village->wid);
$reference = $database->addAttack(($village->wid),$data['u1'],$data['u2'],$data['u3'],$data['u4'],$data['u5'],$data['u6'],$data['u7'],$data['u8'],$data['u9'],$data['u10'],$data['u11'],$data['type'],$post['ctar1'],$post['ctar2'],$post['spy'],$abdata['b1'],$abdata['b2'],$abdata['b3'],$abdata['b4'],$abdata['b5'],$abdata['b6'],$abdata['b7'],$abdata['b8']);
$checkexist = $database->checkVilExist($data['to_vid']);
$checkoexist = $database->checkOasisExist($data['to_vid']);
if($checkexist or $checkoexist){
$database->addMovement(3,$village->wid,$data['to_vid'],$reference,time(),($time+time()));
if(($database->hasBeginnerProtection($village->wid)==1)&&($checkexist)){
mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."users SET protect = 0 WHERE id = ". (int) $session->uid);
}
}
if($form->returnErrors() > 0) {
if ( $data['u11'] > $village->unitarray['hero'] ) {
$form->addError( "error", "You can't send more units than you have" );
}
if ( $data['u11'] < 0 ) {
$form->addError( "error", "You can't send negative units." );
}
if ( $form->returnErrors() > 0 ) {
$_SESSION['errorarray'] = $form->getErrors();
$_SESSION['valuearray'] = $_POST;
header("Location: a2b.php");
header( "Location: a2b.php" );
exit;
}
header("Location: build.php?id=39");
exit;
} else {
if ( $session->access != BANNED ) {
if ( $session->tribe == 1 ) {
$u = "";
} elseif ( $session->tribe == 2 ) {
$u = "1";
} elseif ( $session->tribe == 3 ) {
$u = "2";
} elseif ( $session->tribe == 4 ) {
$u = "3";
} else {
$u = "4";
}
}else{
header("Location: banned.php");
exit;
}
}}
$database->modifyUnit(
$village->wid,
array(
$u . "1",
$u . "2",
$u . "3",
$u . "4",
$u . "5",
$u . "6",
$u . "7",
$u . "8",
$u . "9",
$u . $session->tribe . "0",
"hero"
),
array(
$data['u1'],
$data['u2'],
$data['u3'],
$data['u4'],
$data['u5'],
$data['u6'],
$data['u7'],
$data['u8'],
$data['u9'],
$data['u10'],
$data['u11']
),
array( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )
);
private function sendTroopsBack($post) {
global $form, $database, $village, $generator, $session, $technology;
if($session->access != BANNED){
$enforce=$database->getEnforceArray($post['ckey'],0);
$enforceoasis=$database->getOasisEnforceArray($post['ckey'], 0);
if(($enforce['from']==$village->wid) || ($enforce['vref']==$village->wid) || ($enforceoasis['conqured']==$village->wid)){
$to = $database->getVillage($enforce['from']);
$Gtribe = "";
if ($database->getUserField($to['owner'],'tribe',0) == '2'){ $Gtribe = "1"; } else if ($database->getUserField($to['owner'],'tribe',0) == '3'){ $Gtribe = "2"; } else if ($database->getUserField($to['owner'],'tribe',0) == '4'){ $Gtribe = "3"; }else if ($database->getUserField($to['owner'],'tribe',0) == '5'){ $Gtribe = "4"; }
$data21 = $database->getUserField( $database->getVrefField( $village->wid, 'owner' )['owner'], 'tribe', 0 );
$eigen = $database->getCoor( $village->wid );
$from = array( 'x' => $eigen['x'], 'y' => $eigen['y'] );
$ander = $database->getCoor( $data['to_vid'] );
$to = array( 'x' => $ander['x'], 'y' => $ander['y'] );
$start = ( $data21['tribe'] - 1 ) * 10 + 1;
$end = ( $data21['tribe'] * 10 );
for($i=1; $i<10; $i++){
if(isset($post['t'.$i])){
if($i!=10){
if ($post['t'.$i] > $enforce['u'.$Gtribe.$i])
{
$form->addError("error","You can't send more units than you have");
break;
}
$speeds = array();
$scout = 1;
if($post['t'.$i]<0)
{
$form->addError("error","You can't send negative units.");
break;
}
//find slowest unit.
for ( $i = 1; $i <= 10; $i ++ ) {
if ( isset( $data[ 'u' . $i ] ) ) {
if ( $data[ 'u' . $i ] != '' && $data[ 'u' . $i ] > 0 ) {
if ( $unitarray ) {
reset( $unitarray );
}
} else {
$post['t'.$i.'']='0';
$unitarray = $GLOBALS[ "u" . ( ( $session->tribe - 1 ) * 10 + $i ) ];
$speeds[] = $unitarray['speed'];
}
}
if(isset($post['t11'])){
if ($post['t11'] > $enforce['hero'])
{
$form->addError("error","You can't send more units than you have");
}
if($post['t11']<0)
{
$form->addError("error","You can't send negative units.");
}
}
if ( isset( $data['u11'] ) ) {
if ( $data['u11'] != '' && $data['u11'] > 0 ) {
$heroarray = $database->getHero( $session->uid );
$herodata = $GLOBALS[ "u" . $heroarray[0]['unit'] ];
$speeds[] = $herodata['speed'];
}
}
$artefact = count( $database->getOwnUniqueArtefactInfo2( $session->uid, 2, 3, 0 ) );
$artefact1 = count( $database->getOwnUniqueArtefactInfo2( $village->wid, 2, 1, 1 ) );
$artefact2 = count( $database->getOwnUniqueArtefactInfo2( $session->uid, 2, 2, 0 ) );
if ( $artefact > 0 ) {
$fastertroops = 3;
} else if ( $artefact1 > 0 ) {
$fastertroops = 2;
} else if ( $artefact2 > 0 ) {
$fastertroops = 1.5;
} else {
$fastertroops = 1;
}
$time = round( $generator->procDistanceTime( $from, $to, min( $speeds ), 1 ) / $fastertroops );
$foolartefact = $database->getFoolArtefactInfo( 2, $village->wid, $session->uid );
if ( count( $foolartefact ) > 0 ) {
foreach ( $foolartefact as $arte ) {
if ( $arte['bad_effect'] == 1 ) {
$time *= $arte['effect2'];
} else {
$post['t11']='0';
$time /= $arte['effect2'];
$time = round( $time );
}
}
}
$to_owner = $database->getVillageField( $data['to_vid'], "owner" );
// Check if have WW owner have artefact Rivals great confusion or Artefact of the unique fool with that effect
// If is a WW village you can target on WW , if is not a WW village catapults will target randomly.
// Like it says : Exceptions are the WW which can always be targeted and the treasure chamber which can always be targeted, except with the unique artifact.
// Fixed by Advocaite and Shadow
$q = mysqli_fetch_array( mysqli_query( $GLOBALS['link'], "SELECT Count(*) as Total FROM " . TB_PREFIX . "fdata WHERE f99t = '40' AND vref = " . (int) $data['to_vid'] ), MYSQLI_ASSOC );
$isThere = $q['Total'];
if ( $isThere > 0 ) {
$iswwvilla = 1;
$artefact_2 = count( $database->getOwnUniqueArtefactInfo2( $to_owner, 7, 3, 0 ) );
$artefact1_2 = count( $database->getOwnUniqueArtefactInfo2( $data['to_vid'], 7, 1, 1 ) );
$artefact2_2 = count( $database->getOwnUniqueArtefactInfo2( $to_owner, 7, 2, 0 ) );
$foolartefact2 = $database->getFoolArtefactInfo( 7, $data['to_vid'], $to_owner );
$good_artefact = 0;
if ( count( $foolartefact2 ) > 0 ) {
foreach ( $foolartefact2 as $arte ) {
if ( $arte['bad_effect'] == 0 ) {
$good_artefact = 1;
}
}
}
} else {
$artefact_2 = count( $database->getOwnUniqueArtefactInfo2( $to_owner, 7, 3, 0 ) );
$artefact1_2 = count( $database->getOwnUniqueArtefactInfo2( $data['to_vid'], 7, 1, 1 ) );
$artefact2_2 = count( $database->getOwnUniqueArtefactInfo2( $to_owner, 7, 2, 0 ) );
$foolartefact2 = $database->getFoolArtefactInfo( 7, $data['to_vid'], $to_owner );
$iswwvilla = 0;
$good_artefact = 0;
if ( count( $foolartefact2 ) > 0 ) {
foreach ( $foolartefact2 as $arte ) {
if ( $arte['bad_effect'] == 0 ) {
$good_artefact = 1;
}
}
}
}
if($form->returnErrors() > 0) {
if ( isset( $post['ctar1'] ) ) {
if ( $artefact_2 > 0 or $artefact1_2 > 0 or $artefact2_2 > 0 or $good_artefact == 1 ) {
if ( $post['ctar1'] != 40 or $post['ctar1'] != 27 and $iswwvilla == 1 ) {
$post['ctar1'] = 99;
} else {
$post['ctar1'] = 99;
}
} else {
$post['ctar1'] = $post['ctar1'];
}
} else {
$post['ctar1'] = 0;
}
if ( isset( $post['ctar2'] ) ) {
if ( $artefact_2 > 0 or $artefact1_2 > 0 or $artefact2_2 > 0 or $good_artefact == 1 ) {
if ( $post['ctar2'] != 40 or $post['ctar2'] != 27 and $iswwvilla == 1 ) {
$post['ctar2'] = 99;
} else {
$post['ctar2'] = 99;
}
} else {
$post['ctar2'] = $post['ctar2'];
}
} else {
$post['ctar2'] = 0;
}
if ( isset( $post['spy'] ) ) {
$post['spy'] = $post['spy'];
} else {
$post['spy'] = 0;
}
$abdata = $database->getABTech( $village->wid );
$reference = $database->addAttack( ( $village->wid ), $data['u1'], $data['u2'], $data['u3'], $data['u4'], $data['u5'], $data['u6'], $data['u7'], $data['u8'], $data['u9'], $data['u10'], $data['u11'], $data['type'], $post['ctar1'], $post['ctar2'], $post['spy'], $abdata['b1'], $abdata['b2'], $abdata['b3'], $abdata['b4'], $abdata['b5'], $abdata['b6'], $abdata['b7'], $abdata['b8'] );
$checkexist = $database->checkVilExist( $data['to_vid'] );
$checkoexist = $database->checkOasisExist( $data['to_vid'] );
if ( $checkexist or $checkoexist ) {
$database->addMovement( 3, $village->wid, $data['to_vid'], $reference, time(), ( $time + time() ) );
if ( ( $database->hasBeginnerProtection( $village->wid ) == 1 ) && ( $checkexist ) ) {
mysqli_query( $GLOBALS['link'], "UPDATE " . TB_PREFIX . "users SET protect = 0 WHERE id = " . (int) $session->uid );
}
}
if ( $form->returnErrors() > 0 ) {
$_SESSION['errorarray'] = $form->getErrors();
$_SESSION['valuearray'] = $_POST;
header("Location: a2b.php");
header( "Location: a2b.php" );
exit;
}
header( "Location: build.php?id=39" );
exit;
} else {
header( "Location: banned.php" );
exit;
}
}
}
private function sendTroopsBack($post) {
global $form, $database, $village, $generator, $session, $technology;
if ( $session->access != BANNED ) {
$enforce = $database->getEnforceArray( $post['ckey'], 0 );
$enforceoasis = $database->getOasisEnforceArray( $post['ckey'], 0 );
if ( ( $enforce['from'] == $village->wid ) || ( $enforce['vref'] == $village->wid ) || ( $enforceoasis['conqured'] == $village->wid ) ) {
$to = $database->getVillage( $enforce['from'] );
$Gtribe = "";
if ( $database->getUserField( $to['owner'], 'tribe', 0 ) == '2' ) {
$Gtribe = "1";
} else if ( $database->getUserField( $to['owner'], 'tribe', 0 ) == '3' ) {
$Gtribe = "2";
} else if ( $database->getUserField( $to['owner'], 'tribe', 0 ) == '4' ) {
$Gtribe = "3";
} else if ( $database->getUserField( $to['owner'], 'tribe', 0 ) == '5' ) {
$Gtribe = "4";
}
for ( $i = 1; $i < 10; $i ++ ) {
if ( isset( $post[ 't' . $i ] ) ) {
if ( $i != 10 ) {
if ( $post[ 't' . $i ] > $enforce[ 'u' . $Gtribe . $i ] ) {
$form->addError( "error", "You can't send more units than you have" );
break;
}
if ( $post[ 't' . $i ] < 0 ) {
$form->addError( "error", "You can't send negative units." );
break;
}
}
} else {
$post[ 't' . $i . '' ] = '0';
}
}
if ( isset( $post['t11'] ) ) {
if ( $post['t11'] > $enforce['hero'] ) {
$form->addError( "error", "You can't send more units than you have" );
}
if ( $post['t11'] < 0 ) {
$form->addError( "error", "You can't send negative units." );
}
} else {
$post['t11'] = '0';
}
if ( $form->returnErrors() > 0 ) {
$_SESSION['errorarray'] = $form->getErrors();
$_SESSION['valuearray'] = $_POST;
header( "Location: a2b.php" );
exit;
} else {
//change units
$start = ($database->getUserField($to['owner'],'tribe',0)-1)*10+1;
$end = ($database->getUserField($to['owner'],'tribe',0)*10);
$start = ( $database->getUserField( $to['owner'], 'tribe', 0 ) - 1 ) * 10 + 1;
$end = ( $database->getUserField( $to['owner'], 'tribe', 0 ) * 10 );
$j='1';
for($i=$start;$i<=$end;$i++){
$database->modifyEnforce($post['ckey'],$i,$post['t'.$j.''],0); $j++;
$j = '1';
for ( $i = $start; $i <= $end; $i ++ ) {
$database->modifyEnforce( $post['ckey'], $i, $post[ 't' . $j . '' ], 0 );
$j ++;
}
$database->modifyEnforce($post['ckey'],'hero',$post['t11'],0); $j++;
//get cord
$from = $database->getVillage($enforce['from']);
$fromcoor = $database->getCoor($enforce['from']);
$tocoor = $database->getCoor($enforce['vref']);
$fromCor = array('x'=>$tocoor['x'], 'y'=>$tocoor['y']);
$toCor = array('x'=>$fromcoor['x'], 'y'=>$fromcoor['y']);
$database->modifyEnforce( $post['ckey'], 'hero', $post['t11'], 0 );
$j ++;
//get cord
$from = $database->getVillage( $enforce['from'] );
$fromcoor = $database->getCoor( $enforce['from'] );
$tocoor = $database->getCoor( $enforce['vref'] );
$fromCor = array( 'x' => $tocoor['x'], 'y' => $tocoor['y'] );
$toCor = array( 'x' => $fromcoor['x'], 'y' => $fromcoor['y'] );
$speeds = array();
$speeds = array();
//find slowest unit.
for($i=1;$i<=10;$i++){
if (isset($post['t'.$i])){
if( $post['t'.$i] != '' && $post['t'.$i] > 0){
if($unitarray) { reset($unitarray); }
$unitarray = $GLOBALS["u".(($session->tribe-1)*10+$i)];
$speeds[] = $unitarray['speed'];
} else {
$post['t'.$i.'']='0';
//find slowest unit.
for ( $i = 1; $i <= 10; $i ++ ) {
if ( isset( $post[ 't' . $i ] ) ) {
if ( $post[ 't' . $i ] != '' && $post[ 't' . $i ] > 0 ) {
if ( $unitarray ) {
reset( $unitarray );
}
$unitarray = $GLOBALS[ "u" . ( ( $session->tribe - 1 ) * 10 + $i ) ];
$speeds[] = $unitarray['speed'];
} else {
$post[ 't' . $i . '' ] = '0';
}
} else {
$post[ 't' . $i . '' ] = '0';
}
}
if ( isset( $post['t11'] ) ) {
if ( $post['t11'] != '' && $post['t11'] > 0 ) {
$hero_unit = $database->getHeroField($from['owner'], 'unit')['unit'];
$speeds[] = $GLOBALS[ 'u' . $hero_unit ]['speed'];
} else {
$post['t11'] = '0';
}
} else {
$post['t'.$i.'']='0';
$post['t11'] = '0';
}
}
if (isset($post['t11'])){
if( $post['t11'] != '' && $post['t11'] > 0){
$qh = "SELECT unit FROM ".TB_PREFIX."hero WHERE uid = ".(int) $from['owner']." AND dead = 0";
$resulth = mysqli_query($GLOBALS['link'],$qh);
$hero_f=mysqli_fetch_array($resulth);
$hero_unit=$hero_f['unit'];
$speeds[] = $GLOBALS['u'.$hero_unit]['speed'];
$artefact = count( $database->getOwnUniqueArtefactInfo2( $session->uid, 2, 3, 0 ) );
$artefact1 = count( $database->getOwnUniqueArtefactInfo2( $village->wid, 2, 1, 1 ) );
$artefact2 = count( $database->getOwnUniqueArtefactInfo2( $session->uid, 2, 2, 0 ) );
if ( $artefact > 0 ) {
$fastertroops = 3;
} else if ( $artefact1 > 0 ) {
$fastertroops = 2;
} else if ( $artefact2 > 0 ) {
$fastertroops = 1.5;
} else {
$post['t11']='0';
$fastertroops = 1;
}
$time = round( $generator->procDistanceTime( $fromCor, $toCor, min( $speeds ), 1 ) / $fastertroops );
$foolartefact2 = $database->getFoolArtefactInfo( 2, $village->wid, $session->uid );
if ( count( $foolartefact2 ) > 0 ) {
foreach ( $foolartefact2 as $arte ) {
if ( $arte['bad_effect'] == 1 ) {
$time *= $arte['effect2'];
} else {
$time /= $arte['effect2'];
$time = round( $time );
}
}
} else {
$post['t11']='0';
}
$artefact = count($database->getOwnUniqueArtefactInfo2($session->uid,2,3,0));
$artefact1 = count($database->getOwnUniqueArtefactInfo2($village->wid,2,1,1));
$artefact2 = count($database->getOwnUniqueArtefactInfo2($session->uid,2,2,0));
if($artefact > 0){
$fastertroops = 3;
}else if($artefact1 > 0){
$fastertroops = 2;
}else if($artefact2 > 0){
$fastertroops = 1.5;
}else{
$fastertroops = 1;
}
$time = round($generator->procDistanceTime($fromCor,$toCor,min($speeds),1)/$fastertroops);
$foolartefact2 = $database->getFoolArtefactInfo(2,$village->wid,$session->uid);
if(count($foolartefact2) > 0){
foreach($foolartefact2 as $arte){
if($arte['bad_effect'] == 1){
$time *= $arte['effect2'];
}else{
$time /= $arte['effect2'];
$time = round($time);
}
}
}
$reference = $database->addAttack($enforce['from'],$post['t1'],$post['t2'],$post['t3'],$post['t4'],$post['t5'],$post['t6'],$post['t7'],$post['t8'],$post['t9'],$post['t10'],$post['t11'],2,0,0,0,0);
$database->addMovement(4,$village->wid,$enforce['from'],$reference,time(),($time+time()));
$technology->checkReinf($post['ckey']);
$reference = $database->addAttack( $enforce['from'], $post['t1'], $post['t2'], $post['t3'], $post['t4'], $post['t5'], $post['t6'], $post['t7'], $post['t8'], $post['t9'], $post['t10'], $post['t11'], 2, 0, 0, 0, 0 );
$database->addMovement( 4, $village->wid, $enforce['from'], $reference, time(), ( $time + time() ) );
$technology->checkReinf( $post['ckey'] );
header("Location: build.php?id=39");
exit;
header( "Location: build.php?id=39" );
exit;
}
} else {
$form->addError("error","You cant change someones troops.");
if($form->returnErrors() > 0) {
} else {
$form->addError( "error", "You cant change someones troops." );
if ( $form->returnErrors() > 0 ) {
$_SESSION['errorarray'] = $form->getErrors();
$_SESSION['valuearray'] = $_POST;
header("Location: a2b.php");
header( "Location: a2b.php" );
exit;
}
}
} else {
header( "Location: banned.php" );
exit;
}
}else{
header("Location: banned.php");
exit;
}
}
public function Settlers($post) {
global $form, $database, $village, $session;
if($session->access != BANNED){
$mode = CP;
$total = count($database->getProfileVillages($session->uid));
$need_cps = ${'cp'.$mode}[$total+1];
$cps = $session->cp;
$rallypoint = $database->getResourceLevel($village->wid);
if($rallypoint['f39'] > 0){
if($cps >= $need_cps) {
$unit = ($session->tribe*10);
$database->modifyResource($village->wid,750,750,750,750,0);
$database->modifyUnit($village->wid,array($unit),array(3),array(0));
$database->addMovement(5,$village->wid,$post['s'],0,time(),time()+$post['timestamp']);
header("Location: build.php?id=39");
exit;
if($form->returnErrors() > 0) {
$_SESSION['errorarray'] = $form->getErrors();
$_SESSION['valuearray'] = $_POST;
header("Location: a2b.php");
exit;
}
} else {
header("Location: build.php?id=39");
exit;
}
}else{
header("Location: dorf1.php");
exit;
}
}else{
header("Location: banned.php");
exit;
}
if ( $session->access != BANNED ) {
$mode = CP;
$total = count( $database->getProfileVillages( $session->uid ) );
$need_cps = ${'cp' . $mode}[ $total + 1 ];
$cps = $session->cp;
$rallypoint = $database->getResourceLevel( $village->wid );
if ( $rallypoint['f39'] > 0 ) {
if ( $cps >= $need_cps ) {
$unit = ( $session->tribe * 10 );
$database->modifyResource( $village->wid, 750, 750, 750, 750, 0 );
$database->modifyUnit( $village->wid, array( $unit ), array( 3 ), array( 0 ) );
$database->addMovement( 5, $village->wid, $post['s'], 0, time(), time() + $post['timestamp'] );
header( "Location: build.php?id=39" );
exit;
if ( $form->returnErrors() > 0 ) {
$_SESSION['errorarray'] = $form->getErrors();
$_SESSION['valuearray'] = $_POST;
header( "Location: a2b.php" );
exit;
}
} else {
header( "Location: build.php?id=39" );
exit;
}
} else {
header( "Location: dorf1.php" );
exit;
}
} else {
header( "Location: banned.php" );
exit;
}
}
public function Hero($uid, $all = 0, $include_dead = false) {