PageRenderTime 39ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/GameEngine/db_MYSQL.php

https://bitbucket.org/Dzoki/travianx
PHP | 2277 lines | 1804 code | 266 blank | 207 comment | 269 complexity | 67a2c7e919d7aef4cd73b08d1dac7edc MD5 | raw file
  1. <?php
  2. /** --------------------------------------------------- **\
  3. * | ********* DO NOT REMOVE THIS COPYRIGHT NOTICE ********* |
  4. * +---------------------------------------------------------+
  5. * | Credits: All the developers including the leaders: |
  6. * | Advocaite & Dzoki & Donnchadh |
  7. * | |
  8. * | Copyright: TravianX Project All rights reserved |
  9. * \** --------------------------------------------------- **/
  10. class MYSQL_DB {
  11. var $connection;
  12. function MYSQL_DB() {
  13. $this->connection = mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS) or die(mysql_error());
  14. mysql_select_db(SQL_DB, $this->connection) or die(mysql_error());
  15. }
  16. function register($username, $password, $email, $tribe, $locate, $act) {
  17. $time = time();
  18. $timep = (time() + PROTECTION);
  19. $q = "INSERT INTO " . TB_PREFIX . "users (username,password,access,email,timestamp,tribe,location,act,protect) VALUES ('$username', '$password', " . USER . ", '$email', $time, $tribe, $locate, '$act', $timep)";
  20. if(mysql_query($q, $this->connection)) {
  21. return mysql_insert_id($this->connection);
  22. } else {
  23. return false;
  24. }
  25. }
  26. function activate($username, $password, $email, $tribe, $locate, $act, $act2) {
  27. $time = time();
  28. $q = "INSERT INTO " . TB_PREFIX . "activate (username,password,access,email,tribe,timestamp,location,act,act2) VALUES ('$username', '$password', " . USER . ", '$email', $tribe, $time, $locate, '$act', '$act2')";
  29. if(mysql_query($q, $this->connection)) {
  30. return mysql_insert_id($this->connection);
  31. } else {
  32. return false;
  33. }
  34. }
  35. function unreg($username) {
  36. $q = "DELETE from " . TB_PREFIX . "activate where username = '$username'";
  37. return mysql_query($q, $this->connection);
  38. }
  39. function deleteReinf($id) {
  40. $q = "DELETE from " . TB_PREFIX . "enforcement where id = '$id'";
  41. mysql_query($q, $this->connection);
  42. }
  43. function updateResource($vid, $what, $number) {
  44. $q = "UPDATE " . TB_PREFIX . "vdata set " . $what . "=" . $number . " where wref = $vid";
  45. $result = mysql_query($q, $this->connection);
  46. return mysql_query($q, $this->connection);
  47. }
  48. function checkExist($ref, $mode) {
  49. if(!$mode) {
  50. $q = "SELECT username FROM " . TB_PREFIX . "users where username = '$ref' LIMIT 1";
  51. } else {
  52. $q = "SELECT email FROM " . TB_PREFIX . "users where email = '$ref' LIMIT 1";
  53. }
  54. $result = mysql_query($q, $this->connection);
  55. if(mysql_num_rows($result)) {
  56. return true;
  57. } else {
  58. return false;
  59. }
  60. }
  61. function checkExist_activate($ref, $mode) {
  62. if(!$mode) {
  63. $q = "SELECT username FROM " . TB_PREFIX . "activate where username = '$ref' LIMIT 1";
  64. } else {
  65. $q = "SELECT email FROM " . TB_PREFIX . "activate where email = '$ref' LIMIT 1";
  66. }
  67. $result = mysql_query($q, $this->connection);
  68. if(mysql_num_rows($result)) {
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. }
  74. public function hasBeginnerProtection($vid) {
  75. $q = "SELECT u.protect FROM ".TB_PREFIX."users u,".TB_PREFIX."vdata v WHERE u.id=v.owner AND v.wref=".$vid;
  76. $result = mysql_query($q, $this->connection);
  77. $dbarray = mysql_fetch_array($result);
  78. if(!empty($dbarray)) {
  79. if(time()<$dbarray[0]) {
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. } else {
  85. return false;
  86. }
  87. }
  88. function updateUserField($ref, $field, $value, $switch) {
  89. if(!$switch) {
  90. $q = "UPDATE " . TB_PREFIX . "users set $field = '$value' where username = '$ref'";
  91. } else {
  92. $q = "UPDATE " . TB_PREFIX . "users set $field = '$value' where id = '$ref'";
  93. }
  94. return mysql_query($q, $this->connection);
  95. }
  96. function getSitee($uid) {
  97. $q = "SELECT id from " . TB_PREFIX . "users where sit1 = $uid or sit2 = $uid";
  98. $result = mysql_query($q, $this->connection);
  99. return $this->mysql_fetch_all($result);
  100. }
  101. function removeMeSit($uid, $uid2) {
  102. $q = "UPDATE " . TB_PREFIX . "users set sit1 = 0 where id = $uid and sit1 = $uid2";
  103. mysql_query($q, $this->connection);
  104. $q2 = "UPDATE " . TB_PREFIX . "users set sit2 = 0 where id = $uid and sit2 = $uid2";
  105. mysql_query($q2, $this->connection);
  106. }
  107. function getUserField($ref, $field, $mode) {
  108. if(!$mode) {
  109. $q = "SELECT $field FROM " . TB_PREFIX . "users where id = '$ref'";
  110. } else {
  111. $q = "SELECT $field FROM " . TB_PREFIX . "users where username = '$ref'";
  112. }
  113. $result = mysql_query($q, $this->connection) or die(mysql_error());
  114. $dbarray = mysql_fetch_array($result);
  115. return $dbarray[$field];
  116. }
  117. function getActivateField($ref, $field, $mode) {
  118. if(!$mode) {
  119. $q = "SELECT $field FROM " . TB_PREFIX . "activate where id = '$ref'";
  120. } else {
  121. $q = "SELECT $field FROM " . TB_PREFIX . "activate where username = '$ref'";
  122. }
  123. $result = mysql_query($q, $this->connection) or die(mysql_error());
  124. $dbarray = mysql_fetch_array($result);
  125. return $dbarray[$field];
  126. }
  127. function login($username, $password) {
  128. $q = "SELECT password,sessid FROM " . TB_PREFIX . "users where username = '$username' and access != " . BANNED;
  129. $result = mysql_query($q, $this->connection);
  130. $dbarray = mysql_fetch_array($result);
  131. if($dbarray['password'] == md5($password)) {
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. }
  137. function checkActivate($act) {
  138. $q = "SELECT * FROM " . TB_PREFIX . "activate where act = '$act'";
  139. $result = mysql_query($q, $this->connection);
  140. $dbarray = mysql_fetch_array($result);
  141. return $dbarray;
  142. }
  143. function sitterLogin($username, $password) {
  144. $q = "SELECT sit1,sit2 FROM " . TB_PREFIX . "users where username = '$username' and access != " . BANNED;
  145. $result = mysql_query($q, $this->connection);
  146. $dbarray = mysql_fetch_array($result);
  147. if($dbarray['sit1'] != 0) {
  148. $q2 = "SELECT password FROM " . TB_PREFIX . "users where id = " . $dbarray['sit1'] . " and access != " . BANNED;
  149. $result2 = mysql_query($q2, $this->connection);
  150. $dbarray2 = mysql_fetch_array($result2);
  151. } else
  152. if($dbarray['sit2'] != 0) {
  153. $q3 = "SELECT password FROM " . TB_PREFIX . "users where id = " . $dbarray['sit2'] . " and access != " . BANNED;
  154. $result3 = mysql_query($q3, $this->connection);
  155. $dbarray3 = mysql_fetch_array($result3);
  156. }
  157. if($dbarray['sit1'] != 0 || $dbarray['sit2'] != 0) {
  158. if($dbarray2['password'] == md5($password) || $dbarray3['password'] == md5($password)) {
  159. return true;
  160. } else {
  161. return false;
  162. }
  163. } else {
  164. return false;
  165. }
  166. }
  167. function setDeleting($uid, $mode) {
  168. $time = time() + 72 * 3600;
  169. if(!$mode) {
  170. $q = "INSERT into " . TB_PREFIX . "deleting values ($uid,$time)";
  171. } else {
  172. $q = "DELETE FROM " . TB_PREFIX . "deleting where uid = $uid";
  173. }
  174. mysql_query($q, $this->connection);
  175. }
  176. function isDeleting($uid) {
  177. $q = "SELECT timestamp from " . TB_PREFIX . "deleting where uid = $uid";
  178. $result = mysql_query($q, $this->connection);
  179. $dbarray = mysql_fetch_array($result);
  180. return $dbarray['timestamp'];
  181. }
  182. function modifyGold($userid, $amt, $mode) {
  183. if(!$mode) {
  184. $q = "UPDATE " . TB_PREFIX . "users set gold = gold - $amt where id = $userid";
  185. } else {
  186. $q = "UPDATE " . TB_PREFIX . "users set gold = gold + $amt where id = $userid";
  187. }
  188. return mysql_query($q, $this->connection);
  189. }
  190. /*****************************************
  191. Function to retrieve user array via Username or ID
  192. Mode 0: Search by Username
  193. Mode 1: Search by ID
  194. References: Alliance ID
  195. *****************************************/
  196. function getUserArray($ref, $mode) {
  197. if(!$mode) {
  198. $q = "SELECT * FROM " . TB_PREFIX . "users where username = '$ref'";
  199. } else {
  200. $q = "SELECT * FROM " . TB_PREFIX . "users where id = $ref";
  201. }
  202. $result = mysql_query($q, $this->connection);
  203. return mysql_fetch_array($result);
  204. }
  205. function activeModify($username, $mode) {
  206. $time = time();
  207. if(!$mode) {
  208. $q = "INSERT into " . TB_PREFIX . "active VALUES ('$username',$time)";
  209. } else {
  210. $q = "DELETE FROM " . TB_PREFIX . "active where username = '$username'";
  211. }
  212. return mysql_query($q, $this->connection);
  213. }
  214. function addActiveUser($username, $time) {
  215. $q = "REPLACE into " . TB_PREFIX . "active values ('$username',$time)";
  216. if(mysql_query($q, $this->connection)) {
  217. return true;
  218. } else {
  219. return false;
  220. }
  221. }
  222. function updateActiveUser($username, $time) {
  223. $q = "REPLACE into " . TB_PREFIX . "active values ('$username',$time)";
  224. $q2 = "UPDATE " . TB_PREFIX . "users set timestamp = $time where username = '$username'";
  225. $exec1 = mysql_query($q, $this->connection);
  226. $exec2 = mysql_query($q2, $this->connection);
  227. if($exec1 && $exec2) {
  228. return true;
  229. } else {
  230. return false;
  231. }
  232. }
  233. function checkactiveSession($username, $sessid) {
  234. $q = "SELECT username FROM " . TB_PREFIX . "users where username = '$username' and sessid = '$sessid' LIMIT 1";
  235. $result = mysql_query($q, $this->connection);
  236. if(mysql_num_rows($result) != 0) {
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }
  242. function submitProfile($uid, $gender, $location, $birthday, $des1, $des2) {
  243. $q = "UPDATE " . TB_PREFIX . "users set gender = $gender, location = '$location', birthday = '$birthday', desc1 = '$des1', desc2 = '$des2' where id = $uid";
  244. return mysql_query($q, $this->connection);
  245. }
  246. function gpack($uid, $gpack) {
  247. $q = "UPDATE " . TB_PREFIX . "users set gpack = '$gpack' where id = $uid";
  248. return mysql_query($q, $this->connection);
  249. }
  250. function UpdateOnline($mode, $name = "", $time = "") {
  251. global $session;
  252. if($mode == "login") {
  253. $q = "INSERT IGNORE INTO " . TB_PREFIX . "online (name, time) VALUES ('$name', " . time() . ")";
  254. return mysql_query($q, $this->connection);
  255. } else {
  256. $q = "DELETE FROM " . TB_PREFIX . "online WHERE name ='" . $session->username . "'";
  257. return mysql_query($q, $this->connection);
  258. }
  259. }
  260. function generateBase($sector) {
  261. $qeinde = "9999";
  262. $sector = rand(1, 4);
  263. $query = "select * from " . TB_PREFIX . "wdata where fieldtype = 3 and occupied = 0";
  264. $result = mysql_query($query, $this->connection);
  265. for($i = 0; $row = mysql_fetch_assoc($result); $i++) {
  266. $oke = '1';
  267. $x = $row['x'];
  268. $y = $row['y'];
  269. if($x[0] == "-") {
  270. $x = ($x * -1);
  271. if($sector == '2' or $sector == '4') {
  272. $oke = '0';
  273. }
  274. } else {
  275. if($sector == '1' or $sector == '3') {
  276. $oke = '0';
  277. }
  278. }
  279. if($y[0] == "-") {
  280. $y = ($y * -1);
  281. if($sector == '1' or $sector == '2') {
  282. $oke = '0';
  283. }
  284. } else {
  285. if($sector == '3' or $sector == '4') {
  286. $oke = '0';
  287. }
  288. }
  289. $afstand = sqrt(pow($x, 2) + pow($y, 2));
  290. if($oke == '1') {
  291. if($qeinde > $afstand) {
  292. $rand = rand(1, 10);
  293. if($rand == '3') {
  294. $qeinde = $afstand;
  295. $qid = $row['id'];
  296. }
  297. }
  298. }
  299. }
  300. if(isset($qid)) {
  301. return $qid;
  302. } else {
  303. $query = "select * from " . TB_PREFIX . "wdata where fieldtype = 3 and occupied = 0 LIMIT 0,1";
  304. $result = mysql_query($query, $this->connection);
  305. $row = mysql_fetch_array($result);
  306. return $row['id'];
  307. }
  308. }
  309. function setFieldTaken($id) {
  310. $q = "UPDATE " . TB_PREFIX . "wdata set occupied = 1 where id = $id";
  311. return mysql_query($q, $this->connection);
  312. }
  313. function addVillage($wid, $uid, $username, $capital) {
  314. $total = count($this->getVillagesID($uid));
  315. if($total >= 1) {
  316. $vname = $username . "\'s village " . ($total + 1);
  317. } else {
  318. $vname = $username . "\'s village";
  319. }
  320. $time = time();
  321. $q = "INSERT into " . TB_PREFIX . "vdata (wref, owner, name, capital, pop, cp, celebration, wood, clay, iron, maxstore, crop, maxcrop, lastupdate, created) values ('$wid', '$uid', '$vname', '$capital', 2, 1, 0, 750, 750, 750, ".STORAGE_BASE.", 750, ".STORAGE_BASE.", '$time', '$time')";
  322. return mysql_query($q, $this->connection) or die(mysql_error());
  323. }
  324. function addResourceFields($vid, $type) {
  325. switch($type) {
  326. case 1:
  327. $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)";
  328. break;
  329. case 2:
  330. $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)";
  331. break;
  332. case 3:
  333. $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)";
  334. break;
  335. case 4:
  336. $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)";
  337. break;
  338. case 5:
  339. $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)";
  340. break;
  341. case 6:
  342. $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)";
  343. break;
  344. case 7:
  345. $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)";
  346. break;
  347. case 8:
  348. $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)";
  349. break;
  350. case 9:
  351. $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)";
  352. break;
  353. case 10:
  354. $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)";
  355. break;
  356. case 11:
  357. $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,2,2,3,1,4,4,2,4,4,1,15)";
  358. break;
  359. case 12:
  360. $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,2,1,2,1,15)";
  361. break;
  362. }
  363. return mysql_query($q, $this->connection);
  364. }
  365. function isVillageOases($wref) {
  366. $q = "SELECT id, oasistype FROM " . TB_PREFIX . "wdata where id = $wref";
  367. $result = mysql_query($q, $this->connection);
  368. $dbarray = mysql_fetch_array($result);
  369. return $dbarray['oasistype'];
  370. }
  371. public function VillageOasisCount($vref) {
  372. $q = "SELECT count(*) FROM `".TB_PREFIX."odata` WHERE conqured=$vref";
  373. $result = mysql_query($q, $this->connection);
  374. $row = mysql_fetch_row($result);
  375. return $row[0];
  376. }
  377. public function countOasisTroops($vref){
  378. //count oasis troops: $troops_o
  379. $troops_o=0;
  380. $o_unit2=mysql_query("select * from ".TB_PREFIX."units where `vref`='".$vref."'");
  381. $o_unit=mysql_fetch_array($o_unit2);
  382. for ($i=1;$i<51;$i++)
  383. {
  384. $troops_o+=$o_unit[$i];
  385. }
  386. $troops_o+=$o_unit['hero'];
  387. $o_unit2=mysql_query("select * from ".TB_PREFIX."enforcement where `vref`='".$vref."'");
  388. while ($o_unit=@mysql_fetch_array($o_unit2))
  389. {
  390. for ($i=1;$i<51;$i++)
  391. {
  392. $troops_o+=$o_unit[$i];
  393. }
  394. $troops_o+=$o_unit['hero'];
  395. }
  396. return $troops_o;
  397. }
  398. public function canConquerOasis($vref,$wref) {
  399. $AttackerFields = $this->getResourceLevel($vref);
  400. for($i=19;$i<=38;$i++) {
  401. if($AttackerFields['f'.$i.'t'] == 37) { $HeroMansionLevel = $AttackerFields['f'.$i]; }
  402. }
  403. if($this->VillageOasisCount($vref) < floor(($HeroMansionLevel-5)/5)) {
  404. $OasisInfo = $this->getOasisInfo($wref);
  405. $troopcount = $this->countOasisTroops($wref);
  406. if($OasisInfo['conqured'] == 0 || $OasisInfo['conqured'] != 0 && $OasisInfo['loyalty'] < 99 / min(3,(4-$this->VillageOasisCount($OasisInfo['conqured']))) && $troopcount == 0) {
  407. $CoordsVillage = $this->getCoor($vref);
  408. $CoordsOasis = $this->getCoor($wref);
  409. if(abs($CoordsOasis['x']-$CoordsVillage['x'])<=3 && abs($CoordsOasis['y']-$CoordsVillage['y'])<=3) {
  410. return True;
  411. } else {
  412. return False;
  413. }
  414. } else {
  415. return False;
  416. }
  417. } else {
  418. return False;
  419. }
  420. }
  421. public function conquerOasis($vref,$wref) {
  422. $vinfo = $this->getVillage($vref);
  423. $uid = $vinfo['owner'];
  424. $q = "UPDATE `".TB_PREFIX."odata` SET conqured=$vref,loyalty=100,lastupdated=".time().",owner=$uid,name='Occupied Oasis' WHERE wref=$wref";
  425. return mysql_query($q, $this->connection);
  426. }
  427. public function modifyOasisLoyalty($wref) {
  428. if($this->isVillageOases($wref) != 0) {
  429. $OasisInfo = $this->getOasisInfo($wref);
  430. if($OasisInfo['conqured'] != 0) {
  431. $LoyaltyAmendment = floor(100 / min(3,(4-$this->VillageOasisCount($OasisInfo['conqured']))));
  432. $q = "UPDATE `".TB_PREFIX."odata` SET loyalty=loyalty-$LoyaltyAmendment WHERE wref=$wref";
  433. return mysql_query($q, $this->connection);
  434. }
  435. }
  436. }
  437. function populateOasis() {
  438. $q = "SELECT * FROM " . TB_PREFIX . "wdata where oasistype != 0";
  439. $result = mysql_query($q, $this->connection);
  440. while($row = mysql_fetch_array($result)) {
  441. $wid = $row['id'];
  442. $this->addUnits($wid);
  443. }
  444. }
  445. function populateOasisUnitsLow() {
  446. $q2 = "SELECT * FROM " . TB_PREFIX . "wdata where oasistype != 0";
  447. $result2 = mysql_query($q2, $this->connection);
  448. while($row = mysql_fetch_array($result2)) {
  449. $wid = $row['id'];
  450. $basearray = $this->getMInfo($wid);
  451. //each Troop is a Set for oasis type like mountains have rats spiders and snakes fields tigers elphants clay wolves so on stonger one more not so less
  452. switch($basearray['oasistype']) {
  453. case 1:
  454. case 2:
  455. //+25% lumber per hour
  456. $q = "UPDATE " . TB_PREFIX . "units SET u36 = u36 + '".rand(0,20)."', u37 = u37 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u36 <= '2' AND u37 <= '2'";
  457. $result = mysql_query($q, $this->connection);
  458. break;
  459. case 3:
  460. //+25% lumber and +25% crop per hour
  461. $q = "UPDATE " . TB_PREFIX . "units SET u36 = u36 + '".rand(0,20)."', u37 = u37 + '".rand(0,10)."', u38 = u38 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u36 <= '2' AND u37 <= '2' AND u38 <='2'";
  462. $result = mysql_query($q, $this->connection);
  463. break;
  464. case 4:
  465. case 5:
  466. //+25% clay per hour
  467. $q = "UPDATE " . TB_PREFIX . "units SET u36 = u36 + '".rand(0,20)."', u37 = u37 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u36 <= '2' AND u37 <= '2'";
  468. $result = mysql_query($q, $this->connection);
  469. break;
  470. case 6:
  471. //+25% clay and +25% crop per hour
  472. $q = "UPDATE " . TB_PREFIX . "units SET u36 = u36 + '".rand(0,20)."', u37 = u37 + '".rand(0,10)."', u38 = u38 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u36 <= '2' AND u37 <= '2' AND u38 <='2'";
  473. $result = mysql_query($q, $this->connection);
  474. break;
  475. case 7:
  476. case 8:
  477. //+25% iron per hour
  478. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '".rand(0,20)."', u32 = u32 + '".rand(0,10)."', u34 = u34 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u31 <= '2' AND u32 <= '2'";
  479. $result = mysql_query($q, $this->connection);
  480. break;
  481. case 9:
  482. //+25% iron and +25% crop
  483. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '".rand(0,20)."', u32 = u32 + '".rand(0,10)."', u34 = u34 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u31 <= '2' AND u32 <= '2' AND u34 <='2'";
  484. $result = mysql_query($q, $this->connection);
  485. break;
  486. case 10:
  487. case 11:
  488. //+25% crop per hour
  489. $q = "UPDATE " . TB_PREFIX . "units SET u33 = u33 + '".rand(0,20)."', u37 = u37 + '".rand(0,10)."', u38 = u38 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u33 <= '2' AND u37 <= '2' AND u38 <='2'";
  490. $result = mysql_query($q, $this->connection);
  491. break;
  492. case 12:
  493. //+50% crop per hour
  494. $q = "UPDATE " . TB_PREFIX . "units SET u33 = u33 + '".rand(0,20)."', u37 = u37 + '".rand(0,10)."', u38 = u38 + '".rand(0,10)."', u39 = u39 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u33 <= '2' AND u37 <= '2' AND u38 <='2'AND u38 <='2'";
  495. $result = mysql_query($q, $this->connection);
  496. break;
  497. }
  498. }
  499. }
  500. function populateOasisUnitsHigh() {
  501. $q2 = "SELECT * FROM " . TB_PREFIX . "wdata where oasistype != 0";
  502. $result2 = mysql_query($q2, $this->connection);
  503. while($row = mysql_fetch_array($result2)) {
  504. $wid = $row['id'];
  505. $basearray = $this->getMInfo($wid);
  506. //each Troop is a Set for oasis type like mountains have rats spiders and snakes fields tigers elphants clay wolves so on stonger one more not so less
  507. switch($basearray['oasistype']) {
  508. case 1:
  509. case 2:
  510. //+25% lumber per hour
  511. $q = "UPDATE " . TB_PREFIX . "units SET u36 = u36 + '".rand(15,40)."', u37 = u37 + '".rand(10,20)."' WHERE vref = '" . $wid . "' AND u36 <= '2' AND u37 <= '2'";
  512. $result = mysql_query($q, $this->connection);
  513. break;
  514. case 3:
  515. //+25% lumber and +25% crop per hour
  516. $q = "UPDATE " . TB_PREFIX . "units SET u36 = u36 + '".rand(15,40)."', u37 = u37 + '".rand(10,20)."', u38 = u38 + '".rand(10,20)."' WHERE vref = '" . $wid . "' AND u36 <= '2' AND u37 <= '2' AND u38 <='2'";
  517. $result = mysql_query($q, $this->connection);
  518. break;
  519. case 4:
  520. case 5:
  521. //+25% clay per hour
  522. $q = "UPDATE " . TB_PREFIX . "units SET u36 = u36 + '".rand(15,40)."', u37 = u37 + '".rand(10,20)."' WHERE vref = '" . $wid . "' AND u36 <= '2' AND u37 <= '2'";
  523. $result = mysql_query($q, $this->connection);
  524. break;
  525. case 6:
  526. //+25% clay and +25% crop per hour
  527. $q = "UPDATE " . TB_PREFIX . "units SET u36 = u36 + '".rand(15,40)."', u37 = u37 + '".rand(10,20)."', u38 = u38 + '".rand(10,20)."' WHERE vref = '" . $wid . "' AND u36 <= '2' AND u37 <= '2' AND u38 <='2'";
  528. $result = mysql_query($q, $this->connection);
  529. break;
  530. case 7:
  531. case 8:
  532. //+25% iron per hour
  533. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '".rand(15,40)."', u32 = u32 + '".rand(10,20)."', u34 = u34 + '".rand(10,20)."' WHERE vref = '" . $wid . "' AND u31 <= '2' AND u32 <= '2'";
  534. $result = mysql_query($q, $this->connection);
  535. break;
  536. case 9:
  537. //+25% iron and +25% crop
  538. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '".rand(15,40)."', u32 = u32 + '".rand(10,20)."', u34 = u34 + '".rand(10,20)."' WHERE vref = '" . $wid . "' AND u31 <= '2' AND u32 <= '2' AND u34 <='2'";
  539. $result = mysql_query($q, $this->connection);
  540. break;
  541. case 10:
  542. case 11:
  543. //+25% crop per hour
  544. $q = "UPDATE " . TB_PREFIX . "units SET u33 = u33 + '".rand(0,20)."', u37 = u37 + '".rand(0,10)."', u38 = u38 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u33 <= '2' AND u37 <= '2' AND u38 <='2'";
  545. $result = mysql_query($q, $this->connection);
  546. break;
  547. case 12:
  548. //+50% crop per hour
  549. $q = "UPDATE " . TB_PREFIX . "units SET u33 = u33 + '".rand(0,20)."', u37 = u37 + '".rand(0,10)."', u38 = u38 + '".rand(0,10)."', u39 = u39 + '".rand(0,10)."' WHERE vref = '" . $wid . "' AND u33 <= '2' AND u37 <= '2' AND u38 <='2'AND u38 <='2'";
  550. $result = mysql_query($q, $this->connection);
  551. break;
  552. }
  553. }
  554. }
  555. /***************************
  556. Function to retrieve type of village via ID
  557. References: Village ID
  558. ***************************/
  559. function getVillageType($wref) {
  560. $q = "SELECT id, fieldtype FROM " . TB_PREFIX . "wdata where id = $wref";
  561. $result = mysql_query($q, $this->connection);
  562. $dbarray = mysql_fetch_array($result);
  563. return $dbarray['fieldtype'];
  564. }
  565. /*****************************************
  566. Function to retrieve if is ocuped via ID
  567. References: Village ID
  568. *****************************************/
  569. function getVillageState($wref) {
  570. $q = "SELECT oasistype,occupied FROM " . TB_PREFIX . "wdata where id = $wref";
  571. $result = mysql_query($q, $this->connection);
  572. $dbarray = mysql_fetch_array($result);
  573. if($dbarray['occupied'] != 0 || $dbarray['oasistype'] != 0) {
  574. return true;
  575. } else {
  576. return false;
  577. }
  578. }
  579. function getProfileVillages($uid) {
  580. $q = "SELECT capital,wref,name,pop,created from " . TB_PREFIX . "vdata where owner = $uid order by pop desc";
  581. $result = mysql_query($q, $this->connection);
  582. return $this->mysql_fetch_all($result);
  583. }
  584. function getProfileMedal($uid) {
  585. $q = "SELECT id,categorie,plaats,week,img,points from " . TB_PREFIX . "medal where userid = $uid order by id desc";
  586. $result = mysql_query($q, $this->connection);
  587. return $this->mysql_fetch_all($result);
  588. }
  589. function getProfileMedalAlly($uid) {
  590. $q = "SELECT id,categorie,plaats,week,img,points from " . TB_PREFIX . "allimedal where allyid = $uid order by id desc";
  591. $result = mysql_query($q, $this->connection);
  592. return $this->mysql_fetch_all($result);
  593. }
  594. function getVillageID($uid) {
  595. $q = "SELECT wref FROM " . TB_PREFIX . "vdata WHERE owner = $uid";
  596. $result = mysql_query($q, $this->connection);
  597. $dbarray = mysql_fetch_array($result);
  598. return $dbarray['wref'];
  599. }
  600. function getVillagesID($uid) {
  601. $q = "SELECT wref from " . TB_PREFIX . "vdata where owner = $uid order by capital DESC,pop DESC";
  602. $result = mysql_query($q, $this->connection);
  603. $array = $this->mysql_fetch_all($result);
  604. $newarray = array();
  605. for($i = 0; $i < count($array); $i++) {
  606. array_push($newarray, $array[$i]['wref']);
  607. }
  608. return $newarray;
  609. }
  610. function getVillage($vid) {
  611. $q = "SELECT * FROM " . TB_PREFIX . "vdata where wref = $vid";
  612. $result = mysql_query($q, $this->connection);
  613. return mysql_fetch_array($result);
  614. }
  615. public function getVillageBattleData($vid) {
  616. $q = "SELECT u.id,u.tribe,v.capital,f.f40 AS wall FROM ".TB_PREFIX."users u,".TB_PREFIX."fdata f,".TB_PREFIX."vdata v WHERE u.id=v.owner AND f.vref=v.wref AND v.wref=".$vid;
  617. $result = mysql_query($q, $this->connection);
  618. return mysql_fetch_array($result);
  619. }
  620. public function getPopulation($uid) {
  621. $q = "SELECT sum(pop) AS pop FROM ".TB_PREFIX."vdata WHERE owner=".$uid;
  622. $result = mysql_query($q, $this->connection);
  623. $dbarray = mysql_fetch_array($result);
  624. return $dbarray['pop'];
  625. }
  626. function getOasisV($vid) {
  627. $q = "SELECT * FROM " . TB_PREFIX . "odata where wref = $vid";
  628. $result = mysql_query($q, $this->connection);
  629. return mysql_fetch_array($result);
  630. }
  631. function getMInfo($id) {
  632. $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";
  633. $result = mysql_query($q, $this->connection);
  634. return mysql_fetch_array($result);
  635. }
  636. function getOMInfo($id) {
  637. $q = "SELECT * FROM " . TB_PREFIX . "wdata left JOIN " . TB_PREFIX . "odata ON " . TB_PREFIX . "odata.wref = " . TB_PREFIX . "wdata.id where " . TB_PREFIX . "wdata.id = $id";
  638. $result = mysql_query($q, $this->connection);
  639. return mysql_fetch_array($result);
  640. }
  641. function getOasis($vid) {
  642. $q = "SELECT * FROM " . TB_PREFIX . "odata where conqured = $vid";
  643. $result = mysql_query($q, $this->connection);
  644. return $this->mysql_fetch_all($result);
  645. }
  646. function getOasisInfo($wid) {
  647. $q = "SELECT * FROM " . TB_PREFIX . "odata where wref = $wid";
  648. $result = mysql_query($q, $this->connection);
  649. return mysql_fetch_assoc($result);
  650. }
  651. function getVillageField($ref, $field) {
  652. $q = "SELECT $field FROM " . TB_PREFIX . "vdata where wref = $ref";
  653. $result = mysql_query($q, $this->connection);
  654. $dbarray = mysql_fetch_array($result);
  655. return $dbarray[$field];
  656. }
  657. function getOasisField($ref, $field) {
  658. $q = "SELECT $field FROM " . TB_PREFIX . "odata where wref = $ref";
  659. $result = mysql_query($q, $this->connection);
  660. $dbarray = mysql_fetch_array($result);
  661. return $dbarray[$field];
  662. }
  663. function setVillageField($ref, $field, $value) {
  664. $q = "UPDATE " . TB_PREFIX . "vdata set $field = '$value' where wref = $ref";
  665. return mysql_query($q, $this->connection);
  666. }
  667. function setVillageLevel($ref, $field, $value) {
  668. $q = "UPDATE " . TB_PREFIX . "fdata set " . $field . " = '" . $value . "' where vref = " . $ref . "";
  669. return mysql_query($q, $this->connection);
  670. }
  671. function getResourceLevel($vid) {
  672. $q = "SELECT * from " . TB_PREFIX . "fdata where vref = $vid";
  673. $result = mysql_query($q, $this->connection);
  674. return mysql_fetch_assoc($result);
  675. }
  676. function getAdminLog() {
  677. $q = "SELECT id,user,log,time from " . TB_PREFIX . "admin_log where id != 0 ORDER BY id ASC";
  678. $result = mysql_query($q, $this->connection);
  679. return $this->mysql_fetch_all($result);
  680. }
  681. function getCoor($wref) {
  682. $q = "SELECT x,y FROM " . TB_PREFIX . "wdata where id = $wref";
  683. $result = mysql_query($q, $this->connection);
  684. return mysql_fetch_array($result);
  685. }
  686. function CheckForum($id) {
  687. $q = "SELECT * from " . TB_PREFIX . "forum_cat where alliance = '$id'";
  688. $result = mysql_query($q, $this->connection);
  689. if(mysql_num_rows($result)) {
  690. return true;
  691. } else {
  692. return false;
  693. }
  694. }
  695. function CountCat($id) {
  696. $q = "SELECT count(id) FROM " . TB_PREFIX . "forum_topic where cat = '$id'";
  697. $result = mysql_query($q, $this->connection);
  698. $row = mysql_fetch_row($result);
  699. return $row[0];
  700. }
  701. function LastTopic($id) {
  702. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id' order by post_date";
  703. $result = mysql_query($q, $this->connection);
  704. return $this->mysql_fetch_all($result);
  705. }
  706. function CheckLastTopic($id) {
  707. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id'";
  708. $result = mysql_query($q, $this->connection);
  709. if(mysql_num_rows($result)) {
  710. return true;
  711. } else {
  712. return false;
  713. }
  714. }
  715. function CheckLastPost($id) {
  716. $q = "SELECT * from " . TB_PREFIX . "forum_post where topic = '$id'";
  717. $result = mysql_query($q, $this->connection);
  718. if(mysql_num_rows($result)) {
  719. return true;
  720. } else {
  721. return false;
  722. }
  723. }
  724. function LastPost($id) {
  725. $q = "SELECT * from " . TB_PREFIX . "forum_post where topic = '$id'";
  726. $result = mysql_query($q, $this->connection);
  727. return $this->mysql_fetch_all($result);
  728. }
  729. function CountTopic($id) {
  730. $q = "SELECT count(id) FROM " . TB_PREFIX . "forum_post where owner = '$id'";
  731. $result = mysql_query($q, $this->connection);
  732. $row = mysql_fetch_row($result);
  733. $qs = "SELECT count(id) FROM " . TB_PREFIX . "forum_topic where owner = '$id'";
  734. $results = mysql_query($qs, $this->connection);
  735. $rows = mysql_fetch_row($results);
  736. return $row[0] + $rows[0];
  737. }
  738. function CountPost($id) {
  739. $q = "SELECT count(id) FROM " . TB_PREFIX . "forum_post where topic = '$id'";
  740. $result = mysql_query($q, $this->connection);
  741. $row = mysql_fetch_row($result);
  742. return $row[0];
  743. }
  744. function ForumCat($id) {
  745. $q = "SELECT * from " . TB_PREFIX . "forum_cat where alliance = '$id' ORDER BY id";
  746. $result = mysql_query($q, $this->connection);
  747. return $this->mysql_fetch_all($result);
  748. }
  749. function ForumCatEdit($id) {
  750. $q = "SELECT * from " . TB_PREFIX . "forum_cat where id = '$id'";
  751. $result = mysql_query($q, $this->connection);
  752. return $this->mysql_fetch_all($result);
  753. }
  754. function ForumCatName($id) {
  755. $q = "SELECT forum_name from " . TB_PREFIX . "forum_cat where id = $id";
  756. $result = mysql_query($q, $this->connection);
  757. $dbarray = mysql_fetch_array($result);
  758. return $dbarray['forum_name'];
  759. }
  760. function CheckCatTopic($id) {
  761. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id'";
  762. $result = mysql_query($q, $this->connection);
  763. if(mysql_num_rows($result)) {
  764. return true;
  765. } else {
  766. return false;
  767. }
  768. }
  769. function CheckResultEdit($alli) {
  770. $q = "SELECT * from " . TB_PREFIX . "forum_edit where alliance = '$alli'";
  771. $result = mysql_query($q, $this->connection);
  772. if(mysql_num_rows($result)) {
  773. return true;
  774. } else {
  775. return false;
  776. }
  777. }
  778. function CheckCloseTopic($id) {
  779. $q = "SELECT close from " . TB_PREFIX . "forum_topic where id = '$id'";
  780. $result = mysql_query($q, $this->connection);
  781. $dbarray = mysql_fetch_array($result);
  782. return $dbarray['close'];
  783. }
  784. function CheckEditRes($alli) {
  785. $q = "SELECT result from " . TB_PREFIX . "forum_edit where alliance = '$alli'";
  786. $result = mysql_query($q, $this->connection);
  787. $dbarray = mysql_fetch_array($result);
  788. return $dbarray['result'];
  789. }
  790. function CreatResultEdit($alli, $result) {
  791. $q = "INSERT into " . TB_PREFIX . "forum_edit values (0,'$alli','$result')";
  792. mysql_query($q, $this->connection);
  793. return mysql_insert_id($this->connection);
  794. }
  795. function UpdateResultEdit($alli, $result) {
  796. $date = time();
  797. $q = "UPDATE " . TB_PREFIX . "forum_edit set result = '$result' where alliance = '$alli'";
  798. return mysql_query($q, $this->connection);
  799. }
  800. function UpdateEditTopic($id, $title, $cat) {
  801. $q = "UPDATE " . TB_PREFIX . "forum_topic set title = '$title', cat = '$cat' where id = $id";
  802. return mysql_query($q, $this->connection);
  803. }
  804. function UpdateEditForum($id, $name, $des) {
  805. $q = "UPDATE " . TB_PREFIX . "forum_cat set forum_name = '$name', forum_des = '$des' where id = $id";
  806. return mysql_query($q, $this->connection);
  807. }
  808. function StickTopic($id, $mode) {
  809. $q = "UPDATE " . TB_PREFIX . "forum_topic set stick = '$mode' where id = '$id'";
  810. return mysql_query($q, $this->connection);
  811. }
  812. function ForumCatTopic($id) {
  813. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id' AND stick = '' ORDER BY post_date desc";
  814. $result = mysql_query($q, $this->connection);
  815. return $this->mysql_fetch_all($result);
  816. }
  817. function ForumCatTopicStick($id) {
  818. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id' AND stick = '1' ORDER BY post_date desc";
  819. $result = mysql_query($q, $this->connection);
  820. return $this->mysql_fetch_all($result);
  821. }
  822. function ShowTopic($id) {
  823. $q = "SELECT * from " . TB_PREFIX . "forum_topic where id = '$id'";
  824. $result = mysql_query($q, $this->connection);
  825. return $this->mysql_fetch_all($result);
  826. }
  827. function ShowPost($id) {
  828. $q = "SELECT * from " . TB_PREFIX . "forum_post where topic = '$id'";
  829. $result = mysql_query($q, $this->connection);
  830. return $this->mysql_fetch_all($result);
  831. }
  832. function ShowPostEdit($id) {
  833. $q = "SELECT * from " . TB_PREFIX . "forum_post where id = '$id'";
  834. $result = mysql_query($q, $this->connection);
  835. return $this->mysql_fetch_all($result);
  836. }
  837. function CreatForum($owner, $alli, $name, $des, $area) {
  838. $q = "INSERT into " . TB_PREFIX . "forum_cat values (0,'$owner','$alli','$name','$des','$area')";
  839. mysql_query($q, $this->connection);
  840. return mysql_insert_id($this->connection);
  841. }
  842. function CreatTopic($title, $post, $cat, $owner, $alli, $ends) {
  843. $date = time();
  844. $q = "INSERT into " . TB_PREFIX . "forum_topic values (0,'$title','$post','$date','$date','$cat','$owner','$alli','$ends','','')";
  845. mysql_query($q, $this->connection);
  846. return mysql_insert_id($this->connection);
  847. }
  848. function CreatPost($post, $tids, $owner) {
  849. $date = time();
  850. $q = "INSERT into " . TB_PREFIX . "forum_post values (0,'$post','$tids','$owner','$date')";
  851. mysql_query($q, $this->connection);
  852. return mysql_insert_id($this->connection);
  853. }
  854. function UpdatePostDate($id) {
  855. $date = time();
  856. $q = "UPDATE " . TB_PREFIX . "forum_topic set post_date = '$date' where id = $id";
  857. return mysql_query($q, $this->connection);
  858. }
  859. function EditUpdateTopic($id, $post) {
  860. $q = "UPDATE " . TB_PREFIX . "forum_topic set post = '$post' where id = $id";
  861. return mysql_query($q, $this->connection);
  862. }
  863. function EditUpdatePost($id, $post) {
  864. $q = "UPDATE " . TB_PREFIX . "forum_post set post = '$post' where id = $id";
  865. return mysql_query($q, $this->connection);
  866. }
  867. function LockTopic($id, $mode) {
  868. $q = "UPDATE " . TB_PREFIX . "forum_topic set close = '$mode' where id = '$id'";
  869. return mysql_query($q, $this->connection);
  870. }
  871. function DeleteCat($id) {
  872. $qs = "DELETE from " . TB_PREFIX . "forum_cat where id = '$id'";
  873. $q = "DELETE from " . TB_PREFIX . "forum_topic where cat = '$id'";
  874. mysql_query($qs, $this->connection);
  875. return mysql_query($q, $this->connection);
  876. }
  877. function DeleteTopic($id) {
  878. $qs = "DELETE from " . TB_PREFIX . "forum_topic where id = '$id'";
  879. // $q = "DELETE from ".TB_PREFIX."forum_post where topic = '$id'";//
  880. return mysql_query($qs, $this->connection); //
  881. // mysql_query($q,$this->connection);
  882. }
  883. function DeletePost($id) {
  884. $q = "DELETE from " . TB_PREFIX . "forum_post where id = '$id'";
  885. return mysql_query($q, $this->connection);
  886. }
  887. function getAllianceName($id) {
  888. $q = "SELECT tag from " . TB_PREFIX . "alidata where id = $id";
  889. $result = mysql_query($q, $this->connection);
  890. $dbarray = mysql_fetch_array($result);
  891. return $dbarray['tag'];
  892. }
  893. function getAlliancePermission($ref, $field, $mode) {
  894. if(!$mode) {
  895. $q = "SELECT $field FROM " . TB_PREFIX . "ali_permission where uid = '$ref'";
  896. } else {
  897. $q = "SELECT $field FROM " . TB_PREFIX . "ali_permission where username = '$ref'";
  898. }
  899. $result = mysql_query($q, $this->connection) or die(mysql_error());
  900. $dbarray = mysql_fetch_array($result);
  901. return $dbarray[$field];
  902. }
  903. function getAlliance($id) {
  904. $q = "SELECT * from " . TB_PREFIX . "alidata where id = $id";
  905. $result = mysql_query($q, $this->connection);
  906. return mysql_fetch_assoc($result);
  907. }
  908. function setAlliName($aid, $name, $tag) {
  909. $q = "UPDATE " . TB_PREFIX . "alidata set name = '$name', tag = '$tag' where id = $aid";
  910. return mysql_query($q, $this->connection);
  911. }
  912. function isAllianceOwner($id) {
  913. $q = "SELECT * from " . TB_PREFIX . "alidata where leader = '$id'";
  914. $result = mysql_query($q, $this->connection);
  915. if(mysql_num_rows($result)) {
  916. return true;
  917. } else {
  918. return false;
  919. }
  920. }
  921. function aExist($ref, $type) {
  922. $q = "SELECT $type FROM " . TB_PREFIX . "alidata where $type = '$ref'";
  923. $result = mysql_query($q, $this->connection);
  924. if(mysql_num_rows($result)) {
  925. return true;
  926. } else {
  927. return false;
  928. }
  929. }
  930. function modifyPoints($aid, $points, $amt) {
  931. $q = "UPDATE " . TB_PREFIX . "users set $points = $points + $amt where id = $aid";
  932. return mysql_query($q, $this->connection);
  933. }
  934. function modifyPointsAlly($aid, $points, $amt) {
  935. $q = "UPDATE " . TB_PREFIX . "alidata set $points = $points + $amt where id = $aid";
  936. return mysql_query($q, $this->connection);
  937. }
  938. /*****************************************
  939. Function to create an alliance
  940. References:
  941. *****************************************/
  942. function createAlliance($tag, $name, $uid, $max) {
  943. $q = "INSERT into " . TB_PREFIX . "alidata values (0,'$name','$tag',$uid,0,0,0,'','',$max,'','','','','','','','')";
  944. mysql_query($q, $this->connection);
  945. return mysql_insert_id($this->connection);
  946. }
  947. /*****************************************
  948. Function to insert an alliance new
  949. References:
  950. *****************************************/
  951. function insertAlliNotice($aid, $notice) {
  952. $time = time();
  953. $q = "INSERT into " . TB_PREFIX . "ali_log values (0,'$aid','$notice',$time)";
  954. mysql_query($q, $this->connection);
  955. return mysql_insert_id($this->connection);
  956. }
  957. /*****************************************
  958. Function to delete alliance if empty
  959. References:
  960. *****************************************/
  961. function deleteAlliance($aid) {
  962. $result = mysql_query("SELECT * FROM " . TB_PREFIX . "users where alliance = $aid");
  963. $num_rows = mysql_num_rows($result);
  964. if($num_rows == 0) {
  965. $q = "DELETE FROM " . TB_PREFIX . "alidata WHERE id = $aid";
  966. }
  967. mysql_query($q, $this->connection);
  968. return mysql_insert_id($this->connection);
  969. }
  970. /*****************************************
  971. Function to read all alliance news
  972. References:
  973. *****************************************/
  974. function readAlliNotice($aid) {
  975. $q = "SELECT * from " . TB_PREFIX . "ali_log where aid = $aid ORDER BY date DESC";
  976. $result = mysql_query($q, $this->connection);
  977. return $this->mysql_fetch_all($result);
  978. }
  979. /*****************************************
  980. Function to create alliance permissions
  981. References: ID, notice, description
  982. *****************************************/
  983. function createAlliPermissions($uid, $aid, $rank, $opt1, $opt2, $opt3, $opt4, $opt5, $opt6, $opt7, $opt8) {
  984. $q = "INSERT into " . TB_PREFIX . "ali_permission values(0,'$uid','$aid','$rank','$opt1','$opt2','$opt3','$opt4','$opt5','$opt6','$opt7','$opt8')";
  985. mysql_query($q, $this->connection);
  986. return mysql_insert_id($this->connection);
  987. }
  988. /*****************************************
  989. Function to update alliance permissions
  990. References:
  991. *****************************************/
  992. function deleteAlliPermissions($uid) {
  993. $q = "DELETE from " . TB_PREFIX . "ali_permission where uid = '$uid'";
  994. return mysql_query($q, $this->connection);
  995. }
  996. /*****************************************
  997. Function to update alliance permissions
  998. References:
  999. *****************************************/
  1000. function updateAlliPermissions($uid, $aid, $rank, $opt1, $opt2, $opt3, $opt4, $opt5, $opt6, $opt7) {
  1001. $q = "UPDATE " . TB_PREFIX . "ali_permission SET rank = '$rank', opt1 = '$opt1', opt2 = '$opt2', opt3 = '$opt3', opt4 = '$opt4', opt5 = '$opt5', opt6 = '$opt6', opt7 = '$opt7' where uid = $uid && alliance =$aid";
  1002. return mysql_query($q, $this->connection);
  1003. }
  1004. /*****************************************
  1005. Function to read alliance permissions
  1006. References: ID, notice, description
  1007. *****************************************/
  1008. function getAlliPermissions($uid, $aid) {
  1009. $q = "SELECT * FROM " . TB_PREFIX . "ali_permission where uid = $uid && alliance = $aid";
  1010. $result = mysql_query($q, $this->connection);
  1011. return mysql_fetch_assoc($result);
  1012. }
  1013. /*****************************************
  1014. Function to update an alliance description and notice
  1015. References: ID, notice, description
  1016. *****************************************/
  1017. function submitAlliProfile($aid, $notice, $desc) {
  1018. $q = "UPDATE " . TB_PREFIX . "alidata SET `notice` = '$notice', `desc` = '$desc' where id = $aid";
  1019. return mysql_query($q, $this->connection);
  1020. }
  1021. function diplomacyInviteAdd($alli1, $alli2, $type) {
  1022. $q = "INSERT INTO " . TB_PREFIX . "diplomacy (alli1,alli2,type,accepted) VALUES ($alli1,$alli2," . (int)intval($type) . ",0)";
  1023. return mysql_query($q, $this->connection);
  1024. }
  1025. function diplomacyOwnOffers($session_alliance) {
  1026. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli1 = $session_alliance AND accepted = 0";
  1027. $result = mysql_query($q, $this->connection);
  1028. return $this->mysql_fetch_all($result);
  1029. }
  1030. function getAllianceID($name) {
  1031. $q = "SELECT id FROM " . TB_PREFIX . "alidata WHERE tag ='" . $this->RemoveXSS($name) . "'";
  1032. $result = mysql_query($q, $this->connection);
  1033. $dbarray = mysql_fetch_array($result);
  1034. return $dbarray['id'];
  1035. }
  1036. function getDiplomacy($aid) {
  1037. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE id = $aid";
  1038. $result = mysql_query($q, $this->connection);
  1039. return $this->mysql_fetch_all($result);
  1040. }
  1041. function diplomacyCancelOffer($id) {
  1042. $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id";
  1043. return mysql_query($q, $this->connection);
  1044. }
  1045. function diplomacyInviteAccept($id, $session_alliance) {
  1046. $q = "UPDATE " . TB_PREFIX . "diplomacy SET accepted = 1 WHERE id = $id AND alli2 = $session_alliance";
  1047. return mysql_query($q, $this->connection);
  1048. }
  1049. function diplomacyInviteDenied($id, $session_alliance) {
  1050. $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id AND alli2 = $session_alliance";
  1051. return mysql_query($q, $this->connection);
  1052. }
  1053. function diplomacyInviteCheck($session_alliance) {
  1054. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli2 = $session_alliance AND accepted = 0";
  1055. $result = mysql_query($q, $this->connection);
  1056. return $this->mysql_fetch_all($result);
  1057. }
  1058. function getAllianceDipProfile($aid, $type){
  1059. $q = "SELECT * FROM ".TB_PREFIX."diplomacy WHERE alli1 = '$aid' AND type = '$type' AND accepted = '1'";
  1060. $result = mysql_query($q, $this->connection);
  1061. if(mysql_num_rows($result) == 0){
  1062. $q2 = "SELECT * FROM ".TB_PREFIX."diplomacy WHERE alli2 = '$aid' AND type = '$type' AND accepted = '1'";
  1063. $result2 = mysql_query($q2, $this->connection);
  1064. while($row = mysql_fetch_array($result2)){
  1065. $alliance = $this->getAlliance($row['alli1']);
  1066. $text = "";
  1067. $text .= "<a href=allianz.php?aid=".$alliance['id'].">".$alliance['tag']."</a><br> ";
  1068. }
  1069. }else{
  1070. while($row = mysql_fetch_array($result)){
  1071. $alliance = $this->getAlliance($row['alli2']);
  1072. $text = "";
  1073. $text .= "<a href=allianz.php?aid=".$alliance['id'].">".$alliance['tag']."</a><br> ";
  1074. }
  1075. }
  1076. if(strlen($text) == 0){
  1077. $text = "-<br>";
  1078. }
  1079. return $text;
  1080. }
  1081. function diplomacyExistingRelationships($session_alliance) {
  1082. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli2 = $session_alliance AND accepted = 1";
  1083. $result = mysql_query($q, $this->connection);
  1084. return $this->mysql_fetch_all($result);
  1085. }
  1086. function diplomacyExistingRelationships2($session_alliance) {
  1087. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli1 = $session_alliance AND accepted = 1";
  1088. $result = mysql_query($q, $this->connection);
  1089. return $this->mysql_fetch_all($result);
  1090. }
  1091. function diplomacyCancelExistingRelationship($id, $session_alliance) {
  1092. $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id AND alli2 = $session_alliance";
  1093. return mysql_query($q, $this->connection);
  1094. }
  1095. function getUserAlliance($id) {
  1096. $q = "SELECT " . TB_PREFIX . "alidata.tag from " . TB_PREFIX . "users join " . TB_PREFIX . "alidata where " . TB_PREFIX . "users.alliance = " . TB_PREFIX . "alidata.id and " . TB_PREFIX . "users.id = $id";
  1097. $result = mysql_query($q, $this->connection);
  1098. $dbarray = mysql_fetch_array($result);
  1099. if($dbarray['tag'] == "") {
  1100. return "-";
  1101. } else {
  1102. return $dbarray['tag'];
  1103. }
  1104. }
  1105. function modifyResource($vid, $wood, $clay, $iron, $crop, $mode) {
  1106. if(!$mode) {
  1107. $q = "UPDATE " . TB_PREFIX . "vdata set wood = wood - $wood, clay = clay - $clay, iron = iron - $iron, crop = crop - $crop where wref = $vid";
  1108. } else {
  1109. $q = "UPDATE " . TB_PREFIX . "vdata set wood = wood + $wood, clay = clay + $clay, iron = iron + $iron, crop = crop + $crop where wref = $vid";
  1110. }
  1111. return mysql_query($q, $this->connection);
  1112. }
  1113. function modifyOasisResource($vid, $wood, $clay, $iron, $crop, $mode) {
  1114. if(!$mode) {
  1115. $q = "UPDATE " . TB_PREFIX . "odata set wood = wood - $wood, clay = clay - $clay, iron = iron - $iron, crop = crop - $crop where wref = $vid";
  1116. } else {
  1117. $q = "UPDATE " . TB_PREFIX . "odata set wood = wood + $wood, clay = clay + $clay, iron = iron + $iron, crop = crop + $crop where wref = $vid";
  1118. }
  1119. return mysql_query($q, $this->connection);
  1120. }
  1121. function getFieldLevel($vid, $field) {
  1122. $q = "SELECT f" . $field . " from " . TB_PREFIX . "fdata where vref = $vid";
  1123. $result = mysql_query($q, $this->connection);
  1124. return mysql_result($result, 0);
  1125. }
  1126. function getFieldType($vid, $field) {
  1127. $q = "SELECT f" . $field . "t from " . TB_PREFIX . "fdata where vref = $vid";
  1128. $result = mysql_query($q, $this->connection);
  1129. return mysql_result($result, 0);
  1130. }
  1131. function getVSumField($uid, $field) {
  1132. $q = "SELECT sum(" . $field . ") FROM " . TB_PREFIX . "vdata where owner = $uid";
  1133. $result = mysql_query($q, $this->connection);
  1134. $row = mysql_fetch_row($result);
  1135. return $row[0];
  1136. }
  1137. function updateVillage($vid) {
  1138. $time = time();
  1139. $q = "UPDATE " . TB_PREFIX . "vdata set lastupdate = $time where wref = $vid";
  1140. return mysql_query($q, $this->connection);
  1141. }
  1142. function updateOasis($vid) {
  1143. $time = time();
  1144. $q = "UPDATE " . TB_PREFIX . "odata set lastupdated = $time where wref = $vid";
  1145. return mysql_query($q, $this->connection);
  1146. }
  1147. function setVillageName($vid, $name) {
  1148. $q = "UPDATE " . TB_PREFIX . "vdata set name = '$name' where wref = $vid";
  1149. return mysql_query($q, $this->connection);
  1150. }
  1151. function modifyPop($vid, $pop, $mode) {
  1152. if(!$mode) {
  1153. $q = "UPDATE " . TB_PREFIX . "vdata set pop = pop + $pop where wref = $vid";
  1154. } else {
  1155. $q = "UPDATE " . TB_PREFIX . "vdata set pop = pop - $pop where wref = $vid";
  1156. }
  1157. return mysql_query($q, $this->connection);
  1158. }
  1159. function addCP($ref, $cp) {
  1160. $q = "UPDATE " . TB_PREFIX . "vdata set cp = cp + $cp where wref = $ref";
  1161. return mysql_query($q, $this->connection);
  1162. }
  1163. function addCel($ref, $cel, $type) {
  1164. $q = "UPDATE " . TB_PREFIX . "vdata set celebration = $cel, type= $type where wref = $ref";
  1165. return mysql_query($q, $this->connection);
  1166. }
  1167. function getCel() {
  1168. $time = time();
  1169. $q = "SELECT * FROM " . TB_PREFIX . "vdata where celebration < $time AND celebration != 0";
  1170. $result = mysql_query($q, $this->connection);
  1171. return $this->mysql_fetch_all($result);
  1172. }
  1173. function clearCel($ref) {
  1174. $q = "UPDATE " . TB_PREFIX . "vdata set celebration = 0, type = 0 where wref = $ref";
  1175. return mysql_query($q, $this->connection);
  1176. }
  1177. function setCelCp($user, $cp) {
  1178. $q = "UPDATE " . TB_PREFIX . "users set cp = cp + $cp where id = $user";
  1179. return mysql_query($q, $this->connection);
  1180. }
  1181. function clearExpansionSlot($id) {
  1182. for($i = 1; $i <= 3; $i++) {
  1183. $q = "UPDATE " . TB_PREFIX . "vdata SET exp" . $i . "=0 WHERE exp" . $i . "=" . $id;
  1184. mysql_query($q, $this->connection);
  1185. }
  1186. }
  1187. function getInvitation($uid) {
  1188. $q = "SELECT * FROM " . TB_PREFIX . "ali_invite where uid = $uid";
  1189. $result = mysql_query($q, $this->connection);
  1190. return $this->mysql_fetch_all($result);
  1191. }
  1192. function getAliInvitations($aid) {
  1193. $q = "SELECT * FROM " . TB_PREFIX . "ali_invite where alliance = $aid && accept = 0";
  1194. $result = mysql_query($q, $this->connection);
  1195. return $this->mysql_fetch_all($result);
  1196. }
  1197. function sendInvitation($uid, $alli, $sender) {
  1198. $time = time();
  1199. $q = "INSERT INTO " . TB_PREFIX . "ali_invite values (0,$uid,$alli,$sender,$time,0)";
  1200. return mysql_query($q, $this->connection) or die(mysql_error());
  1201. }
  1202. function removeInvitation($id) {
  1203. $q = "DELETE FROM " . TB_PREFIX . "ali_invite where id = $id";
  1204. return mysql_query($q, $this->connection);
  1205. }
  1206. function sendMessage($client, $owner, $topic, $message, $send) {
  1207. $time = time();
  1208. $q = "INSERT INTO " . TB_PREFIX . "mdata values (0,$client,$owner,'$topic',\"$message\",0,0,$send,$time)";
  1209. return mysql_query($q, $this->connection);
  1210. }
  1211. function setArchived($id) {
  1212. $q = "UPDATE " . TB_PREFIX . "mdata set archived = 1 where id = $id";
  1213. return mysql_query($q, $this->connection);
  1214. }
  1215. function setNorm($id) {
  1216. $q = "UPDATE " . TB_PREFIX . "mdata set archived = 0 where id = $id";
  1217. return mysql_query($q, $this->connection);
  1218. }
  1219. /***************************
  1220. Function to get messages
  1221. Mode 1: Get inbox
  1222. Mode 2: Get sent
  1223. Mode 3: Get message
  1224. Mode 4: Set viewed
  1225. Mode 5: Remove message
  1226. Mode 6: Retrieve archive
  1227. References: User ID/Message ID, Mode
  1228. ***************************/
  1229. function getMessage($id, $mode) {
  1230. global $session;
  1231. switch($mode) {
  1232. case 1:
  1233. $q = "SELECT * FROM " . TB_PREFIX . "mdata WHERE target = $id and send = 0 and archived = 0 ORDER BY time DESC";
  1234. break;
  1235. case 2:
  1236. // removed send no longer needed as we dont send 2 messages any more just 1
  1237. $q = "SELECT * FROM " . TB_PREFIX . "mdata WHERE owner = $id ORDER BY time DESC";
  1238. break;
  1239. case 3:
  1240. $q = "SELECT * FROM " . TB_PREFIX . "mdata where id = $id";
  1241. break;
  1242. case 4:
  1243. $q = "UPDATE " . TB_PREFIX . "mdata set viewed = 1 where id = $id AND target = $session->uid";
  1244. break;
  1245. case 5:
  1246. $q = "DELETE FROM " . TB_PREFIX . "mdata where id = $id";
  1247. break;
  1248. case 6:
  1249. $q = "SELECT * FROM " . TB_PREFIX . "mdata where target = $id and send = 0 and archived = 1";
  1250. break;
  1251. }
  1252. if($mode <= 3 || $mode == 6) {
  1253. $result = mysql_query($q, $this->connection);
  1254. return $this->mysql_fetch_all($result);
  1255. } else {
  1256. return mysql_query($q, $this->connection);
  1257. }
  1258. }
  1259. function unarchiveNotice($id) {
  1260. $q = "UPDATE " . TB_PREFIX . "ndata set ntype = archive, archive = 0 where id = $id";
  1261. return mysql_query($q, $this->connection);
  1262. }
  1263. function archiveNotice($id) {
  1264. $q = "update " . TB_PREFIX . "ndata set archive = ntype, ntype = 9 where id = $id";
  1265. return mysql_query($q, $this->connection);
  1266. }
  1267. function removeNotice($id) {
  1268. $q = "DELETE FROM " . TB_PREFIX . "ndata where id = $id";
  1269. return mysql_query($q, $this->connection);
  1270. }
  1271. function noticeViewed($id) {
  1272. $q = "UPDATE " . TB_PREFIX . "ndata set viewed = 1 where id = $id";
  1273. return mysql_query($q, $this->connection);
  1274. }
  1275. function addNotice($uid, $type, $topic, $data, $time = 0) {
  1276. if($time == 0) {
  1277. $time = time();
  1278. }
  1279. echo $q = "INSERT INTO " . TB_PREFIX . "ndata (id, uid, topic, ntype, data, time, viewed) values (0,'$uid','$topic',$type,'$data',$time,0)";
  1280. return mysql_query($q, $this->connection) or die(mysql_error());
  1281. }
  1282. function getNotice($uid) {
  1283. $q = "SELECT * FROM " . TB_PREFIX . "ndata where uid = $uid ORDER BY time DESC";
  1284. $result = mysql_query($q, $this->connection);
  1285. return $this->mysql_fetch_all($result);
  1286. }
  1287. function addBuilding($wid, $field, $type, $loop, $time) {
  1288. $x = "UPDATE " . TB_PREFIX . "fdata SET f" . $field . "t=" . $type . " WHERE vref=" . $wid;
  1289. mysql_query($x, $this->connection) or die(mysql_error());
  1290. $q = "INSERT into " . TB_PREFIX . "bdata values (0,$wid,$field,$type,$loop,$time)";
  1291. return mysql_query($q, $this->connection);
  1292. }
  1293. function removeBuilding($d) {
  1294. global $building;
  1295. $jobLoopconID = -1;
  1296. $SameBuildCount = 0;
  1297. $jobs = $building->buildArray;
  1298. for($i = 0; $i < sizeof($jobs); $i++) {
  1299. if($jobs[$i]['id'] == $d) {
  1300. $jobDeleted = $i;
  1301. }
  1302. if($jobs[$i]['loopcon'] == 1) {
  1303. $jobLoopconID = $i;
  1304. }
  1305. }
  1306. if(count($jobs) > 1 && ($jobs[0]['field'] == $jobs[1]['field'])) {
  1307. $SameBuildCount = 1;
  1308. }
  1309. if(count($jobs) > 2 && ($jobs[0]['field'] == $jobs[2]['field'])) {
  1310. $SameBuildCount = 2;
  1311. }
  1312. if(count($jobs) > 2 && ($jobs[1]['field'] == $jobs[2]['field'])) {
  1313. $SameBuildCount = 3;
  1314. }
  1315. if($SameBuildCount > 0) {
  1316. if($d == $jobs[floor($SameBuildCount / 3)]['id'] || $d == $jobs[floor($SameBuildCount / 2) + 1]['id']) {
  1317. $q = "UPDATE " . TB_PREFIX . "bdata SET loopcon=0,timestamp=" . $jobs[floor($SameBuildCount / 3)]['timestamp'] . " WHERE ID=" . $jobs[floor($SameBuildCount / 3)]['id'] . " OR ID=" . $jobs[floor($SameBuildCount / 2) + 1]['id'];
  1318. mysql_query($q, $this->connection);
  1319. }
  1320. } else {
  1321. if($jobs[$jobDeleted]['field'] >= 19) {
  1322. $x = "SELECT f" . $jobs[$jobDeleted]['field'] . " FROM " . TB_PREFIX . "fdata WHERE vref=" . $jobs[$jobDeleted]['wid'];
  1323. $result = mysql_query($x, $this->connection) or die(mysql_error());
  1324. $fieldlevel = mysql_fetch_row($result);
  1325. if($fieldlevel[0] == 0) {
  1326. $x = "UPDATE " . TB_PREFIX . "fdata SET f" . $jobs[$jobDeleted]['field'] . "t=0 WHERE vref=" . $jobs[$jobDeleted]['wid'];
  1327. mysql_query($x, $this->connection) or die(mysql_error());
  1328. }
  1329. }
  1330. if(($jobLoopconID >= 0) && ($jobs[$jobDeleted]['loopcon'] != 1)) {
  1331. if(($jobs[$jobLoopconID]['field'] <= 18 && $jobs[$jobDeleted]['field'] <= 18) || ($jobs[$jobLoopconID]['field'] >= 19 && $jobs[$jobDeleted]['field'] >= 19)) {
  1332. $uprequire = $building->resourceRequired($jobs[$jobLoopconID]['field'], $jobs[$jobLoopconID]['type']);
  1333. $x = "UPDATE " . TB_PREFIX . "bdata SET loopcon=0,timestamp=" . (time() + $uprequire['time']) . " WHERE wid=" . $jobs[$jobDeleted]['wid'] . " AND loopcon=1";
  1334. mysql_query($x, $this->connection) or die(mysql_error());
  1335. }
  1336. }
  1337. }
  1338. $q = "DELETE FROM " . TB_PREFIX . "bdata where id = $d";
  1339. return mysql_query($q, $this->connection);
  1340. }
  1341. function addDemolition($wid, $field) {
  1342. global $building, $village;
  1343. $q = "DELETE FROM ".TB_PREFIX."bdata WHERE field=$field AND wid=$wid";
  1344. mysql_query($q, $this->connection);
  1345. $uprequire = $building->resourceRequired($field,$village->resarray['f'.$field.'t'],0);
  1346. $q = "INSERT INTO ".TB_PREFIX."demolition VALUES (".$wid.",".$field.",".($this->getFieldLevel($wid,$field)-1).",".(time()+floor($uprequire['time']/2)).")";
  1347. return mysql_query($q, $this->connection);
  1348. }
  1349. function getDemolition($wid = 0) {
  1350. if($wid) {
  1351. $q = "SELECT * FROM " . TB_PREFIX . "demolition WHERE vref=" . $wid;
  1352. } else {
  1353. $q = "SELECT * FROM " . TB_PREFIX . "demolition WHERE timetofinish<=" . time();
  1354. }
  1355. $result = mysql_query($q, $this->connection);
  1356. if(!empty($result)) {
  1357. return $this->mysql_fetch_all($result);
  1358. } else {
  1359. return NULL;
  1360. }
  1361. }
  1362. function finishDemolition($wid) {
  1363. $q = "UPDATE " . TB_PREFIX . "demolition SET timetofinish=" . time() . " WHERE vref=" . $wid;
  1364. return mysql_query($q, $this->connection);
  1365. }
  1366. function delDemolition($wid) {
  1367. $q = "DELETE FROM " . TB_PREFIX . "demolition WHERE vref=" . $wid;
  1368. return mysql_query($q, $this->connection);
  1369. }
  1370. function getJobs($wid) {
  1371. $q = "SELECT * FROM " . TB_PREFIX . "bdata where wid = $wid order by timestamp ASC";
  1372. $result = mysql_query($q, $this->connection);
  1373. return $this->mysql_fetch_all($result);
  1374. }
  1375. function getVillageByName($name) {
  1376. $name = mysql_real_escape_string($name, $this->connection);
  1377. $q = "SELECT wref FROM " . TB_PREFIX . "vdata where name = '$name' limit 1";
  1378. $result = mysql_query($q, $this->connection);
  1379. $dbarray = mysql_fetch_array($result);
  1380. return $dbarray['wref'];
  1381. }
  1382. /***************************
  1383. Function to set accept flag on market
  1384. References: id
  1385. ***************************/
  1386. function setMarketAcc($id) {
  1387. $q = "UPDATE " . TB_PREFIX . "market set accept = 1 where id = $id";
  1388. return mysql_query($q, $this->connection);
  1389. }
  1390. /***************************
  1391. Function to send resource to other village
  1392. Mode 0: Send
  1393. Mode 1: Cancel
  1394. References: Wood/ID, Clay, Iron, Crop, Mode
  1395. ***************************/
  1396. function sendResource($ref, $clay, $iron, $crop, $merchant, $mode) {
  1397. if(!$mode) {
  1398. $q = "INSERT INTO " . TB_PREFIX . "send values (0,$ref,$clay,$iron,$crop,$merchant)";
  1399. mysql_query($q, $this->connection);
  1400. return mysql_insert_id($this->connection);
  1401. } else {
  1402. $q = "DELETE FROM " . TB_PREFIX . "send where id = $ref";
  1403. return mysql_query($q, $this->connection);
  1404. }
  1405. }
  1406. /***************************
  1407. Function to get resources back if you delete offer
  1408. References: VillageRef (vref)
  1409. Made by: Dzoki
  1410. ***************************/
  1411. function getResourcesBack($vref, $gtype, $gamt) {
  1412. //Xtype (1) = wood, (2) = clay, (3) = iron, (4) = crop
  1413. if($gtype == 1) {
  1414. $q = "UPDATE " . TB_PREFIX . "vdata SET `wood` = `wood` + '$gamt' WHERE wref = $vref";
  1415. return mysql_query($q, $this->connection);
  1416. } else
  1417. if($gtype == 2) {
  1418. $q = "UPDATE " . TB_PREFIX . "vdata SET `clay` = `clay` + '$gamt' WHERE wref = $vref";
  1419. return mysql_query($q, $this->connection);
  1420. } else
  1421. if($gtype == 3) {
  1422. $q = "UPDATE " . TB_PREFIX . "vdata SET `iron` = `iron` + '$gamt' WHERE wref = $vref";
  1423. return mysql_query($q, $this->connection);
  1424. } else
  1425. if($gtype == 4) {
  1426. $q = "UPDATE " . TB_PREFIX . "vdata SET `crop` = `crop` + '$gamt' WHERE wref = $vref";
  1427. return mysql_query($q, $this->connection);
  1428. }
  1429. }
  1430. /***************************
  1431. Function to get info about offered resources
  1432. References: VillageRef (vref)
  1433. Made by: Dzoki
  1434. ***************************/
  1435. function getMarketField($vref, $field) {
  1436. $q = "SELECT $field FROM " . TB_PREFIX . "market where vref = '$vref'";
  1437. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1438. $dbarray = mysql_fetch_array($result);
  1439. return $dbarray[$field];
  1440. }
  1441. function removeAcceptedOffer($id) {
  1442. $q = "DELETE FROM " . TB_PREFIX . "market where id = $id";
  1443. $result = mysql_query($q, $this->connection);
  1444. return mysql_fetch_assoc($result);
  1445. }
  1446. /***************************
  1447. Function to add market offer
  1448. Mode 0: Add
  1449. Mode 1: Cancel
  1450. References: Village, Give, Amt, Want, Amt, Time, Alliance, Mode
  1451. ***************************/
  1452. function addMarket($vid, $gtype, $gamt, $wtype, $wamt, $time, $alliance, $merchant, $mode) {
  1453. if(!$mode) {
  1454. $q = "INSERT INTO " . TB_PREFIX . "market values (0,$vid,$gtype,$gamt,$wtype,$wamt,0,$time,$alliance,$merchant)";
  1455. mysql_query($q, $this->connection);
  1456. return mysql_insert_id($this->connection);
  1457. } else {
  1458. $q = "DELETE FROM " . TB_PREFIX . "market where id = $gtype and vref = $vid";
  1459. return mysql_query($q, $this->connection);
  1460. }
  1461. }
  1462. /***************************
  1463. Function to get market offer
  1464. References: Village, Mode
  1465. ***************************/
  1466. function getMarket($vid, $mode) {
  1467. $alliance = $this->getUserField($this->getVillageField($vid, "owner"), "alliance", 0);
  1468. if(!$mode) {
  1469. $q = "SELECT * FROM " . TB_PREFIX . "market where vref = $vid and accept = 0";
  1470. } else {
  1471. $q = "SELECT * FROM " . TB_PREFIX . "market where vref != $vid and alliance = $alliance or vref != $vid and alliance = 0 and accept = 0";
  1472. }
  1473. $result = mysql_query($q, $this->connection);
  1474. return $this->mysql_fetch_all($result);
  1475. }
  1476. /***************************
  1477. Function to get market offer
  1478. References: ID
  1479. ***************************/
  1480. function getMarketInfo($id) {
  1481. $q = "SELECT * FROM " . TB_PREFIX . "market where id = $id";
  1482. $result = mysql_query($q, $this->connection);
  1483. return mysql_fetch_assoc($result);
  1484. }
  1485. function setMovementProc($moveid) {
  1486. $q = "UPDATE " . TB_PREFIX . "movement set proc = 1 where moveid = $moveid";
  1487. return mysql_query($q, $this->connection);
  1488. }
  1489. /***************************
  1490. Function to retrieve used merchant
  1491. References: Village
  1492. ***************************/
  1493. function totalMerchantUsed($vid) {
  1494. $time = time();
  1495. $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";
  1496. $result = mysql_query($q, $this->connection);
  1497. $row = mysql_fetch_row($result);
  1498. $q2 = "SELECT sum(ref) from " . TB_PREFIX . "movement where sort_type = 2 and " . TB_PREFIX . "movement.to = $vid and proc = 0";
  1499. $result2 = mysql_query($q2, $this->connection);
  1500. $row2 = mysql_fetch_row($result2);
  1501. $q3 = "SELECT sum(merchant) from " . TB_PREFIX . "market where vref = $vid and accept = 0";
  1502. $result3 = mysql_query($q3, $this->connection);
  1503. $row3 = mysql_fetch_row($result3);
  1504. return $row[0] + $row2[0] + $row3[0];
  1505. }
  1506. /***************************
  1507. Function to retrieve movement of village
  1508. Type 0: Send Resource
  1509. Type 1: Send Merchant
  1510. Type 2: Return Resource
  1511. Type 3: Attack
  1512. Type 4: Return
  1513. Type 5: Settler
  1514. Type 6: Bounty Type 7: Reinf.
  1515. Mode 0: Send/Out
  1516. Mode 1: Recieve/In
  1517. References: Type, Village, Mode
  1518. ***************************/
  1519. function getMovement($type, $village, $mode) {
  1520. $time = time();
  1521. if(!$mode) {
  1522. $where = "from";
  1523. } else {
  1524. $where = "to";
  1525. }
  1526. switch($type) {
  1527. case 0:
  1528. $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";
  1529. break;
  1530. case 2:
  1531. $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";
  1532. break;
  1533. case 3:
  1534. $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";
  1535. break;
  1536. case 4:
  1537. $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";
  1538. break;
  1539. case 5:
  1540. $q = "SELECT * FROM " . TB_PREFIX . "movement where " . TB_PREFIX . "movement." . $where . " = $village and sort_type = 5 and proc = 0 ORDER BY endtime ASC";
  1541. break;
  1542. case 6:
  1543. $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";
  1544. break;
  1545. case 34:
  1546. $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";
  1547. break;
  1548. case 99:
  1549. $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";
  1550. break;
  1551. }
  1552. $result = mysql_query($q, $this->connection);
  1553. $array = $this->mysql_fetch_all($result);
  1554. return $array;
  1555. }
  1556. //=================== Fixed By Salmon+ =========================
  1557. function getResourceOneField($village,$id) {
  1558. $q = "SELECT * FROM ".TB_PREFIX."movement, ".TB_PREFIX."send where ".TB_PREFIX."movement.ref = ".TB_PREFIX."send.id and ".TB_PREFIX."movement.proc = 0 and sort_type = 6 and " . TB_PREFIX . "movement.moveid = $id";
  1559. //echo$q;
  1560. // $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "send where " . TB_PREFIX . "movement.to = $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";
  1561. // echo$q;
  1562. $result = mysql_query($q, $this->connection);
  1563. $array = $this->mysql_fetch_all($result);
  1564. return $array;
  1565. }
  1566. function checkMyVillages($wref,$uid) {
  1567. $q = "SELECT owner from " . TB_PREFIX . "vdata where wref = $wref";
  1568. $result = mysql_query($q, $this->connection);
  1569. $data=$this->mysql_fetch_all($result);
  1570. if ($data[owner]=$uid){
  1571. return true;
  1572. }else{
  1573. return false;
  1574. }
  1575. }
  1576. function getTotalBounty($units,$att_tribe) {
  1577. $start = ($att_tribe-1)*10+1;
  1578. $end = ($att_tribe*10);
  1579. $max_bounty = 0;
  1580. for($i=$start;$i<=$end;$i++) {
  1581. global ${'u'.$i};
  1582. // echo $units['t'.$i]."||".${'u'.$i}['cap'];
  1583. $max_bounty += $units['t'.$i]*${'u'.$i}['cap'];
  1584. }
  1585. return $max_bounty;
  1586. }
  1587. function getReportOfVillage ($uid,$myvillage,$dataarray) {
  1588. // var_dump($dataarray);
  1589. $villageid=$dataarray;
  1590. if ($myvillage == 1){
  1591. $q = "SELECT * FROM " . TB_PREFIX . "ndata where uid = $uid AND (ntype = 4 OR ntype = 0) AND data LIKE '%$villageid%' ORDER BY time DESC";
  1592. }else{
  1593. $q = "SELECT * FROM " . TB_PREFIX . "ndata where uid = $uid AND ntype != 4 AND data LIKE '%$villageid%' ORDER BY time DESC";
  1594. }
  1595. //$q = "SELECT * FROM " . TB_PREFIX . "ndata where data LIKE '%$villageid%' ORDER BY time DESC";
  1596. $result = mysql_query($q, $this->connection);
  1597. return $this->mysql_fetch_all($result);
  1598. }
  1599. function checkMyVillage ($villageid){
  1600. global $session;
  1601. $q="SELECT * FROM " . TB_PREFIX . "vdata WHERE wref=".$villageid." AND owner=".$session->uid;
  1602. $result = mysql_query($q);
  1603. if (mysql_num_rows($result)>0){
  1604. $check=1;
  1605. }else{
  1606. $check=0;
  1607. }
  1608. return $check;
  1609. }
  1610. function modifyHeroAttack($aid, $unit, $amt) {
  1611. $q = "UPDATE " . TB_PREFIX . "hero set $unit = $unit - $amt where id = $aid";
  1612. return mysql_query($q, $this->connection);
  1613. }
  1614. /*
  1615. function heroLvlUp($hero_info,$hero_levels,$uid){
  1616. // $datareturn=array();
  1617. if ($hero_info['experience'] > $hero_levels[$hero_info['level']+1]) {
  1618. mysql_query("UPDATE ".TB_PREFIX."hero SET `level` = `level` + 1 where `uid`='".$uid."'");
  1619. mysql_query("UPDATE ".TB_PREFIX."hero SET `points` = `points` + 5 where `uid`='".$uid."'");
  1620. // $hero = mysql_query("SELECT * FROM " . TB_PREFIX . "hero WHERE `uid` = " . $uid . "");
  1621. // $hero_info = mysql_fetch_array($hero);
  1622. $datareturn=true;
  1623. // $datareturn[1]=$hero_info;
  1624. } else {
  1625. $datareturn=false;
  1626. // $datareturn[1]=false;
  1627. }
  1628. return $datareturn;
  1629. }
  1630. function heroMultiLvlUp($hero_info,$hero_levels,$uid){
  1631. $i=0;
  1632. while ($i<1){
  1633. if (($this->heroLvlUp($hero_info,$hero_levels,$uid)==true){
  1634. }else{
  1635. $i=1;
  1636. }
  1637. }
  1638. }*/
  1639. //============================================================================================
  1640. function addA2b($ckey, $timestamp, $to, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11, $type) {
  1641. $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')";
  1642. mysql_query($q, $this->connection);
  1643. return mysql_insert_id($this->connection);
  1644. }
  1645. function getA2b($ckey, $check) {
  1646. $q = "SELECT * from " . TB_PREFIX . "a2b where ckey = '" . $ckey . "' AND time_check = '" . $check . "'";
  1647. $result = mysql_query($q, $this->connection);
  1648. if($result) {
  1649. return mysql_fetch_assoc($result);
  1650. } else {
  1651. return false;
  1652. }
  1653. }
  1654. function addMovement($type, $from, $to, $ref, $endtime) {
  1655. $q = "INSERT INTO " . TB_PREFIX . "movement values (0,$type,$from,$to,$ref,$endtime,0)";
  1656. return mysql_query($q, $this->connection);
  1657. }
  1658. 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) {
  1659. $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)";
  1660. mysql_query($q, $this->connection);
  1661. return mysql_insert_id($this->connection);
  1662. }
  1663. function modifyAttack($aid, $unit, $amt) {
  1664. $unit = 't' . $unit;
  1665. $q = "UPDATE " . TB_PREFIX . "attacks set $unit = $unit - $amt where id = $aid";
  1666. return mysql_query($q, $this->connection);
  1667. }
  1668. function getRanking() {
  1669. $q = "SELECT id,username,alliance,ap,apall,dp,dpall,access FROM " . TB_PREFIX . "users WHERE tribe<=3 AND access<" . (INCLUDE_ADMIN ? "10" : "8");
  1670. $result = mysql_query($q, $this->connection);
  1671. return $this->mysql_fetch_all($result);
  1672. }
  1673. function getVRanking() {
  1674. $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");
  1675. $result = mysql_query($q, $this->connection);
  1676. return $this->mysql_fetch_all($result);
  1677. }
  1678. function getARanking() {
  1679. $q = "SELECT id,name,tag FROM " . TB_PREFIX . "alidata where id != ''";
  1680. $result = mysql_query($q, $this->connection);
  1681. return $this->mysql_fetch_all($result);
  1682. }
  1683. function getHeroRanking() {
  1684. $q = "SELECT * FROM " . TB_PREFIX . "hero WHERE dead = 0";
  1685. $result = mysql_query($q, $this->connection);
  1686. return $this->mysql_fetch_all($result);
  1687. }
  1688. function getAllMember($aid) {
  1689. $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";
  1690. $result = mysql_query($q, $this->connection);
  1691. return $this->mysql_fetch_all($result);
  1692. }
  1693. function addUnits($vid) {
  1694. $q = "INSERT into " . TB_PREFIX . "units (vref) values ($vid)";
  1695. return mysql_query($q, $this->connection);
  1696. }
  1697. function getUnit($vid) {
  1698. $q = "SELECT * from " . TB_PREFIX . "units where vref = $vid";
  1699. $result = mysql_query($q, $this->connection);
  1700. if (!empty($result)) {
  1701. return mysql_fetch_assoc($result);
  1702. } else {
  1703. return NULL;
  1704. }
  1705. }
  1706. function getHero($uid=0,$all=0) {
  1707. if ($all) {
  1708. $q = "SELECT * FROM ".TB_PREFIX."hero WHERE uid=$uid";
  1709. } elseif (!$uid) {
  1710. $q = "SELECT * FROM ".TB_PREFIX."hero";
  1711. } else {
  1712. $q = "SELECT * FROM ".TB_PREFIX."hero WHERE dead=0 AND uid=$uid LIMIT 1";
  1713. }
  1714. $result = mysql_query($q, $this->connection);
  1715. if (!empty($result)) {
  1716. return $this->mysql_fetch_all($result);
  1717. } else {
  1718. return NULL;
  1719. }
  1720. }
  1721. function getHeroField($uid,$field){
  1722. $q = "SELECT * FROM ".TB_PREFIX."hero WHERE uid = $uid";
  1723. $result = mysql_query($q,$this->connection);
  1724. return $this->mysql_fetch_all($result);
  1725. }
  1726. function modifyHero($column,$value,$heroid,$mode=0) {
  1727. if(!$mode) {
  1728. $q = "UPDATE `".TB_PREFIX."hero` SET $column = $value WHERE heroid = $heroid";
  1729. } elseif($mode=1) {
  1730. $q = "UPDATE `".TB_PREFIX."hero` SET $column = $column + $value WHERE heroid = $heroid";
  1731. } else {
  1732. $q = "UPDATE `".TB_PREFIX."hero` SET $column = $column - $value WHERE heroid = $heroid";
  1733. }
  1734. return mysql_query($q, $this->connection);
  1735. }
  1736. function modifyHeroXp($column,$value,$heroid) {
  1737. $q = "UPDATE ".TB_PREFIX."hero SET $column = $column + $value WHERE uid=$heroid";
  1738. return mysql_query($q, $this->connection);
  1739. }
  1740. function addTech($vid) {
  1741. $q = "INSERT into " . TB_PREFIX . "tdata (vref) values ($vid)";
  1742. return mysql_query($q, $this->connection);
  1743. }
  1744. function addABTech($vid) {
  1745. $q = "INSERT into " . TB_PREFIX . "abdata (vref) values ($vid)";
  1746. return mysql_query($q, $this->connection);
  1747. }
  1748. function getABTech($vid) {
  1749. $q = "SELECT * FROM " . TB_PREFIX . "abdata where vref = $vid";
  1750. $result = mysql_query($q, $this->connection);
  1751. return mysql_fetch_assoc($result);
  1752. }
  1753. function addResearch($vid, $tech, $time) {
  1754. $q = "INSERT into " . TB_PREFIX . "research values (0,$vid,'$tech',$time)";
  1755. return mysql_query($q, $this->connection);
  1756. }
  1757. function getResearching($vid) {
  1758. $q = "SELECT * FROM " . TB_PREFIX . "research where vref = $vid";
  1759. $result = mysql_query($q, $this->connection);
  1760. return $this->mysql_fetch_all($result);
  1761. }
  1762. function checkIfResearched($vref, $unit) {
  1763. $q = "SELECT $unit FROM " . TB_PREFIX . "tdata WHERE vref = $vref";
  1764. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1765. $dbarray = mysql_fetch_array($result);
  1766. return $dbarray[$unit];
  1767. }
  1768. function getTech($vid) {
  1769. $q = "SELECT * from " . TB_PREFIX . "tdata where vref = $vid";
  1770. $result = mysql_query($q, $this->connection);
  1771. return mysql_fetch_assoc($result);
  1772. }
  1773. function getTraining($vid) {
  1774. $q = "SELECT * FROM " . TB_PREFIX . "training where vref = $vid ORDER BY id";
  1775. $result = mysql_query($q, $this->connection);
  1776. return $this->mysql_fetch_all($result);
  1777. }
  1778. function countTraining($vid) {
  1779. $q = "SELECT * FROM " . TB_PREFIX . "training WHERE vref = $vid";
  1780. $result = mysql_query($q, $this->connection);
  1781. $row = mysql_fetch_row($result);
  1782. return $row[0];
  1783. }
  1784. function trainUnit($vid, $unit, $amt, $pop, $each, $time, $mode) {
  1785. global $village, $building, $session, $technology;
  1786. if(!$mode) {
  1787. $barracks = array(1, 2, 3, 11, 12, 13, 14, 21, 22, 31, 32, 33, 34, 41, 42, 43, 44);
  1788. $stables = array(4, 5, 6, 15, 16, 23, 24, 25, 26, 35, 36, 45, 46);
  1789. $workshop = array(7, 8, 17, 18, 27, 28, 37, 38, 47, 48);
  1790. $residence = array(9, 10, 19, 20, 29, 30, 39, 40, 49, 50);
  1791. if(in_array($unit, $barracks)) {
  1792. $queued = $technology->getTrainingList(1);
  1793. } elseif(in_array($unit, $stables)) {
  1794. $queued = $technology->getTrainingList(2);
  1795. } elseif(in_array($unit, $workshop)) {
  1796. $queued = $technology->getTrainingList(3);
  1797. } elseif(in_array($unit, $residence)) {
  1798. $queued = $technology->getTrainingList(4);
  1799. }
  1800. if(count($queued) > 0) {
  1801. $time = $queued[count($queued) - 1]['commence'] + $queued[count($queued) - 1]['eachtime'] * $queued[count($queued) - 1]['amt'];
  1802. }
  1803. $now = time();
  1804. $q = "INSERT INTO " . TB_PREFIX . "training values (0,$vid,$unit,$amt,$pop,$now,$each,$time)";
  1805. } else {
  1806. $q = "DELETE FROM " . TB_PREFIX . "training where id = $vid";
  1807. }
  1808. return mysql_query($q, $this->connection);
  1809. }
  1810. function updateTraining($id, $trained) {
  1811. $time = time();
  1812. $q = "UPDATE " . TB_PREFIX . "training set amt = amt - $trained, timestamp = $time where id = $id";
  1813. return mysql_query($q, $this->connection);
  1814. }
  1815. function modifyUnit($vref, $unit, $amt, $mode) {
  1816. if($unit == 230) {
  1817. $unit = 30;
  1818. }
  1819. if($unit == 231) {
  1820. $unit = 31;
  1821. }
  1822. if($unit == 120) {
  1823. $unit = 20;
  1824. }
  1825. if($unit == 121) {
  1826. $unit = 21;
  1827. }
  1828. if ($unit =="hero"){
  1829. $unit = 'hero';
  1830. } else {$unit = 'u' . $unit;}
  1831. if(!$mode) {
  1832. $q = "UPDATE " . TB_PREFIX . "units set $unit = $unit - $amt where vref = $vref";
  1833. } else {
  1834. $q = "UPDATE " . TB_PREFIX . "units set $unit = $unit + $amt where vref = $vref";
  1835. }
  1836. return mysql_query($q, $this->connection);
  1837. }
  1838. function getEnforce($vid, $from) {
  1839. $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = $from and vref = $vid";
  1840. $result = mysql_query($q, $this->connection);
  1841. return mysql_fetch_assoc($result);
  1842. }
  1843. function addEnforce($data) {
  1844. $q = "INSERT into " . TB_PREFIX . "enforcement (vref,`from`) values (" . $data['to'] . "," . $data['from'] . ")";
  1845. mysql_query($q, $this->connection);
  1846. $id = mysql_insert_id($this->connection);
  1847. $owntribe = $this->getUserField($this->getVillageField($data['from'], "owner"), "tribe", 0);
  1848. $start = ($owntribe - 1) * 10 + 1;
  1849. $end = ($owntribe * 10);
  1850. //add unit
  1851. $j = '1';
  1852. for($i = $start; $i <= $end; $i++) {
  1853. $this->modifyEnforce($id, $i, $data['t' . $j . ''], 1);
  1854. $j++;
  1855. }
  1856. $this->modifyEnforce($id,'hero',$data['t11'],1);
  1857. return mysql_insert_id($this->connection);
  1858. }
  1859. function modifyEnforce($id, $unit, $amt, $mode) {
  1860. if($unit != 'hero') { $unit = 'u' . $unit; }
  1861. if(!$mode) {
  1862. $q = "UPDATE " . TB_PREFIX . "enforcement set $unit = $unit - $amt where id = $id";
  1863. } else {
  1864. $q = "UPDATE " . TB_PREFIX . "enforcement set $unit = $unit + $amt where id = $id";
  1865. }
  1866. mysql_query($q, $this->connection);
  1867. }
  1868. function getEnforceArray($id, $mode) {
  1869. if(!$mode) {
  1870. $q = "SELECT * from " . TB_PREFIX . "enforcement where id = $id";
  1871. } else {
  1872. $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = $id";
  1873. }
  1874. $result = mysql_query($q, $this->connection);
  1875. return mysql_fetch_assoc($result);
  1876. }
  1877. function getEnforceVillage($id, $mode) {
  1878. if(!$mode) {
  1879. $q = "SELECT * from " . TB_PREFIX . "enforcement where vref = $id";
  1880. } else {
  1881. $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = $id";
  1882. }
  1883. $result = mysql_query($q, $this->connection);
  1884. return $this->mysql_fetch_all($result);
  1885. }
  1886. function getVillageMovement($id) {
  1887. $vinfo = $this->getVillage($id);
  1888. $vtribe = $this->getUserField($vinfo['owner'], "tribe", 0);
  1889. $movingunits = array();
  1890. $outgoingarray = $this->getMovement(3, $id, 0);
  1891. if(!empty($outgoingarray)) {
  1892. foreach($outgoingarray as $out) {
  1893. for($i = 1; $i <= 10; $i++) {
  1894. $movingunits['u' . (($vtribe - 1) * 10 + $i)] += $out['t' . $i];
  1895. }
  1896. $movingunits['hero'] += $out['t11'];
  1897. }
  1898. }
  1899. $returningarray = $this->getMovement(4, $id, 1);
  1900. if(!empty($returningarray)) {
  1901. foreach($returningarray as $ret) {
  1902. if($ret['attack_type'] != 1) {
  1903. for($i = 1; $i <= 10; $i++) {
  1904. $movingunits['u' . (($vtribe - 1) * 10 + $i)] += $ret['t' . $i];
  1905. }
  1906. $movingunits['hero'] += $ret['t11'];
  1907. }
  1908. }
  1909. }
  1910. $settlerarray = $this->getMovement(5, $id, 0);
  1911. if(!empty($settlerarray)) {
  1912. $movingunits['u' . ($vtribe * 10)] += 3 * count($settlerarray);
  1913. }
  1914. return $movingunits;
  1915. }
  1916. ################# -START- ##################
  1917. ## WORLD WONDER STATISTICS FUNCTIONS! ##
  1918. ############################################
  1919. /***************************
  1920. Function to get all World Wonders
  1921. Made by: Dzoki
  1922. ***************************/
  1923. function getWW() {
  1924. $q = "SELECT * FROM " . TB_PREFIX . "fdata WHERE f99t = 40";
  1925. $result = mysql_query($q, $this->connection);
  1926. if(mysql_num_rows($result)) {
  1927. return true;
  1928. } else {
  1929. return false;
  1930. }
  1931. }
  1932. /***************************
  1933. Function to get world wonder level!
  1934. Made by: Dzoki
  1935. ***************************/
  1936. function getWWLevel($vref) {
  1937. $q = "SELECT f99 FROM " . TB_PREFIX . "fdata WHERE vref = $vref";
  1938. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1939. $dbarray = mysql_fetch_array($result);
  1940. return $dbarray['f99'];
  1941. }
  1942. /***************************
  1943. Function to get world wonder owner ID!
  1944. Made by: Dzoki
  1945. ***************************/
  1946. function getWWOwnerID($vref) {
  1947. $q = "SELECT owner FROM " . TB_PREFIX . "vdata WHERE wref = $vref";
  1948. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1949. $dbarray = mysql_fetch_array($result);
  1950. return $dbarray['owner'];
  1951. }
  1952. /***************************
  1953. Function to get user alliance name!
  1954. Made by: Dzoki
  1955. ***************************/
  1956. function getUserAllianceID($id) {
  1957. $q = "SELECT alliance FROM " . TB_PREFIX . "users where id = $id";
  1958. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1959. $dbarray = mysql_fetch_array($result);
  1960. return $dbarray['alliance'];
  1961. }
  1962. /***************************
  1963. Function to get WW name
  1964. Made by: Dzoki
  1965. ***************************/
  1966. function getWWName($vref) {
  1967. $q = "SELECT wwname FROM " . TB_PREFIX . "fdata WHERE vref = $vref";
  1968. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1969. $dbarray = mysql_fetch_array($result);
  1970. return $dbarray['wwname'];
  1971. }
  1972. /***************************
  1973. Function to change WW name
  1974. Made by: Dzoki
  1975. ***************************/
  1976. function submitWWname($vref, $name) {
  1977. $q = "UPDATE " . TB_PREFIX . "fdata SET `wwname` = '$name' WHERE " . TB_PREFIX . "fdata.`vref` = $vref";
  1978. return mysql_query($q, $this->connection);
  1979. }
  1980. //medal functions
  1981. function addclimberpop($user, $cp) {
  1982. $q = "UPDATE " . TB_PREFIX . "users set Rc = Rc + '$cp' where id = $user";
  1983. return mysql_query($q, $this->connection);
  1984. }
  1985. function addclimberrankpop($user, $cp) {
  1986. $q = "UPDATE " . TB_PREFIX . "users set clp = clp + '$cp' where id = $user";
  1987. return mysql_query($q, $this->connection);
  1988. }
  1989. function removeclimberrankpop($user, $cp) {
  1990. $q = "UPDATE " . TB_PREFIX . "users set clp = clp - '$cp'' where id = $user";
  1991. return mysql_query($q, $this->connection);
  1992. }
  1993. function updateoldrank($user, $cp) {
  1994. $q = "UPDATE " . TB_PREFIX . "users set oldrank = '$cp' where id = $user";
  1995. return mysql_query($q, $this->connection);
  1996. }
  1997. function removeclimberpop($user, $cp) {
  1998. $q = "UPDATE " . TB_PREFIX . "users set Rc = Rc - '$cp' where id = $user";
  1999. return mysql_query($q, $this->connection);
  2000. }
  2001. // ALLIANCE MEDAL FUNCTIONS
  2002. function addclimberpopAlly($user, $cp) {
  2003. $q = "UPDATE " . TB_PREFIX . "alidata set Rc = Rc + '$cp' where id = $user";
  2004. return mysql_query($q, $this->connection);
  2005. }
  2006. function addclimberrankpopAlly($user, $cp) {
  2007. $q = "UPDATE " . TB_PREFIX . "alidata set clp = clp + '$cp' where id = $user";
  2008. return mysql_query($q, $