PageRenderTime 72ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/GameEngine/Database/db_MYSQL.php

http://traviant4.codeplex.com
PHP | 3174 lines | 2752 code | 258 blank | 164 comment | 239 complexity | e62bb7b17c1eb923737ba01e347014dd MD5 | raw file
  1. <?php
  2. class MYSQL_DB {
  3. var $connection;
  4. function MYSQL_DB() {
  5. $this->connection = mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS) or die(mysql_error());
  6. mysql_select_db(SQL_DB, $this->connection) or die(mysql_error());
  7. }
  8. function register($username, $password, $email, $tribe, $locate, $act) {
  9. $time = time();
  10. $timep = ($time + PROTECTION);
  11. $rand = rand(8900, 9000);
  12. $q = "INSERT INTO " . TB_PREFIX . "users (username,password,access,email,timestamp,tribe,location,act,protect,clp,cp) VALUES ('$username', '$password', " . USER . ", '$email', $time, $tribe, $locate, '$act', $timep, '$rand', 1)";
  13. if(mysql_query($q, $this->connection)) {
  14. return mysql_insert_id($this->connection);
  15. } else {
  16. return false;
  17. }
  18. }
  19. function activate($username, $password, $email, $tribe, $locate, $act, $act2) {
  20. $time = time();
  21. $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')";
  22. if(mysql_query($q, $this->connection)) {
  23. return mysql_insert_id($this->connection);
  24. } else {
  25. return false;
  26. }
  27. }
  28. function unreg($username) {
  29. $q = "DELETE from " . TB_PREFIX . "activate where username = '$username'";
  30. return mysql_query($q, $this->connection);
  31. }
  32. function deleteReinf($id) {
  33. $q = "DELETE from " . TB_PREFIX . "enforcement where id = '$id'";
  34. mysql_query($q, $this->connection);
  35. }
  36. function updateResource($vid, $what, $number) {
  37. $q = "UPDATE " . TB_PREFIX . "vdata set " . $what . "=" . $number . " where wref = $vid";
  38. $result = mysql_query($q, $this->connection);
  39. return mysql_query($q, $this->connection);
  40. }
  41. function checkExist($ref, $mode) {
  42. if(!$mode) {
  43. $q = "SELECT username FROM " . TB_PREFIX . "users where username = '$ref' LIMIT 1";
  44. } else {
  45. $q = "SELECT email FROM " . TB_PREFIX . "users where email = '$ref' LIMIT 1";
  46. }
  47. $result = mysql_query($q, $this->connection);
  48. if(mysql_num_rows($result)) {
  49. return true;
  50. } else {
  51. return false;
  52. }
  53. }
  54. function checkExist_activate($ref, $mode) {
  55. if(!$mode) {
  56. $q = "SELECT username FROM " . TB_PREFIX . "activate where username = '$ref' LIMIT 1";
  57. } else {
  58. $q = "SELECT email FROM " . TB_PREFIX . "activate where email = '$ref' LIMIT 1";
  59. }
  60. $result = mysql_query($q, $this->connection);
  61. if(mysql_num_rows($result)) {
  62. return true;
  63. } else {
  64. return false;
  65. }
  66. }
  67. function updateUserField($ref, $field, $value, $mode) {
  68. if(!$mode) {
  69. $q = "UPDATE " . TB_PREFIX . "users set $field = '$value' where username = '$ref'";
  70. } elseif($mode==1) {
  71. $q = "UPDATE " . TB_PREFIX . "users set $field = '$value' where id = '$ref'";
  72. } elseif($mode==2) {
  73. $q = "UPDATE " . TB_PREFIX . "users set $field = $field + '$value' where id = '$ref'";
  74. }
  75. return mysql_query($q, $this->connection);
  76. }
  77. function getSitee($uid) {
  78. $q = "SELECT id from " . TB_PREFIX . "users where sit1 = $uid or sit2 = $uid";
  79. $result = mysql_query($q, $this->connection);
  80. return $this->mysql_fetch_all($result);
  81. }
  82. function getSitee1($uid) {
  83. $q = "SELECT * from " . TB_PREFIX . "users where sit1 = $uid";
  84. $result = mysql_query($q, $this->connection);
  85. $dbarray = mysql_fetch_array($result);
  86. return $dbarray;
  87. }
  88. function getSitee2($uid) {
  89. $q = "SELECT * from " . TB_PREFIX . "users where sit2 = $uid";
  90. $result = mysql_query($q, $this->connection);
  91. $dbarray = mysql_fetch_array($result);
  92. return $dbarray;
  93. }
  94. function removeMeSit($uid, $uid2) {
  95. $q = "UPDATE " . TB_PREFIX . "users set sit1 = 0 where id = $uid and sit1 = $uid2";
  96. mysql_query($q, $this->connection);
  97. $q2 = "UPDATE " . TB_PREFIX . "users set sit2 = 0 where id = $uid and sit2 = $uid2";
  98. mysql_query($q2, $this->connection);
  99. }
  100. function getUserField($ref, $field, $mode) {
  101. if(!$mode) {
  102. $q = "SELECT $field FROM " . TB_PREFIX . "users where id = '$ref'";
  103. } else {
  104. $q = "SELECT $field FROM " . TB_PREFIX . "users where username = '$ref'";
  105. }
  106. $result = mysql_query($q, $this->connection) or die(mysql_error());
  107. $dbarray = mysql_fetch_array($result);
  108. return $dbarray[$field];
  109. }
  110. function getActivateField($ref, $field, $mode) {
  111. if(!$mode) {
  112. $q = "SELECT $field FROM " . TB_PREFIX . "activate where id = '$ref'";
  113. } else {
  114. $q = "SELECT $field FROM " . TB_PREFIX . "activate where username = '$ref'";
  115. }
  116. $result = mysql_query($q, $this->connection) or die(mysql_error());
  117. $dbarray = mysql_fetch_array($result);
  118. return $dbarray[$field];
  119. }
  120. function login($username, $password) {
  121. $q = "SELECT password,sessid FROM " . TB_PREFIX . "users where username = '$username'";
  122. $result = mysql_query($q, $this->connection);
  123. $dbarray = mysql_fetch_array($result);
  124. if($dbarray['password'] == md5($password)) {
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. function checkActivate($act) {
  131. $q = "SELECT * FROM " . TB_PREFIX . "activate where act = '$act'";
  132. $result = mysql_query($q, $this->connection);
  133. $dbarray = mysql_fetch_array($result);
  134. return $dbarray;
  135. }
  136. function sitterLogin($username, $password) {
  137. $q = "SELECT sit1,sit2 FROM " . TB_PREFIX . "users where username = '$username' and access != " . BANNED;
  138. $result = mysql_query($q, $this->connection);
  139. $dbarray = mysql_fetch_array($result);
  140. if($dbarray['sit1'] != 0) {
  141. $q2 = "SELECT password FROM " . TB_PREFIX . "users where id = " . $dbarray['sit1'] . " and access != " . BANNED;
  142. $result2 = mysql_query($q2, $this->connection);
  143. $pw_sit1 = mysql_fetch_array($result2);
  144. }
  145. elseif($dbarray['sit2'] != 0) {
  146. $q3 = "SELECT password FROM " . TB_PREFIX . "users where id = " . $dbarray['sit2'] . " and access != " . BANNED;
  147. $result3 = mysql_query($q3, $this->connection);
  148. $pw_sit2 = mysql_fetch_array($result3);
  149. }
  150. if($dbarray['sit1'] != 0 || $dbarray['sit2'] != 0) {
  151. if($pw_sit1['password'] == md5($password) || $pw_sit2['password'] == md5($password)) {
  152. return true;
  153. } else {
  154. return false;
  155. }
  156. } else {
  157. return false;
  158. }
  159. }
  160. function setDeleting($uid, $mode) {
  161. $time = time() + 72 * 3600;
  162. if(!$mode) {
  163. $q = "INSERT into " . TB_PREFIX . "deleting values ($uid,$time)";
  164. } else {
  165. $q = "DELETE FROM " . TB_PREFIX . "deleting where uid = $uid";
  166. }
  167. mysql_query($q, $this->connection);
  168. }
  169. function isDeleting($uid) {
  170. $q = "SELECT timestamp from " . TB_PREFIX . "deleting where uid = $uid";
  171. $result = mysql_query($q, $this->connection);
  172. $dbarray = mysql_fetch_array($result);
  173. return $dbarray['timestamp'];
  174. }
  175. function modifyGold($userid, $amt, $mode) {
  176. if(!$mode) {
  177. $q = "UPDATE " . TB_PREFIX . "users set gold = gold - $amt where id = $userid";
  178. } else {
  179. $q = "UPDATE " . TB_PREFIX . "users set gold = gold + $amt where id = $userid";
  180. }
  181. return mysql_query($q, $this->connection);
  182. }
  183. /*****************************************
  184. Function to retrieve user array via Username or ID
  185. Mode 0: Search by Username
  186. Mode 1: Search by ID
  187. References: Alliance ID
  188. *****************************************/
  189. function getUserArray($ref, $mode) {
  190. if(!$mode) {
  191. $q = "SELECT * FROM " . TB_PREFIX . "users where username = '$ref'";
  192. } else {
  193. $q = "SELECT * FROM " . TB_PREFIX . "users where id = $ref";
  194. }
  195. $result = mysql_query($q, $this->connection);
  196. return mysql_fetch_array($result);
  197. }
  198. function getUserWithEmail($email) {
  199. $q = "SELECT * FROM " . TB_PREFIX . "users where email = '$email'";
  200. $result = mysql_query($q, $this->connection);
  201. return mysql_fetch_array($result);
  202. }
  203. function activeModify($username, $mode) {
  204. $time = time();
  205. if(!$mode) {
  206. $q = "INSERT into " . TB_PREFIX . "active VALUES ('$username',$time)";
  207. } else {
  208. $q = "DELETE FROM " . TB_PREFIX . "active where username = '$username'";
  209. }
  210. return mysql_query($q, $this->connection);
  211. }
  212. function addActiveUser($username, $time) {
  213. $q = "REPLACE into " . TB_PREFIX . "active values ('$username',$time)";
  214. if(mysql_query($q, $this->connection)) {
  215. return true;
  216. } else {
  217. return false;
  218. }
  219. }
  220. function updateActiveUser($username, $time) {
  221. $q = "REPLACE into " . TB_PREFIX . "active (`username`, `timestamp`) values ('$username',$time)";
  222. $q2 = "UPDATE " . TB_PREFIX . "users set timestamp = $time where username = '$username'";
  223. $exec1 = mysql_query($q, $this->connection);
  224. $exec2 = mysql_query($q2, $this->connection);
  225. if($exec1 && $exec2) {
  226. return true;
  227. } else {
  228. return false;
  229. }
  230. }
  231. function checkSitter($username){
  232. $q = "SELECT * FROM ".TB_PREFIX."online WHERE name = '".$username."'";
  233. $result = mysql_query($q, $this->connection);
  234. $dbarray = mysql_fetch_array($result);
  235. return $dbarray['sitter'];
  236. }
  237. public function canConquerOasis($vref,$wref) {
  238. $AttackerFields = $this->getResourceLevel($vref);
  239. for($i=19;$i<=38;$i++) {
  240. if($AttackerFields['f'.$i.'t'] == 37) { $HeroMansionLevel = $AttackerFields['f'.$i]; }
  241. }
  242. if($this->VillageOasisCount($vref) < floor(($HeroMansionLevel-5)/5)) {
  243. $OasisInfo = $this->getOasisInfo($wref);
  244. $troopcount = $this->countOasisTroops($wref);
  245. if($OasisInfo['conqured'] == 0 || $OasisInfo['conqured'] != 0 && $OasisInfo['loyalty'] < 99 / min(3,(4-$this->VillageOasisCount($OasisInfo['conqured']))) && $troopcount == 0) {
  246. $CoordsVillage = $this->getCoor($vref);
  247. $CoordsOasis = $this->getCoor($wref);
  248. if(abs($CoordsOasis['x']-$CoordsVillage['x'])<=3 && abs($CoordsOasis['y']-$CoordsVillage['y'])<=3) {
  249. return True;
  250. } else {
  251. return False;
  252. }
  253. } else {
  254. return False;
  255. }
  256. } else {
  257. return False;
  258. }
  259. }
  260. public function conquerOasis($vref,$wref) {
  261. $vinfo = $this->getVillage($vref);
  262. $uid = $vinfo['owner'];
  263. $q = "UPDATE `".TB_PREFIX."odata` SET conqured=$vref,loyalty=100,lastupdated=".time().",owner=$uid,name='Occupied Oasis' WHERE wref=$wref";
  264. return mysql_query($q, $this->connection);
  265. }
  266. public function modifyOasisLoyalty($wref) {
  267. if($this->isVillageOases($wref) != 0) {
  268. $OasisInfo = $this->getOasisInfo($wref);
  269. if($OasisInfo['conqured'] != 0) {
  270. $LoyaltyAmendment = floor(100 / min(3,(4-$this->VillageOasisCount($OasisInfo['conqured']))));
  271. $q = "UPDATE `".TB_PREFIX."odata` SET loyalty=loyalty-$LoyaltyAmendment WHERE wref=$wref";
  272. return mysql_query($q, $this->connection);
  273. }
  274. }
  275. }
  276. function checkactiveSession($username, $sessid) {
  277. $user = $this->getUserArray($username, 0);
  278. $sessidarray = explode("+", $user['sessid']);
  279. if(in_array($sessid, $sessidarray)) {
  280. return true;
  281. } else {
  282. return false;
  283. }
  284. }
  285. function submitProfile($uid, $gender, $location, $birthday, $des1, $des2) {
  286. $q = "UPDATE " . TB_PREFIX . "users set gender = $gender, location = '$location', birthday = '$birthday', desc1 = '$des1', desc2 = '$des2' where id = $uid";
  287. return mysql_query($q, $this->connection);
  288. }
  289. function gpack($uid, $gpack) {
  290. $q = "UPDATE " . TB_PREFIX . "users set gpack = '$gpack' where id = $uid";
  291. return mysql_query($q, $this->connection);
  292. }
  293. function UpdateOnline($mode, $name = "", $sit = 0) {
  294. global $session;
  295. if($mode == "login") {
  296. $q = "INSERT IGNORE INTO " . TB_PREFIX . "online (name, time, sitter) VALUES ('$name', ".time().", ".$sit.")";
  297. return mysql_query($q, $this->connection);
  298. } else {
  299. $q = "DELETE FROM " . TB_PREFIX . "online WHERE name ='" . $session->username . "'";
  300. return mysql_query($q, $this->connection);
  301. }
  302. }
  303. //Choice placement where sector
  304. function generateBase($sector){
  305. $sector = ($sector == 0) ? rand(1, 4) : $sector;
  306. // (-/-) SW
  307. if($sector == 1){
  308. $x_a = (WORLD_MAX - (WORLD_MAX*2));
  309. $x_b = 0;
  310. $y_a = (WORLD_MAX - (WORLD_MAX*2));
  311. $y_b = 0;
  312. $order = "ORDER BY y DESC,x DESC";
  313. $mmm = rand(-1, -20);
  314. $x_y = "AND x < -4 AND y < $mmm";
  315. }
  316. // (+/-) SE
  317. elseif($sector == 2){
  318. $x_a = (WORLD_MAX - (WORLD_MAX*2));
  319. $x_b = 0;
  320. $y_a = 0;
  321. $y_b = WORLD_MAX;
  322. $order = "ORDER BY y ASC,x DESC";
  323. $mmm = rand(1, 20);
  324. $x_y = "AND x < -4 AND y > $mmm";
  325. }
  326. // (+/+) NE
  327. elseif($sector == 3){
  328. $x_a = 0;
  329. $x_b = WORLD_MAX;
  330. $y_a = 0;
  331. $y_b = WORLD_MAX;
  332. $order = "ORDER BY y,x ASC";
  333. $mmm = rand(1, 20);
  334. $x_y = "AND x > 4 AND y > $mmm";
  335. }
  336. // (-/+) NW
  337. elseif($sector == 4){
  338. $x_a = 0;
  339. $x_b = WORLD_MAX;
  340. $y_a = (WORLD_MAX - (WORLD_MAX*2));
  341. $y_b = 0;
  342. $order = "ORDER BY y DESC, x ASC";
  343. $mmm = rand(-1, -20);
  344. $x_y = "AND x > 4 AND y < $mmm";
  345. }
  346. $q = "SELECT * FROM ".TB_PREFIX."wdata where fieldtype = 3 and occupied = 0 $x_y and (x BETWEEN $x_a AND $x_b) and (y BETWEEN $y_a AND $y_b) $order LIMIT 20";
  347. $result = mysql_query($q, $this->connection);
  348. $dbarray = mysql_fetch_array($result);
  349. return $dbarray['id'];
  350. }
  351. function setFieldTaken($id) {
  352. $q = "UPDATE " . TB_PREFIX . "wdata set occupied = 1 where id = $id";
  353. return mysql_query($q, $this->connection);
  354. }
  355. function addVillage($wid, $uid, $username, $capital) {
  356. $total = count($this->getVillagesID($uid));
  357. if($total >= 1) {
  358. $vname = "????? ????";
  359. } else {
  360. $vname = "????? ".$username;
  361. }
  362. $time = time();
  363. $q = "INSERT into " . TB_PREFIX . "vdata (wref, owner, name, capital, pop, cp, celebration, wood, clay, iron, maxstore, crop, maxcrop, lastupdate, created) values
  364. ('$wid', '$uid', '$vname', '$capital', 2, 1, 0, 780, 780, 780, 800, 780, 800, '$time', '$time')";
  365. return mysql_query($q, $this->connection) or die(mysql_error());
  366. }
  367. function addResourceFields($vid, $type) {
  368. switch($type) {
  369. case 1:
  370. $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)";
  371. break;
  372. case 2:
  373. $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)";
  374. break;
  375. case 3:
  376. $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)";
  377. break;
  378. case 4:
  379. $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)";
  380. break;
  381. case 5:
  382. $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)";
  383. break;
  384. case 6:
  385. $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)";
  386. break;
  387. case 7:
  388. $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)";
  389. break;
  390. case 8:
  391. $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)";
  392. break;
  393. case 9:
  394. $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)";
  395. break;
  396. case 10:
  397. $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)";
  398. break;
  399. case 11:
  400. $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)";
  401. break;
  402. case 12:
  403. $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)";
  404. break;
  405. }
  406. return mysql_query($q, $this->connection);
  407. }
  408. function isVillageOases($wref) {
  409. $q = "SELECT id, oasistype FROM " . TB_PREFIX . "wdata where id = $wref";
  410. $result = mysql_query($q, $this->connection);
  411. $dbarray = mysql_fetch_array($result);
  412. return $dbarray['oasistype'];
  413. }
  414. public function VillageOasisCount($vref) {
  415. $q = "SELECT count(*) FROM ".TB_PREFIX."odata WHERE conqured=$vref";
  416. $result = mysql_query($q, $this->connection);
  417. $row = mysql_fetch_row($result);
  418. return $row[0];
  419. }
  420. function populateOasis() {
  421. $q = "SELECT * FROM " . TB_PREFIX . "wdata where oasistype != 0";
  422. $result = mysql_query($q, $this->connection);
  423. while($row = mysql_fetch_array($result)) {
  424. $wid = $row['id'];
  425. $this->addUnits($wid);
  426. }
  427. }
  428. /***************************
  429. Function to retrieve type of village via ID
  430. References: Village ID
  431. ***************************/
  432. function getVillageType($wref) {
  433. $q = "SELECT id, fieldtype FROM " . TB_PREFIX . "wdata where id = $wref";
  434. $result = mysql_query($q, $this->connection);
  435. $dbarray = mysql_fetch_array($result);
  436. return $dbarray['fieldtype'];
  437. }
  438. function getVillageData($wref) {
  439. $q = "SELECT * FROM " . TB_PREFIX . "wdata where id = $wref";
  440. $result = mysql_query($q, $this->connection);
  441. return $this->mysql_fetch_all($result);
  442. }
  443. function getVillageType2($wref) {
  444. $q = "SELECT * FROM " . TB_PREFIX . "wdata where id = $wref";
  445. $result = mysql_query($q, $this->connection);
  446. $dbarray = mysql_fetch_array($result);
  447. return $dbarray['oasistype'];
  448. }
  449. function getVilWref($x, $y) {
  450. $q = "SELECT * FROM " . TB_PREFIX . "wdata where x = $x AND y = $y";
  451. $result = mysql_query($q, $this->connection);
  452. $dbarray = mysql_fetch_array($result);
  453. return $dbarray['id'];
  454. }
  455. function checkVilExist($wref) {
  456. $q = "SELECT * FROM " . TB_PREFIX . "vdata where wref = '$wref'";
  457. $result = mysql_query($q, $this->connection);
  458. if(mysql_num_rows($result)) {
  459. return true;
  460. } else {
  461. return false;
  462. }
  463. }
  464. function oasischecker($x, $y) {
  465. $q = "SELECT * FROM " . TB_PREFIX . "wdata where x = $x AND y = $y";
  466. $result = mysql_query($q, $this->connection);
  467. return $this->mysql_fetch_all($result);
  468. }
  469. /*****************************************
  470. Function to retrieve if is ocuped via ID
  471. References: Village ID
  472. *****************************************/
  473. function getVillageState($wref) {
  474. $q = "SELECT oasistype,occupied FROM " . TB_PREFIX . "wdata where id = $wref";
  475. $result = mysql_query($q, $this->connection);
  476. $dbarray = mysql_fetch_array($result);
  477. if($dbarray['occupied'] != 0 || $dbarray['oasistype'] != 0) {
  478. return true;
  479. } else {
  480. return false;
  481. }
  482. }
  483. function getProfileVillages($uid) {
  484. $q = "SELECT * from " . TB_PREFIX . "vdata where owner = $uid order by pop desc";
  485. $result = mysql_query($q, $this->connection);
  486. return $this->mysql_fetch_all($result);
  487. }
  488. function getProfileMedal($uid) {
  489. $q = "SELECT id,categorie,plaats,week,img,points from " . TB_PREFIX . "medal where userid = $uid order by id desc";
  490. $result = mysql_query($q, $this->connection);
  491. return $this->mysql_fetch_all($result);
  492. }
  493. function getProfileMedalAlly($uid) {
  494. $q = "SELECT id,categorie,plaats,week,img,points from " . TB_PREFIX . "allimedal where allyid = $uid order by id desc";
  495. $result = mysql_query($q, $this->connection);
  496. return $this->mysql_fetch_all($result);
  497. }
  498. function getVillageID($uid) {
  499. $q = "SELECT wref FROM " . TB_PREFIX . "vdata WHERE owner = $uid";
  500. $result = mysql_query($q, $this->connection);
  501. $dbarray = mysql_fetch_array($result);
  502. return $dbarray['wref'];
  503. }
  504. function getVillagesID($uid) {
  505. $q = "SELECT wref from " . TB_PREFIX . "vdata where owner = $uid order by capital DESC";
  506. $result = mysql_query($q, $this->connection);
  507. $array = $this->mysql_fetch_all($result);
  508. $newarray = array();
  509. for($i = 0; $i < count($array); $i++) {
  510. array_push($newarray, $array[$i]['wref']);
  511. }
  512. return $newarray;
  513. }
  514. function getVillage($vid) {
  515. $q = "SELECT * FROM " . TB_PREFIX . "vdata where wref = $vid";
  516. $result = mysql_query($q, $this->connection);
  517. return mysql_fetch_array($result);
  518. }
  519. function getOasisV($vid) {
  520. $q = "SELECT * FROM " . TB_PREFIX . "odata where wref = $vid";
  521. $result = mysql_query($q, $this->connection);
  522. return mysql_fetch_array($result);
  523. }
  524. function getAInfo($id) {
  525. $q = "SELECT * FROM " . TB_PREFIX . "wdata where id = $id";
  526. $result = mysql_query($q, $this->connection);
  527. return mysql_fetch_array($result);
  528. }
  529. function getMInfo($id) {
  530. $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";
  531. $result = mysql_query($q, $this->connection);
  532. return mysql_fetch_array($result);
  533. }
  534. function getOMInfo($id) {
  535. $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";
  536. $result = mysql_query($q, $this->connection);
  537. return mysql_fetch_array($result);
  538. }
  539. function getOasis($vid) {
  540. $q = "SELECT * FROM " . TB_PREFIX . "odata where conqured = $vid";
  541. $result = mysql_query($q, $this->connection);
  542. return $this->mysql_fetch_all($result);
  543. }
  544. function getOasisInfo($wid) {
  545. $q = "SELECT * FROM " . TB_PREFIX . "odata where wref = $wid";
  546. $result = mysql_query($q, $this->connection);
  547. return mysql_fetch_assoc($result);
  548. }
  549. function getVillageField($ref, $field) {
  550. $q = "SELECT $field FROM " . TB_PREFIX . "vdata where wref = $ref";
  551. $result = mysql_query($q, $this->connection);
  552. $dbarray = mysql_fetch_array($result);
  553. return $dbarray[$field];
  554. }
  555. function getOasisField($ref, $field) {
  556. $q = "SELECT $field FROM " . TB_PREFIX . "odata where wref = $ref";
  557. $result = mysql_query($q, $this->connection);
  558. $dbarray = mysql_fetch_array($result);
  559. return $dbarray[$field];
  560. }
  561. function setVillageField($ref, $field, $value) {
  562. $q = "UPDATE " . TB_PREFIX . "vdata set $field = '$value' where wref = $ref";
  563. return mysql_query($q, $this->connection);
  564. }
  565. function setVillageLevel($ref, $field, $value) {
  566. $q = "UPDATE " . TB_PREFIX . "fdata set " . $field . " = '" . $value . "' where vref = " . $ref . "";
  567. return mysql_query($q, $this->connection);
  568. }
  569. function getResourceLevel($vid) {
  570. $q = "SELECT * from " . TB_PREFIX . "fdata where vref = $vid";
  571. $result = mysql_query($q, $this->connection);
  572. return mysql_fetch_assoc($result);
  573. }
  574. function getAdminLog() {
  575. $q = "SELECT id,user,log,time from " . TB_PREFIX . "admin_log where id != 0 ORDER BY id DESC";
  576. $result = mysql_query($q, $this->connection);
  577. return $this->mysql_fetch_all($result);
  578. }
  579. function delAdminLog($id) {
  580. $q = "DELETE FROM " . TB_PREFIX . "admin_log where id = $id";
  581. return mysql_query($q, $this->connection);
  582. }
  583. function getCoor($wref) {
  584. $q = "SELECT x,y FROM " . TB_PREFIX . "wdata where id = $wref";
  585. $result = mysql_query($q, $this->connection);
  586. return mysql_fetch_array($result);
  587. }
  588. function CheckForum($id) {
  589. $q = "SELECT * from " . TB_PREFIX . "forum_cat where alliance = '$id'";
  590. $result = mysql_query($q, $this->connection);
  591. if(mysql_num_rows($result)) {
  592. return true;
  593. } else {
  594. return false;
  595. }
  596. }
  597. function CountCat($id) {
  598. $q = "SELECT count(id) FROM " . TB_PREFIX . "forum_topic where cat = '$id'";
  599. $result = mysql_query($q, $this->connection);
  600. $row = mysql_fetch_row($result);
  601. return $row[0];
  602. }
  603. function LastTopic($id) {
  604. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id' order by post_date";
  605. $result = mysql_query($q, $this->connection);
  606. return $this->mysql_fetch_all($result);
  607. }
  608. function CheckLastTopic($id) {
  609. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id'";
  610. $result = mysql_query($q, $this->connection);
  611. if(mysql_num_rows($result)) {
  612. return true;
  613. } else {
  614. return false;
  615. }
  616. }
  617. function CheckLastPost($id) {
  618. $q = "SELECT * from " . TB_PREFIX . "forum_post where topic = '$id'";
  619. $result = mysql_query($q, $this->connection);
  620. if(mysql_num_rows($result)) {
  621. return true;
  622. } else {
  623. return false;
  624. }
  625. }
  626. function LastPost($id) {
  627. $q = "SELECT * from " . TB_PREFIX . "forum_post where topic = '$id'";
  628. $result = mysql_query($q, $this->connection);
  629. return $this->mysql_fetch_all($result);
  630. }
  631. function CountTopic($id) {
  632. $q = "SELECT count(id) FROM " . TB_PREFIX . "forum_post where owner = '$id'";
  633. $result = mysql_query($q, $this->connection);
  634. $row = mysql_fetch_row($result);
  635. $qs = "SELECT count(id) FROM " . TB_PREFIX . "forum_topic where owner = '$id'";
  636. $results = mysql_query($qs, $this->connection);
  637. $rows = mysql_fetch_row($results);
  638. return $row[0] + $rows[0];
  639. }
  640. function CountPost($id) {
  641. $q = "SELECT count(id) FROM " . TB_PREFIX . "forum_post where topic = '$id'";
  642. $result = mysql_query($q, $this->connection);
  643. $row = mysql_fetch_row($result);
  644. return $row[0];
  645. }
  646. function ForumCat($id) {
  647. $q = "SELECT * from " . TB_PREFIX . "forum_cat where alliance = '$id' ORDER BY id";
  648. $result = mysql_query($q, $this->connection);
  649. return $this->mysql_fetch_all($result);
  650. }
  651. function ForumCatEdit($id) {
  652. $q = "SELECT * from " . TB_PREFIX . "forum_cat where id = '$id'";
  653. $result = mysql_query($q, $this->connection);
  654. return $this->mysql_fetch_all($result);
  655. }
  656. function ForumCatName($id) {
  657. $q = "SELECT forum_name from " . TB_PREFIX . "forum_cat where id = $id";
  658. $result = mysql_query($q, $this->connection);
  659. $dbarray = mysql_fetch_array($result);
  660. return $dbarray['forum_name'];
  661. }
  662. function CheckCatTopic($id) {
  663. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id'";
  664. $result = mysql_query($q, $this->connection);
  665. if(mysql_num_rows($result)) {
  666. return true;
  667. } else {
  668. return false;
  669. }
  670. }
  671. function CheckResultEdit($alli) {
  672. $q = "SELECT * from " . TB_PREFIX . "forum_edit where alliance = '$alli'";
  673. $result = mysql_query($q, $this->connection);
  674. if(mysql_num_rows($result)) {
  675. return true;
  676. } else {
  677. return false;
  678. }
  679. }
  680. function CheckCloseTopic($id) {
  681. $q = "SELECT close from " . TB_PREFIX . "forum_topic where id = '$id'";
  682. $result = mysql_query($q, $this->connection);
  683. $dbarray = mysql_fetch_array($result);
  684. return $dbarray['close'];
  685. }
  686. function CheckEditRes($alli) {
  687. $q = "SELECT result from " . TB_PREFIX . "forum_edit where alliance = '$alli'";
  688. $result = mysql_query($q, $this->connection);
  689. $dbarray = mysql_fetch_array($result);
  690. return $dbarray['result'];
  691. }
  692. function CreatResultEdit($alli, $result) {
  693. $q = "INSERT into " . TB_PREFIX . "forum_edit values (0,'$alli','$result')";
  694. mysql_query($q, $this->connection);
  695. return mysql_insert_id($this->connection);
  696. }
  697. function UpdateResultEdit($alli, $result) {
  698. $date = time();
  699. $q = "UPDATE " . TB_PREFIX . "forum_edit set result = '$result' where alliance = '$alli'";
  700. return mysql_query($q, $this->connection);
  701. }
  702. function UpdateEditTopic($id, $title, $cat) {
  703. $q = "UPDATE " . TB_PREFIX . "forum_topic set title = '$title', cat = '$cat' where id = $id";
  704. return mysql_query($q, $this->connection);
  705. }
  706. function UpdateEditForum($id, $name, $des) {
  707. $q = "UPDATE " . TB_PREFIX . "forum_cat set forum_name = '$name', forum_des = '$des' where id = $id";
  708. return mysql_query($q, $this->connection);
  709. }
  710. function StickTopic($id, $mode) {
  711. $q = "UPDATE " . TB_PREFIX . "forum_topic set stick = '$mode' where id = '$id'";
  712. return mysql_query($q, $this->connection);
  713. }
  714. function ForumCatTopic($id) {
  715. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id' AND stick = '' ORDER BY post_date desc";
  716. $result = mysql_query($q, $this->connection);
  717. return $this->mysql_fetch_all($result);
  718. }
  719. function ForumCatTopicStick($id) {
  720. $q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id' AND stick = '1' ORDER BY post_date desc";
  721. $result = mysql_query($q, $this->connection);
  722. return $this->mysql_fetch_all($result);
  723. }
  724. function ShowTopic($id) {
  725. $q = "SELECT * from " . TB_PREFIX . "forum_topic where id = '$id'";
  726. $result = mysql_query($q, $this->connection);
  727. return $this->mysql_fetch_all($result);
  728. }
  729. function ShowPost($id) {
  730. $q = "SELECT * from " . TB_PREFIX . "forum_post where topic = '$id'";
  731. $result = mysql_query($q, $this->connection);
  732. return $this->mysql_fetch_all($result);
  733. }
  734. function ShowPostEdit($id) {
  735. $q = "SELECT * from " . TB_PREFIX . "forum_post where id = '$id'";
  736. $result = mysql_query($q, $this->connection);
  737. return $this->mysql_fetch_all($result);
  738. }
  739. function CreatForum($owner, $alli, $name, $des, $area) {
  740. $q = "INSERT into " . TB_PREFIX . "forum_cat values (0,'$owner','$alli','$name','$des','$area')";
  741. mysql_query($q, $this->connection);
  742. return mysql_insert_id($this->connection);
  743. }
  744. function CreatTopic($title, $post, $cat, $owner, $alli, $ends) {
  745. $date = time();
  746. $q = "INSERT into " . TB_PREFIX . "forum_topic values (0,'$title','$post','$date','$date','$cat','$owner','$alli','$ends','','')";
  747. mysql_query($q, $this->connection);
  748. return mysql_insert_id($this->connection);
  749. }
  750. function CreatPost($post, $tids, $owner) {
  751. $date = time();
  752. $q = "INSERT into " . TB_PREFIX . "forum_post values (0,'$post','$tids','$owner','$date')";
  753. mysql_query($q, $this->connection);
  754. return mysql_insert_id($this->connection);
  755. }
  756. function UpdatePostDate($id) {
  757. $date = time();
  758. $q = "UPDATE " . TB_PREFIX . "forum_topic set post_date = '$date' where id = $id";
  759. return mysql_query($q, $this->connection);
  760. }
  761. function EditUpdateTopic($id, $post) {
  762. $q = "UPDATE " . TB_PREFIX . "forum_topic set post = '$post' where id = $id";
  763. return mysql_query($q, $this->connection);
  764. }
  765. function EditUpdatePost($id, $post) {
  766. $q = "UPDATE " . TB_PREFIX . "forum_post set post = '$post' where id = $id";
  767. return mysql_query($q, $this->connection);
  768. }
  769. function LockTopic($id, $mode) {
  770. $q = "UPDATE " . TB_PREFIX . "forum_topic set close = '$mode' where id = '$id'";
  771. return mysql_query($q, $this->connection);
  772. }
  773. function DeleteCat($id) {
  774. $qs = "DELETE from " . TB_PREFIX . "forum_cat where id = '$id'";
  775. $q = "DELETE from " . TB_PREFIX . "forum_topic where cat = '$id'";
  776. mysql_query($qs, $this->connection);
  777. return mysql_query($q, $this->connection);
  778. }
  779. function DeleteTopic($id) {
  780. $qs = "DELETE from " . TB_PREFIX . "forum_topic where id = '$id'";
  781. // $q = "DELETE from ".TB_PREFIX."forum_post where topic = '$id'";//
  782. return mysql_query($qs, $this->connection); //
  783. // mysql_query($q,$this->connection);
  784. }
  785. function DeletePost($id) {
  786. $q = "DELETE from " . TB_PREFIX . "forum_post where id = '$id'";
  787. return mysql_query($q, $this->connection);
  788. }
  789. function getAllianceName($id) {
  790. $q = "SELECT tag from " . TB_PREFIX . "alidata where id = $id";
  791. $result = mysql_query($q, $this->connection);
  792. $dbarray = mysql_fetch_array($result);
  793. return $dbarray['tag'];
  794. }
  795. function getAlliancePermission($ref, $field, $mode) {
  796. if(!$mode) {
  797. $q = "SELECT $field FROM " . TB_PREFIX . "ali_permission where uid = '$ref'";
  798. } else {
  799. $q = "SELECT $field FROM " . TB_PREFIX . "ali_permission where username = '$ref'";
  800. }
  801. $result = mysql_query($q, $this->connection) or die(mysql_error());
  802. $dbarray = mysql_fetch_array($result);
  803. return $dbarray[$field];
  804. }
  805. function getAlliance($id) {
  806. $q = "SELECT * from " . TB_PREFIX . "alidata where id = $id";
  807. $result = mysql_query($q, $this->connection);
  808. return mysql_fetch_assoc($result);
  809. }
  810. function setAlliName($aid, $name, $tag) {
  811. $q = "UPDATE " . TB_PREFIX . "alidata set name = '$name', tag = '$tag' where id = $aid";
  812. return mysql_query($q, $this->connection);
  813. }
  814. function isAllianceOwner($id) {
  815. $q = "SELECT * from " . TB_PREFIX . "alidata where leader = '$id'";
  816. $result = mysql_query($q, $this->connection);
  817. if(mysql_num_rows($result)) {
  818. return true;
  819. } else {
  820. return false;
  821. }
  822. }
  823. function aExist($ref, $type) {
  824. $q = "SELECT $type FROM " . TB_PREFIX . "alidata where $type = '$ref'";
  825. $result = mysql_query($q, $this->connection);
  826. if(mysql_num_rows($result)) {
  827. return true;
  828. } else {
  829. return false;
  830. }
  831. }
  832. function modifyPoints($aid, $points, $amt) {
  833. $q = "UPDATE " . TB_PREFIX . "users set $points = $points + $amt where id = $aid";
  834. return mysql_query($q, $this->connection);
  835. }
  836. function modifyPointsAlly($aid, $points, $amt) {
  837. $q = "UPDATE " . TB_PREFIX . "alidata set $points = $points + $amt where id = $aid";
  838. return mysql_query($q, $this->connection);
  839. }
  840. /*****************************************
  841. Function to create an alliance
  842. References:
  843. *****************************************/
  844. function createAlliance($tag, $name, $uid, $max) {
  845. $q = "INSERT into " . TB_PREFIX . "alidata values (0,'$name','$tag',$uid,0,0,0,'','',$max,'','','','','','','','')";
  846. mysql_query($q, $this->connection);
  847. return mysql_insert_id($this->connection);
  848. }
  849. /*****************************************
  850. Function to insert an alliance new
  851. References:
  852. *****************************************/
  853. function insertAlliNotice($aid, $notice) {
  854. $time = time();
  855. $q = "INSERT into " . TB_PREFIX . "ali_log values (0,'$aid','$notice',$time)";
  856. mysql_query($q, $this->connection);
  857. return mysql_insert_id($this->connection);
  858. }
  859. /*****************************************
  860. Function to delete alliance if empty
  861. References:
  862. *****************************************/
  863. function deleteAlliance($aid) {
  864. $result = mysql_query("SELECT * FROM " . TB_PREFIX . "users where alliance = $aid");
  865. $num_rows = mysql_num_rows($result);
  866. if($num_rows == 0) {
  867. $q = "DELETE FROM " . TB_PREFIX . "alidata WHERE id = $aid";
  868. }
  869. mysql_query($q, $this->connection);
  870. return mysql_insert_id($this->connection);
  871. }
  872. /*****************************************
  873. Function to read all alliance news
  874. References:
  875. *****************************************/
  876. function readAlliNotice($aid) {
  877. $q = "SELECT * from " . TB_PREFIX . "ali_log where aid = $aid ORDER BY date DESC";
  878. $result = mysql_query($q, $this->connection);
  879. return $this->mysql_fetch_all($result);
  880. }
  881. /*****************************************
  882. Function to create alliance permissions
  883. References: ID, notice, description
  884. *****************************************/
  885. function createAlliPermissions($uid, $aid, $rank, $opt1, $opt2, $opt3, $opt4, $opt5, $opt6, $opt7, $opt8) {
  886. $q = "INSERT into " . TB_PREFIX . "ali_permission values(0,'$uid','$aid','$rank','$opt1','$opt2','$opt3','$opt4','$opt5','$opt6','$opt7','$opt8')";
  887. mysql_query($q, $this->connection);
  888. return mysql_insert_id($this->connection);
  889. }
  890. /*****************************************
  891. Function to update alliance permissions
  892. References:
  893. *****************************************/
  894. function deleteAlliPermissions($uid) {
  895. $q = "DELETE from " . TB_PREFIX . "ali_permission where uid = '$uid'";
  896. return mysql_query($q, $this->connection);
  897. }
  898. /*****************************************
  899. Function to update alliance permissions
  900. References:
  901. *****************************************/
  902. function updateAlliPermissions($uid, $aid, $rank, $opt1, $opt2, $opt3, $opt4, $opt5, $opt6, $opt7) {
  903. $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";
  904. return mysql_query($q, $this->connection);
  905. }
  906. /*****************************************
  907. Function to read alliance permissions
  908. References: ID, notice, description
  909. *****************************************/
  910. function getAlliPermissions($uid, $aid) {
  911. $q = "SELECT * FROM " . TB_PREFIX . "ali_permission where uid = $uid && alliance = $aid";
  912. $result = mysql_query($q, $this->connection);
  913. return mysql_fetch_assoc($result);
  914. }
  915. /*****************************************
  916. Function to update an alliance description and notice
  917. References: ID, notice, description
  918. *****************************************/
  919. function submitAlliProfile($aid, $notice, $desc) {
  920. $q = "UPDATE " . TB_PREFIX . "alidata SET `notice` = '$notice', `desc` = '$desc' where id = $aid";
  921. return mysql_query($q, $this->connection);
  922. }
  923. function diplomacyInviteAdd($alli1, $alli2, $type) {
  924. $q = "INSERT INTO " . TB_PREFIX . "diplomacy (alli1,alli2,type,accepted) VALUES ($alli1,$alli2," . (int)intval($type) . ",0)";
  925. return mysql_query($q, $this->connection);
  926. }
  927. function diplomacyOwnOffers($session_alliance) {
  928. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli1 = $session_alliance AND accepted = 0";
  929. $result = mysql_query($q, $this->connection);
  930. return $this->mysql_fetch_all($result);
  931. }
  932. function getAllianceID($name) {
  933. $q = "SELECT id FROM " . TB_PREFIX . "alidata WHERE tag ='" . $this->RemoveXSS($name) . "'";
  934. $result = mysql_query($q, $this->connection);
  935. $dbarray = mysql_fetch_array($result);
  936. return $dbarray['id'];
  937. }
  938. function getDiplomacy($aid) {
  939. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE id = $aid";
  940. $result = mysql_query($q, $this->connection);
  941. return $this->mysql_fetch_all($result);
  942. }
  943. function diplomacyCancelOffer($id) {
  944. $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id";
  945. return mysql_query($q, $this->connection);
  946. }
  947. function diplomacyInviteAccept($id, $session_alliance) {
  948. $q = "UPDATE " . TB_PREFIX . "diplomacy SET accepted = 1 WHERE id = $id AND alli2 = $session_alliance";
  949. return mysql_query($q, $this->connection);
  950. }
  951. function diplomacyInviteDenied($id, $session_alliance) {
  952. $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id AND alli2 = $session_alliance";
  953. return mysql_query($q, $this->connection);
  954. }
  955. function diplomacyInviteCheck($session_alliance) {
  956. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli2 = $session_alliance AND accepted = 0";
  957. $result = mysql_query($q, $this->connection);
  958. return $this->mysql_fetch_all($result);
  959. }
  960. function diplomacyExistingRelationships($session_alliance) {
  961. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli2 = $session_alliance AND accepted = 1";
  962. $result = mysql_query($q, $this->connection);
  963. return $this->mysql_fetch_all($result);
  964. }
  965. function diplomacyExistingRelationships2($session_alliance) {
  966. $q = "SELECT * FROM " . TB_PREFIX . "diplomacy WHERE alli1 = $session_alliance AND accepted = 1";
  967. $result = mysql_query($q, $this->connection);
  968. return $this->mysql_fetch_all($result);
  969. }
  970. function diplomacyCancelExistingRelationship($id, $session_alliance) {
  971. $q = "DELETE FROM " . TB_PREFIX . "diplomacy WHERE id = $id AND alli2 = $session_alliance";
  972. return mysql_query($q, $this->connection);
  973. }
  974. function getUserAlliance($id) {
  975. $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";
  976. $result = mysql_query($q, $this->connection);
  977. $dbarray = mysql_fetch_array($result);
  978. if($dbarray['tag'] == "") {
  979. return "-";
  980. } else {
  981. return $dbarray['tag'];
  982. }
  983. }
  984. function modifyResource($vid, $wood, $clay, $iron, $crop, $mode) {
  985. if(!$mode) {
  986. $q = "UPDATE " . TB_PREFIX . "vdata set wood = wood - $wood, clay = clay - $clay, iron = iron - $iron, crop = crop - $crop where wref = $vid";
  987. } else {
  988. $q = "UPDATE " . TB_PREFIX . "vdata set wood = wood + $wood, clay = clay + $clay, iron = iron + $iron, crop = crop + $crop where wref = $vid";
  989. }
  990. return mysql_query($q, $this->connection);
  991. }
  992. function modifyOasisResource($vid, $wood, $clay, $iron, $crop, $mode) {
  993. if(!$mode) {
  994. $q = "UPDATE " . TB_PREFIX . "odata set wood = wood - $wood, clay = clay - $clay, iron = iron - $iron, crop = crop - $crop where wref = $vid";
  995. } else {
  996. $q = "UPDATE " . TB_PREFIX . "odata set wood = wood + $wood, clay = clay + $clay, iron = iron + $iron, crop = crop + $crop where wref = $vid";
  997. }
  998. return mysql_query($q, $this->connection);
  999. }
  1000. function getFieldLevel($vid, $field) {
  1001. $q = "SELECT f" . $field . " from " . TB_PREFIX . "fdata where vref = $vid";
  1002. $result = mysql_query($q, $this->connection);
  1003. return mysql_result($result, 0);
  1004. }
  1005. function getFieldType($vid, $field) {
  1006. $q = "SELECT f" . $field . "t from " . TB_PREFIX . "fdata where vref = $vid";
  1007. $result = mysql_query($q, $this->connection);
  1008. return mysql_result($result, 0);
  1009. }
  1010. function getVSumField($uid, $field) {
  1011. $q = "SELECT sum(" . $field . ") FROM " . TB_PREFIX . "vdata where owner = $uid";
  1012. $result = mysql_query($q, $this->connection);
  1013. $row = mysql_fetch_row($result);
  1014. return $row[0];
  1015. }
  1016. function updateVillage($vid) {
  1017. $time = time();
  1018. $q = "UPDATE " . TB_PREFIX . "vdata set lastupdate = $time where wref = $vid";
  1019. return mysql_query($q, $this->connection);
  1020. }
  1021. function updateOasis($vid) {
  1022. $time = time();
  1023. $q = "UPDATE " . TB_PREFIX . "odata set lastupdated = $time where wref = $vid";
  1024. return mysql_query($q, $this->connection);
  1025. }
  1026. function setVillageName($vid, $name) {
  1027. $q = "UPDATE " . TB_PREFIX . "vdata set name = '$name' where wref = $vid";
  1028. return mysql_query($q, $this->connection);
  1029. }
  1030. function modifyPop($vid, $pop, $mode) {
  1031. if(!$mode) {
  1032. $q = "UPDATE " . TB_PREFIX . "vdata set pop = pop + $pop where wref = $vid";
  1033. } else {
  1034. $q = "UPDATE " . TB_PREFIX . "vdata set pop = pop - $pop where wref = $vid";
  1035. }
  1036. return mysql_query($q, $this->connection);
  1037. }
  1038. function addCP($ref, $cp) {
  1039. $q = "UPDATE " . TB_PREFIX . "vdata set cp = cp + '$cp' where wref = '$ref'";
  1040. return mysql_query($q, $this->connection);
  1041. }
  1042. function addCel($ref, $cel, $type) {
  1043. $q = "UPDATE " . TB_PREFIX . "vdata set celebration = $cel, type= $type where wref = $ref";
  1044. return mysql_query($q, $this->connection);
  1045. }
  1046. function getCel() {
  1047. $time = time();
  1048. $q = "SELECT * FROM " . TB_PREFIX . "vdata where celebration < $time AND celebration != 0";
  1049. $result = mysql_query($q, $this->connection);
  1050. return $this->mysql_fetch_all($result);
  1051. }
  1052. function clearCel($ref) {
  1053. $q = "UPDATE " . TB_PREFIX . "vdata set celebration = 0, type = 0 where wref = $ref";
  1054. return mysql_query($q, $this->connection);
  1055. }
  1056. function setCelCp($user, $cp) {
  1057. $q = "UPDATE " . TB_PREFIX . "users set cp = cp + $cp where id = $user";
  1058. return mysql_query($q, $this->connection);
  1059. }
  1060. function clearExpansionSlot($id) {
  1061. for($i = 1; $i <= 3; $i++) {
  1062. $q = "UPDATE " . TB_PREFIX . "vdata SET exp" . $i . "=0 WHERE exp" . $i . "=" . $id;
  1063. mysql_query($q, $this->connection);
  1064. }
  1065. }
  1066. function getInvitation($uid) {
  1067. $q = "SELECT * FROM " . TB_PREFIX . "ali_invite where uid = $uid";
  1068. $result = mysql_query($q, $this->connection);
  1069. return $this->mysql_fetch_all($result);
  1070. }
  1071. function getAliInvitations($aid) {
  1072. $q = "SELECT * FROM " . TB_PREFIX . "ali_invite where alliance = $aid && accept = 0";
  1073. $result = mysql_query($q, $this->connection);
  1074. return $this->mysql_fetch_all($result);
  1075. }
  1076. function sendInvitation($uid, $alli, $sender) {
  1077. $time = time();
  1078. $q = "INSERT INTO " . TB_PREFIX . "ali_invite values (0,$uid,$alli,$sender,$time,0)";
  1079. return mysql_query($q, $this->connection) or die(mysql_error());
  1080. }
  1081. function removeInvitation($id) {
  1082. $q = "DELETE FROM " . TB_PREFIX . "ali_invite where id = $id";
  1083. return mysql_query($q, $this->connection);
  1084. }
  1085. function delMessage($id) {
  1086. $q = "DELETE FROM " . TB_PREFIX . "mdata WHERE id = $id";
  1087. return mysql_query($q, $this->connection);
  1088. }
  1089. function delNotice($id, $uid) {
  1090. $q = "DELETE FROM " . TB_PREFIX . "ndata WHERE id = $id AND uid = $uid";
  1091. return mysql_query($q, $this->connection);
  1092. }
  1093. function sendMessage($client, $owner, $topic, $message, $send) {
  1094. $time = time();
  1095. $q = "INSERT INTO " . TB_PREFIX . "mdata values (0,$client,$owner,'$topic',\"$message\",0,0,$send,$time)";
  1096. return mysql_query($q, $this->connection);
  1097. }
  1098. function setArchived($id) {
  1099. $q = "UPDATE " . TB_PREFIX . "mdata set archived = 1 where id = $id";
  1100. return mysql_query($q, $this->connection);
  1101. }
  1102. function setNorm($id) {
  1103. $q = "UPDATE " . TB_PREFIX . "mdata set archived = 0 where id = $id";
  1104. return mysql_query($q, $this->connection);
  1105. }
  1106. /***************************
  1107. Function to get messages
  1108. Mode 1: Get inbox
  1109. Mode 2: Get sent
  1110. Mode 3: Get message
  1111. Mode 4: Set viewed
  1112. Mode 5: Remove message
  1113. Mode 6: Retrieve archive
  1114. References: User ID/Message ID, Mode
  1115. ***************************/
  1116. function getMessage($id, $mode) {
  1117. global $session;
  1118. switch($mode) {
  1119. case 1:
  1120. $q = "SELECT * FROM " . TB_PREFIX . "mdata WHERE target = $id and send = 0 and archived = 0 ORDER BY time DESC";
  1121. break;
  1122. case 2:
  1123. // removed send no longer needed as we dont send 2 messages any more just 1
  1124. $q = "SELECT * FROM " . TB_PREFIX . "mdata WHERE owner = $id ORDER BY time DESC";
  1125. break;
  1126. case 3:
  1127. $q = "SELECT * FROM " . TB_PREFIX . "mdata where id = $id";
  1128. break;
  1129. case 4:
  1130. $q = "UPDATE " . TB_PREFIX . "mdata set viewed = 1 where id = $id AND target = $session->uid";
  1131. break;
  1132. case 5:
  1133. $q = "DELETE FROM " . TB_PREFIX . "mdata where id = $id";
  1134. break;
  1135. case 6:
  1136. $q = "SELECT * FROM " . TB_PREFIX . "mdata where target = $id and send = 0 and archived = 1";
  1137. break;
  1138. case 7:
  1139. $q = "SELECT * FROM " . TB_PREFIX . "mdata where target = $id and viewed = 0 and archived = 0";
  1140. break;
  1141. case 8:
  1142. $q = "SELECT * FROM " . TB_PREFIX . "ndata where uid = $id and viewed = 0";
  1143. break;
  1144. }
  1145. if($mode <= 3 || $mode >= 7) {
  1146. $result = mysql_query($q, $this->connection);
  1147. return $this->mysql_fetch_all($result);
  1148. } else {
  1149. return mysql_query($q, $this->connection);
  1150. }
  1151. }
  1152. function unarchiveNotice($id) {
  1153. $q = "UPDATE " . TB_PREFIX . "ndata set archive = 0 where id = $id";
  1154. return mysql_query($q, $this->connection);
  1155. }
  1156. function archiveNotice($id) {
  1157. $q = "update " . TB_PREFIX . "ndata set archive = 1 where id = $id";
  1158. return mysql_query($q, $this->connection);
  1159. }
  1160. function removeNotice($id) {
  1161. $q = "DELETE FROM " . TB_PREFIX . "ndata where id = $id";
  1162. return mysql_query($q, $this->connection);
  1163. }
  1164. function noticeViewed($id) {
  1165. $q = "UPDATE " . TB_PREFIX . "ndata set viewed = 1 where id = $id";
  1166. return mysql_query($q, $this->connection);
  1167. }
  1168. function addNotice($uid, $toWref, $ally, $type, $topic, $data, $time = 0) {
  1169. if($time == 0) {
  1170. $time = time();
  1171. }
  1172. $q = "INSERT INTO " . TB_PREFIX . "ndata (id, uid, toWref, ally, topic, ntype, data, time, viewed) values (0,'$uid','$toWref','$ally','$topic',$type,'$data',$time,0)";
  1173. return mysql_query($q, $this->connection) or die(mysql_error());
  1174. }
  1175. function getNotice($uid) {
  1176. $q = "SELECT * FROM " . TB_PREFIX . "ndata where uid = $uid ORDER BY time DESC";
  1177. $result = mysql_query($q, $this->connection);
  1178. return $this->mysql_fetch_all($result);
  1179. }
  1180. function getAttacks($ref) {
  1181. $q = "SELECT * FROM " . TB_PREFIX . "attacks where id = '$ref'";
  1182. $result = mysql_query($q, $this->connection);
  1183. return $this->mysql_fetch_all($result);
  1184. }
  1185. function getAlliAttacks($aid) {
  1186. $q = "SELECT * FROM " . TB_PREFIX . "ndata WHERE ally = $aid ORDER BY time DESC";
  1187. }
  1188. function addBuilding($wid, $field, $type, $loop, $time) {
  1189. $x = "UPDATE " . TB_PREFIX . "fdata SET f" . $field . "t=" . $type . " WHERE vref=" . $wid;
  1190. mysql_query($x, $this->connection) or die(mysql_error());
  1191. $q = "INSERT into " . TB_PREFIX . "bdata values (0,$wid,$field,$type,$loop,$time)";
  1192. return mysql_query($q, $this->connection);
  1193. }
  1194. function removeBuilding($d) {
  1195. global $building;
  1196. $jobLoopconID = -1;
  1197. $SameBuildCount = 0;
  1198. $jobs = $building->buildArray;
  1199. for($i = 0; $i < sizeof($jobs); $i++) {
  1200. if($jobs[$i]['id'] == $d) {
  1201. $jobDeleted = $i;
  1202. }
  1203. if($jobs[$i]['loopcon'] == 1) {
  1204. $jobLoopconID = $i;
  1205. }
  1206. }
  1207. if(count($jobs) > 1 && ($jobs[0]['field'] == $jobs[1]['field'])) {
  1208. $SameBuildCount = 1;
  1209. }
  1210. if(count($jobs) > 2 && ($jobs[0]['field'] == $jobs[2]['field'])) {
  1211. $SameBuildCount = 2;
  1212. }
  1213. if(count($jobs) > 2 && ($jobs[1]['field'] == $jobs[2]['field'])) {
  1214. $SameBuildCount = 3;
  1215. }
  1216. if($SameBuildCount > 0) {
  1217. if($d == $jobs[floor($SameBuildCount / 3)]['id'] || $d == $jobs[floor($SameBuildCount / 2) + 1]['id']) {
  1218. $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'];
  1219. mysql_query($q, $this->connection);
  1220. }
  1221. } else {
  1222. if($jobs[$jobDeleted]['field'] >= 19) {
  1223. $x = "SELECT f" . $jobs[$jobDeleted]['field'] . " FROM " . TB_PREFIX . "fdata WHERE vref=" . $jobs[$jobDeleted]['wid'];
  1224. $result = mysql_query($x, $this->connection) or die(mysql_error());
  1225. $fieldlevel = mysql_fetch_row($result);
  1226. if($fieldlevel[0] == 0) {
  1227. $x = "UPDATE " . TB_PREFIX . "fdata SET f" . $jobs[$jobDeleted]['field'] . "t=0 WHERE vref=" . $jobs[$jobDeleted]['wid'];
  1228. mysql_query($x, $this->connection) or die(mysql_error());
  1229. }
  1230. }
  1231. if(($jobLoopconID >= 0) && ($jobs[$jobDeleted]['loopcon'] != 1)) {
  1232. if(($jobs[$jobLoopconID]['field'] <= 18 && $jobs[$jobDeleted]['field'] <= 18) || ($jobs[$jobLoopconID]['field'] >= 19 && $jobs[$jobDeleted]['field'] >= 19)) {
  1233. $uprequire = $building->resourceRequired($jobs[$jobLoopconID]['field'], $jobs[$jobLoopconID]['type']);
  1234. $x = "UPDATE " . TB_PREFIX . "bdata SET loopcon=0,timestamp=" . (time() + $uprequire['time']) . " WHERE wid=" . $jobs[$jobDeleted]['wid'] . " AND loopcon=1";
  1235. mysql_query($x, $this->connection) or die(mysql_error());
  1236. }
  1237. }
  1238. }
  1239. $q = "DELETE FROM " . TB_PREFIX . "bdata where id = $d";
  1240. return mysql_query($q, $this->connection);
  1241. }
  1242. function addDemolition($wid, $field) {
  1243. global $building, $village;
  1244. $q = "DELETE FROM ".TB_PREFIX."bdata WHERE field=$field AND wid=$wid";
  1245. mysql_query($q, $this->connection);
  1246. $uprequire = $building->resourceRequired($field,$village->resarray['f'.$field.'t']);
  1247. $q = "INSERT INTO ".TB_PREFIX."demolition VALUES (".$wid.",".$field.",".($this->getFieldLevel($wid,$field)-1).",".(time()+floor($uprequire['time']/2)).")";
  1248. return mysql_query($q, $this->connection);
  1249. }
  1250. function getDemolition($wid = 0) {
  1251. if($wid) {
  1252. $q = "SELECT * FROM " . TB_PREFIX . "demolition WHERE vref=" . $wid;
  1253. } else {
  1254. $q = "SELECT * FROM " . TB_PREFIX . "demolition WHERE timetofinish<=" . time();
  1255. }
  1256. $result = mysql_query($q, $this->connection);
  1257. if(!empty($result)) {
  1258. return $this->mysql_fetch_all($result);
  1259. } else {
  1260. return NULL;
  1261. }
  1262. }
  1263. function finishDemolition($wid) {
  1264. $q = "UPDATE " . TB_PREFIX . "demolition SET timetofinish=" . time() . " WHERE vref=" . $wid;
  1265. return mysql_query($q, $this->connection);
  1266. }
  1267. function delDemolition($wid) {
  1268. $q = "DELETE FROM " . TB_PREFIX . "demolition WHERE vref=" . $wid;
  1269. return mysql_query($q, $this->connection);
  1270. }
  1271. function getJobs($wid) {
  1272. $q = "SELECT * FROM " . TB_PREFIX . "bdata where wid = $wid order by ID ASC";
  1273. $result = mysql_query($q, $this->connection);
  1274. return $this->mysql_fetch_all($result);
  1275. }
  1276. function getVillageByName($name) {
  1277. $name = mysql_real_escape_string($name, $this->connection);
  1278. $q = "SELECT wref FROM " . TB_PREFIX . "vdata where name = '$name' limit 1";
  1279. $result = mysql_query($q, $this->connection);
  1280. $dbarray = mysql_fetch_array($result);
  1281. return $dbarray['wref'];
  1282. }
  1283. /***************************
  1284. Function to set accept flag on market
  1285. References: id
  1286. ***************************/
  1287. function setMarketAcc($id) {
  1288. $q = "UPDATE " . TB_PREFIX . "market set accept = 1 where id = $id";
  1289. return mysql_query($q, $this->connection);
  1290. }
  1291. /***************************
  1292. Function to send resource to other village
  1293. Mode 0: Send
  1294. Mode 1: Cancel
  1295. References: Wood/ID, Clay, Iron, Crop, Mode
  1296. ***************************/
  1297. function sendResource($ref, $clay, $iron, $crop, $merchant, $mode) {
  1298. if(!$mode) {
  1299. $q = "INSERT INTO " . TB_PREFIX . "send values (0,$ref,$clay,$iron,$crop,$merchant)";
  1300. mysql_query($q, $this->connection);
  1301. return mysql_insert_id($this->connection);
  1302. } else {
  1303. $q = "DELETE FROM " . TB_PREFIX . "send where id = $ref";
  1304. return mysql_query($q, $this->connection);
  1305. }
  1306. }
  1307. /***************************
  1308. Function to get resources back if you delete offer
  1309. References: VillageRef (vref)
  1310. Made by: Dzoki
  1311. ***************************/
  1312. function getResourcesBack($vref, $gtype, $gamt) {
  1313. //Xtype (1) = wood, (2) = clay, (3) = iron, (4) = crop
  1314. if($gtype == 1) {
  1315. $q = "UPDATE " . TB_PREFIX . "vdata SET `wood` = `wood` + '$gamt' WHERE wref = $vref";
  1316. return mysql_query($q, $this->connection);
  1317. } else
  1318. if($gtype == 2) {
  1319. $q = "UPDATE " . TB_PREFIX . "vdata SET `clay` = `clay` + '$gamt' WHERE wref = $vref";
  1320. return mysql_query($q, $this->connection);
  1321. } else
  1322. if($gtype == 3) {
  1323. $q = "UPDATE " . TB_PREFIX . "vdata SET `iron` = `iron` + '$gamt' WHERE wref = $vref";
  1324. return mysql_query($q, $this->connection);
  1325. } else
  1326. if($gtype == 4) {
  1327. $q = "UPDATE " . TB_PREFIX . "vdata SET `crop` = `crop` + '$gamt' WHERE wref = $vref";
  1328. return mysql_query($q, $this->connection);
  1329. }
  1330. }
  1331. /***************************
  1332. Function to get info about offered resources
  1333. References: VillageRef (vref)
  1334. Made by: Dzoki
  1335. ***************************/
  1336. function getMarketField($vref, $field) {
  1337. $q = "SELECT $field FROM " . TB_PREFIX . "market where vref = '$vref'";
  1338. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1339. $dbarray = mysql_fetch_array($result);
  1340. return $dbarray[$field];
  1341. }
  1342. function removeAcceptedOffer($id) {
  1343. $q = "DELETE FROM " . TB_PREFIX . "market where id = $id";
  1344. $result = mysql_query($q, $this->connection);
  1345. return mysql_fetch_assoc($result);
  1346. }
  1347. /***************************
  1348. Function to add market offer
  1349. Mode 0: Add
  1350. Mode 1: Cancel
  1351. References: Village, Give, Amt, Want, Amt, Time, Alliance, Mode
  1352. ***************************/
  1353. function addMarket($vid, $gtype, $gamt, $wtype, $wamt, $time, $alliance, $merchant, $mode) {
  1354. if(!$mode) {
  1355. $q = "INSERT INTO " . TB_PREFIX . "market values (0,$vid,$gtype,$gamt,$wtype,$wamt,0,$time,$alliance,$merchant)";
  1356. mysql_query($q, $this->connection);
  1357. return mysql_insert_id($this->connection);
  1358. } else {
  1359. $q = "DELETE FROM " . TB_PREFIX . "market where id = $gtype and vref = $vid";
  1360. return mysql_query($q, $this->connection);
  1361. }
  1362. }
  1363. /***************************
  1364. Function to get market offer
  1365. References: Village, Mode
  1366. ***************************/
  1367. function getMarket($vid, $mode) {
  1368. $alliance = $this->getUserField($this->getVillageField($vid, "owner"), "alliance", 0);
  1369. if(!$mode) {
  1370. $q = "SELECT * FROM " . TB_PREFIX . "market where vref = $vid and accept = 0 ORDER BY id DESC";
  1371. } else {
  1372. $q = "SELECT * FROM " . TB_PREFIX . "market where vref != $vid and alliance = $alliance or vref != $vid and alliance = 0 and accept = 0 ORDER BY id DESC";
  1373. }
  1374. $result = mysql_query($q, $this->connection);
  1375. return $this->mysql_fetch_all($result);
  1376. }
  1377. /***************************
  1378. Function to get market offer
  1379. References: ID
  1380. ***************************/
  1381. function getMarketInfo($id) {
  1382. $q = "SELECT * FROM " . TB_PREFIX . "market where id = $id";
  1383. $result = mysql_query($q, $this->connection);
  1384. return mysql_fetch_assoc($result);
  1385. }
  1386. function setMovementProc($moveid) {
  1387. $q = "UPDATE " . TB_PREFIX . "movement set proc = 1 where moveid = $moveid";
  1388. return mysql_query($q, $this->connection);
  1389. }
  1390. /***************************
  1391. Function to retrieve used merchant
  1392. References: Village
  1393. ***************************/
  1394. function totalMerchantUsed($vid) {
  1395. $time = time();
  1396. $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";
  1397. $result = mysql_query($q, $this->connection);
  1398. $row = mysql_fetch_row($result);
  1399. $q2 = "SELECT sum(ref) from " . TB_PREFIX . "movement where sort_type = 2 and " . TB_PREFIX . "movement.to = $vid and proc = 0";
  1400. $result2 = mysql_query($q2, $this->connection);
  1401. $row2 = mysql_fetch_row($result2);
  1402. $q3 = "SELECT sum(merchant) from " . TB_PREFIX . "market where vref = $vid and accept = 0";
  1403. $result3 = mysql_query($q3, $this->connection);
  1404. $row3 = mysql_fetch_row($result3);
  1405. return $row[0] + $row2[0] + $row3[0];
  1406. }
  1407. /***************************
  1408. Function to retrieve movement of village
  1409. Type 0: Send Resource
  1410. Type 1: Send Merchant
  1411. Type 2: Return Resource
  1412. Type 3: Attack
  1413. Type 4: Return
  1414. Type 5: Settler
  1415. Type 6: Bounty
  1416. Type 7: Reinf.
  1417. Type 9: Adventure
  1418. Mode 0: Send/Out
  1419. Mode 1: Recieve/In
  1420. References: Type, Village, Mode
  1421. ***************************/
  1422. function getMovement($type, $village, $mode) {
  1423. $time = time();
  1424. if(!$mode) {
  1425. $where = "from";
  1426. } else {
  1427. $where = "to";
  1428. }
  1429. switch($type) {
  1430. case 0:
  1431. $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";
  1432. break;
  1433. case 2:
  1434. $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";
  1435. break;
  1436. case 3:
  1437. $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";
  1438. break;
  1439. case 4:
  1440. $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";
  1441. break;
  1442. case 5:
  1443. $q = "SELECT * FROM " . TB_PREFIX . "movement where " . TB_PREFIX . "movement." . $where . " = $village and sort_type = 5 and proc = 0";
  1444. break;
  1445. case 6:
  1446. $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";
  1447. break;
  1448. case 9:
  1449. $q = "SELECT * FROM " . TB_PREFIX . "movement where " . TB_PREFIX . "movement." . $where . " = $village and sort_type = 9 and proc = 0";
  1450. break;
  1451. case 34:
  1452. $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 .
  1453. "movement.sort_type = 4 ORDER BY endtime ASC";
  1454. break;
  1455. }
  1456. $result = mysql_query($q, $this->connection);
  1457. $array = $this->mysql_fetch_all($result);
  1458. return $array;
  1459. }
  1460. /***************************
  1461. Function to retrieve movement of village
  1462. Type 3: Attack
  1463. Type 4: Return
  1464. Type 5: Settler
  1465. Type 6: Bounty
  1466. Type 7: Reinf.
  1467. Type 9: Adventure
  1468. Mode 0: Send/Out
  1469. Mode 1: Recieve/In
  1470. References: Type, Village, Mode
  1471. ***************************/
  1472. function getMovement2($type, $village, $mode) {
  1473. $time = time();
  1474. if(!$mode) {
  1475. $where = "from";
  1476. } else {
  1477. $where = "to";
  1478. }
  1479. switch($type) {
  1480. case 3:
  1481. $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type != 2 ORDER BY endtime DESC";
  1482. break;
  1483. case 34:
  1484. $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type != 3 and " . TB_PREFIX . "attacks.attack_type != 4 or " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX .
  1485. "movement.sort_type = 4 ORDER BY endtime DESC";
  1486. break;
  1487. case 5:
  1488. $q = "SELECT * FROM " . TB_PREFIX . "movement where " . TB_PREFIX . "movement." . $where . " = $village and sort_type = 5 and proc = 0";
  1489. break;
  1490. case 7:
  1491. $q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement." . $where . " = $village and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type = 2 ORDER BY endtime DESC";
  1492. case 9:
  1493. $q = "SELECT * FROM " . TB_PREFIX . "movement where " . TB_PREFIX . "movement." . $where . " = $village and sort_type = 9 and proc = 0";
  1494. break;
  1495. }
  1496. $result = mysql_query($q, $this->connection);
  1497. $array = $this->mysql_fetch_all($result);
  1498. return $array;
  1499. }
  1500. function addA2b($ckey, $timestamp, $to, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11, $type) {
  1501. $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')";
  1502. mysql_query($q, $this->connection);
  1503. return mysql_insert_id($this->connection);
  1504. }
  1505. function getA2b($ckey, $check) {
  1506. $q = "SELECT * from " . TB_PREFIX . "a2b where ckey = '" . $ckey . "' AND time_check = '" . $check . "'";
  1507. $result = mysql_query($q, $this->connection);
  1508. if($result) {
  1509. return mysql_fetch_assoc($result);
  1510. } else {
  1511. return false;
  1512. }
  1513. }
  1514. function addMovement($type, $from, $to, $ref, $data, $endtime) {
  1515. $q = "INSERT INTO " . TB_PREFIX . "movement values (0,$type,$from,$to,$ref,'$data',$endtime,0)";
  1516. return mysql_query($q, $this->connection);
  1517. }
  1518. function addAttack($vid, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11, $type, $ctar1, $ctar2, $spy) {
  1519. $q = "INSERT INTO " . TB_PREFIX . "attacks values (0,$vid,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10,$t11,$type,$ctar1,$ctar2,$spy)";
  1520. mysql_query($q, $this->connection);
  1521. return mysql_insert_id($this->connection);
  1522. }
  1523. function modifyAttack($aid, $unit, $amt) {
  1524. $unit = 't' . $unit;
  1525. $q = "UPDATE " . TB_PREFIX . "attacks set $unit = $unit - $amt where id = $aid";
  1526. return mysql_query($q, $this->connection);
  1527. }
  1528. function getRanking() {
  1529. $q = "SELECT id,username,alliance,ap,apall,dp,dpall,access FROM " . TB_PREFIX . "users WHERE tribe<=3 AND access<" . (INCLUDE_ADMIN ? "10" : "8");
  1530. $result = mysql_query($q, $this->connection);
  1531. return $this->mysql_fetch_all($result);
  1532. }
  1533. function getBuildList($type) {
  1534. $q = "SELECT * FROM " . TB_PREFIX . "bdata WHERE type = $type";
  1535. $result = mysql_query($q, $this->connection);
  1536. return $this->mysql_fetch_all($result);
  1537. }
  1538. function getVRanking() {
  1539. $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");
  1540. $result = mysql_query($q, $this->connection);
  1541. return $this->mysql_fetch_all($result);
  1542. }
  1543. function getARanking() {
  1544. $q = "SELECT id,name,tag FROM " . TB_PREFIX . "alidata where id != ''";
  1545. $result = mysql_query($q, $this->connection);
  1546. return $this->mysql_fetch_all($result);
  1547. }
  1548. function getHeroRanking() {
  1549. $q = "SELECT * FROM " . TB_PREFIX . "hero";
  1550. $result = mysql_query($q, $this->connection);
  1551. return $this->mysql_fetch_all($result);
  1552. }
  1553. function getAllMember($aid) {
  1554. $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";
  1555. $result = mysql_query($q, $this->connection);
  1556. return $this->mysql_fetch_all($result);
  1557. }
  1558. function addUnits($vid) {
  1559. $q = "INSERT into " . TB_PREFIX . "units (vref) values ($vid)";
  1560. return mysql_query($q, $this->connection);
  1561. }
  1562. function getUnit($vid) {
  1563. $q = "SELECT * FROM " . TB_PREFIX . "units where vref = ".$vid."";
  1564. $result = mysql_query($q, $this->connection);
  1565. if (!empty($result)) {
  1566. return mysql_fetch_assoc($result);
  1567. } else {
  1568. return NULL;
  1569. }
  1570. }
  1571. function getHUnit($vid) {
  1572. $q = "SELECT hero FROM " . TB_PREFIX . "units where vref = ".$vid."";
  1573. $result = mysql_query($q, $this->connection);
  1574. $dbarray = mysql_fetch_array($result);
  1575. if ($dbarray['hero']!=0) {
  1576. return true;
  1577. } else {
  1578. return false;
  1579. }
  1580. }
  1581. function getHero($uid=0) {
  1582. if (!$uid) {
  1583. $q = "SELECT * FROM ".TB_PREFIX."hero";
  1584. } else {
  1585. $q = "SELECT * FROM ".TB_PREFIX."hero WHERE dead=0 AND uid=$uid LIMIT 1";
  1586. }
  1587. $result = mysql_query($q, $this->connection);
  1588. if (!empty($result)) {
  1589. return $this->mysql_fetch_all($result);
  1590. } else {
  1591. return NULL;
  1592. }
  1593. }
  1594. function modifyHero($column,$value,$heroid,$mode=0) {
  1595. if(!$mode){
  1596. $q = "UPDATE ".TB_PREFIX."hero SET $column = $value WHERE heroid = $heroid";
  1597. } elseif($mode==1){
  1598. $q = "UPDATE ".TB_PREFIX."hero SET $column = $column + $value WHERE heroid = $heroid";
  1599. } elseif($mode==2){
  1600. $q = "UPDATE ".TB_PREFIX."hero SET $column = $column - $value WHERE heroid = $heroid";
  1601. }
  1602. return mysql_query($q, $this->connection);
  1603. }
  1604. function modifyHero2($column,$value,$uid,$mode) {
  1605. if(!$mode){
  1606. $q = "UPDATE ".TB_PREFIX."hero SET $column = $value WHERE uid = $uid";
  1607. } elseif($mode==1){
  1608. $q = "UPDATE ".TB_PREFIX."hero SET $column = $column + $value WHERE uid = $uid";
  1609. } elseif($mode==2){
  1610. $q = "UPDATE ".TB_PREFIX."hero SET $column = $column - $value WHERE uid = $uid";
  1611. }
  1612. return mysql_query($q, $this->connection);
  1613. }
  1614. function addTech($vid) {
  1615. $q = "INSERT into " . TB_PREFIX . "tdata (vref) values ($vid)";
  1616. return mysql_query($q, $this->connection);
  1617. }
  1618. function addABTech($vid) {
  1619. $q = "INSERT into " . TB_PREFIX . "abdata (vref) values ($vid)";
  1620. return mysql_query($q, $this->connection);
  1621. }
  1622. function getABTech($vid) {
  1623. $q = "SELECT * FROM " . TB_PREFIX . "abdata where vref = $vid";
  1624. $result = mysql_query($q, $this->connection);
  1625. return mysql_fetch_assoc($result);
  1626. }
  1627. function addResearch($vid, $tech, $time) {
  1628. $q = "INSERT into " . TB_PREFIX . "research values (0,$vid,'$tech',$time)";
  1629. return mysql_query($q, $this->connection);
  1630. }
  1631. function getResearching($vid) {
  1632. $q = "SELECT * FROM " . TB_PREFIX . "research where vref = $vid";
  1633. $result = mysql_query($q, $this->connection);
  1634. return $this->mysql_fetch_all($result);
  1635. }
  1636. function checkIfResearched($vref, $unit) {
  1637. $q = "SELECT $unit FROM " . TB_PREFIX . "tdata WHERE vref = $vref";
  1638. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1639. $dbarray = mysql_fetch_array($result);
  1640. return $dbarray[$unit];
  1641. }
  1642. function getTech($vid) {
  1643. $q = "SELECT * from " . TB_PREFIX . "tdata where vref = $vid";
  1644. $result = mysql_query($q, $this->connection);
  1645. return mysql_fetch_assoc($result);
  1646. }
  1647. function getTraining($vid) {
  1648. $q = "SELECT * FROM " . TB_PREFIX . "training where vref = $vid ORDER BY id";
  1649. $result = mysql_query($q, $this->connection);
  1650. return $this->mysql_fetch_all($result);
  1651. }
  1652. function countTraining($vid) {
  1653. $q = "SELECT * FROM " . TB_PREFIX . "training WHERE vref = $vid";
  1654. $result = mysql_query($q, $this->connection);
  1655. $row = mysql_fetch_row($result);
  1656. return $row[0];
  1657. }
  1658. function trainUnit($vid, $unit, $amt, $pop, $each, $time, $mode) {
  1659. global $village, $building, $session, $technology;
  1660. if(!$mode) {
  1661. $barracks = array(1, 2, 3, 11, 12, 13, 14, 21, 22, 31, 32, 33, 34, 41, 42, 43, 44);
  1662. $stables = array(4, 5, 6, 15, 16, 23, 24, 25, 26, 35, 36, 45, 46);
  1663. $workshop = array(7, 8, 17, 18, 27, 28, 37, 38, 47, 48);
  1664. $residence = array(9, 10, 19, 20, 29, 30, 39, 40, 49, 50);
  1665. if(in_array($unit, $barracks)) {
  1666. $queued = $technology->getTrainingList(1);
  1667. } elseif(in_array($unit, $stables)) {
  1668. $queued = $technology->getTrainingList(2);
  1669. } elseif(in_array($unit, $workshop)) {
  1670. $queued = $technology->getTrainingList(3);
  1671. } elseif(in_array($unit, $residence)) {
  1672. $queued = $technology->getTrainingList(4);
  1673. }
  1674. if(count($queued) > 0) {
  1675. $time = $queued[count($queued) - 1]['commence'] + $queued[count($queued) - 1]['eachtime'] * $queued[count($queued) - 1]['amt'];
  1676. }
  1677. $now = time();
  1678. $q = "INSERT INTO " . TB_PREFIX . "training values (0,$vid,$unit,$amt,$pop,$now,$each,$time)";
  1679. } else {
  1680. $q = "DELETE FROM " . TB_PREFIX . "training where id = $vid";
  1681. }
  1682. return mysql_query($q, $this->connection);
  1683. }
  1684. function getHeroTrain($vid) {
  1685. $q = "SELECT * from " . TB_PREFIX . "training where vref = $vid and unit = 0";
  1686. $result = mysql_query($q, $this->connection);
  1687. $dbarray = mysql_fetch_array($result);
  1688. if(empty($result)) {
  1689. return false;
  1690. } else {
  1691. return $dbarray;
  1692. }
  1693. }
  1694. function trainHero($vid, $each, $mode) {
  1695. if(!$mode) {
  1696. $time = time();
  1697. $q = "INSERT INTO " . TB_PREFIX . "training values (0,$vid,0,1,6,$time,$each,$time)";
  1698. } else {
  1699. $q = "DELETE FROM " . TB_PREFIX . "training where id = $vid";
  1700. }
  1701. return mysql_query($q, $this->connection);
  1702. }
  1703. function updateTraining($id, $trained) {
  1704. $time = time();
  1705. $q = "UPDATE " . TB_PREFIX . "training set amt = amt - $trained, timestamp = $time where id = $id";
  1706. return mysql_query($q, $this->connection);
  1707. }
  1708. function modifyUnit($vref, $unit, $amt, $mode) {
  1709. if($unit == 230) {
  1710. $unit = 30;
  1711. }
  1712. if($unit == 231) {
  1713. $unit = 31;
  1714. }
  1715. if($unit == 120) {
  1716. $unit = 20;
  1717. }
  1718. if($unit == 121) {
  1719. $unit = 21;
  1720. }
  1721. if($unit == 'hero'){
  1722. $unit = 'hero';
  1723. }else{
  1724. $unit = 'u' . $unit;
  1725. }
  1726. if(!$mode) {
  1727. $q = "UPDATE " . TB_PREFIX . "units set $unit = $unit - $amt where vref = $vref";
  1728. } else {
  1729. $q = "UPDATE " . TB_PREFIX . "units set $unit = $unit + $amt where vref = $vref";
  1730. }
  1731. return mysql_query($q, $this->connection);
  1732. }
  1733. function getEnforce($vid, $from) {
  1734. $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = $from and vref = $vid";
  1735. $result = mysql_query($q, $this->connection);
  1736. return mysql_fetch_assoc($result);
  1737. }
  1738. function checkEnforce($vid, $from) {
  1739. $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = $from and vref = $vid";
  1740. $result = mysql_query($q, $this->connection);
  1741. if(!empty($result)) {
  1742. return mysql_insert_id($this->connection);
  1743. }else{
  1744. return true;
  1745. }
  1746. }
  1747. function addEnforce($data) {
  1748. $q = "INSERT into " . TB_PREFIX . "enforcement (vref,`from`) values (" . $data['to'] . "," . $data['from'] . ")";
  1749. mysql_query($q, $this->connection);
  1750. $id = mysql_insert_id($this->connection);
  1751. $owntribe = $this->getUserField($this->getVillageField($data['from'], "owner"), "tribe", 0);
  1752. $start = ($owntribe - 1) * 10 + 1;
  1753. $end = ($owntribe * 10);
  1754. //add unit
  1755. $j = '1';
  1756. for($i = $start; $i <= $end; $i++) {
  1757. $this->modifyEnforce($id, $i, $data['t' . $j . ''], 1);
  1758. $j++;
  1759. }
  1760. return mysql_insert_id($this->connection);
  1761. }
  1762. function addHeroEnforce($data) {
  1763. $q = "INSERT into " . TB_PREFIX . "enforcement (`vref`,`from`,`hero`) values (" . $data['to'] . "," . $data['from'] . ",1)";
  1764. mysql_query($q, $this->connection);
  1765. }
  1766. function modifyEnforce($id, $unit, $amt, $mode) {
  1767. if($unit == 'hero'){
  1768. $unit = 'hero';
  1769. }else{
  1770. $unit = 'u' . $unit;
  1771. }
  1772. if(!$mode) {
  1773. $q = "UPDATE " . TB_PREFIX . "enforcement set $unit = $unit - $amt where id = $id";
  1774. } else {
  1775. $q = "UPDATE " . TB_PREFIX . "enforcement set $unit = $unit + $amt where id = $id";
  1776. }
  1777. mysql_query($q, $this->connection);
  1778. }
  1779. function getEnforceArray($id, $mode) {
  1780. if(!$mode) {
  1781. $q = "SELECT * from " . TB_PREFIX . "enforcement where id = $id";
  1782. } else {
  1783. $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = $id";
  1784. }
  1785. $result = mysql_query($q, $this->connection);
  1786. return mysql_fetch_assoc($result);
  1787. }
  1788. function getEnforceVillage($id, $mode) {
  1789. if(!$mode) {
  1790. $q = "SELECT * from " . TB_PREFIX . "enforcement where `vref` = '$id'";
  1791. } else {
  1792. $q = "SELECT * from " . TB_PREFIX . "enforcement where `from` = '$id'";
  1793. }
  1794. $result = mysql_query($q, $this->connection);
  1795. return $this->mysql_fetch_all($result);
  1796. }
  1797. function getVillageMovement($id) {
  1798. $vinfo = $this->getVillage($id);
  1799. $vtribe = $this->getUserField($vinfo['owner'], "tribe", 0);
  1800. $movingunits = array();
  1801. $outgoingarray = $this->getMovement(3, $id, 0);
  1802. if(!empty($outgoingarray)) {
  1803. foreach($outgoingarray as $out) {
  1804. for($i = 1; $i <= 10; $i++) {
  1805. $movingunits['u' . (($vtribe - 1) * 10 + $i)] += $out['t' . $i];
  1806. }
  1807. }
  1808. }
  1809. $returningarray = $this->getMovement(4, $id, 1);
  1810. if(!empty($returningarray)) {
  1811. foreach($returningarray as $ret) {
  1812. if($ret['attack_type'] != 1) {
  1813. for($i = 1; $i <= 10; $i++) {
  1814. $movingunits['u' . (($vtribe - 1) * 10 + $i)] += $ret['t' . $i];
  1815. }
  1816. }
  1817. }
  1818. }
  1819. $settlerarray = $this->getMovement(5, $id, 0);
  1820. if(!empty($settlerarray)) {
  1821. $movingunits['u' . ($vtribe * 10)] += 3 * count($settlerarray);
  1822. }
  1823. return $movingunits;
  1824. }
  1825. ################# -START- ##################
  1826. ## WORLD WONDER STATISTICS FUNCTIONS! ##
  1827. ############################################
  1828. /***************************
  1829. Function to get all World Wonders
  1830. Made by: Dzoki
  1831. ***************************/
  1832. function getWW() {
  1833. $q = "SELECT * FROM " . TB_PREFIX . "fdata WHERE f99t = 40";
  1834. $result = mysql_query($q, $this->connection);
  1835. if(mysql_num_rows($result)) {
  1836. return true;
  1837. } else {
  1838. return false;
  1839. }
  1840. }
  1841. /***************************
  1842. Function to get world wonder level!
  1843. Made by: Dzoki
  1844. ***************************/
  1845. function getWWLevel($vref) {
  1846. $q = "SELECT f99 FROM " . TB_PREFIX . "fdata WHERE vref = $vref";
  1847. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1848. $dbarray = mysql_fetch_array($result);
  1849. return $dbarray['f99'];
  1850. }
  1851. /***************************
  1852. Function to get world wonder owner ID!
  1853. Made by: Dzoki
  1854. ***************************/
  1855. function getWWOwnerID($vref) {
  1856. $q = "SELECT owner FROM " . TB_PREFIX . "vdata WHERE wref = $vref";
  1857. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1858. $dbarray = mysql_fetch_array($result);
  1859. return $dbarray['owner'];
  1860. }
  1861. /***************************
  1862. Function to get user alliance name!
  1863. Made by: Dzoki
  1864. ***************************/
  1865. function getUserAllianceID($id) {
  1866. $q = "SELECT alliance FROM " . TB_PREFIX . "users where id = $id";
  1867. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1868. $dbarray = mysql_fetch_array($result);
  1869. return $dbarray['alliance'];
  1870. }
  1871. /***************************
  1872. Function to get WW name
  1873. Made by: Dzoki
  1874. ***************************/
  1875. function getWWName($vref) {
  1876. $q = "SELECT wwname FROM " . TB_PREFIX . "fdata WHERE vref = $vref";
  1877. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1878. $dbarray = mysql_fetch_array($result);
  1879. return $dbarray['wwname'];
  1880. }
  1881. /***************************
  1882. Function to change WW name
  1883. Made by: Dzoki
  1884. ***************************/
  1885. function submitWWname($vref, $name) {
  1886. $q = "UPDATE " . TB_PREFIX . "fdata SET `wwname` = '$name' WHERE " . TB_PREFIX . "fdata.`vref` = $vref";
  1887. return mysql_query($q, $this->connection);
  1888. }
  1889. //medal functions
  1890. function addclimberpop($user, $cp) {
  1891. $q = "UPDATE " . TB_PREFIX . "users set Rc = Rc + '$cp' where id = $user";
  1892. return mysql_query($q, $this->connection);
  1893. }
  1894. function addclimberrankpop($user, $cp) {
  1895. $q = "UPDATE " . TB_PREFIX . "users set clp = clp + '$cp' where id = $user";
  1896. return mysql_query($q, $this->connection);
  1897. }
  1898. function removeclimberrankpop($user, $cp) {
  1899. $q = "UPDATE " . TB_PREFIX . "users set clp = clp - '$cp' where id = $user";
  1900. return mysql_query($q, $this->connection);
  1901. }
  1902. function updateoldrank($user, $cp) {
  1903. $q = "UPDATE " . TB_PREFIX . "users set oldrank = '$cp' where id = $user";
  1904. return mysql_query($q, $this->connection);
  1905. }
  1906. function removeclimberpop($user, $cp) {
  1907. $q = "UPDATE " . TB_PREFIX . "users set Rc = Rc - '$cp' where id = $user";
  1908. return mysql_query($q, $this->connection);
  1909. }
  1910. // ALLIANCE MEDAL FUNCTIONS
  1911. function addclimberpopAlly($user, $cp) {
  1912. $q = "UPDATE " . TB_PREFIX . "alidata set Rc = Rc + '$cp' where id = $user";
  1913. return mysql_query($q, $this->connection);
  1914. }
  1915. function addclimberrankpopAlly($user, $cp) {
  1916. $q = "UPDATE " . TB_PREFIX . "alidata set clp = clp + '$cp' where id = $user";
  1917. return mysql_query($q, $this->connection);
  1918. }
  1919. function removeclimberrankpopAlly($user, $cp) {
  1920. $q = "UPDATE " . TB_PREFIX . "alidata set clp = clp - '$cp'' where id = $user";
  1921. return mysql_query($q, $this->connection);
  1922. }
  1923. function updateoldrankAlly($user, $cp) {
  1924. $q = "UPDATE " . TB_PREFIX . "alidata set oldrank = '$cp' where id = $user";
  1925. return mysql_query($q, $this->connection);
  1926. }
  1927. function removeclimberpopAlly($user, $cp) {
  1928. $q = "UPDATE " . TB_PREFIX . "alidata set Rc = Rc - '$cp' where id = $user";
  1929. return mysql_query($q, $this->connection);
  1930. }
  1931. function modifyCommence($id) {
  1932. $time = time();
  1933. $q = "UPDATE " . TB_PREFIX . "training set commence = $time WHERE id=$id";
  1934. return mysql_query($q, $this->connection);
  1935. }
  1936. function getTrainingList() {
  1937. $q = "SELECT * FROM " . TB_PREFIX . "training where vref != ''";
  1938. $result = mysql_query($q, $this->connection);
  1939. return $this->mysql_fetch_all($result);
  1940. }
  1941. function getNeedDelete() {
  1942. $time = time();
  1943. $q = "SELECT uid FROM " . TB_PREFIX . "deleting where timestamp < $time";
  1944. $result = mysql_query($q, $this->connection);
  1945. return $this->mysql_fetch_all($result);
  1946. }
  1947. function countUser() {
  1948. $q = "SELECT count(id) FROM " . TB_PREFIX . "users where id != 0";
  1949. $result = mysql_query($q, $this->connection);
  1950. $row = mysql_fetch_row($result);
  1951. return $row[0];
  1952. }
  1953. function countAlli() {
  1954. $q = "SELECT count(id) FROM " . TB_PREFIX . "alidata where id != 0";
  1955. $result = mysql_query($q, $this->connection);
  1956. $row = mysql_fetch_row($result);
  1957. return $row[0];
  1958. }
  1959. /***************************
  1960. Function to process MYSQLi->fetch_all (Only exist in MYSQL)
  1961. References: Result
  1962. ***************************/
  1963. function mysql_fetch_all($result) {
  1964. $all = array();
  1965. if($result) {
  1966. while($row = mysql_fetch_assoc($result)) {
  1967. $all[] = $row;
  1968. }
  1969. return $all;
  1970. }
  1971. }
  1972. function query_return($q) {
  1973. $result = mysql_query($q, $this->connection);
  1974. return $this->mysql_fetch_all($result);
  1975. }
  1976. /***************************
  1977. Function to do free query
  1978. References: Query
  1979. ***************************/
  1980. function query($query) {
  1981. return mysql_query($query, $this->connection);
  1982. }
  1983. function RemoveXSS($val) {
  1984. return htmlspecialchars($val, ENT_QUOTES);
  1985. }
  1986. //MARKET FIXES
  1987. function getWoodAvailable($wref) {
  1988. $q = "SELECT wood FROM " . TB_PREFIX . "vdata WHERE wref = $wref";
  1989. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1990. $dbarray = mysql_fetch_array($result);
  1991. return $dbarray['wood'];
  1992. }
  1993. function getClayAvailable($wref) {
  1994. $q = "SELECT clay FROM " . TB_PREFIX . "vdata WHERE wref = $wref";
  1995. $result = mysql_query($q, $this->connection) or die(mysql_error());
  1996. $dbarray = mysql_fetch_array($result);
  1997. return $dbarray['clay'];
  1998. }
  1999. function getIronAvailable($wref) {
  2000. $q = "SELECT iron FROM " . TB_PREFIX . "vdata WHERE wref = $wref";
  2001. $result = mysql_query($q, $this->connection) or die(mysql_error());
  2002. $dbarray = mysql_fetch_array($result);
  2003. return $dbarray['iron'];
  2004. }
  2005. function getCropAvailable($wref) {
  2006. $q = "SELECT crop FROM " . TB_PREFIX . "vdata WHERE wref = $wref";
  2007. $result = mysql_query($q, $this->connection) or die(mysql_error());
  2008. $dbarray = mysql_fetch_array($result);
  2009. return $dbarray['crop'];
  2010. }
  2011. function Getowner($vid) {
  2012. $s = "SELECT owner FROM " . TB_PREFIX . "vdata where wref = $vid";
  2013. $result1 = mysql_query($s, $this->connection);
  2014. $row1 = mysql_fetch_row($result1);
  2015. return $row1[0];
  2016. }
  2017. public function debug($time, $uid, $debug_info) {
  2018. $q = "INSERT INTO " . TB_PREFIX . "debug_info (time,uid,debug_info) VALUES ($time,$uid,$debug_info)";
  2019. if(mysql_query($q, $this->connection)) {
  2020. return mysql_insert_id($this->connection);
  2021. } else {
  2022. return false;
  2023. }
  2024. }
  2025. function poulateOasisdata() {
  2026. $q2 = "SELECT * FROM " . TB_PREFIX . "wdata where oasistype != 0";
  2027. $result2 = mysql_query($q2, $this->connection);
  2028. while($row = mysql_fetch_array($result2)) {
  2029. $wid = $row['id'];
  2030. switch($row['oasistype']) {
  2031. case 1:
  2032. $tt = "1000,1000,1000,1000,1000,1000";
  2033. break;
  2034. case 2:
  2035. $tt = "2000,1000,1000,2000,1000,2000";
  2036. break;
  2037. case 3:
  2038. $tt = "2000,1000,1000,2000,2000,2000";
  2039. break;
  2040. case 4:
  2041. $tt = "1000,1000,1000,1000,1000,1000";
  2042. break;
  2043. case 5:
  2044. $tt = "1000,2000,1000,2000,1000,2000";
  2045. break;
  2046. case 6:
  2047. $tt = "1000,2000,1000,2000,1000,2000";
  2048. break;
  2049. case 7:
  2050. $tt = "1000,1000,1000,1000,1000,1000";
  2051. break;
  2052. case 8:
  2053. $tt = "1000,1000,2000,2000,1000,2000";
  2054. break;
  2055. case 9:
  2056. $tt = "1000,1000,2000,2000,2000,2000";
  2057. break;
  2058. case 10:
  2059. $tt = "1000,1000,1000,1000,1000,1000";
  2060. break;
  2061. case 11:
  2062. $tt = "1000,1000,1000,2000,2000,2000";
  2063. break;
  2064. case 12:
  2065. $tt = "1000,1000,1000,2000,2000,2000";
  2066. break;
  2067. }
  2068. $basearray = $this->getOMInfo($wid);
  2069. //We switch type of oasis and instert record with apropriate infomation.
  2070. $q = "INSERT into " . TB_PREFIX . "odata VALUES ('" . $basearray['id'] . "'," . $basearray['oasistype'] . ",0,".$tt."," . time() . ",100,3,'????? ????? ????')";
  2071. $result = mysql_query($q, $this->connection);
  2072. }
  2073. }
  2074. public function getAvailableExpansionTraining() {
  2075. global $building, $session, $technology, $village;
  2076. $q = "SELECT (IF(exp1=0,1,0)+IF(exp2=0,1,0)+IF(exp3=0,1,0)) FROM " . TB_PREFIX . "vdata WHERE wref = $village->wid";
  2077. $result = mysql_query($q, $this->connection);
  2078. $row = mysql_fetch_row($result);
  2079. $maxslots = $row[0];
  2080. $residence = $building->getTypeLevel(25);
  2081. $palace = $building->getTypeLevel(26);
  2082. if($residence > 0) {
  2083. $maxslots -= (3 - floor($residence / 10));
  2084. }
  2085. if($palace > 0) {
  2086. $maxslots -= (3 - floor(($palace - 5) / 5));
  2087. }
  2088. $q = "SELECT (u10+u20+u30) FROM " . TB_PREFIX . "units WHERE vref = $village->wid";
  2089. $result = mysql_query($q, $this->connection);
  2090. $row = mysql_fetch_row($result);
  2091. $settlers = $row[0];
  2092. $q = "SELECT (u9+u19+u29) FROM " . TB_PREFIX . "units WHERE vref = $village->wid";
  2093. $result = mysql_query($q, $this->connection);
  2094. $row = mysql_fetch_row($result);
  2095. $chiefs = $row[0];
  2096. $settlers += 3 * count($this->getMovement(5, $village->wid, 0));
  2097. $current_movement = $this->getMovement(3, $village->wid, 0);
  2098. if(!empty($current_movement)) {
  2099. foreach($current_movement as $build) {
  2100. $settlers += $build['t10'];
  2101. $chiefs += $build['t9'];
  2102. }
  2103. }
  2104. $current_movement = $this->getMovement(3, $village->wid, 1);
  2105. if(!empty($current_movement)) {
  2106. foreach($current_movement as $build) {
  2107. $settlers += $build['t10'];
  2108. $chiefs += $build['t9'];
  2109. }
  2110. }
  2111. $current_movement = $this->getMovement(4, $village->wid, 0);
  2112. if(!empty($current_movement)) {
  2113. foreach($current_movement as $build) {
  2114. $settlers += $build['t10'];
  2115. $chiefs += $build['t9'];
  2116. }
  2117. }
  2118. $current_movement = $this->getMovement(4, $village->wid, 1);
  2119. if(!empty($current_movement)) {
  2120. foreach($current_movement as $build) {
  2121. $settlers += $build['t10'];
  2122. $chiefs += $build['t9'];
  2123. }
  2124. }
  2125. $q = "SELECT (u10+u20+u30) FROM " . TB_PREFIX . "enforcement WHERE `from` = $village->wid";
  2126. $result = mysql_query($q, $this->connection);
  2127. $row = mysql_fetch_row($result);
  2128. if(!empty($row)) {
  2129. foreach($row as $reinf) {
  2130. $settlers += $reinf[0];
  2131. }
  2132. }
  2133. $q = "SELECT (u9+u19+u29) FROM " . TB_PREFIX . "enforcement WHERE `from` = $village->wid";
  2134. $result = mysql_query($q, $this->connection);
  2135. $row = mysql_fetch_row($result);
  2136. if(!empty($row)) {
  2137. foreach($row as $reinf) {
  2138. $chiefs += $reinf[0];
  2139. }
  2140. }
  2141. $trainlist = $technology->getTrainingList(4);
  2142. if(!empty($trainlist)) {
  2143. foreach($trainlist as $train) {
  2144. if($train['unit'] % 10 == 0) {
  2145. $settlers += $train['amt'];
  2146. }
  2147. if($train['unit'] % 10 == 9) {
  2148. $chiefs += $train['amt'];
  2149. }
  2150. }
  2151. }
  2152. // trapped settlers/chiefs calculation required
  2153. $settlerslots = $maxslots * 3 - $settlers - $chiefs * 3;
  2154. $chiefslots = $maxslots - $chiefs - floor(($settlers + 2) / 3);
  2155. if(!$technology->getTech(($session->tribe - 1) * 10 + 9)) {
  2156. $chiefslots = 0;
  2157. }
  2158. $slots = array("chiefs" => $chiefslots, "settlers" => $settlerslots);
  2159. return $slots;
  2160. }
  2161. function addArtefact($vref, $owner, $type, $size, $name, $desc, $effect, $img) {
  2162. $q = "INSERT INTO `" . TB_PREFIX . "artefacts` (`vref`, `owner`, `type`, `size`, `conquered`, `name`, `desc`, `effect`, `img`) VALUES ('$vref', '$owner', '$type', '$size', '" . time() . "', '$name', '$desc', '$effect', '$img')";
  2163. return mysql_query($q, $this->connection);
  2164. }
  2165. function getOwnArtefactInfo($vref) {
  2166. $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vref";
  2167. $result = mysql_query($q, $this->connection);
  2168. return mysql_fetch_array($result);
  2169. }
  2170. function getOwnArtefactInfoByType($vref, $type) {
  2171. $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vref AND type = $type";
  2172. $result = mysql_query($q, $this->connection);
  2173. return mysql_fetch_array($result);
  2174. }
  2175. function getOwnUniqueArtefactInfo($id, $type, $size) {
  2176. $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE owner = $id AND type = $type AND size=$size";
  2177. $result = mysql_query($q, $this->connection);
  2178. return mysql_fetch_array($result);
  2179. }
  2180. function getArtefactInfo() {
  2181. $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE id > 0";
  2182. $result = mysql_query($q, $this->connection);
  2183. return mysql_fetch_array($result);
  2184. }
  2185. function claimArtefact($vref, $ovref, $id) {
  2186. $time = time();
  2187. $q = "UPDATE " . TB_PREFIX . "artefacts SET vref = $vref, owner = $id WHERE vref = $ovref";
  2188. $result = mysql_query($q, $this->connection);
  2189. return mysql_fetch_array($result);
  2190. }
  2191. function getArtefactDetails($id) {
  2192. $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE id = " . $id . "";
  2193. $result = mysql_query($q, $this->connection);
  2194. return mysql_fetch_array($result);
  2195. }
  2196. function HeroFace($uid) {
  2197. $q = "SELECT * FROM " . TB_PREFIX . "heroface WHERE uid = ".$uid."";
  2198. $result = mysql_query($q, $this->connection);
  2199. return mysql_fetch_array($result);
  2200. }
  2201. function addHeroFace($uid, $bread, $ear, $eye, $eyebrow, $face, $hair, $mouth, $nose, $color) {
  2202. $q = "INSERT INTO `" . TB_PREFIX . "heroface` (`beard`, `ear`, `eye`, `eyebrow`, `face`, `hair`, `mouth`, `nose`, `color`, `foot`, `helmet`, `horse`, `leftHand`, `rightHand`) VALUES ('$bread', '$ear', '$eye', '$eyebrow', '$face', '$hair', '$mouth', '$nose', '$color', '0', '0', '0', 'leftHand', 'rightHand')";
  2203. return mysql_query($q, $this->connection);
  2204. }
  2205. function modifyHeroFace($uid,$column,$value) {
  2206. $q = "UPDATE ".TB_PREFIX."heroface SET $column = $value WHERE uid = $uid";
  2207. return mysql_query($q, $this->connection);
  2208. }
  2209. function modifyHeroXp($column,$value,$uid) {
  2210. $q = "UPDATE ".TB_PREFIX."hero SET $column = $column + $value WHERE uid = $uid";
  2211. return mysql_query($q, $this->connection);
  2212. }
  2213. function populateOasisUnitsLow() {
  2214. $q2 = "SELECT * FROM " . TB_PREFIX . "wdata where oasistype != 0";
  2215. $result2 = mysql_query($q2, $this->connection);
  2216. while($row = mysql_fetch_array($result2)) {
  2217. $wid = $row['id'];
  2218. $basearray = $this->getMInfo($wid);
  2219. //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
  2220. switch($basearray['oasistype']) {
  2221. case 1:
  2222. case 2:
  2223. // Oasis Random populate
  2224. $UP35 = rand(5, 30);
  2225. $UP36 = rand(5, 30);
  2226. $UP37 = rand(0, 30);
  2227. //+25% lumber per hour
  2228. $q = "UPDATE " . TB_PREFIX . "units SET u35 = u35 + '" . $UP35 . "', u36 = u36 + '" . $UP36 . "', u37 = u37 + '" . $UP37 . "' WHERE vref = '" . $wid . "'";
  2229. $result = mysql_query($q, $this->connection);
  2230. break;
  2231. case 3:
  2232. // Oasis Random populate
  2233. $UP35 = rand(5, 30);
  2234. $UP36 = rand(5, 30);
  2235. $UP37 = rand(1, 30);
  2236. $UP39 = rand(0, 10);
  2237. $fil = rand(0,20);
  2238. if($fil == 1){
  2239. $UP40 = rand(0, 31);
  2240. }else{
  2241. $UP40 = 0;
  2242. }
  2243. //+25% lumber per hour
  2244. $q = "UPDATE " . TB_PREFIX . "units SET u35 = u35 + '" . $UP35 . "', u36 = u36 + '" . $UP36 . "', u37 = u37 + '" . $UP37 . "', u39 = u39 + '" . $UP39 . "', u40 = u40 + '" . $UP40 . "' WHERE vref = '" . $wid . "'";
  2245. $result = mysql_query($q, $this->connection);
  2246. break;
  2247. case 4:
  2248. case 5:
  2249. // Oasis Random populate
  2250. $UP31 = rand(5, 40);
  2251. $UP32 = rand(5, 30);
  2252. $UP35 = rand(0, 25);
  2253. //+25% lumber per hour
  2254. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '" . $UP31 . "', u32 = u32 + '" . $UP32 . "', u35 = u35 + '" . $UP35 . "' WHERE vref = '" . $wid . "'";
  2255. $result = mysql_query($q, $this->connection);
  2256. break;
  2257. case 6:
  2258. // Oasis Random populate
  2259. $UP31 = rand(5, 40);
  2260. $UP32 = rand(5, 30);
  2261. $UP35 = rand(1, 25);
  2262. $UP38 = rand(0, 15);
  2263. $fil = rand(0,20);
  2264. if($fil == 1){
  2265. $UP40 = rand(0, 31);
  2266. }else{
  2267. $UP40 = 0;
  2268. }
  2269. //+25% lumber per hour
  2270. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '" . $UP31 . "', u32 = u32 + '" . $UP32 . "', u35 = u35 + '" . $UP35 . "', u38 = u38 + '" . $UP38 . "', u40 = u40 + '" . $UP40 . "' WHERE vref = '" . $wid . "'";
  2271. $result = mysql_query($q, $this->connection);
  2272. break;
  2273. case 7:
  2274. case 8:
  2275. // Oasis Random populate
  2276. $UP31 = rand(5, 40);
  2277. $UP32 = rand(5, 30);
  2278. $UP34 = rand(0, 25);
  2279. //+25% lumber per hour
  2280. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '" . $UP31 . "', u32 = u32 + '" . $UP32 . "', u34 = u34 + '" . $UP34 . "' WHERE vref = '" . $wid . "'";
  2281. $result = mysql_query($q, $this->connection);
  2282. break;
  2283. case 9:
  2284. // Oasis Random populate
  2285. $UP31 = rand(5, 40);
  2286. $UP32 = rand(5, 30);
  2287. $UP34 = rand(1, 25);
  2288. $UP37 = rand(0, 15);
  2289. $fil = rand(0,20);
  2290. if($fil == 1){
  2291. $UP40 = rand(0, 31);
  2292. }else{
  2293. $UP40 = 0;
  2294. }
  2295. //+25% lumber per hour
  2296. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '" . $UP31 . "', u32 = u32 + '" . $UP32 . "', u34 = u34 + '" . $UP34 . "', u37 = u37 + '" . $UP37 . "', u40 = u40 + '" . $UP40 . "' WHERE vref = '" . $wid . "'";
  2297. $result = mysql_query($q, $this->connection);
  2298. break;
  2299. case 10:
  2300. case 11:
  2301. // Oasis Random populate
  2302. $UP31 = rand(5, 40);
  2303. $UP33 = rand(5, 30);
  2304. $UP37 = rand(1, 25);
  2305. $UP39 = rand(0, 25);
  2306. //+25% lumber per hour
  2307. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '" . $UP31 . "', u33 = u33 + '" . $UP33 . "', u37 = u37 + '" . $UP37 . "', u39 = u39 + '" . $UP39 . "' WHERE vref = '" . $wid . "'";
  2308. $result = mysql_query($q, $this->connection);
  2309. break;
  2310. case 12:
  2311. // Oasis Random populate
  2312. $UP31 = rand(5, 40);
  2313. $UP33 = rand(5, 30);
  2314. $UP38 = rand(1, 25);
  2315. $UP39 = rand(0, 25);
  2316. $fil = rand(0,20);
  2317. if($fil == 1){
  2318. $UP40 = rand(0, 31);
  2319. }else{
  2320. $UP40 = 0;
  2321. }
  2322. //+25% lumber per hour
  2323. $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '" . $UP31 . "', u33 = u33 + '" . $UP33 . "', u38 = u38 + '" . $UP38 . "', u39 = u39 + '" . $UP39 . "', u40 = u40 + '" . $UP40 . "' WHERE vref = '" . $wid . "'";
  2324. $result = mysql_query($q, $this->connection);
  2325. break;
  2326. }
  2327. }
  2328. }
  2329. public function hasBeginnerProtection($vid) {
  2330. $q = "SELECT u.protect FROM ".TB_PREFIX."users u,".TB_PREFIX."vdata v WHERE u.id=v.owner AND v.wref=".$vid;
  2331. $result = mysql_query($q, $this->connection);
  2332. $dbarray = mysql_fetch_array($result);
  2333. if(!empty($dbarray)) {
  2334. if(time()<$dbarray[0]) {
  2335. return true;
  2336. } else {
  2337. return false;
  2338. }
  2339. } else {
  2340. return false;
  2341. }
  2342. }
  2343. function addCLP($uid, $clp) {
  2344. $q = "UPDATE " . TB_PREFIX . "users set clp = clp + $clp where id = $uid";
  2345. return mysql_query($q, $this->connection);
  2346. }
  2347. function sendwlcMessage($client, $owner, $topic, $message, $send) {
  2348. $time = time();
  2349. $q = "INSERT INTO " . TB_PREFIX . "mdata values (0,$client,$owner,'$topic',\"$message\",1,0,$send,$time)";
  2350. return mysql_query($q, $this->connection);
  2351. }
  2352. function getLinks($id){
  2353. $q = 'SELECT * FROM ' . TB_PREFIX . 'links WHERE `userid` = ' . $id . ' ORDER BY `pos` ASC';
  2354. $result = mysql_query($q, $this->connection);
  2355. return $this->mysql_fetch_all($result);
  2356. }
  2357. function getFarmlist($uid){
  2358. $q = 'SELECT * FROM ' . TB_PREFIX . 'farmlist WHERE owner = ' . $uid . ' ORDER BY name ASC';
  2359. $result = mysql_query($q, $this->connection);
  2360. $dbarray = mysql_fetch_array($result);
  2361. if($dbarray['id']!=0) {
  2362. return true;
  2363. } else {
  2364. return false;
  2365. }
  2366. }
  2367. function getRaidList($id) {
  2368. $q = "SELECT * FROM " . TB_PREFIX . "raidlist WHERE id = ".$id."";
  2369. $result = mysql_query($q, $this->connection);
  2370. return mysql_fetch_array($result);
  2371. }
  2372. function getAllAuction() {
  2373. $q = "SELECT * FROM " . TB_PREFIX . "auction WHERE finish = 0";
  2374. $result = mysql_query($q, $this->connection);
  2375. return mysql_fetch_array($result);
  2376. }
  2377. function getVilFarmlist($wref){
  2378. $q = 'SELECT * FROM ' . TB_PREFIX . 'farmlist WHERE wref = ' . $wref . ' ORDER BY wref ASC';
  2379. $result = mysql_query($q, $this->connection);
  2380. $dbarray = mysql_fetch_array($result);
  2381. if($dbarray['id']!=0) {
  2382. return true;
  2383. } else {
  2384. return false;
  2385. }
  2386. }
  2387. function delFarmList($id, $owner) {
  2388. $q = "DELETE FROM " . TB_PREFIX . "farmlist where id = $id and owner = $owner";
  2389. return mysql_query($q, $this->connection);
  2390. }
  2391. function delSlotFarm($id) {
  2392. $q = "DELETE FROM " . TB_PREFIX . "raidlist where id = $id";
  2393. return mysql_query($q, $this->connection);
  2394. }
  2395. function createFarmList($wref, $owner, $name) {
  2396. $q = "INSERT INTO " . TB_PREFIX . "farmlist (`wref`, `owner`, `name`) VALUES ('$wref', '$owner', '$name')";
  2397. return mysql_query($q, $this->connection);
  2398. }
  2399. function addSlotFarm($lid, $towref, $x, $y, $distance, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) {
  2400. $q = "INSERT INTO " . TB_PREFIX . "raidlist (`lid`, `towref`, `x`, `y`, `distance`, `t1`, `t2`, `t3`, `t4`, `t5`, `t6`, `t7`, `t8`, `t9`, `t10`) VALUES ('$lid', '$towref', '$x', '$y', '$distance', '$t1', '$t2', '$t3', '$t4', '$t5', '$t6', '$t7', '$t8', '$t9', '$t10')";
  2401. return mysql_query($q, $this->connection);
  2402. }
  2403. function editSlotFarm($eid, $lid, $wref, $x, $y, $dist, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) {
  2404. $q = "UPDATE " . TB_PREFIX . "raidlist set lid = '$lid', towref = '$wref', x = '$x', y = '$y', t1 = '$t1', t2 = '$t2', t3 = '$t3', t4 = '$t4', t5 = '$t5', t6 = '$t6', t7 = '$t7', t8 = '$t8', t9 = '$t9', t10 = '$t10' WHERE id = $eid";
  2405. return mysql_query($q, $this->connection);
  2406. }
  2407. function getBerichte($uid) {
  2408. $q = "SELECT id FROM " . TB_PREFIX . "ndata where uid = $uid";
  2409. $result = mysql_query($q, $this->connection);
  2410. $dbarray = mysql_fetch_array($result);
  2411. return $dbarray['id'];
  2412. }
  2413. function removeOases($wref) {
  2414. $q = "UPDATE ".TB_PREFIX."odata SET conqured = 0, owner = 3, name = '????? ????? ????' WHERE wref = $wref";
  2415. return mysql_query($q, $this->connection);
  2416. }
  2417. function getArrayMemberVillage($uid){
  2418. $q = 'SELECT a.wref, a.name, b.x, b.y from '.TB_PREFIX.'vdata AS a left join '.TB_PREFIX.'wdata AS b ON b.id = a.wref where owner = '.$uid.' order by capital DESC,pop DESC';
  2419. $result = mysql_query($q, $this->connection);
  2420. $array = $this->mysql_fetch_all($result);
  2421. return $array;
  2422. }
  2423. function getNoticeData($nid) {
  2424. $q = "SELECT * FROM " . TB_PREFIX . "ndata where id = $nid";
  2425. $result = mysql_query($q, $this->connection);
  2426. $dbarray = mysql_fetch_array($result);
  2427. return $dbarray['data'];
  2428. }
  2429. function setSilver($uid, $silver, $mode) {
  2430. if(!$mode){
  2431. $q = "UPDATE " . TB_PREFIX . "users set silver = silver - $silver where id = $uid";
  2432. }else{
  2433. $q = "UPDATE " . TB_PREFIX . "users set silver = silver + $silver where id = $uid";
  2434. }
  2435. return mysql_query($q, $this->connection);
  2436. }
  2437. function getAuctionSilver($uid) {
  2438. $q = "SELECT * FROM " . TB_PREFIX . "auction where uid = $uid and finish = 0";
  2439. $result = mysql_query($q, $this->connection);
  2440. return mysql_fetch_array($result);
  2441. }
  2442. function getAuctionData($id) {
  2443. $q = "SELECT * FROM " . TB_PREFIX . "auction where id = $id";
  2444. $result = mysql_query($q, $this->connection);
  2445. return mysql_fetch_array($result);
  2446. }
  2447. function delAuction($id) {
  2448. $aucData = $this->getAuctionData($id);
  2449. $btype = $aucData['btype'];
  2450. if($btype>=7 || $btype!=12 || $btype!=13){
  2451. $this->editHeroNum($aucData['itemid'], $aucData['num'], 1);
  2452. $this->editProcItem($aucData['itemid'], 0);
  2453. $q = "DELETE FROM " . TB_PREFIX . "auction where id = $id and finish = 0";
  2454. }else{
  2455. $this->editProcItem($aucData['itemid'], 0);
  2456. $q = "DELETE FROM " . TB_PREFIX . "auction where id = $id and finish = 0";
  2457. }
  2458. return mysql_query($q, $this->connection);
  2459. }
  2460. function getAuctionUser($uid) {
  2461. $q = "SELECT * FROM " . TB_PREFIX . "auction where owner = $uid";
  2462. $result = mysql_query($q, $this->connection);
  2463. return mysql_fetch_array($result);
  2464. }
  2465. function addAuction($owner, $itemid, $btype, $type, $amount) {
  2466. $time = time()+AUCTIONTIME;
  2467. if($btype==7 || $btype==8 || $btype==9 || $btype==10 || $btype==11 || $btype==13 || $btype==14){
  2468. $silver = $amount;
  2469. $itemData = $this->getItemData($itemid);
  2470. if($amount == $itemData['num']){
  2471. $q = "INSERT INTO " . TB_PREFIX . "auction (`owner`, `itemid`, `btype`, `type`, `num`, `uid`, `bids`, `silver`, `time`, `finish`) VALUES ('$owner', '$itemid', '$btype', '$type', '$amount', 0, 0, '$silver', '$time', 0)";
  2472. $this->editProcItem($itemid, 1);
  2473. }else{
  2474. $this->editHeroNum($itemid, $amount, 0);
  2475. $q = "INSERT INTO " . TB_PREFIX . "auction (`owner`, `itemid`, `btype`, `type`, `num`, `uid`, `bids`, `silver`, `time`, `finish`) VALUES ('$owner', '$itemid', '$btype', '$type', '$amount', 0, 0, '$silver', '$time', 0)";
  2476. $this->editProcItem($itemid, 0);
  2477. }
  2478. }else{
  2479. $silver = 100;
  2480. $q = "INSERT INTO " . TB_PREFIX . "auction (`owner`, `itemid`, `btype`, `type`, `num`, `uid`, `bids`, `silver`, `time`, `finish`) VALUES ('$owner', '$itemid', '$btype', '$type', '$amount', 0, 0, '$silver', '$time', 0)";
  2481. $this->editProcItem($itemid, 1);
  2482. }
  2483. return mysql_query($q, $this->connection);
  2484. }
  2485. function addBid($id, $uid, $silver) {
  2486. $q = "UPDATE " . TB_PREFIX . "auction set uid = $uid, silver = $silver, bids = bids + 1 where id = $id";
  2487. return mysql_query($q, $this->connection);
  2488. }
  2489. function removeBidNotice($id) {
  2490. $q = "DELETE FROM " . TB_PREFIX . "auction where id = $id";
  2491. return mysql_query($q, $this->connection);
  2492. }
  2493. function addHeroItem($uid, $btype, $type, $num) {
  2494. $q = "INSERT INTO " . TB_PREFIX . "heroitems (`uid`, `btype`, `type`, `num`, `proc`) VALUES ('$uid', '$btype', '$type', '$num', 0)";
  2495. return mysql_query($q, $this->connection);
  2496. }
  2497. function checkHeroItem($uid, $btype){
  2498. $q = "SELECT * FROM ".TB_PREFIX."heroitems WHERE uid = '$uid' and btype = '$btype' and proc = 0";
  2499. $result = mysql_query($q, $this->connection);
  2500. $dbarray = mysql_fetch_array($result);
  2501. if($dbarray['btype']==$btype) {
  2502. return $dbarray['id'];
  2503. } else {
  2504. return false;
  2505. }
  2506. }
  2507. function checkAttack($wref, $toWref){
  2508. $q = "SELECT * FROM ".TB_PREFIX."movement WHERE `from` = '$wref' AND `to` = '$toWref' AND `proc` = '0' AND `sort_type` = '3'";
  2509. $result = mysql_query($q, $this->connection);
  2510. if(mysql_num_rows($result)) {
  2511. return mysql_fetch_array($result);
  2512. } else {
  2513. return false;
  2514. }
  2515. }
  2516. function getHeroItemID($uid, $btype) {
  2517. $q = "SELECT * FROM " . TB_PREFIX . "heroitems where uid = ".$uid." AND btype = ".$btype."";
  2518. $result = mysql_query($q, $this->connection);
  2519. $dbarray = mysql_fetch_array($result);
  2520. return $dbarray['id'];
  2521. }
  2522. function getHeroItemID2($uid, $btype, $type) {
  2523. $q = "SELECT * FROM " . TB_PREFIX . "heroitems where uid = ".$uid." AND btype = ".$btype." AND type = ".$type."";
  2524. $result = mysql_query($q, $this->connection);
  2525. $dbarray = mysql_fetch_array($result);
  2526. return $dbarray['id'];
  2527. }
  2528. function getItemData($id) {
  2529. $q = "SELECT * FROM " . TB_PREFIX . "heroitems WHERE id = $id";
  2530. $result = mysql_query($q, $this->connection);
  2531. return mysql_fetch_array($result);
  2532. }
  2533. function editHeroNum($id, $num, $mode) {
  2534. if($mode==0){
  2535. $q = "UPDATE " . TB_PREFIX . "heroitems set num = num - $num where id = $id and proc = 0";
  2536. }elseif($mode==1){
  2537. $q = "UPDATE " . TB_PREFIX . "heroitems set num = num + $num where id = $id and proc = 0";
  2538. }else{
  2539. $q = "UPDATE " . TB_PREFIX . "heroitems set num = $num where id = $id and proc = 0";
  2540. }
  2541. return mysql_query($q, $this->connection);
  2542. }
  2543. function editProcItem($id, $mode) {
  2544. if($mode==0){
  2545. $q = "UPDATE " . TB_PREFIX . "heroitems set proc = 0 where id = $id";
  2546. }else{
  2547. $q = "UPDATE " . TB_PREFIX . "heroitems set proc = 1 where id = $id";
  2548. }
  2549. return mysql_query($q, $this->connection);
  2550. }
  2551. function editBid($id, $silver) {
  2552. $q = "UPDATE " . TB_PREFIX . "auction set silver = $silver where id = $id";
  2553. return mysql_query($q, $this->connection);
  2554. }
  2555. function checkBid($id, $silver){
  2556. $q = "SELECT * FROM " . TB_PREFIX . "auction WHERE id = '$id'";
  2557. $result = mysql_query($q, $this->connection);
  2558. $dbarray = mysql_fetch_array($result);
  2559. if($dbarray['silver']>=$silver) {
  2560. return false;
  2561. } else {
  2562. return true;
  2563. }
  2564. }
  2565. function getBidData($id) {
  2566. $q = "SELECT * FROM " . TB_PREFIX . "auction WHERE id = $id";
  2567. $result = mysql_query($q, $this->connection);
  2568. return mysql_fetch_array($result);
  2569. }
  2570. function setHeroInventory($uid, $field, $value) {
  2571. $q = "UPDATE " . TB_PREFIX . "heroinventory set $field = '$value' where uid = $uid";
  2572. return mysql_query($q, $this->connection);
  2573. }
  2574. function getHeroInventory($uid) {
  2575. $q = "SELECT * FROM " . TB_PREFIX . "heroinventory WHERE uid = $uid";
  2576. $result = mysql_query($q, $this->connection);
  2577. return mysql_fetch_array($result);
  2578. }
  2579. function getHeroData($uid) {
  2580. $q = "SELECT * FROM " . TB_PREFIX . "hero WHERE uid = $uid";
  2581. $result = mysql_query($q, $this->connection);
  2582. return mysql_fetch_array($result);
  2583. }
  2584. function getFLData($id) {
  2585. $q = "SELECT * FROM " . TB_PREFIX . "farmlist where id = $id";
  2586. $result = mysql_query($q, $this->connection);
  2587. return mysql_fetch_array($result);
  2588. }
  2589. function getHeroField($uid, $field) {
  2590. $q = "SELECT ".$field." FROM " . TB_PREFIX . "hero WHERE uid = $uid";
  2591. $result = mysql_query($q, $this->connection);
  2592. $dbarray = mysql_fetch_array($result);
  2593. return $dbarray[$field];
  2594. }
  2595. function getVFH($uid) {
  2596. $q = "SELECT wref FROM " . TB_PREFIX . "vdata WHERE owner = $uid and capital = 1";
  2597. $result = mysql_query($q, $this->connection);
  2598. $dbarray = mysql_fetch_array($result);
  2599. return $dbarray['wref'];
  2600. }
  2601. function getNotice2($id, $field) {
  2602. $q = "SELECT ".$field." FROM " . TB_PREFIX . "ndata where `id` = '$id'";
  2603. $result = mysql_query($q, $this->connection);
  2604. $dbarray = mysql_fetch_array($result);
  2605. return $dbarray[$field];
  2606. }
  2607. function addAdventure($wref, $uid){
  2608. $time = time()+(3600*120);
  2609. $ddd = rand(0,3);
  2610. if($ddd == 1){ $dif = 1; }else{ $dif = 0; }
  2611. $sql = mysql_query("SELECT * FROM ".TB_PREFIX."wdata ORDER BY id DESC LIMIT 1");
  2612. $query = mysql_fetch_array($sql, $this->connection);
  2613. $lastw = 641601;
  2614. if(($wref-10000)<=10){
  2615. $w1 = rand(10,($wref+10000));
  2616. }
  2617. elseif(($wref+10000) >= $lastw){
  2618. $w1 = rand(($wref-10000),($lastw-1000));
  2619. }
  2620. else{
  2621. $w1 = rand(($wref-10000),($wref+10000));
  2622. }
  2623. $q = "INSERT into " . TB_PREFIX . "adventure (`wref`, `uid`, `dif`, `time`, `end`) values ('$w1', '$uid', '$dif', '$time', 0)";
  2624. return mysql_query($q, $this->connection) or die(mysql_error());
  2625. }
  2626. function addHero($uid){
  2627. $time = time();
  2628. $hash = md5($time);
  2629. $q = "INSERT into " . TB_PREFIX . "hero (`uid`, `wref`, `level`, `speed`, `points`, `experience`, `dead`, `health`, `power`, `offBonus`, `defBonus`, `product`, `r0`, `autoregen`, `lastupdate`, `lastadv`, `hash`) values
  2630. ('$uid', 0, 0, '10', 0, '2', 0, '100', '100', 0, 0, '120', '30', '10', '$time', '$time', '$hash')";
  2631. return mysql_query($q, $this->connection) or die(mysql_error());
  2632. }
  2633. // Add new password => mode:0
  2634. // Add new email => mode: 1
  2635. function addNewProc($uid, $npw, $nemail, $act, $mode) {
  2636. $time = time();
  2637. if(!$mode){
  2638. $q = "INSERT into " . TB_PREFIX . "newproc (uid, npw, act, time, proc) values ('$uid', '$npw', '$act', '$time', 0)";
  2639. }else{
  2640. $q = "INSERT into " . TB_PREFIX . "newproc (uid, nemail, act, time, proc) values ('$uid', '$nemail', '$act', '$time', 0)";
  2641. }
  2642. return mysql_query($q, $this->connection) or die(mysql_error());
  2643. }
  2644. function checkProcExist($uid) {
  2645. $q = "SELECT * FROM " . TB_PREFIX . "newproc where uid = '$uid' and proc = 0";
  2646. $result = mysql_query($q, $this->connection);
  2647. if(mysql_num_rows($result)) {
  2648. return false;
  2649. } else {
  2650. return true;
  2651. }
  2652. }
  2653. function removeProc($uid) {
  2654. $q = "DELETE FROM " . TB_PREFIX . "newproc where uid = $uid";
  2655. return mysql_query($q, $this->connection);
  2656. }
  2657. function checkBan($uid){
  2658. $q = "SELECT * FROM " . TB_PREFIX . "banlist WHERE uid = $uid";
  2659. $result = mysql_query($q, $this->connection);
  2660. if(mysql_num_rows($result)) {
  2661. return true;
  2662. }else{
  2663. return false;
  2664. }
  2665. }
  2666. function getNewProc($uid) {
  2667. $q = "SELECT * FROM " . TB_PREFIX . "newproc WHERE uid = $uid";
  2668. $result = mysql_query($q, $this->connection);
  2669. if(mysql_num_rows($result)) {
  2670. return mysql_fetch_array($result);
  2671. } else {
  2672. return false;
  2673. }
  2674. }
  2675. function getAdventure($uid, $wref) {
  2676. $q = "SELECT * FROM " . TB_PREFIX . "adventure WHERE uid = $uid and wref = '".$wref."'";
  2677. $result = mysql_query($q, $this->connection);
  2678. if(mysql_num_rows($result)) {
  2679. return mysql_fetch_array($result);
  2680. } else {
  2681. return false;
  2682. }
  2683. }
  2684. function editTableField($table, $field, $value, $refField, $ref) {
  2685. $q = "UPDATE " . TB_PREFIX . "".$table." set $field = '$value' where ".$refField." = '$ref'";
  2686. return mysql_query($q, $this->connection);
  2687. }
  2688. function HeroItemsNum($uid) {
  2689. $q = "SELECT * FROM " . TB_PREFIX . "heroitems where uid = '$uid'";
  2690. $result = mysql_query($q, $this->connection);
  2691. return mysql_num_rows($result);
  2692. }
  2693. function addHeroinventory($uid){
  2694. $q = "INSERT into " . TB_PREFIX . "heroinventory (`uid`) values ('$uid')";
  2695. return mysql_query($q, $this->connection) or die(mysql_error());
  2696. }
  2697. function config() {
  2698. $q = "SELECT * FROM " . TB_PREFIX . "config";
  2699. $result = mysql_query($q, $this->connection);
  2700. return mysql_fetch_array($result);
  2701. }
  2702. function getAllianceDipProfile($aid, $type){
  2703. $q = "SELECT * FROM ".TB_PREFIX."diplomacy WHERE alli1 = '$aid' AND type = '$type' AND accepted = '1'";
  2704. $result = mysql_query($q, $this->connection);
  2705. if(mysql_num_rows($result) == 0){
  2706. $q2 = "SELECT * FROM ".TB_PREFIX."diplomacy WHERE alli2 = '$aid' AND type = '$type' AND accepted = '1'";
  2707. $result2 = mysql_query($q2, $this->connection);
  2708. while($row = mysql_fetch_array($result2)){
  2709. $alliance = $this->getAlliance($row['alli1']);
  2710. $text = "";
  2711. $text .= "<a href=allianz.php?aid=".$alliance['id'].">".$alliance['tag']."</a><br> ";
  2712. }
  2713. }else{
  2714. while($row = mysql_fetch_array($result)){
  2715. $alliance = $this->getAlliance($row['alli2']);
  2716. $text = "";
  2717. $text .= "<a href=allianz.php?aid=".$alliance['id'].">".$alliance['tag']."</a><br> ";
  2718. }
  2719. }
  2720. if(strlen($text) == 0){
  2721. $text = "-<br>";
  2722. }
  2723. return $text;
  2724. }
  2725. public function canClaimArtifact ($vref,$type) {
  2726. $DefenderFields = $this->getResourceLevel($vref);
  2727. for($i=19;$i<=38;$i++) {
  2728. if($AttackerFields['f'.$i.'t'] == 27) {
  2729. $defcanclaim = FALSE;
  2730. $defTresuaryLevel = $AttackerFields['f'.$i];
  2731. } else {
  2732. $defcanclaim = TRUE;
  2733. }
  2734. }
  2735. $AttackerFields = $this->getResourceLevel($vref);
  2736. for($i=19;$i<=38;$i++) {
  2737. if($AttackerFields['f'.$i.'t'] == 27) {
  2738. $attTresuaryLevel = $AttackerFields['f'.$i];
  2739. if ($attTresuaryLevel >= 10){
  2740. $villageartifact = TRUE;
  2741. }else{
  2742. $villageartifact = FALSE;
  2743. }
  2744. if ($attTresuaryLevel == 20){
  2745. $accountartifact = TRUE;
  2746. }else{
  2747. $accountartifact = FALSE;
  2748. }
  2749. }
  2750. }
  2751. if ($type == 1) {
  2752. if ($defcanclaim == TRUE && $villageartifact == TRUE){
  2753. return TRUE;
  2754. }
  2755. }else if($type == 2) {
  2756. if ($defcanclaim == TRUE && $accountartifact == TRUE){
  2757. return TRUE;
  2758. }
  2759. }else if($type == 3) {
  2760. if ($defcanclaim == TRUE && $accountartifact == TRUE){
  2761. return TRUE;
  2762. }
  2763. }else { return FALSE; }
  2764. }
  2765. function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
  2766. if(!isset($pct)){
  2767. return false;
  2768. }
  2769. $pct /= 100;
  2770. // Get image width and height
  2771. $w = imagesx( $src_im );
  2772. $h = imagesy( $src_im );
  2773. // Turn alpha blending off
  2774. imagealphablending( $src_im, false );
  2775. // Find the most opaque pixel in the image (the one with the smallest alpha value)
  2776. $minalpha = 127;
  2777. for( $x = 0; $x < $w; $x++ )
  2778. for( $y = 0; $y < $h; $y++ ){
  2779. $alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF;
  2780. if( $alpha < $minalpha ){
  2781. $minalpha = $alpha;
  2782. }
  2783. }
  2784. //loop through image pixels and modify alpha for each
  2785. for( $x = 0; $x < $w; $x++ ){
  2786. for( $y = 0; $y < $h; $y++ ){
  2787. //get current alpha value (represents the TANSPARENCY!)
  2788. $colorxy = imagecolorat( $src_im, $x, $y );
  2789. $alpha = ( $colorxy >> 24 ) & 0xFF;
  2790. //calculate new alpha
  2791. if( $minalpha !== 127 ){
  2792. $alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha );
  2793. } else {
  2794. $alpha += 127 * $pct;
  2795. }
  2796. //get the color index with new alpha
  2797. $alphacolorxy = imagecolorallocatealpha( $src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
  2798. //set pixel with the new color + opacity
  2799. if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){
  2800. return false;
  2801. }
  2802. }
  2803. }
  2804. // The image copy
  2805. imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  2806. }
  2807. }
  2808. ;
  2809. $database = new MYSQL_DB;
  2810. ?>