From cc9a42a30c0e4cc35a41461c822c747dbd5069ff Mon Sep 17 00:00:00 2001 From: Shadow Date: Tue, 21 Jan 2014 09:38:31 +0200 Subject: [PATCH] Update db_MYSQL.php --- GameEngine/Database/db_MYSQL.php | 1591 +++++++++++++++++++++++++++++- 1 file changed, 1549 insertions(+), 42 deletions(-) diff --git a/GameEngine/Database/db_MYSQL.php b/GameEngine/Database/db_MYSQL.php index b1646054..29026b52 100644 --- a/GameEngine/Database/db_MYSQL.php +++ b/GameEngine/Database/db_MYSQL.php @@ -50,6 +50,12 @@ class MYSQL_DB { return false; } } + + function updateDate($userName, $timeZone, $dateFormat) + { + $q = "UPDATE users set timeZone = $timeZone, dateFormat = $dateFormat where username = '" . $userName . "'"; + return mysql_query($q,$this->connection); + } function unreg($username) { $q = "DELETE from " . TB_PREFIX . "activate where username = '$username'"; @@ -80,12 +86,6 @@ class MYSQL_DB { return false; } } - - function updateDate($userName, $timeZone, $dateFormat) - { - $q = "UPDATE users set timeZone = $timeZone, dateFormat = $dateFormat where username = '" . $userName . "'"; - return mysql_query($q,$this->connection); - } function checkExist_activate($ref, $mode) { @@ -373,20 +373,40 @@ class MYSQL_DB { return mysql_query($q, $this->connection); } } + /* + function generateBase($sector) { + switch($sector) { + case 1: + $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x < 0 and y > 0 and occupied = 0"; + break; + case 2: + $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x > 0 and y > 0 and occupied = 0"; + break; + case 3: + $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x < 0 and y < 0 and occupied = 0"; + break; + case 4: + $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x > 0 and y < 0 and occupied = 0"; + break; + } + $result = mysql_query($q, $this->connection); + $num_rows = mysql_num_rows($result); + $result = $this->mysql_fetch_all($result); + $base = rand(0, ($num_rows-1)); + return $result[$base]['id']; + } + */ function generateBase($sector, $mode=1) { - $num_rows = 0; - $count_while = 0; - while (!$num_rows){ if (!$mode) { $gamesday=time()-COMMENCE; - if ($gamesday<3600*24*10 && $count_while==0) { //10 day + if ($gamesday<3600*24*10) { //10 day $wide1=1; $wide2=20; - } elseif ($gamesday<3600*24*20 && $count_while==1) { //20 day + } elseif ($gamesday<3600*24*20) { //20 day $wide1=20; $wide2=40; - } elseif ($gamesday<3600*24*30 && $count_while==2) { //30 day + } elseif ($gamesday<3600*24*30) { //30 day $wide1=40; $wide2=80; } else { // over 30 day @@ -414,8 +434,6 @@ class MYSQL_DB { } $result = mysql_query($q, $this->connection); $num_rows = mysql_num_rows($result); - $count_while++; - } $result = $this->mysql_fetch_all($result); $base = rand(0, ($num_rows-1)); return $result[$base]['id']; @@ -479,14 +497,12 @@ class MYSQL_DB { } return mysql_query($q, $this->connection); } - function isVillageOases($wref) { - $q = "SELECT id, oasistype FROM " . TB_PREFIX . "wdata where id = $wref"; - $result = mysql_query($q, $this->connection); - if($result){ - $dbarray = mysql_fetch_array($result); - return $dbarray['oasistype']; - }else return 0; - } + function isVillageOases($wref) { + $q = "SELECT id, oasistype FROM " . TB_PREFIX . "wdata where id = $wref"; + $result = mysql_query($q, $this->connection); + $dbarray = mysql_fetch_array($result); + return $dbarray['oasistype']; + } public function VillageOasisCount($vref) { $q = "SELECT count(*) FROM `".TB_PREFIX."odata` WHERE conqured=$vref"; @@ -1215,25 +1231,12 @@ class MYSQL_DB { return mysql_query($q, $this->connection); } - function DeleteCat($id) { - $qs = "DELETE from " . TB_PREFIX . "forum_cat where id = '$id'"; - $q = "DELETE from " . TB_PREFIX . "forum_topic where cat = '$id'"; - $q2="SELECT id from ".TB_PREFIX."forum_topic where cat ='$id'"; - $result = mysql_query($q2, $this->connection); - if (!empty($result)) { - $array=$this->mysql_fetch_all($result); - foreach($array as $ss) { - $this->DeleteSurvey($ss['id']); - } - } - mysql_query($qs, $this->connection); - return mysql_query($q, $this->connection); - } - - function DeleteSurvey($id) { - $qs = "DELETE from " . TB_PREFIX . "forum_survey where topic = '$id'"; - return mysql_query($qs, $this->connection); - } + function DeleteCat($id) { + $qs = "DELETE from " . TB_PREFIX . "forum_cat where id = '$id'"; + $q = "DELETE from " . TB_PREFIX . "forum_topic where cat = '$id'"; + mysql_query($qs, $this->connection); + return mysql_query($q, $this->connection); + } function DeleteTopic($id) { $qs = "DELETE from " . TB_PREFIX . "forum_topic where id = '$id'"; @@ -2223,4 +2226,1508 @@ class MYSQL_DB { function sendResource($ref, $clay, $iron, $crop, $merchant, $mode) { if(!$mode) { $q = "INSERT INTO " . TB_PREFIX . "send values (0,$ref,$clay,$iron,$crop,$merchant)"; - mys + mysql_query($q, $this->connection); + return mysql_insert_id($this->connection); + } else { + $q = "DELETE FROM " . TB_PREFIX . "send where id = $ref"; + return mysql_query($q, $this->connection); + } + } + + /*************************** + Function to get resources back if you delete offer + References: VillageRef (vref) + Made by: Dzoki + ***************************/ + + function getResourcesBack($vref, $gtype, $gamt) { + //Xtype (1) = wood, (2) = clay, (3) = iron, (4) = crop + if($gtype == 1) { + $q = "UPDATE " . TB_PREFIX . "vdata SET `wood` = `wood` + '$gamt' WHERE wref = $vref"; + return mysql_query($q, $this->connection); + } else + if($gtype == 2) { + $q = "UPDATE " . TB_PREFIX . "vdata SET `clay` = `clay` + '$gamt' WHERE wref = $vref"; + return mysql_query($q, $this->connection); + } else + if($gtype == 3) { + $q = "UPDATE " . TB_PREFIX . "vdata SET `iron` = `iron` + '$gamt' WHERE wref = $vref"; + return mysql_query($q, $this->connection); + } else + if($gtype == 4) { + $q = "UPDATE " . TB_PREFIX . "vdata SET `crop` = `crop` + '$gamt' WHERE wref = $vref"; + return mysql_query($q, $this->connection); + } + } + + /*************************** + Function to get info about offered resources + References: VillageRef (vref) + Made by: Dzoki + ***************************/ + + function getMarketField($vref, $field) { + $q = "SELECT $field FROM " . TB_PREFIX . "market where vref = '$vref'"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray[$field]; + } + + function removeAcceptedOffer($id) { + $q = "DELETE FROM " . TB_PREFIX . "market where id = $id"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_assoc($result); + } + + /*************************** + Function to add market offer + Mode 0: Add + Mode 1: Cancel + References: Village, Give, Amt, Want, Amt, Time, Alliance, Mode + ***************************/ + function addMarket($vid, $gtype, $gamt, $wtype, $wamt, $time, $alliance, $merchant, $mode) { + if(!$mode) { + $q = "INSERT INTO " . TB_PREFIX . "market values (0,$vid,$gtype,$gamt,$wtype,$wamt,0,$time,$alliance,$merchant)"; + mysql_query($q, $this->connection); + return mysql_insert_id($this->connection); + } else { + $q = "DELETE FROM " . TB_PREFIX . "market where id = $gtype and vref = $vid"; + return mysql_query($q, $this->connection); + } + } + + /*************************** + Function to get market offer + References: Village, Mode + ***************************/ + function getMarket($vid, $mode) { + $alliance = $this->getUserField($this->getVillageField($vid, "owner"), "alliance", 0); + if(!$mode) { + $q = "SELECT * FROM " . TB_PREFIX . "market where vref = $vid and accept = 0"; + } else { + $q = "SELECT * FROM " . TB_PREFIX . "market where vref != $vid and alliance = $alliance or vref != $vid and alliance = 0 and accept = 0"; + } + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + /*************************** + Function to get market offer + References: ID + ***************************/ + function getMarketInfo($id) { + $q = "SELECT * FROM " . TB_PREFIX . "market where id = $id"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_assoc($result); + } + + function setMovementProc($moveid) { + $q = "UPDATE " . TB_PREFIX . "movement set proc = 1 where moveid = $moveid"; + return mysql_query($q, $this->connection); + } + + /*************************** + Function to retrieve used merchant + References: Village + ***************************/ + function totalMerchantUsed($vid) { + $time = time(); + $q = "SELECT sum(" . TB_PREFIX . "send.merchant) from " . TB_PREFIX . "send, " . TB_PREFIX . "movement where " . TB_PREFIX . "movement.from = $vid and " . TB_PREFIX . "send.id = " . TB_PREFIX . "movement.ref and " . TB_PREFIX . "movement.proc = 0 and sort_type = 0"; + $result = mysql_query($q, $this->connection); + $row = mysql_fetch_row($result); + $q2 = "SELECT sum(ref) from " . TB_PREFIX . "movement where sort_type = 2 and " . TB_PREFIX . "movement.to = $vid and proc = 0"; + $result2 = mysql_query($q2, $this->connection); + $row2 = mysql_fetch_row($result2); + $q3 = "SELECT sum(merchant) from " . TB_PREFIX . "market where vref = $vid and accept = 0"; + $result3 = mysql_query($q3, $this->connection); + $row3 = mysql_fetch_row($result3); + return $row[0] + $row2[0] + $row3[0]; + } + + function getMovement($type, $village, $mode) { + $time = time(); + if(!$mode) { + $where = "from"; + } else { + $where = "to"; + } + switch($type) { + case 0: + $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "send where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "send.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 0 ORDER BY endtime ASC"; + break; + case 1: + $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "send where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "send.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 6 ORDER BY endtime ASC"; + break; + case 2: + $q = "SELECT * FROM " . TB_PREFIX . "movement where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 2 ORDER BY endtime ASC"; + break; + case 3: + $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 ORDER BY endtime ASC"; + break; + case 4: + $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 4 ORDER BY endtime ASC"; + break; + case 5: + $q = "SELECT * FROM " . TB_PREFIX . "movement where " . TB_PREFIX . "movement." . $where . " = $village and sort_type = 5 and proc = 0 ORDER BY endtime ASC"; + break; + case 6: + $q = "SELECT * FROM " . TB_PREFIX . "movement," . TB_PREFIX . "odata, " . TB_PREFIX . "attacks where " . TB_PREFIX . "odata.wref = $village and " . TB_PREFIX . "movement.to = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "attacks.attack_type != 1 and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 ORDER BY endtime ASC"; + //$q = "SELECT * FROM " . TB_PREFIX . "movement," . TB_PREFIX . "odata, " . TB_PREFIX . "attacks where " . TB_PREFIX . "odata.conqured = $village and " . TB_PREFIX . "movement.to = " . TB_PREFIX . "odata.wref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 ORDER BY endtime ASC"; + break; + case 7: + $q = "SELECT * FROM " . TB_PREFIX . "movement where " . TB_PREFIX . "movement." . $where . " = $village and sort_type = 4 and ref = 0 and proc = 0 ORDER BY endtime ASC"; + break; + case 8: + $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type = 1 ORDER BY endtime ASC"; + break; + case 34: + $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 or " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 4 ORDER BY endtime ASC"; + break; + } + $result = mysql_query($q, $this->connection); + $array = $this->mysql_fetch_all($result); + return $array; + } + + function addA2b($ckey, $timestamp, $to, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11, $type) { + $q = "INSERT INTO " . TB_PREFIX . "a2b (ckey,time_check,to_vid,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,type) VALUES ('$ckey', '$timestamp', '$to', '$t1', '$t2', '$t3', '$t4', '$t5', '$t6', '$t7', '$t8', '$t9', '$t10', '$t11', '$type')"; + mysql_query($q, $this->connection); + return mysql_insert_id($this->connection); + } + + function getA2b($ckey, $check) { + $q = "SELECT * from " . TB_PREFIX . "a2b where ckey = '" . $ckey . "' AND time_check = '" . $check . "'"; + $result = mysql_query($q, $this->connection); + if($result) { + return mysql_fetch_assoc($result); + } else { + return false; + } + } + + function addMovement($type, $from, $to, $ref, $time, $endtime, $send = 1, $wood = 0, $clay = 0, $iron = 0, $crop = 0, $ref2 = 0) { + $q = "INSERT INTO " . TB_PREFIX . "movement values (0,$type,$from,$to,$ref,$ref2,$time,$endtime,0,$send,$wood,$clay,$iron,$crop)"; + return mysql_query($q, $this->connection); + } + + function addAttack($vid, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11, $type, $ctar1, $ctar2, $spy,$b1=0,$b2=0,$b3=0,$b4=0,$b5=0,$b6=0,$b7=0,$b8=0) { + $q = "INSERT INTO " . TB_PREFIX . "attacks values (0,$vid,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10,$t11,$type,$ctar1,$ctar2,$spy,$b1,$b2,$b3,$b4,$b5,$b6,$b7,$b8)"; + mysql_query($q, $this->connection); + return mysql_insert_id($this->connection); + } + + function modifyAttack($aid, $unit, $amt) { + $unit = 't' . $unit; + $q = "UPDATE " . TB_PREFIX . "attacks set $unit = $unit - $amt where id = $aid"; + return mysql_query($q, $this->connection); + } + + function modifyAttack2($aid, $unit, $amt) { + $unit = 't' . $unit; + $q = "UPDATE " . TB_PREFIX . "attacks set $unit = $unit + $amt where id = $aid"; + return mysql_query($q, $this->connection); + } + + function modifyAttack3($aid, $units) { + $q = "UPDATE ".TB_PREFIX."attacks set $units WHERE id = $aid"; + return mysql_query($q, $this->connection); + } + + function getRanking() { + $q = "SELECT id,username,alliance,ap,apall,dp,dpall,access FROM " . TB_PREFIX . "users WHERE tribe<=3 AND access<" . (INCLUDE_ADMIN ? "10" : "8"); + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getVRanking() { + $q = "SELECT v.wref,v.name,v.owner,v.pop FROM " . TB_PREFIX . "vdata AS v," . TB_PREFIX . "users AS u WHERE v.owner=u.id AND u.tribe<=3 AND v.wref != '' AND u.access<" . (INCLUDE_ADMIN ? "10" : "8"); + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getARanking() { + $q = "SELECT id,name,tag,oldrank,Aap,Adp FROM " . TB_PREFIX . "alidata where id != '' ORDER BY id DESC"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getUserByTribe($tribe){ + $q = "SELECT * FROM " . TB_PREFIX . "users where tribe = $tribe"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getUserByAlliance($aid){ + $q = "SELECT * FROM " . TB_PREFIX . "users where alliance = $aid"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getHeroRanking() { + $q = "SELECT * FROM " . TB_PREFIX . "hero WHERE dead = 0"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getAllMember($aid) { + $q = "SELECT * FROM " . TB_PREFIX . "users where alliance = $aid order by (SELECT sum(pop) FROM " . TB_PREFIX . "vdata WHERE owner = " . TB_PREFIX . "users.id) desc, " . TB_PREFIX . "users.id desc"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getAllMember2($aid) { + $q = "SELECT * FROM " . TB_PREFIX . "users where alliance = $aid order by (SELECT sum(pop) FROM " . TB_PREFIX . "vdata WHERE owner = " . TB_PREFIX . "users.id) desc, " . TB_PREFIX . "users.id desc LIMIT 1"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_array($result); + } + + function addUnits($vid) { + $q = "INSERT into " . TB_PREFIX . "units (vref) values ($vid)"; + return mysql_query($q, $this->connection); + } + + function getUnit($vid) { + $q = "SELECT * from " . TB_PREFIX . "units where vref = $vid"; + $result = mysql_query($q, $this->connection); + if (!empty($result)) { + return mysql_fetch_assoc($result); + } else { + return NULL; + } + } + + function getUnitsNumber($vid) { + $q = "SELECT * from " . TB_PREFIX . "units where vref = $vid"; + $result = mysql_query($q, $this->connection); + $dbarray = mysql_fetch_assoc($result); + $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; + } + + function getHero($uid=0,$all=0) { + if ($all) { + $q = "SELECT * FROM ".TB_PREFIX."hero WHERE uid=$uid"; + } elseif (!$uid) { + $q = "SELECT * FROM ".TB_PREFIX."hero"; + } else { + $q = "SELECT * FROM ".TB_PREFIX."hero WHERE dead=0 AND uid=$uid LIMIT 1"; + } + $result = mysql_query($q, $this->connection); + if (!empty($result)) { + return $this->mysql_fetch_all($result); + } else { + return NULL; + } + } + + function getHeroField($uid,$field){ + $q = "SELECT * FROM ".TB_PREFIX."hero WHERE uid = $uid"; + $result = mysql_query($q,$this->connection); + return $this->mysql_fetch_all($result); + } + + function modifyHero($column,$value,$heroid,$mode=0) { + if(!$mode) { + $q = "UPDATE `".TB_PREFIX."hero` SET $column = $value WHERE heroid = $heroid"; + } elseif($mode=1) { + $q = "UPDATE `".TB_PREFIX."hero` SET $column = $column + $value WHERE heroid = $heroid"; + } else { + $q = "UPDATE `".TB_PREFIX."hero` SET $column = $column - $value WHERE heroid = $heroid"; + } + return mysql_query($q, $this->connection); + } + + function modifyHeroByOwner($column,$value,$uid,$mode=0) { + if(!$mode) { + $q = "UPDATE `".TB_PREFIX."hero` SET $column = $value WHERE uid = $uid"; + } elseif($mode=1) { + $q = "UPDATE `".TB_PREFIX."hero` SET $column = $column + $value WHERE uid = $uid"; + } else { + $q = "UPDATE `".TB_PREFIX."hero` SET $column = $column - $value WHERE uid = $uid"; + } + return mysql_query($q, $this->connection); + } + + function modifyHeroXp($column,$value,$heroid) { + $q = "UPDATE ".TB_PREFIX."hero SET $column = $column + $value WHERE uid=$heroid"; + return mysql_query($q, $this->connection); + } + + function addTech($vid) { + $q = "INSERT into " . TB_PREFIX . "tdata (vref) values ($vid)"; + return mysql_query($q, $this->connection); + } + + function addABTech($vid) { + $q = "INSERT into " . TB_PREFIX . "abdata (vref) values ($vid)"; + return mysql_query($q, $this->connection); + } + + function getABTech($vid) { + $q = "SELECT * FROM " . TB_PREFIX . "abdata where vref = $vid"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_assoc($result); + } + + function addResearch($vid, $tech, $time) { + $q = "INSERT into " . TB_PREFIX . "research values (0,$vid,'$tech',$time)"; + return mysql_query($q, $this->connection); + } + + function getResearching($vid) { + $q = "SELECT * FROM " . TB_PREFIX . "research where vref = $vid"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function checkIfResearched($vref, $unit) { + $q = "SELECT $unit FROM " . TB_PREFIX . "tdata WHERE vref = $vref"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray[$unit]; + } + + function getTech($vid) { + $q = "SELECT * from " . TB_PREFIX . "tdata where vref = $vid"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_assoc($result); + } + + function getTraining($vid) { + $q = "SELECT * FROM " . TB_PREFIX . "training where vref = $vid ORDER BY id"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function countTraining($vid) { + $q = "SELECT * FROM " . TB_PREFIX . "training WHERE vref = $vid"; + $result = mysql_query($q, $this->connection); + $row = mysql_fetch_row($result); + return $row[0]; + } + + function trainUnit($vid, $unit, $amt, $pop, $each, $time, $mode) { + global $village, $building, $session, $technology; + + if(!$mode) { + $barracks = array(1,2,3,11,12,13,14,21,22,31,32,33,34,35,36,37,38,39,40,41,42,43,44); + // fix by brainiac - THANK YOU + $greatbarracks = array(61,62,63,71,72,73,74,81,82,91,92,93,94,95,96,97,98,99,100,101,102,103,104); + $stables = array(4,5,6,15,16,23,24,25,26,45,46); + $greatstables = array(64,65,66,75,76,83,84,85,86,105,106); + $workshop = array(7,8,17,18,27,28,47,48); + $greatworkshop = array(67,68,77,78,87,88,107,108); + $residence = array(9,10,19,20,29,30,49,50); + $trapper = array(99); + + if(in_array($unit, $barracks)) { + $queued = $technology->getTrainingList(1); + } elseif(in_array($unit, $stables)) { + $queued = $technology->getTrainingList(2); + } elseif(in_array($unit, $workshop)) { + $queued = $technology->getTrainingList(3); + } elseif(in_array($unit, $residence)) { + $queued = $technology->getTrainingList(4); + } elseif(in_array($unit, $greatstables)) { + $queued = $technology->getTrainingList(6); + } elseif(in_array($unit, $greatbarracks)) { + $queued = $technology->getTrainingList(5); + } elseif(in_array($unit, $greatworkshop)) { + $queued = $technology->getTrainingList(7); + } elseif(in_array($unit, $trapper)) { + $queued = $technology->getTrainingList(8); + } + $now = time(); + + $uid = $this->getVillageField($vid, "owner"); + $artefact = count($this->getOwnUniqueArtefactInfo2($uid,5,3,0)); + $artefact1 = count($this->getOwnUniqueArtefactInfo2($vid,5,1,1)); + $artefact2 = count($this->getOwnUniqueArtefactInfo2($uid,5,2,0)); + if($artefact > 0){ + $time = $now+round(($time-$now)/2); + $each /= 2; + $each = round($each); + }else if($artefact1 > 0){ + $time = $now+round(($time-$now)/2); + $each /= 2; + $each = round($each); + }else if($artefact2 > 0){ + $time = $now+round(($time-$now)/4*3); + $each /= 4; + $each = round($each); + $each *= 3; + $each = round($each); + } + $foolartefact = $this->getFoolArtefactInfo(5,$vid,$uid); + if(count($foolartefact) > 0){ + foreach($foolartefact as $arte){ + if($arte['bad_effect'] == 1){ + $each *= $arte['effect2']; + }else{ + $each /= $arte['effect2']; + $each = round($each); + } + } + } + if($each == 0){ $each = 1; } + $time2 = $now+$each; + if(count($queued) > 0) { + $time += $queued[count($queued) - 1]['timestamp'] - $now; + $time2 += $queued[count($queued) - 1]['timestamp'] - $now; + } + // TROOPS MAKE SUM IN BARAKS , ETC + //if($queued[count($queued) - 1]['unit'] == $unit){ + //$time = $amt*$queued[count($queued) - 1]['eachtime']; + //$q = "UPDATE " . TB_PREFIX . "training SET amt = amt + $amt, timestamp = timestamp + $time WHERE id = ".$queued[count($queued) - 1]['id'].""; + //}else{ + $q = "INSERT INTO " . TB_PREFIX . "training values (0,$vid,$unit,$amt,$pop,$time,$each,$time2)"; + //} + } else { + $q = "DELETE FROM " . TB_PREFIX . "training where id = $vid"; + } + return mysql_query($q, $this->connection); + } + + function updateTraining($id, $trained, $each) { + $q = "UPDATE " . TB_PREFIX . "training set amt = amt - $trained, timestamp2 = timestamp2 + $each where id = $id"; + return mysql_query($q, $this->connection); + } + + function modifyUnit($vref, $array_unit, $array_amt, $array_mode){ + $i = -1; + $units=''; + $number = count($array_unit); + foreach($array_unit as $unit){ + if($unit == 230){$unit = 30;} + if($unit == 231){$unit = 31;} + if($unit == 120){$unit = 20;} + if($unit == 121){$unit = 21;} + if($unit =="hero"){$unit = 'hero';} + else{$unit = 'u' . $unit;} + + ++$i; + $units .= $unit.' = '.$unit.' '.(($array_mode[$i] == 1)? '+':'-').' '.$array_amt[$i].(($number > $i+1) ? ', ' : ''); + } + + $q = "UPDATE ".TB_PREFIX."units set $units WHERE vref = $vref"; + return mysql_query($q, $this->connection); + } + + function getEnforce($vid, $from) { + $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = $from and vref = $vid"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_assoc($result); + } + + function getOasisEnforce($ref, $mode=0) { + if (!$mode) { + $q = "SELECT e.*,o.conqured FROM ".TB_PREFIX."enforcement as e LEFT JOIN ".TB_PREFIX."odata as o ON e.vref=o.wref where o.conqured = $ref AND e.from !=$ref"; + }else{ + $q = "SELECT e.*,o.conqured FROM ".TB_PREFIX."enforcement as e LEFT JOIN ".TB_PREFIX."odata as o ON e.vref=o.wref where o.conqured = $ref"; + } + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getOasisEnforceArray($id, $mode=0) { + if (!$mode) { + $q = "SELECT e.*,o.conqured FROM ".TB_PREFIX."enforcement as e LEFT JOIN ".TB_PREFIX."odata as o ON e.vref=o.wref where e.id = $id"; + }else{ + $q = "SELECT e.*,o.conqured FROM ".TB_PREFIX."enforcement as e LEFT JOIN ".TB_PREFIX."odata as o ON e.from=o.wref where e.id =$id"; + } + $result = mysql_query($q, $this->connection); + return mysql_fetch_assoc($result); + } + + function getEnforceControllTroops($vid) { + $q = "SELECT * from " . TB_PREFIX . "enforcement where vref = $vid"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_assoc($result); + } + + function addEnforce($data) { + $q = "INSERT into " . TB_PREFIX . "enforcement (vref,`from`) values (" . $data['to'] . "," . $data['from'] . ")"; + mysql_query($q, $this->connection); + $id = mysql_insert_id($this->connection); + $owntribe = $this->getUserField($this->getVillageField($data['from'], "owner"), "tribe", 0); + $start = ($owntribe - 1) * 10 + 1; + $end = ($owntribe * 10); + //add unit + $j = '1'; + for($i = $start; $i <= $end; $i++) { + $this->modifyEnforce($id, $i, $data['t' . $j . ''], 1); + $j++; + } + $this->modifyEnforce($id,'hero',$data['t11'],1); + return mysql_insert_id($this->connection); + } + + function addEnforce2($data,$tribe,$dead1,$dead2,$dead3,$dead4,$dead5,$dead6,$dead7,$dead8,$dead9,$dead10,$dead11) { + $q = "INSERT into " . TB_PREFIX . "enforcement (vref,`from`) values (" . $data['to'] . "," . $data['from'] . ")"; + mysql_query($q, $this->connection); + $id = mysql_insert_id($this->connection); + $owntribe = $this->getUserField($this->getVillageField($data['from'], "owner"), "tribe", 0); + $start = ($owntribe - 1) * 10 + 1; + $end = ($owntribe * 10); + $start2 = ($tribe - 1) * 10 + 1; + $start3 = ($tribe - 1) * 10; + if($start3 == 0){ + $start3 = ""; + } + $end2 = ($tribe * 10); + //add unit + $j = '1'; + for($i = $start; $i <= $end; $i++) { + $this->modifyEnforce($id, $i, $data['t' . $j . ''], 1); + $this->modifyEnforce($id, $i, ${dead.$j}, 0); + $j++; + } + $this->modifyEnforce($id,'hero',$data['t11'],1); + $this->modifyEnforce($id,'hero',$dead11,0); + return mysql_insert_id($this->connection); + } + + function modifyEnforce($id, $unit, $amt, $mode) { + if($unit != 'hero') { $unit = 'u' . $unit; } + if(!$mode) { + $q = "UPDATE " . TB_PREFIX . "enforcement set $unit = $unit - $amt where id = $id"; + } else { + $q = "UPDATE " . TB_PREFIX . "enforcement set $unit = $unit + $amt where id = $id"; + } + mysql_query($q, $this->connection); + } + + function getEnforceArray($id, $mode) { + if(!$mode) { + $q = "SELECT * from " . TB_PREFIX . "enforcement where id = $id"; + } else { + $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = $id"; + } + $result = mysql_query($q, $this->connection); + return mysql_fetch_assoc($result); + } + + function getEnforceVillage($id, $mode) { + if(!$mode) { + $q = "SELECT * from " . TB_PREFIX . "enforcement where vref = $id"; + } else { + $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = $id"; + } + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getVillageMovement($id) { + $vinfo = $this->getVillage($id); + $vtribe = $this->getUserField($vinfo['owner'], "tribe", 0); + $movingunits = array(); + $outgoingarray = $this->getMovement(3, $id, 0); + if(!empty($outgoingarray)) { + foreach($outgoingarray as $out) { + for($i = 1; $i <= 10; $i++) { + $movingunits['u' . (($vtribe - 1) * 10 + $i)] += $out['t' . $i]; + } + $movingunits['hero'] += $out['t11']; + } + } + $returningarray = $this->getMovement(4, $id, 1); + if(!empty($returningarray)) { + foreach($returningarray as $ret) { + if($ret['attack_type'] != 1) { + for($i = 1; $i <= 10; $i++) { + $movingunits['u' . (($vtribe - 1) * 10 + $i)] += $ret['t' . $i]; + } + $movingunits['hero'] += $ret['t11']; + } + } + } + $settlerarray = $this->getMovement(5, $id, 0); + if(!empty($settlerarray)) { + $movingunits['u' . ($vtribe * 10)] += 3 * count($settlerarray); + } + return $movingunits; + } + + ################# -START- ################## + ## WORLD WONDER STATISTICS FUNCTIONS! ## + ############################################ + + /*************************** + Function to get all World Wonders + Made by: Dzoki + ***************************/ + + function getWW() { + $q = "SELECT * FROM " . TB_PREFIX . "fdata WHERE f99t = 40"; + $result = mysql_query($q, $this->connection); + if(mysql_num_rows($result)) { + return true; + } else { + return false; + } + } + + /*************************** + Function to get world wonder level! + Made by: Dzoki + ***************************/ + + function getWWLevel($vref) { + $q = "SELECT f99 FROM " . TB_PREFIX . "fdata WHERE vref = $vref"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray['f99']; + } + + /*************************** + Function to get world wonder owner ID! + Made by: Dzoki + ***************************/ + + function getWWOwnerID($vref) { + $q = "SELECT owner FROM " . TB_PREFIX . "vdata WHERE wref = $vref"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray['owner']; + } + + /*************************** + Function to get user alliance name! + Made by: Dzoki + ***************************/ + + function getUserAllianceID($id) { + $q = "SELECT alliance FROM " . TB_PREFIX . "users where id = $id"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray['alliance']; + } + + /*************************** + Function to get WW name + Made by: Dzoki + ***************************/ + + function getWWName($vref) { + $q = "SELECT wwname FROM " . TB_PREFIX . "fdata WHERE vref = $vref"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray['wwname']; + } + + /*************************** + Function to change WW name + Made by: Dzoki + ***************************/ + + function submitWWname($vref, $name) { + $q = "UPDATE " . TB_PREFIX . "fdata SET `wwname` = '$name' WHERE " . TB_PREFIX . "fdata.`vref` = $vref"; + return mysql_query($q, $this->connection); + } + + //medal functions + function addclimberpop($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "users set Rc = Rc + '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + function addclimberrankpop($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "users set clp = clp + '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + function removeclimberrankpop($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "users set clp = clp - '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + function setclimberrankpop($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "users set clp = '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + function updateoldrank($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "users set oldrank = '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + function removeclimberpop($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "users set Rc = Rc - '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + // ALLIANCE MEDAL FUNCTIONS + function addclimberpopAlly($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "alidata set Rc = Rc + '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + function addclimberrankpopAlly($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "alidata set clp = clp + '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + function removeclimberrankpopAlly($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "alidata set clp = clp - '$cp'' where id = $user"; + return mysql_query($q, $this->connection); + } + function updateoldrankAlly($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "alidata set oldrank = '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + function removeclimberpopAlly($user, $cp) { + $q = "UPDATE " . TB_PREFIX . "alidata set Rc = Rc - '$cp' where id = $user"; + return mysql_query($q, $this->connection); + } + + function getTrainingList() { + $q = "SELECT * FROM " . TB_PREFIX . "training where vref != ''"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getNeedDelete() { + $time = time(); + $q = "SELECT uid FROM " . TB_PREFIX . "deleting where timestamp < $time"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function countUser() { + $q = "SELECT count(id) FROM " . TB_PREFIX . "users where id > 5"; + $result = mysql_query($q, $this->connection); + $row = mysql_fetch_row($result); + return $row[0]; + } + + function countAlli() { + $q = "SELECT count(id) FROM " . TB_PREFIX . "alidata where id != 0"; + $result = mysql_query($q, $this->connection); + $row = mysql_fetch_row($result); + return $row[0]; + } + + /*************************** + Function to process MYSQLi->fetch_all (Only exist in MYSQL) + References: Result + ***************************/ + function mysql_fetch_all($result) { + $all = array(); + if($result) { + while($row = mysql_fetch_assoc($result)) { + $all[] = $row; + } + return $all; + } + } + + function query_return($q) { + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + /*************************** + Function to do free query + References: Query + ***************************/ + function query($query) { + return mysql_query($query, $this->connection); + } + + function RemoveXSS($val) { + return htmlspecialchars($val, ENT_QUOTES); + } + + //MARKET FIXES + function getWoodAvailable($wref) { + $q = "SELECT wood FROM " . TB_PREFIX . "vdata WHERE wref = $wref"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray['wood']; + } + + function getClayAvailable($wref) { + $q = "SELECT clay FROM " . TB_PREFIX . "vdata WHERE wref = $wref"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray['clay']; + } + + function getIronAvailable($wref) { + $q = "SELECT iron FROM " . TB_PREFIX . "vdata WHERE wref = $wref"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray['iron']; + } + + function getCropAvailable($wref) { + $q = "SELECT crop FROM " . TB_PREFIX . "vdata WHERE wref = $wref"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + return $dbarray['crop']; + } + + function Getowner($vid) { + $s = "SELECT owner FROM " . TB_PREFIX . "vdata where wref = $vid"; + $result1 = mysql_query($s, $this->connection); + $row1 = mysql_fetch_row($result1); + return $row1[0]; + } + + public function debug($time, $uid, $debug_info) { + $q = "INSERT INTO " . TB_PREFIX . "debug_info (time,uid,debug_info) VALUES ($time,$uid,$debug_info)"; + if(mysql_query($q, $this->connection)) { + return mysql_insert_id($this->connection); + } else { + return false; + } + } + + function populateOasisdata() { + $q2 = "SELECT * FROM " . TB_PREFIX . "wdata where oasistype != 0"; + $result2 = mysql_query($q2, $this->connection); + while($row = mysql_fetch_array($result2)) { + $wid = $row['id']; + $basearray = $this->getOMInfo($wid); + //We switch type of oasis and instert record with apropriate infomation. + $q = "INSERT into " . TB_PREFIX . "odata VALUES ('" . $basearray['id'] . "'," . $basearray['oasistype'] . ",0,800,800,800,800,800,800," . time() . "," . time() . ",100,2,'Unoccupied Oasis',".rand(0,2).")"; + $result = mysql_query($q, $this->connection); + } + } + + public function getAvailableExpansionTraining() { + global $building, $session, $technology, $village; + $q = "SELECT (IF(exp1=0,1,0)+IF(exp2=0,1,0)+IF(exp3=0,1,0)) FROM " . TB_PREFIX . "vdata WHERE wref = $village->wid"; + $result = mysql_query($q, $this->connection); + $row = mysql_fetch_row($result); + $maxslots = $row[0]; + $residence = $building->getTypeLevel(25); + $palace = $building->getTypeLevel(26); + if($residence > 0) { + $maxslots -= (3 - floor($residence / 10)); + } + if($palace > 0) { + $maxslots -= (3 - floor(($palace - 5) / 5)); + } + + $q = "SELECT (u10+u20+u30) FROM " . TB_PREFIX . "units WHERE vref = $village->wid"; + $result = mysql_query($q, $this->connection); + $row = mysql_fetch_row($result); + $settlers = $row[0]; + $q = "SELECT (u9+u19+u29) FROM " . TB_PREFIX . "units WHERE vref = $village->wid"; + $result = mysql_query($q, $this->connection); + $row = mysql_fetch_row($result); + $chiefs = $row[0]; + + $settlers += 3 * count($this->getMovement(5, $village->wid, 0)); + $current_movement = $this->getMovement(3, $village->wid, 0); + if(!empty($current_movement)) { + foreach($current_movement as $build) { + $settlers += $build['t10']; + $chiefs += $build['t9']; + } + } + $current_movement = $this->getMovement(3, $village->wid, 1); + if(!empty($current_movement)) { + foreach($current_movement as $build) { + $settlers += $build['t10']; + $chiefs += $build['t9']; + } + } + $current_movement = $this->getMovement(4, $village->wid, 0); + if(!empty($current_movement)) { + foreach($current_movement as $build) { + $settlers += $build['t10']; + $chiefs += $build['t9']; + } + } + $current_movement = $this->getMovement(4, $village->wid, 1); + if(!empty($current_movement)) { + foreach($current_movement as $build) { + $settlers += $build['t10']; + $chiefs += $build['t9']; + } + } + $q = "SELECT (u10+u20+u30) FROM " . TB_PREFIX . "enforcement WHERE `from` = $village->wid"; + $result = mysql_query($q, $this->connection); + $row = mysql_fetch_row($result); + if(!empty($row)) { + foreach($row as $reinf) { + $settlers += $reinf[0]; + } + } + $q = "SELECT (u9+u19+u29) FROM " . TB_PREFIX . "enforcement WHERE `from` = $village->wid"; + $result = mysql_query($q, $this->connection); + $row = mysql_fetch_row($result); + if(!empty($row)) { + foreach($row as $reinf) { + $chiefs += $reinf[0]; + } + } + $trainlist = $technology->getTrainingList(4); + if(!empty($trainlist)) { + foreach($trainlist as $train) { + if($train['unit'] % 10 == 0) { + $settlers += $train['amt']; + } + if($train['unit'] % 10 == 9) { + $chiefs += $train['amt']; + } + } + } + // trapped settlers/chiefs calculation required + + $settlerslots = $maxslots * 3 - $settlers - $chiefs * 3; + $chiefslots = $maxslots - $chiefs - floor(($settlers + 2) / 3); + + if(!$technology->getTech(($session->tribe - 1) * 10 + 9)) { + $chiefslots = 0; + } + $slots = array("chiefs" => $chiefslots, "settlers" => $settlerslots); + return $slots; + } + + function addArtefact($vref, $owner, $type, $size, $name, $desc, $effect, $img) { + $q = "INSERT INTO `" . TB_PREFIX . "artefacts` (`vref`, `owner`, `type`, `size`, `conquered`, `name`, `desc`, `effect`, `img`, `active`) VALUES ('$vref', '$owner', '$type', '$size', '" . time() . "', '$name', '$desc', '$effect', '$img', '0')"; + return mysql_query($q, $this->connection); + } + + function getOwnArtefactInfo($vref) { + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vref"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_array($result); + } + + function getOwnArtefactInfo2($vref) { + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vref"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getOwnArtefactInfo3($uid) { + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE owner = $uid"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getOwnArtefactInfoByType($vref, $type) { + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vref AND type = $type order by size"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_array($result); + } + + function getOwnArtefactInfoByType2($vref, $type) { + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vref AND type = $type"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getOwnUniqueArtefactInfo($id, $type, $size) { + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE owner = $id AND type = $type AND size=$size"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_array($result); + } + + function getOwnUniqueArtefactInfo2($id, $type, $size, $mode) { + if(!$mode){ + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE owner = $id AND active = 1 AND type = $type AND size=$size"; + }else{ + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $id AND active = 1 AND type = $type AND size=$size"; + } + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getFoolArtefactInfo($type,$vid,$uid) { + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vid AND type = 8 AND kind = $type OR owner = $uid AND size > 1 AND active = 1 AND type = 8 AND kind = $type"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function claimArtefact($vref, $ovref, $id) { + $time = time(); + $q = "UPDATE " . TB_PREFIX . "artefacts SET vref = $vref, owner = $id, conquered = $time, active = 1 WHERE vref = $ovref"; + return mysql_query($q, $this->connection); + } + + public function canClaimArtifact($from,$vref,$type,$kind) { + //fix by Ronix + global $session, $form; + $type1 = $type2 = $type3 = 0; + + $artifact = $this->getOwnArtefactInfo($from); + if (!empty($artifact)) { + $form->addError("error","Treasury is full. Your hero could not claim the artefact and"); + return false; + } + $uid=$session->uid; + $q="SELECT Count(type) AS totals, + SUM(IF(type = '1',1,0)) small, + SUM(IF(type = '2',1,0)) great, + SUM(IF(type = '3',1,0)) unique, + FROM ".TB_PREFIX."artefacts WHERE owner = ".$uid; + $result = mysql_query($q, $this->connection); + $artifact= $this->mysql_fetch_all($result); + if($artifact['totals'] < 3 || $kind==11) { + $DefenderFields = $this->getResourceLevel($vref); + $defcanclaim = TRUE; + for($i=19;$i<=38;$i++) { + if($DefenderFields['f'.$i.'t'] == 27) { + $defTresuaryLevel = $DefenderFields['f'.$i]; + if($defTresuaryLevel > 0) { + $defcanclaim = FALSE; + $form->addError("error","Treasury has not been destroyed. Your hero could not claim the artefact and"); + return false; + } else { + $defcanclaim = TRUE; + } + } + } + $AttackerFields = $this->getResourceLevel($from,2); + for($i=19;$i<=38;$i++) { + if($AttackerFields['f'.$i.'t'] == 27) { + $attTresuaryLevel = $AttackerFields['f'.$i]; + if ($attTresuaryLevel >= 10) { + $villageartifact = TRUE; + } else { + $villageartifact = FALSE; + } + if ($attTresuaryLevel >= 20){ + $accountartifact = TRUE; + } else { + $accountartifact = FALSE; + } + } + } + if (($artifact['great']>0 || $artifact['unique']>0) && $type>1) { + $form->addError("error","Max num. of great/unique artefacts. Your hero could not claim the artefact and"); + return FALSE; + } + if (($type == 1 && ($villageartifact || $accountartifact)) || (($type == 2 || $type == 3)&& $accountartifact)) { + if($this->getVillageField($from,"capital")==1) { + $form->addError("error","Ancient Construction Plan cannot kept in capital village"); + return FALSE; + }else{ + return TRUE; + } + } else { + $form->addError("error","Your level treasury is low. Your hero could not claim the artefact"); + return FALSE; + } + } else { + $form->addError("error","Max num. of artefacts. Your hero could not claim the artefact and"); + return FALSE; + } +} + + function getArtefactDetails($id) { + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE id = " . $id . ""; + $result = mysql_query($q, $this->connection); + return mysql_fetch_array($result); + } + + function getMovementById($id){ + $q = "SELECT * FROM ".TB_PREFIX."movement WHERE moveid = ".$id.""; + $result = mysql_query($q); + $array = $this->mysql_fetch_all($result); + return $array; + } + + function getLinks($id){ + $q = 'SELECT * FROM `' . TB_PREFIX . 'links` WHERE `userid` = ' . $id . ' ORDER BY `pos` ASC'; + return mysql_query($q, $this->connection); + } + + function removeLinks($id,$uid){ + $q = "DELETE FROM " . TB_PREFIX . "links WHERE `id` = ".$id." and `userid` = ".$uid.""; + return mysql_query($q, $this->connection); + } + + function getVilFarmlist($wref){ + $q = 'SELECT * FROM ' . TB_PREFIX . 'farmlist WHERE wref = ' . $wref . ' ORDER BY wref ASC'; + $result = mysql_query($q, $this->connection); + $dbarray = mysql_fetch_array($result); + + if($dbarray['id']!=0) { + return true; + } else { + return false; + } + + } + + function getRaidList($id) { + $q = "SELECT * FROM " . TB_PREFIX . "raidlist WHERE id = ".$id.""; + $result = mysql_query($q, $this->connection); + return mysql_fetch_array($result); + } + + function delFarmList($id, $owner) { + $q = "DELETE FROM " . TB_PREFIX . "farmlist where id = $id and owner = $owner"; + return mysql_query($q, $this->connection); + } + + function delSlotFarm($id) { + $q = "DELETE FROM " . TB_PREFIX . "raidlist where id = $id"; + return mysql_query($q, $this->connection); + } + + function createFarmList($wref, $owner, $name) { + $q = "INSERT INTO " . TB_PREFIX . "farmlist (`wref`, `owner`, `name`) VALUES ('$wref', '$owner', '$name')"; + return mysql_query($q, $this->connection); + } + + function addSlotFarm($lid, $towref, $x, $y, $distance, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) { + $q = "INSERT INTO " . TB_PREFIX . "raidlist (`lid`, `towref`, `x`, `y`, `distance`, `t1`, `t2`, `t3`, `t4`, `t5`, `t6`, `t7`, `t8`, `t9`, `t10`) VALUES ('$lid', '$towref', '$x', '$y', '$distance', '$t1', '$t2', '$t3', '$t4', '$t5', '$t6', '$t7', '$t8', '$t9', '$t10')"; + return mysql_query($q, $this->connection); + } + + function editSlotFarm($eid, $lid, $wref, $x, $y, $dist, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) { + $q = "UPDATE " . TB_PREFIX . "raidlist set lid = '$lid', towref = '$wref', x = '$x', y = '$y', t1 = '$t1', t2 = '$t2', t3 = '$t3', t4 = '$t4', t5 = '$t5', t6 = '$t6', t7 = '$t7', t8 = '$t8', t9 = '$t9', t10 = '$t10' WHERE id = $eid"; + return mysql_query($q, $this->connection); + } + + function getArrayMemberVillage($uid){ + $q = 'SELECT a.wref, a.name, b.x, b.y from '.TB_PREFIX.'vdata AS a left join '.TB_PREFIX.'wdata AS b ON b.id = a.wref where owner = '.$uid.' order by capital DESC,pop DESC'; + $result = mysql_query($q, $this->connection); + $array = $this->mysql_fetch_all($result); + return $array; + } + + function addPassword($uid, $npw, $cpw){ + $q = "REPLACE INTO `" . TB_PREFIX . "password`(uid, npw, cpw) VALUES ($uid, '$npw', '$cpw')"; + mysql_query($q, $this->connection) or die(mysql_error()); + } + + function resetPassword($uid, $cpw){ + $q = "SELECT npw FROM `" . TB_PREFIX . "password` WHERE uid = $uid AND cpw = '$cpw' AND used = 0"; + $result = mysql_query($q, $this->connection) or die(mysql_error()); + $dbarray = mysql_fetch_array($result); + + if(!empty($dbarray)) { + if(!$this->updateUserField($uid, 'password', md5($dbarray['npw']), 1)) return false; + $q = "UPDATE `" . TB_PREFIX . "password` SET used = 1 WHERE uid = $uid AND cpw = '$cpw' AND used = 0"; + mysql_query($q, $this->connection) or die(mysql_error()); + return true; + } + + return false; + } + + function getCropProdstarv($wref) { + global $bid4,$bid8,$bid9,$sesion,$technology; + + $basecrop = $grainmill = $bakery = 0; + $owner = $this->getVrefField($wref, 'owner'); + $bonus = $this->getUserField($owner, b4, 0); + + $buildarray = $this->getResourceLevel($wref); + $cropholder = array(); + for($i=1;$i<=38;$i++) { + if($buildarray['f'.$i.'t'] == 4) { + array_push($cropholder,'f'.$i); + } + if($buildarray['f'.$i.'t'] == 8) { + $grainmill = $buildarray['f'.$i]; + } + if($buildarray['f'.$i.'t'] == 9) { + $bakery = $buildarray['f'.$i]; + } + } + $q = "SELECT type FROM `" . TB_PREFIX . "odata` WHERE conqured = $wref"; + $oasis = $this->query_return($q); + foreach($oasis as $oa){ + switch($oa['type']) { + case 1: + case 2: + $wood += 1; + break; + case 3: + $wood += 1; + $cropo += 1; + break; + case 4: + case 5: + $clay += 1; + break; + case 6: + $clay += 1; + $cropo += 1; + break; + case 7: + case 8: + $iron += 1; + break; + case 9: + $iron += 1; + $cropo += 1; + break; + case 10: + case 11: + $cropo += 1; + break; + case 12: + $cropo += 2; + break; + } + } + for($i=0;$i<=count($cropholder)-1;$i++) { $basecrop+= $bid4[$buildarray[$cropholder[$i]]]['prod']; } + $crop = $basecrop + $basecrop * 0.25 * $cropo; + if($grainmill >= 1 || $bakery >= 1) { + $crop += $basecrop /100 * ($bid8[$grainmill]['attri'] + $bid9[$bakery]['attri']); + } + if($bonus > time()) { + $crop *= 1.25; + } + $crop *= SPEED; + return $crop; + } + + //general statistics + + function addGeneralAttack($casualties) { + $time = time(); + $q = "INSERT INTO " . TB_PREFIX . "general values (0,'$casualties','$time',1)"; + return mysql_query($q, $this->connection) or die(mysql_error()); + } + + function getAttackByDate($time) { + $q = "SELECT * FROM " . TB_PREFIX . "general where shown = 1"; + $result = $this->query_return($q); + $attack = 0; + foreach($result as $general){ + if(date("j. M",$time) == date("j. M",$general['time'])){ + $attack += 1; + } + } + return $attack; + } + + function getAttackCasualties($time) { + $q = "SELECT * FROM " . TB_PREFIX . "general where shown = 1"; + $result = $this->query_return($q); + $casualties = 0; + foreach($result as $general){ + if(date("j. M",$time) == date("j. M",$general['time'])){ + $casualties += $general['casualties']; + } + } + return $casualties; + } + + //end general statistics + + function addFriend($uid, $column, $friend) { + $q = "UPDATE " . TB_PREFIX . "users SET $column = $friend WHERE id = $uid"; + return mysql_query($q, $this->connection); + } + + function deleteFriend($uid, $column) { + $q = "UPDATE " . TB_PREFIX . "users SET $column = 0 WHERE id = $uid"; + return mysql_query($q, $this->connection); + } + + function checkFriends($uid) { + $user = $this->getUserArray($uid, 1); + for($i=0;$i<=19;$i++) { + if($user['friend'.$i] == 0 && $user['friend'.$i.'wait'] == 0){ + for($j=$i+1;$j<=19;$j++) { + $k = $j-1; + if($user['friend'.$j] != 0){ + $friend = $this->getUserField($uid, "friend".$j, 0); + $this->addFriend($uid,"friend".$k,$friend); + $this->deleteFriend($uid,"friend".$j); + } + if($user['friend'.$j.'wait'] == 0){ + $friendwait = $this->getUserField($uid, "friend".$j."wait", 0); + $this->addFriend($sessionuid,"friend".$k."wait",$friendwait); + $this->deleteFriend($uid,"friend".$j."wait"); + } + } + } + } + } + + function setVillageEvasion($vid) { + $village = $this->getVillage($vid); + if($village['evasion'] == 0){ + $q = "UPDATE " . TB_PREFIX . "vdata SET evasion = 1 WHERE wref = $vid"; + }else{ + $q = "UPDATE " . TB_PREFIX . "vdata SET evasion = 0 WHERE wref = $vid"; + } + return mysql_query($q, $this->connection); + } + + function addPrisoners($wid,$from,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10,$t11) { + $q = "INSERT INTO " . TB_PREFIX . "prisoners values (0,$wid,$from,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10,$t11)"; + mysql_query($q, $this->connection); + return mysql_insert_id($this->connection); + } + + function updatePrisoners($wid,$from,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10,$t11) { + $q = "UPDATE " . TB_PREFIX . "prisoners set t1 = t1 + $t1, t2 = t2 + $t2, t3 = t3 + $t3, t4 = t4 + $t4, t5 = t5 + $t5, t6 = t6 + $t6, t7 = t7 + $t7, t8 = t8 + $t8, t9 = t9 + $t9, t10 = t10 + $t10, t11 = t11 + $t11 where wref = $wid and ".TB_PREFIX."prisoners.from = $from"; + return mysql_query($q, $this->connection) or die(mysql_error()); + } + + function getPrisoners($wid,$mode=0) { + if(!$mode) { + $q = "SELECT * FROM " . TB_PREFIX . "prisoners where wref = $wid"; + }else { + $q = "SELECT * FROM " . TB_PREFIX . "prisoners where `from` = $wid"; + } + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getPrisoners2($wid,$from) { + $q = "SELECT * FROM " . TB_PREFIX . "prisoners where wref = $wid and " . TB_PREFIX . "prisoners.from = $from"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function getPrisonersByID($id) { + $q = "SELECT * FROM " . TB_PREFIX . "prisoners where id = $id"; + $result = mysql_query($q, $this->connection); + return mysql_fetch_array($result); + } + + function getPrisoners3($from) { + $q = "SELECT * FROM " . TB_PREFIX . "prisoners where " . TB_PREFIX . "prisoners.from = $from"; + $result = mysql_query($q, $this->connection); + return $this->mysql_fetch_all($result); + } + + function deletePrisoners($id) { + $q = "DELETE from " . TB_PREFIX . "prisoners where id = '$id'"; + mysql_query($q, $this->connection); + } + + /*************************** + Function to get Hero Dead + Made by: Shadow and brainiacX + ***************************/ + + function getHeroDead($id) { + $q = "SELECT dead FROM " . TB_PREFIX . "hero WHERE `uid` = $id"; + $result = mysql_query($q, $this->connection); + $notend= mysql_fetch_array($result); + return $notend['dead']; + } + + /*************************** + Function to get Hero In Revive + Made by: Shadow + ***************************/ + + function getHeroInRevive($id) { + $q = "SELECT inrevive FROM " . TB_PREFIX . "hero WHERE `uid` = $id"; + $result = mysql_query($q, $this->connection); + $notend= mysql_fetch_array($result); + return $notend['inrevive']; + } + + /*************************** + Function to get Hero In Train + Made by: Shadow + ***************************/ + + function getHeroInTraining($id) { + $q = "SELECT intraining FROM " . TB_PREFIX . "hero WHERE `uid` = $id"; + $result = mysql_query($q, $this->connection); + $notend= mysql_fetch_array($result); + return $notend['intraining']; + } + + /*************************** + Function to check Hero Not in Village + Made by: Shadow and brainiacX + ***************************/ + + function HeroNotInVil($id) { + $heronum=0; + $outgoingarray = $this->getMovement(3, $id, 0); + if(!empty($outgoingarray)) { + foreach($outgoingarray as $out) { + $heronum += $out['t11']; + } + } + $returningarray = $this->getMovement(4, $id, 1); + if(!empty($returningarray)) { + foreach($returningarray as $ret) { + if($ret['attack_type'] != 1) { + $heronum += $ret['t11']; + } + } + } + return $heronum; + } + + /*************************** + Function to Kill hero if not found + Made by: Shadow and brainiacX + ***************************/ + + function KillMyHero($id) { + $q = "UPDATE " . TB_PREFIX . "hero set dead = 1 where uid = ".$id; + return mysql_query($q, $this->connection); + } + + /*************************** + Function checkAttack + Made by: Shadow + ***************************/ + + function checkAttack($wref, $toWref){ + $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement.from = $wref and " . TB_PREFIX . "movement.to = $toWref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and (" . TB_PREFIX . "attacks.attack_type = 3 or " . TB_PREFIX . "attacks.attack_type = 4) ORDER BY endtime ASC"; + $result = mysql_query($q, $this->connection); + if(mysql_num_rows($result)) { + return true; + } else { + return false; + } + } + + /*************************** + Function checkEnforce + Made by: Shadow + ***************************/ + + function checkEnforce($wref, $toWref) { + $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement.from = $wref and " . TB_PREFIX . "movement.to = $toWref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type = 2 ORDER BY endtime ASC"; + $result = mysql_query($q, $this->connection); + if(mysql_num_rows($result)) { + return true; + }else{ + return false; + } + } + + /*************************** + Function checkScout + Made by: yi12345 + ***************************/ + + function checkScout($wref, $toWref) { + $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement.from = $wref and " . TB_PREFIX . "movement.to = $toWref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type = 1 ORDER BY endtime ASC"; + $result = mysql_query($q, $this->connection); + if(mysql_num_rows($result)) { + return true; + }else{ + return false; + } + } + +}; + +$database = new MYSQL_DB; + +?>