Temos pavadinimas: WordPress, Shopify ir PHPFusion programuotojų bendruomenė :: Reikia pagalbos ištaisyti error'ui

Parašė Skirmantas.· 2011 Kov. 15 17:03:50
#7

Dar vienas... Nori tuo datebase.php? va imk:
<?php
/******Database.php*******
-- Class MYSQL_DB and MYSQLi_DB consists of the database releated functions of Travian Clone.
-- Revision: Beta 3
-- Author: akakori
-- Homepage: -
-- Beta Server: http://www.game-chaos.com/travian
-- Please do not remove this section
************************/
include("constant.php");

class MYSQL_DB {
   
   var $connection;
   
   function MYSQL_DB() {
      $this->connection = mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS) or die(mysql_error());
      mysql_select_db(SQL_DB, $this->connection) or die(mysql_error());
   }
   
   /***************************
   Function to check existance of email or the username.
   Mode 0: username;
   Mode 1: Email;
   References: Reference, Mode
   ***************************/
   function checkExist($ref,$mode) {
      if(!$mode) {
         $q = "SELECT username FROM ".TB_PREFIX."users where username = '$ref' LIMIT 1";
      }
      else {
         $q = "SELECT email FROM ".TB_PREFIX."users where email = '$ref' LIMIT 1";
      }
      $result = mysql_query($q, $this->connection);
      if(mysql_num_rows($result)) {
         return true;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to check the existing session if its correct.
   References: Username, Session ID
   ***************************/
   function checkactiveSession($username,$sessid) {
      $q = "SELECT username FROM ".TB_PREFIX."users where username = '$username' and sessid = '$sessid' LIMIT 1";
      $result = mysql_query($q, $this->connection);
      if(mysql_num_rows($result) != 0) {
         return true;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to update any field in users.
   References: Username, Field Name, Value
   ***************************/
   function updateUserField($username,$field,$value) {
      $q = "UPDATE ".TB_PREFIX."users set $field = '$value' where username = '$username'";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to login users
   References: Username, Password
   ***************************/
   function login($username,$password) {
      $q = "SELECT password,sessid FROM ".TB_PREFIX."users where username = '$username' and access != ".BANNED;
      $result = mysql_query($q, $this->connection);
      $dbarray = mysql_fetch_array($result);
      if($dbarray['password'] == md5($password)) {
         return true;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to register users
   References: Username, Password, Email, Tribe, Activation
   ***************************/
   function register($username,$password,$email,$tribe,$act) {
      $time = time();
      $q = "INSERT INTO ".TB_PREFIX."users (username,password,access,email,timestamp,tribe,act) VALUES ('$username', '$password', ".USER.", '$email', $time, $tribe, '$act')";
      if(mysql_query($q,$this->connection)) {
         return mysql_insert_id($this->connection);
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to delete users
   References: User ID
   ***************************/
   function deleteAccount($id) {
      $time = time();
      $allvillage = $this->getAVillage($id);
      for($i=0;$i<=count($allvillage)-1;$i++) {
         $q = "DELETE FROM ".TB_PREFIX."constdata where vref = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
         $q = "DELETE FROM ".TB_PREFIX."fdata where vref = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
         $q = "UPDATE ".TB_PREFIX."wdata set occupied = 1 where id = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
         $q = "DELETE FROM ".TB_PREFIX."baselog where vid = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
         $q = "DELETE FROM ".TB_PREFIX."market where vref = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
         $q = "DELETE FROM ".TB_PREFIX."movement where send = ".$allvillage[$i]['wref']." or recieve = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
         $q = "DELETE FROM ".TB_PREFIX."units where vref = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
         $q = "DELETE FROM ".TB_PREFIX."enforcement where vref = ".$allvillage[$i]['wref']." or from = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connnection);
         $q = "DELETE FROM ".TB_PREFIX."training where vref = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
         $q = "DELETE FROM ".TB_PREFIX."heroes where vref = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
         $q = "DELETE FROM ".TB_PREFIX."research where vref = ".$allvillage[$i]['wref'];
         mysql_query($q,$this->connection);
      }
      $q = "DELETE FROM ".TB_PREFIX."mdata where sender = $id or recieve = $id";
      mysql_query($q,$this->connection);
      $q = "DELETE FROM ".TB_PREFIX."userlink where uref = $id";
      mysql_query($q,$this->connection);
      $q = "DELETE FROM ".TB_PREFIX."mardata where sender = $id or recieve = $id";
      mysql_query($q,$this->connection);
      $q = "SELECT * FROM ".TB_PREFIX."vdata where owner = $id and town = 0";
      $result = mysql_query($q,$this->connection);
      if(mysql_num_rows($result) > 0) {
         $array = $this->mysql_fetch_all($result);
         for($i=0;$i<=count($array)-1;$i++) {
            $q = "UPDATE ".TB_PREFIX."wdata set occupied = 0 where id = ".$array[$i]['wref'];
            mysql_query($q,$this->connection);
         }
      }
      $q = "DELETE FROM ".TB_PREFIX."vdata where owner = $id";
      mysql_query($q,$this->connection);
      $q = "DELETE FROM ".TB_PREFIX."users where id = $id";
      mysql_query($q,$this->connection);
   }
   
   /**************************
   Function to calculate registered users
   References: -
   **************************/
   function countUsers() {
      $q = "SELECT * FROM ".TB_PREFIX."users";
      $result = mysql_query($q,$this->connection);
      $num_rows = mysql_num_rows($result);
      return $result;
   }
   
   /**************************
   Function to give or remove gold
   Mode 0: Remove
   Mode 1: Give
   References: User ID, Amount, Mode
   **************************/
   function modifyGold($id,$amt,$mode) {
      if(!$mode) {
         $q = "UPDATE ".TB_PREFIX."USERS set gold = gold - $amt where id = $id";
      }
      else {
         $q = "UPDATE ".TB_PREFIX."USERS set gold = gold + $amt where id = $id";
      }
      return mysql_query($q,$this->connection);
   }
   
   /**************************
   Function to check plus
   References: Userid
   **************************/
   function checkPlus($id) {
      $q = "SELECT plus from ".TB_PREFIX."users where id = $id";
      $result = mysql_query($q,$this->connection);
      if(mysql_result($result,0) > time()) {
         return true;
      }
      else {
         $this->modifyPlus($id,0);
      }
   }
   
   /**************************
   Function to give or remove plus
   Mode 0: Remove
   Mode 1: Give
   References: Userid, Mode
   **************************/
   function modifyPlus($id,$mode) {
      if(!$mode) {
         $q = "UPDATE ".TB_PREFIX."users set access = ".USER." where id = $id and access < ".MODERATOR."";
      }
      else {
         $q = "UPDATE ".TB_PREFIX."users set access = ".PLUS." where id = $id and access < ".MODERATOR."";
      }
      return mysql_query($q,$this->connection);
   }
   
   /**************************
   Function to generate base for new registration
   Sector 0: Random all
   Sector 1: -x, +y
   Sector 2: +x, +y
   Sector 3: -x, -y
   Sector 4: +x, -y
   References: Sector
   **************************/
   function generateBase($sector) {
      $count = $this->countUsers();
      $starter = -3;
      if($count > 20) {
         $starter = -3 * round($count/20);
      }
      switch($sector) {
         case 0:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and occupied = 0 and x > $starter and y > $starter LIMIT 200";
         break;
         case 1:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x < 0 and y > 0 and occupied = 0 LIMIT 200";
         break;
         case 2:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x > 0 and y > 0 and occupied = 0 LIMIT 200";
         break;
         case 3:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x < 0 and y < 0 and occupied = 0 LIMIT 200";
         break;
         case 4:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x > 0 and y < 0 and occupied = 0 LIMIT 200";
         break;
      }
         $result = mysql_query($q);
         $num_rows = mysql_num_rows($result);
         $result = $this->mysql_fetch_all($result);
         $base = rand(0, ($num_rows-1));
         return $result[$base]['id'];
   }
   
   /***************************
   Function to retrieve data from specific field in users
   Mode 0: Search by ID
   Mode 1: Search by Username
   References: Reference, Field, Mode
   ***************************/
   function getUserField($ref,$field,$mode) {
      if(!$mode) {
         $q = "SELECT $field FROM ".TB_PREFIX."users where id = $ref";
      }
      else {
         $q = "SELECT $field FROM ".TB_PREFIX."users where username = '$ref'";
      }
      $result = mysql_query($q, $this->connection);
      $dbarray = mysql_fetch_array($result);
      return $dbarray[$field];
   }
   
   /***************************
   Function to retrieve data from specific field in vdata
   References: Reference, Field
   ***************************/
   function getVillageField($ref,$field) {
      $q = "SELECT $field FROM ".TB_PREFIX."vdata where id = $ref";
      $result = mysql_query($q, $this->connection);
      $dbarray = mysql_fetch_array($result);
      return $dbarray[$field];
   }
   
   /***************************
   Function to retrieve alliance name via ID
   References: Alliance ID
   ***************************/
   function getUserAlliance($id) {
      $q = "SELECT ".TB_PREFIX."alidata.name from ".TB_PREFIX."users join ".TB_PREFIX."alidata where ".TB_PREFIX."users.alliance = ".TB_PREFIX."alidata.id";
      $result = mysql_query($q, $this->connection);
      $dbarray = mysql_fetch_array($result);
      return $dbarray['name'];
   }
   
   /***************************
   Function to retrieve user array via Username or ID
   Mode 0: Search by Username
   Mode 1: Search by ID
   References: Alliance ID
   ***************************/
   function getUserArray($ref,$mode) {
      if(!$mode) {
         $q = "SELECT * FROM ".TB_PREFIX."users where username = '$ref'";
      }
      else {
         $q = "SELECT * FROM ".TB_PREFIX."users where id = $ref";
      }
      $result = mysql_query($q, $this->connection);
      return mysql_fetch_array($result);
   }
   
   /***************************
   Function to update active user in database
   References: Username, Time
   ***************************/
   function updateActiveUser($username,$time) {
      $q = "UPDATE ".TB_PREFIX."users set timestamp = $time where username = '$username'";
      $exec1 = mysql_query($q,$this->connection);
      $q = "REPLACE into ".TB_PREFIX."active values ('$username',$time)";
      $exec2 = mysql_query($q, $this->connection);
      if($exec1 && $exec2) {
         return true;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to add or remove active user
   Mode 0: Add active
   Mode 1: Remove active
   References: Username, Mode
   ***************************/
   function activeModify($username,$mode) {
      $time = time();
      if(!$mode) {
         $q = "INSERT into ".TB_PREFIX."active VALUES ('$username',$time)";
      }
      else {
         $q = "DELETE FROM ".TB_PREFIX."active where username = '$username'";
      }
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to automatically clear inactive user
   References: Nil
   ***************************/
   function removeInactiveUsers(){
      $timeout = time()-USER_TIMEOUT*60;
      $q = "DELETE FROM ".TB_PREFIX."active WHERE timestamp < $timeout";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to add cultral points to village or users
   Mode 0: Add to village
   Mode 1: Add to user
   References: Reference, Points, Mode
   ***************************/
   function addCP($ref,$cp,$mode) {
      if(!$mode) {
         $q = "UPDATE ".TB_PREFIX."vdata set cp = cp + $cp where wref = $ref and town = 1";
      }
      else {
         $q = "UPDATE ".TB_PREFIX."users set cp = cp + $cp where id = $ref and town = 1";
      }
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to get world field details
   References: Field ID
   ***************************/
   function getFieldDetails($id) {
      $q = "SELECT * FROM ".TB_PREFIX."wdata where id = $id";
      $result = mysql_query($q, $this->connection);
      return mysql_fetch_array($result);
   }
   
   /***************************
   Function to set occupied on world data
   References: Field ID
   ***************************/
   function setFieldTaken($id) {
      $q = "UPDATE ".TB_PREFIX."wdata set occupied = 1 where id = $id";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to add village or oasis to user
   Mode 0: Add Village
   Mode 1: Add Oasis
   References: Field ID, User ID, Username, Capital, Mode
   ***************************/
   function addVillage($wid,$uid,$username,$capital,$oasis) {
      $total = $this->getAVillageCount($uid);
      if ($total >= 1) {
      $vname = $username."\'s village ".($total+1);
      }
      else {
         $vname = $username."\'s village";
      }
      $time = time();
      if($oasis) {
         $q = "INSERT into ".TB_PREFIX."vdata values ($wid, $uid, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, $time)";
      }
      else {
         $q = "INSERT into ".TB_PREFIX."vdata values ($wid, $uid, '$vname', $capital, 1, 2, 0, 750, 750, 750, 800, 750, 800, $time)";
      }
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to retrieve capital of user
   References: User ID
   ***************************/
   function getCapital($uid) {
      $q = "SELECT * FROM ".TB_PREFIX."vdata where owner = $uid and capital = 1 and town = 1";
      $result = mysql_query($q, $this->connection);
      return mysql_fetch_array($result);
   }
   
   /***************************
   Function to retrieve specific village of user
   References: Village ID, User ID
   ***************************/
   function getVillage($vid,$uid) {
      $q = "SELECT * FROM ".TB_PREFIX."vdata where wref = $vid and owner = $uid and town = 1";
      $result = mysql_query($q, $this->connection);
      return mysql_fetch_array($result);
   }
   
   /***************************
   Function to retrieve owner of village
   References: Village ID
   ***************************/
   function getVillageOwner($vid) {
      $q = "SELECT owner FROM ".TB_PREFIX."vdata where wref = $vid";
      $result = mysql_query($q, $this->connection);
      $dbarray = mysql_fetch_array($result);
      return $dbarray['owner'];
   }
   
   /***************************
   Function to count number of village owned by user
   References: User ID
   ***************************/
   function getAVillageCount($uid) {
      $q = "SELECT * FROM ".TB_PREFIX."vdata where owner = $uid and town = 1";
      $result = mysql_query($q, $this->connection);
      return mysql_num_rows($result);
   }
   
   /***************************
   Function to count number of pop in village owned by user
   References: User ID
   ***************************/
   function getAVillagePop($uid) {
      $q = "SELECT sum(pop) FROM ".TB_PREFIX."vdata where owner = $uid and town = 1";
      $result = mysql_query($q, $this->connection);
      $row = mysql_fetch_row($result);
      return $row[0];
   }
   
   /***************************
   Function to retrieve all village of user
   References: User ID
   ***************************/
   function getAVillage($uid) {
      $q = "SELECT * FROM ".TB_PREFIX."vdata where owner = $uid and town = 1";
      $result = mysql_query($q, $this->connection);
      return $this->mysql_fetch_all($result);;
   }
   
   /***************************
   Function to retrieve coordinate of village
   References: Village ID
   ***************************/
   function getCoor($wref) {
      $q = "SELECT x,y FROM ".TB_PREFIX."wdata where id = $wref";
      $result = mysql_query($q, $this->connection);
      return mysql_fetch_array($result);
   }
   
   /***************************
   Function to retrieve type of oasis via user id
   References: User ID
   ***************************/
   function getOasisType($uid) {
      $q = "select ".TB_PREFIX."_wdata.oasistype from ".TB_PREFIX."_wdata,".TB_PREFIX."_vdata where ".TB_PREFIX."_wdata.id = ".TB_PREFIX."_vdata.wref and ".TB_PREFIX."_vdata.town = 0 and ".TB_PREFIX."_vdata.owner = $uid";
      $result = mysql_query($q, $this->connection);
      if($result) {
      return mysql_fetch_array($result,MYSQL_NUM);
      }
      else {
         return array(0);
      }
   }
   
   /***************************
   Function to retrieve type of village via ID
   References: Village ID
   ***************************/
   function getVillageType($wref) {
      $q = "SELECT fieldtype FROM ".TB_PREFIX."wdata where id = $wref";
      $result = mysql_query($q, $this->connection);
      $dbarray = mysql_fetch_array($result);
      return $dbarray['fieldtype'];
   }
   
   /***************************
   Function to retrieve wref from village by name
   References: Village Name
   ***************************/
   function getVillageByName($name) {
      $name = mysql_real_escape_string($name,$this->connection);
      $q = "SELECT wref FROM ".TB_PREFIX."vdata where name = '$name'";
      $result = mysql_query($q,$this->connection);
      $dbarray = mysql_fetch_array($result);
      return $dbarray['wref'];
   }
   
   /***************************
   Function to update specific field of village
   References: Field, Value, Village ID
   ***************************/
   function putVillageStats($field, $value, $vid) {
      $q = "UPDATE ".TB_PREFIX."vdata set $field = $value where wref = $vid";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to set updated time
   References: Village ID
   ***************************/
   function updateVillage($vid) {
      $time = time();
      $q = "UPDATE ".TB_PREFIX."vdata set lastupdate = $time where wref = $vid";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to add resource fields for village
   References: Village ID, Village Type
   ***************************/
   function addResourceFields($vid,$type) {
      switch($type) {
         case 1:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,4,4,1,4,4,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";
         break;
         case 2:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,4,1,3,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";
         break;
         case 3:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,1,3,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";
         break;
         case 4:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,1,2,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";
         break;
         case 5:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,1,3,1,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";
         break;
         case 6:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,4,4,1,3,4,4,4,4,4,4,4,4,4,4,4,2,4,4,1,15)";
         break;
         case 7:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,4,1,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";
         break;
         case 8:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,4,4,1,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";
         break;
         case 9:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,4,4,1,1,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";
         break;
         case 10:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,4,1,2,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";
         break;
         case 11:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,1,1,3,1,4,4,3,3,4,4,3,1,4,4,2,4,4,1,15)";
         break;
         case 12:
         $q = "INSERT into ".TB_PREFIX."fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,1,1,2,2,3,4,4,3,3,4,4,1,4,1,2,1,1,15)";
         break;
      }
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to add construction fields
   References: Village ID
   ***************************/
   function addConst($vid) {
      $q = "INSERT into ".TB_PREFIX."constdata (vref) values ($vid)";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to add units default data
   References: Village ID
   ***************************/
   function addUnits($vid) {
      $q = "INSERT into ".TB_PREFIX."units (vref) values ($vid)";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to get running construction
   References: Village ID
   ***************************/
   function getResourceCount($vid) {
      $q = "SELECT count from ".TB_PREFIX."constdata where vref = $vid";
      $result = mysql_query($q, $this->connection);
      $dbarray = mysql_fetch_array($result);
      return $dbarray['count'];
   }
   
   /***************************
   Function to start resource upgrade
   Mode 1: Construction 1
   Mode 2: Construction 2
   Mode 3: Construction 3
   Mode 4: Construction 4
   References: Village ID, Field, Endtime, BID, Log ID, Construction Number
   ***************************/
   function startResourceUpgrade($vid,$field,$endtime,$type,$logid,$mode) {
      switch($mode) {
         case 1:
         $q = "UPDATE ".TB_PREFIX."constdata set upgrading1 = $field, endtime1 = $endtime, utype1 = $type, logid1 = $logid, count = count + 1 where vref = $vid";
         break;
         case 2:
         $q = "UPDATE ".TB_PREFIX."constdata set upgrading2 = $field, endtime2 = $endtime, utype2 = $type,logid2 = $logid, count = count + 1 where vref = $vid";
         break;
         case 3:
         $q = "UPDATE ".TB_PREFIX."constdata set upgrading3 = $field, endtime3 = $endtime, utype3 = $type,logid3 = $logid, count = count + 1 where vref = $vid";
         break;
         case 4:
         $q = "UPDATE ".TB_PREFIX."constdata set upgrading4 = $field, endtime4 = $endtime, utype4 = $type,logid4 = $logid, count = count + 1 where vref = $vid";
         break;
      }
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to cancel resource upgrade
   References: Village ID
   ***************************/
   function cancelResourceUpgrade($vid) {
      $count = $this->getResourceCount($vid);
      if($count > 0) {
         $count -= 1;
      }
      $q = "UPDATE ".TB_PREFIX."constdata set upgrading1 = 0 , endtime1 = 0 , utype1 = 0, logid1 = 0, count = $count where vref = $vid";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to get running upgrades and auto assign
   References: Village ID
   ***************************/
   function getRunningResource($vid) {
      $now = time();
      $q = "SELECT * from ".TB_PREFIX."constdata where vref = $vid";
      $result = mysql_query($q, $this->connection);
      $array = mysql_fetch_array($result);
      if($array['count'] != 0) {
            for($i=1;$i<=$array['count'];$i++) {
               if($array['upgrading'.$i] != 0) {
                  $_SESSION['runres'.$i] = $array['upgrading'.$i];
                  $_SESSION['runtype'.$i] = $array['utype'.$i];
                  $_SESSION['logid'.$i] = $array['logid'.$i];
               }
            }
         $timearray = array(($array['endtime1'] - $now),($array['endtime2'] - $now),($array['endtime3'] - $now),($array['endtime4'] - $now));
         return $timearray;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to get running data only
   References: Village ID
   ***************************/
   function getRunningData($vid) {
      $now = time();
      $q = "SELECT * from ".TB_PREFIX."constdata where vref = $vid";
      $result = mysql_query($q, $this->connection);
      return mysql_fetch_array($result,MYSQL_ASSOC);
   }
   
   /***************************
   Function to shift upgrades infront
   Mode 1: Shift 1-4
   Mode 2: Shift 2-4
   Mode 3: Shift 3-4
   Mode 4: Shift 4/ Clear 4
   Mode 5: Force finish All
   References: Village ID, Mode
   ***************************/
   function shiftUpgrading($vid,$mode) {
      $count = $this->getResourceCount($vid);
      if($count > 0) {
         $count -= 1;
      }
         switch($mode) {
            case 1:
            $q = "UPDATE ".TB_PREFIX."constdata set upgrading1 = upgrading2, endtime1 = endtime2,utype1 = utype2,logid1 = logid2, endtime2 = endtime3, upgrading2 = upgrading3, utype2 = utype3,logid2 = logid3, endtime3 = endtime4, upgrading3 = upgrading4, utype3 = utype4,logid3 = logid4, endtime4 = 0, upgrading4 = 0, utype4 = 0,logid4 = 0, count = $count where vref = $vid";
            break;
            case 2:
            $q = "UPDATE ".TB_PREFIX."constdata set endtime2 = endtime3, upgrading2 = upgrading3, utype2 = utype3,logid2 = logid3, endtime3 = endtime4, upgrading3 = upgrading4, utype3 = utype4,logid3 = logid4, endtime4 = 0, upgrading4 = 0, utype4 = 0,logid4 = 0, count = $count where vref = $vid";
            break;
            case 3:
            $q = "UPDATE ".TB_PREFIX."constdata set endtime3 = endtime4, upgrading3 = upgrading4, utype3 = utype4,logid3 = logid4, endtime4 = 0, upgrading4 = 0, utype4 = 0,logid4 = 0, count = count - 1 where vref = $vid";
            break;
            case 4:
            $q = "UPDATE ".TB_PREFIX."constdata set endtime4 = 0, upgrading4 = 0, utype4 = 0,logid4 = 0, count = $count where vref = $vid";
            break;
            case 5:
            $q = "REPLACE INTO ".TB_PREFIX."constdata (vref) values ($vid)";
            break;
         }
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to retrieve field levels and type
   References: Village ID
   ***************************/
   function getResourceLevel($vid) {
      $q = "SELECT * from ".TB_PREFIX."fdata where vref = $vid";
      $result = mysql_query($q, $this->connection);
      return mysql_fetch_assoc($result);
   }
   
   /***************************
   Function to up level resource field
   References: Field, Village ID
   ***************************/
   function updateResourceFields($field,$vid) {
      $q = "UPDATE ".TB_PREFIX."fdata set f".$field." = f".$field." + 1 where vref = $vid";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to set resource field type
   References: Village ID, Field, Type
   ***************************/
   function updateType($vid,$field,$type) {
      $q = "UPDATE ".TB_PREFIX."fdata set f".$field."t = $type where vref = $vid";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to modify resource
   Mode 0: Add resource
   Mode 1: Remove resource
   References: Village ID, Wood, Clay, Iron, Crop, Mode
   ***************************/
   function modifyResource($vid,$wood,$clay,$iron,$crop,$mode) {
      if(!$mode) {
         $q = "UPDATE ".TB_PREFIX."vdata set wood = wood - $wood, clay = clay - $clay, iron = iron - $iron, crop = crop - $crop where wref = $vid and town = 1";
      }
      else {
         $q = "UPDATE ".TB_PREFIX."vdata set wood = wood + $wood, clay = clay + $clay, iron = iron + $iron, crop = crop + $crop where wref = $vid and town = 1";
      }
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to consume crop
   References: Village ID, Crop
   ***************************/
   function villageUpkeep($vid,$upkeep) {
      $q = "UPDATE ".TB_PREFIX."vdata set crop = crop - $upkeep where wref = $vid and town = 1";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to prune resource except crop
   References: Village ID, Type
   ***************************/
   function pruneResource($vid,$resource) {
      $q = "UPDATE ".TB_PREFIX."vdata set $resource = maxstore where wref = $vid and town = 1";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to prune crop
   References: Village ID, crop
   ***************************/
   function pruneCrop($vid) {
      $q = "UPDATE ".TB_PREFIX."vdata set crop = maxcrop where wref = $vid and town = 1";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to modify population
   Mode 0: Add population
   Mode 1: Remove population
   References: Village ID, Population, Mode
   ***************************/
   function modifyPop($vid,$pop,$mode) {
      if(!$mode) {
         $q = "UPDATE ".TB_PREFIX."vdata set pop = pop + $pop where wref = $vid and town = 1";
      }
      else {
         $q = "UPDATE ".TB_PREFIX."vdata set pop = pop - $pop where wref = $vid and town = 1";
      }
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to add building log
   Type 1: Start Building - Field, Type, Pop
   Type 2: Start Research
   Type 3: Send Resource - Recieve Village, Time, Send ID
   Type 4: Train Unit - Type, Amt, Time
   References: Village ID, Type, References
   ***************************/
   function addRlog($vid,$type,$ref1,$ref2,$ref3) {
      $q = "INSERT into ".TB_PREFIX."baselog values (0, $vid, $type, $ref1, $ref2, $ref3, CURRENT_TIMESTAMP)";
      mysql_query($q, $this->connection);
      return mysql_insert_id($this->connection);
   }
   
   /***************************
   Function to search building log
   References: Village ID, Comment
   ***************************/
   function getRlog($vid,$like) {
      $q = "SELECT id FROM ".TB_PREFIX."baselog where comment like '$like' and vid = $vid";
      $result = mysql_query($q, $this->connection);
      $dbarray = mysql_fetch_array($result);
      return $dbarray['id'];
   }
   
   /***************************
   Function to remove building log
   References: Log ID
   ***************************/
   function removeRlog($id) {
      $q = "DELETE FROM ".TB_PREFIX."baselog where id = $id";
      return mysql_query($q, $this->connection);
   }

   /***************************
   Function to get Map info array (Obsolete)
   References: X, Y
   ***************************/
   function getMInfoArray($x,$y) {
      $q = "SELECT * FROM ".TB_PREFIX."wdata left JOIN ".TB_PREFIX."vdata ON ".TB_PREFIX."vdata.wref = ".TB_PREFIX."wdata.id where (x=".$x[0]." || x=".$x[1]." || x=".$x[2]." || x=".$x[3]."|| x=".$x[4]." || x=".$x[5]." || x=".$x[6].") and (y=".$y[0]." || y=".$y[1]." ||y=".$y[2]."||y=".$y[3]."||y=".$y[4]."||y=".$y[5]."||y=".$y[6].") order by ".TB_PREFIX."wdata.x asc, ".TB_PREFIX."wdata.y asc LIMIT 49";
      $result = mysql_query($q, $this->connection);
      return mysql_fetch_array($result,MYSQL_BOTH);
   }
   
   /***************************
   Function to get Map info by World ID
   References: World ID
   ***************************/
   function getMInfo($id) {
      $q = "SELECT * FROM ".TB_PREFIX."wdata left JOIN ".TB_PREFIX."vdata ON ".TB_PREFIX."vdata.wref = ".TB_PREFIX."wdata.id where ".TB_PREFIX."wdata.id = $id";
      $result = mysql_query($q, $this->connection);
      return mysql_fetch_array($result);
   }
   
   /***************************
   Function to get messages
   Mode 1: Get inbox
   Mode 2: Get sent
   Mode 3: Get message
   Mode 4: Set viewed
   Mode 5: Remove message
   Mode 6: Retrieve archive
   References: User ID/Message ID, Mode
   ***************************/
   function getMessage($id,$mode) {
      switch($mode) {
         case 1:
         $q = "SELECT * FROM ".TB_PREFIX."mdata WHERE recieve = $id";
         break;
         case 2:
         $q = "SELECT * FROM ".TB_PREFIX."mdata WHERE sender = $id";
         break;
         case 3:
         $q = "SELECT * FROM ".TB_PREFIX."mdata where id = $id";
         break;
         case 4:
         $q = "UPDATE ".TB_PREFIX."mdata set viewed = 1 where id = $id";
         break;
         case 5:
         $q = "DELETE FROM ".TB_PREFIX."mdata where id = $id";
         break;
         case 6:
         $q = "SELECT * FROM ".TB_PREFIX."mardata where recieve = $id";
         break;
      }
      if($mode <= 3 || $mode == 6) {
         $result = mysql_query($q, $this->connection);
         return $this->mysql_fetch_all($result);
      }
      else {
         return mysql_query($q, $this->connection);
      }
   }
   
   /***************************
   Function to see if there is unread message or notice
   Mode 0: Message
   Mode 1: Notice
   References: User, Mode
   ***************************/
   function gotUnread($id,$mode) {
      if(!$mode) {
         $q = "SELECT * FROM ".TB_PREFIX."mdata where recieve = $id & viewed = 0";
      }
      else {
         $q = "SELECT * FROM ".TB_PREFIX."notice where recieve = $id & viewed = 0";
      }
      $result = mysql_query($q, $this->connection);
      if(mysql_affected_rows($this->connection) != 0) {
         return true;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to process MYSQLi->fetch_all (Only exist in MYSQL)
   References: Result
   ***************************/
   function mysql_fetch_all($result) {
      $all = array();
      while ($row = mysql_fetch_assoc($result)){ $all[] = $row; }
      return $all;
      }
   
   /***************************
   Function to send message
   Mode 0: Normal Send
   Mode 1: Add to archive
   References: Reciever, Sender, Topic, Message, Mode
   ***************************/
   function sendMessage($client,$from,$topic,$message,$mode) {
      $time = time();
      $to = $this->getUserField($client,'id',1);
      if(!$mode) {
      $q = "INSERT INTO ".TB_PREFIX."mdata values (0,$to, $from,'$topic','$message',0,$time)";
      }
      else {
         $q = "INSERT INTO ".TB_PREFIX."mardata values (0,$to, $from,'$topic','$message',0,$time)";
      }
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to add movement to other village
   Type 0: Send Resource
   Type 1: Send Merchant
   Type 2: Return Resource
   Type 3: Attack
   Type 4: Return
   Type 5: Settler
   References: Type, From, To, Reference, Endtime
   ***************************/
   function addMovement($type,$from,$to,$ref,$endtime) {
      $q = "INSERT INTO ".TB_PREFIX."movement values (0,$type,$from,$to,$ref,$endtime,0)";
      return mysql_query($q, $this->connection);
   }
   
   /***************************
   Function to retrieve movement of village
   Type 0: Send Resource
   Type 1: Send Merchant
   Type 2: Return Resource
   Type 3: Attack
   Type 4: Return
   Type 5: Settler
   Mode 0: Send/Out
   Mode 1: Recieve/In
   References: Type, Village, Mode
   ***************************/
   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"; break;
         case 2: $q = "SELECT * FROM ".TB_PREFIX."movement where ".TB_PREFIX."movement.".$where." = $village and proc = 0 and type = 2"; break;
      }
      $result = mysql_query($q, $this->connection);
      $array = $this->mysql_fetch_all($result);
      return $array;
   }
   
   /***************************
   Function to set process flag on movement
   References: Moveid
   ***************************/
   function setMovementProc($moveid) {
      $q = "UPDATE ".TB_PREFIX."movement set proc = 1 where moveid = $moveid";
      return mysql_query($q,$this->connection);
   }
   
   /***************************
   Function to set accept flag on market
   References: id
   ***************************/
   function setMarketAcc($id) {
      $q = "UPDATE ".TB_PREFIX."movement set accept = 1 where id = $id";
      return mysql_query($q,$this->connection);
   }
   
   /***************************
   Function to send resource to other village
   Mode 0: Send
   Mode 1: Cancel
   References: Wood/ID, Clay, Iron, Crop, Mode
   ***************************/
   function sendResource($ref,$clay,$iron,$crop,$merchant,$mode) {
      if(!$mode) {
         $q = "INSERT INTO ".TB_PREFIX."send values (0,$ref,$clay,$iron,$crop,$merchant)";
         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 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->getVillageOwner($vid),"alliance",0);
      if(!$mode) {
         $q = "SELECT * FROM ".TB_PREFIX."market where vref = $vid";
      }
      else {
         $q = "SELECT * FROM ".TB_PREFIX."market where vref != $vid and alliance = $alliance or vref != $vid and alliance = 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 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 type = 0";
       $result = mysql_query($q, $this->connection);
       $row = mysql_fetch_row($result);
       $q2 = "SELECT sum(ref) from ".TB_PREFIX."movement where 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";
       $result3 = mysql_query($q3, $this->connection);
       $row3 = mysql_fetch_row($result3);
       return $row[0]+$row2[0]+$row3[0];
   }
   
   /***************************
   Function to get unit array
   Mode 0: From
   Mode 1: Vref
   References: Village, Mode
   ***************************/
   function getEnforce($vid,$mode) {
      if($mode) {
         $q = "SELECT * from ".TB_PREFIX."enforcement where vref = $vid";
      }
      else {
         $q = "SELECT * from ".TB_PREFIX."enforcement where from = $vid";
      }
      $result = mysql_query($q,$this->connection);
      return $this->mysql_fetch_all($result);
   }
   
   /***************************
   Function to get unit array
   References: Village
   ***************************/
   function getUnit($vid) {
      $q = "SELECT * from ".TB_PREFIX."units where vref = $vid";
      $result = mysql_query($q,$this->connection);
      return mysql_fetch_assoc($result);
   }
   
   /***************************
   Function to get unit array
   Mode 0: Train
   Mode 1: Cancel
   References: Village, Unit, Amount, Each, Time, Mode
   ***************************/
   function trainUnit($vid,$unit,$amt,$each,$time,$mode) {
      if(!$mode) {
         $q = "INSERT INTO ".TB_PREFIX."training values (0,$vid,$unit,$amt,$time,$each)";
      }
      else {
         $q = "DELETe FROM ".TB_PREFIX."training where id = $vid";
      }
      return mysql_query($q,$this->connection);
   }
   
   /***************************
   Function to get unit array
   References: ID
   ***************************/
   function updateTraining($id) {
      $q = "UPDATE INTO ".TB_PREFIX."training set amt = amt - 1 where id = $id";
      return mysql_query($q,$this->connection);
   }
   
   /***************************
   Function to get oncurrent training
   References: Village
   ***************************/
   function getTraining($vid) {
      $q = "SELECT * FROM ".TB_PREFIX."training where vref = $vid";
      $result = mysql_query($q,$this->connection);
      return $this->mysql_fetch_all($result);
   }
   
   /***************************
   Function to get modify unit
   Mode 0: Remove
   Mode 1: Add
   References: Village, Unit, Amount, Mode
   ***************************/
   function modifyUnit($vref,$unit,$amt,$mode) {
      $unit = 'u'.$unit;
      if(!$mode) {
         $q = "UPDATE ".TB_PREFIX."units set $unit = $unit - $amt where vref = $vref";
      }
      else {
         $q = "UPDATE ".TB_PREFIX."units set $unit = $unit + $amt where vref = $vref";
      }
      return mysql_query($q,$this->connection);
   }
   
   /***************************
   Function to get tech array
   References: Village
   ***************************/
   function getTech($vid) {
      $q = "SELECT tech from ".TB_PREFIX."research where vref = $vid and timestamp <= time()";
      $result = mysql_query($q, $this->connection);
      $holder = array();
      if(mysql_affected_rows($this->connection) != 0) {
         foreach($this->mysql_fetch_all($result) as $value) {
            array_push($holder,$value['tech']);
         }
         return $holder;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to get ranking array
   References: -
   ***************************/
   function getRanking() {
      if(INCLUDE_ADMIN) {
         $q = "SELECT id,username,cp,ap,dp FROM ".TB_PREFIX."users where access != ".BANNED;
      }
      else {
         $q = "SELECT id,username,cp,ap,dp FROM ".TB_PREFIX."users where access = 1";
      }
      $result = mysql_query($q, $this->connection);
      return $this->msqyl_fetch_all($result);
   }
   
   /***************************
   Function to do free query
   References: Query
   ***************************/
   function query($query) {
      return mysql_query($query, $this->connection);
   }
};

class MYSQLi_DB {
   
   var $connection;
   
   function MYSQLi_DB() {
      $this->connection = mysqli_connect(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB) or die(mysqli_error());
   }
   
   /***************************
   Function to check existance of email or the username.
   Mode 0: username;
   Mode 1: Email;
   References: Reference, Mode
   ***************************/
   function checkExist($ref,$mode) {
      if(!$mode) {
         $q = "SELECT username FROM ".TB_PREFIX."users where username = '$ref' LIMIT 1";
      }
      else {
         $q = "SELECT email FROM ".TB_PREFIX."users where email = '$ref' LIMIT 1";
      }
      $result = $this->connection->query($q);
      if(mysqli_num_rows($result)) {
         return true;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to check the existing session if its correct.
   References: Username, Session ID
   ***************************/
   function checkactiveSession($username,$sessid) {
      $q = "SELECT username FROM ".TB_PREFIX."users where username = '$username' and sessid = '$sessid' LIMIT 1";
      $result = $this->connection->query($q);
      if(mysqli_num_rows($result) != 0) {
         return true;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to update any field in users.
   References: Username, Field Name, Value
   ***************************/
   function updateUserField($username,$field,$value) {
      $q = "UPDATE ".TB_PREFIX."users set $field = '$value' where username = '$username'";
      return $this->connection->query($q);
   }
   
   /***************************
   Function to login users
   References: Username, Password
   ***************************/
   function login($username,$password) {
      $q = "SELECT password FROM ".TB_PREFIX."users where username = '$username' and access != ".BANNED;
      $result = $this->connection->query($q);
      $row = $result->fetch_assoc();
      if($row['password'] == md5($password)) {
         return true;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to register users
   References: Username, Password, Email, Tribe, Activation
   ***************************/
   function register($username,$password,$email,$tribe,$act) {
      $time = time();
      $q = "INSERT INTO ".TB_PREFIX."users (username,password,access,email,timestamp,tribe,act) VALUES ('$username', '$password', ".USER.", '$email', $time, $tribe, '$act')";
      if($this->connection->query($q)) {
         return $this->connection->insert_id;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to delete users
   References: User ID
   ***************************/
   function deleteAccount($id) {
      $time = time();
      $allvillage = $this->getAVillage($id);
      for($i=0;$i<=count($allvillage)-1;$i++) {
         $q = "DELETE FROM ".TB_PREFIX."constdata where vref = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "DELETE FROM ".TB_PREFIX."fdata where vref = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "UPDATE ".TB_PREFIX."wdata set occupied = 1 where id = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "DELETE FROM ".TB_PREFIX."baselog where vid = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "DELETE FROM ".TB_PREFIX."market where vref = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "DELETE FROM ".TB_PREFIX."movement where send = ".$allvillage[$i]['wref']." or recieve = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "DELETE FROM ".TB_PREFIX."units where vref = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "DELETE FROM ".TB_PREFIX."enforcement where vref = ".$allvillage[$i]['wref']." or from = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "DELETE FROM ".TB_PREFIX."training where vref = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "DELETE FROM ".TB_PREFIX."heroes where vref = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
         $q = "DELETE FROM ".TB_PREFIX."research where vref = ".$allvillage[$i]['wref'];
         $this->connection->query($q);
      }
      $q = "DELETE FROM ".TB_PREFIX."mdata where sender = $id or recieve = $id";
      $this->connection->query($q);
      $q = "DELETE FROM ".TB_PREFIX."userlink where uref = $id";
      $this->connection->query($q);
      $q = "DELETE FROM ".TB_PREFIX."mardata where sender = $id or recieve = $id";
      $this->connection->query($q);
      $q = "SELECT * FROM ".TB_PREFIX."vdata where owner = $id and town = 0";
      $result = $this->connection->query($q);
      if($this->connection->affected_rows > 0) {
         $array = $this->mysql_fetch_all($result);
         for($i=0;$i<=count($array)-1;$i++) {
            $q = "UPDATE ".TB_PREFIX."wdata set occupied = 0 where id = ".$array[$i]['wref'];
            $this->connection->query($q);
         }
      }
      $q = "DELETE FROM ".TB_PREFIX."vdata where owner = $id";
      $this->connection->query($q);
      $q = "DELETE FROM ".TB_PREFIX."users where id = $id";
      $this->connection->query($q);
   }
   
   /**************************
   Function to calculate registered users
   References: -
   **************************/
   function countUsers() {
      $q = "SELECT * FROM ".TB_PREFIX."users";
      $result = $this->connection->query($q);
      return $result->num_rows;
   }
   
   
   /**************************
   Function to give or remove gold
   Mode 0: Remove
   Mode 1: Give
   References: User ID, Amount, Mode
   **************************/
   function modifyGold($id,$amt,$mode) {
      if(!$mode) {
         $q = "UPDATE ".TB_PREFIX."USERS set gold = gold - $amt where id = $id";
      }
      else {
         $q = "UPDATE ".TB_PREFIX."USERS set gold = gold + $amt where id = $id";
      }
      return $this->connection->query($q);
   }
   
   /**************************
   Function to check plus
   References: Userid
   **************************/
   function checkPlus($id) {
      $q = "SELECT plus from ".TB_PREFIX."users where id = $id";
      $result = $this->connection->query($q);
      $row = $result->fetch_assoc();
      if($row['plus'] > time()) {
         return true;
      }
      else {
         $this->modifyPlus($id,0);
      }
   }
   
   /**************************
   Function to give or remove plus
   Mode 0: Remove
   Mode 1: Give
   References: Userid, Mode
   **************************/
   function modifyPlus($id,$mode) {
      if(!$mode) {
         $q = "UPDATE ".TB_PREFIX."users set access = ".USER." where id = $id and access < ".MODERATOR."";
      }
      else {
         $q = "UPDATE ".TB_PREFIX."users set access = ".PLUS." where id = $id and access < ".MODERATOR."";
      }
      return $this->connection->query($q);
   }
   
   /**************************
   Function to generate base for new registration
   Sector 0: Random all
   Sector 1: -x, +y
   Sector 2: +x, +y
   Sector 3: -x, -y
   Sector 4: +x, -y
   References: Sector
   **************************/
   function generateBase($sector) {
      $count = $this->countUsers();
      $starter = -3;
      if($count > 20) {
         $starter = -3 * round($count/20);
      }
      switch($sector) {
         case 0:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and occupied = 0 and x > $starter and y > $starter LIMIT 200";
         break;
         case 1:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x < 0 and y > 0 and occupied = 0 LIMIT 200";
         break;
         case 2:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x > 0 and y > 0 and occupied = 0 LIMIT 200";
         break;
         case 3:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x < 0 and y < 0 and occupied = 0 LIMIT 200";
         break;
         case 4:
         $q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x > 0 and y < 0 and occupied = 0 LIMIT 200";
         break;
      }
         $result = $this->connection->query($q);
         $num_rows = $result->num_rows;
         $row = $result->fetch_all(MYSQLI_ASSOC);
         $base = rand(0, ($num_rows-1));
         return $row[$base]['id'];
   }
   
   /***************************
   Function to retrieve data from specific field in users
   Mode 0: Search by ID
   Mode 1: Search by Username
   References: Reference, Field, Mode
   ***************************/
   function getUserField($ref,$field,$mode) {
      if(!$mode) {
         $q = "SELECT $field FROM ".TB_PREFIX."users where id = $ref";
      }
      else {
         $q = "SELECT $field FROM ".TB_PREFIX."users where username = '$ref'";
      }
      $result = $this->connection->query($q);
      $obj = $result->fetch_object();
      return $obj->$field;
   }
   
   /***************************
   Function to retrieve data from specific field in vdata
   References: Reference, Field
   ***************************/
   function getVillageField($ref,$field) {
      $q = "SELECT $field FROM ".TB_PREFIX."vdata where wref = $ref";
      $result = $this->connection->query($q);
      $obj = $result->fetch_object();
      return $obj->$field;
   }
   
   /***************************
   Function to retrieve alliance name via ID
   References: Alliance ID
   ***************************/
   function getUserAlliance($id) {
      $q = "SELECT ".TB_PREFIX."alidata.name from ".TB_PREFIX."users join ".TB_PREFIX."alidata where ".TB_PREFIX."users.alliance = ".TB_PREFIX."alidata.id";
      $result = $this->connection->query($q);
      $obj = $result->fetch_object();
      if($this->connection->affected_rows != 0) {
      return $obj->name;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to retrieve user array via Username or ID
   Mode 0: Search by Username
   Mode 1: Search by ID
   References: Alliance ID
   ***************************/
   function getUserArray($ref,$mode) {
      if(!$mode) {
         $q = "SELECT * FROM ".TB_PREFIX."users where username = '$ref'";
      }
      else {
         $q = "SELECT * FROM ".TB_PREFIX."users where id = $ref";
      }
      $result = $this->connection->query($q);
      return $result->fetch_assoc();
   }
   
   /***************************
   Function to update active user in database
   References: Username, Time
   ***************************/
   function updateActiveUser($username,$time) {
      $q = "UPDATE ".TB_PREFIX."users set timestamp = $time where username = '$username'";
      $exec1 = $this->connection->query($q);
      $q = "REPLACE into ".TB_PREFIX."active values ('$username',$time)";
      $exec2 = $this->connection->query($q);
      if($exec1 && $exec2) {
         return true;
      }
      else {
         return false;
      }
   }
   
   /***************************
   Function to add or remove active user
   Mode 0: Add active
   Mode 1: Remove active
   References: Username, Mode
   ***************************/
   function activeModify($username,$mode) {
      $time = time();
      if(!$mode) {
         $q = "INSERT into ".TB_PREFIX."active VALUES ('$username',$time)";
      }
      else {
         $q = "DELETE FROM ".TB_PREFIX."active where username = '$username'";
      }
      return $this->connection->query($q);
   }
   
   /***************************
   Function to automatically clear inactive user
   References: Nil
   ***************************/
   function removeInactiveUsers(){
      $timeout = time()-USER_TIMEOUT*60;
      $q = "DELETE FROM ".TB_PREFIX."active WHERE timestamp < $timeout";
      return $this->connection->query($q);
   }
   
   /***************************
   Function to add cultral points to village or users
   Mode 0: Add to village
   Mode 1: Add to user
   References: Reference, Points, Mode
   ***************************/
   function addCP($ref,$cp,$mode) {
      if(!$mode) {
         $q = "UPDATE ".TB_PREFIX."vdata set cp = cp + $cp where wref = $ref and town = 1";
      }
      else {
         $q = "UPDATE ".TB_PREFIX."users set cp = cp + $cp where id = $ref and town = 1";
      }
      return $this->connection->query($q);
   }
   
   /***************************
   Function to get world field details
   References: Field ID
   ***************************/
   function getFieldDetails($id) {
      $q = "SELECT * FROM ".TB_PREFIX."wdata where id = $id";
      $result = $this->connection->query($q);
      return $result->fetch_assoc();
   }
   
   /***************************
   Function to set occupied on world data
   References: Field ID
   ***************************/
   function setFieldTaken($id) {
      $q = "UPDATE ".TB_PREFIX."wdata set occupied = 1 where id = $id";
      return $this->connection->query($q);
   }
   
   /***************************
   Function to add village or oasis to user
   Mode 0: Add Village
   Mode 1: Add Oasis
   References: Field ID, User ID, Username, Capital, Mode
   ***************************/
   function addVillage($wid,$uid,$username,$capital,$oasis) {
      $total = $this->getAVillageCount($uid);
      if ($total >= 1) {
      $vname = $username."\'s village ".($total+1);
      }
      else {
         $vname = $username."\'s village";
      }
      $time = time();
      if($oasis) {
         $q = "INSERT into ".TB_PREFIX."vdata values ($wid, $uid, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, $time)";
      }
      else {
         $q = "INSERT into ".TB_PREFIX."vdata values ($wid, $uid, '$vname', $capital, 1, 2, 0, 750, 750, 750, 800, 750, 800, $time)";
      }
      return $this->connection->query($q);
   }
   
   /***************************
   Function to retrieve capital of user
   References: User ID
   ***************************/
   function getCapital($uid) {
      $q = "SELECT * FROM ".TB_PREFIX."vdata where owner = $uid and capital = 1 and town = 1";
      $result = $this->connection->query($q);
      return $result->fetch_assoc();
   }
   
   /***************************
   Function to retrieve any village of user
   References: Village ID, User ID
   ***************************/
   function getVillage($vid,$uid) {
      $q = "SELECT * FROM ".TB_PREFIX."vdata where wref = $vid and owner = $uid and town = 1";
      $result = $this->connection->query($q);
      return $result->fetch_assoc();
   }
   
   /***************************
   Function to retrieve owner of village
   References: Village ID
   ***************************/
   function getVillageOwner($vid) {
      $q = "SELECT owner FROM ".TB_PREFIX."vdata where wref = $vid";
      $result = $this->connection->query($q);
      $row = $result->fetch_assoc();
      return $row['owner'];
   }
   
   /***************************
   Function to count number of village owned by user
   References: User ID
   ***************************/
   function getAVillageCount($uid) {
      $q = "SELECT * FROM ".TB_PREFIX."vdata where owner = $uid and town = 1";
      $result = $this->connection->query($q);
      return $result->num_rows;
   }
   
   /***************************
   Function to count number of pop in village owned by user
   References: User ID
   ***************************/
   function getAVillagePop($uid) {
      $q = "SELECT sum(pop) FROM ".TB_PREFIX."vdata where owner = $uid and town = 1";
      $result = $this->connection->query($q);
      $row = $result->fetch_row();
      return $row[0];
   }
   
   /***************************
   Function to retrieve all village of user
   References: User ID
   ***************************/
   function getAVillage($uid) {
      $q = "SELECT * FROM ".TB_PREFIX."vdata where owner = $uid and town = 1";
      $result = $this->connection->query($q);
      return $result->fetch_all(MYSQLI_ASSOC);
   }
   
   /***************************
   Function to retrieve coordinate of village
   References: Village ID
   ***************************/
   function getCoor($wref) {
      $q = "SELECT x,y FROM ".TB_PREFIX."wdata where id = $wref";
      $result = $this->connection->query($q);
      return $result->fetch_assoc();
   }
   
   /***************************
   Function to retrieve type of oasis via user id
   References: User ID
   ***************************/
   function getOasisType($uid) {
      $q = "select ".TB_PREFIX."_wdata.oasistype from ".TB_PREFIX."_wdata,".TB_PREFIX."_vdata where ".TB_PREFIX."_wdata.id = ".TB_PREFIX."_vdata.wref and ".TB_PREFIX."_vdata.town = 0 and ".TB_PREFIX."_vdata.owner = $uid";
      $result = $this->connection->query($q);
      if($result) {
      return $result->fetch_all(MYSQL_NUM);
      }
      else {
         return array(0);
      }
   }
   
   /***************************
   Function to retrieve type of village via ID
   References: Village ID
   ***************************/
   function getVillageType($wref) {
      $q = "SELECT fieldtype FROM ".TB_PREFIX."wdata where id = $wref";
      $result = $this->connection->query($q);
      $row = $result->fetch_assoc();
      return $row['fieldtype'];
   }




(čia baigiasi simboliai leistini simboliai)

Redagavo Skirmantas.· 2011 Kov. 15 17:03:31