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

/GameEngine/Admin/database.php

https://github.com/DEVVELO/TravianZ-By-Shadowss
PHP | 858 lines | 747 code | 76 blank | 35 comment | 180 complexity | 6aa0844b14cd4118b8af39cd6d56ed29 MD5 | raw file
  1. <?php
  2. #################################################################################
  3. ## ##
  4. ## -= YOU MUST NOT REMOVE OR CHANGE THIS NOTICE =- ##
  5. ## ##
  6. ## --------------------------------------------------------------------------- ##
  7. ## ##
  8. ## Project: TravianZ ##
  9. ## Version: 05.03.2014 ##
  10. ## Filename: GameEngine/Admin/database.php ##
  11. ## Developed by: Dzoki ##
  12. ## Edited by: Shadow and ronix ##
  13. ## License: Creative Commons BY-NC-SA 3.0 ##
  14. ## Copyright: TravianZ (c) 2014 - All rights reserved ##
  15. ## URLs: http://travian.shadowss/ro ##
  16. ## Source code: https://github.com/Shadowss/TravianZ ##
  17. ## ##
  18. #################################################################################
  19. if(isset($gameinstall) && $gameinstall == 1){
  20. include_once("../../GameEngine/config.php");
  21. include_once("../../GameEngine/Data/buidata.php");
  22. }else{
  23. include_once("../GameEngine/Data/unitdata.php");
  24. include_once("../GameEngine/Technology.php");
  25. include_once("../GameEngine/Data/buidata.php");
  26. }
  27. class adm_DB {
  28. var $connection;
  29. function adm_DB(){
  30. global $database;
  31. $this->connection = mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS) or die(mysql_error());
  32. mysql_select_db(SQL_DB, $this->connection) or die(mysql_error());
  33. }
  34. function Login($username,$password){
  35. $q = "SELECT password FROM ".TB_PREFIX."users where username = '$username' and access >= ".MULTIHUNTER;
  36. $result = mysql_query($q, $this->connection);
  37. $dbarray = mysql_fetch_array($result);
  38. if($dbarray['password'] == md5($password)) {
  39. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,'X','$username logged in (IP: <b>".$_SERVER['REMOTE_ADDR']."</b>)',".time().")");
  40. return true;
  41. }
  42. else {
  43. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,'X','<font color=\'red\'><b>IP: ".$_SERVER['REMOTE_ADDR']." tried to log in with username <u> $username</u> but access was denied!</font></b>',".time().")");
  44. return false;
  45. }
  46. }
  47. function recountPopUser($uid){
  48. global $database;
  49. $villages = $database->getProfileVillages($uid);
  50. for ($i = 0; $i <= count($villages)-1; $i++) {
  51. $vid = $villages[$i]['wref'];
  52. $this->recountPop($vid);
  53. $this->recountCP($vid);
  54. }
  55. }
  56. function recountPop($vid){
  57. global $database;
  58. $fdata = $database->getResourceLevel($vid);
  59. $popTot = 0;
  60. for ($i = 1; $i <= 40; $i++) {
  61. $lvl = $fdata["f".$i];
  62. $building = $fdata["f".$i."t"];
  63. if($building>0 && $lvl>0){
  64. $popTot += $this->buildingPOP($building,$lvl);
  65. }
  66. }
  67. $q = "UPDATE ".TB_PREFIX."vdata set pop = $popTot where wref = $vid";
  68. mysql_query($q, $this->connection);
  69. }
  70. function recountCP($vid){
  71. global $database;
  72. $fdata = $database->getResourceLevel($vid);
  73. $popTot = 0;
  74. for ($i = 1; $i <= 40; $i++) {
  75. $lvl = $fdata["f".$i];
  76. $building = $fdata["f".$i."t"];
  77. if($building>0 && $lvl>0){
  78. $popTot += $this->buildingCP($building,$lvl);
  79. }
  80. }
  81. $q = "UPDATE ".TB_PREFIX."vdata set cp = $popTot where wref = $vid";
  82. mysql_query($q, $this->connection);
  83. }
  84. function buildingPOP($f,$lvl){
  85. $name = "bid".$f;
  86. global $$name;
  87. $popT = 0;
  88. $dataarray = $$name;
  89. for ($i = 1; $i <= $lvl; $i++) {
  90. $popT += $dataarray[$i]['pop'];
  91. }
  92. return $popT;
  93. }
  94. function buildingCP($f,$lvl){
  95. $name = "bid".$f;
  96. global $$name;
  97. $popT = 0;
  98. $dataarray = $$name;
  99. for ($i = 1; $i <= $lvl; $i++) {
  100. $popT += $dataarray[$i]['cp'];
  101. }
  102. return $popT;
  103. }
  104. function getWref($x,$y) {
  105. $q = "SELECT id FROM ".TB_PREFIX."wdata where x = $x and y = $y";
  106. $result = mysql_query($q, $this->connection);
  107. $r = mysql_fetch_array($result);
  108. return $r['id'];
  109. }
  110. function AddVillage($post){
  111. global $database;
  112. $wid = $this->getWref($post['x'],$post['y']);
  113. $uid = $post['uid'];
  114. $status = $database->getVillageState($wid);
  115. $status = 0;
  116. if($status == 0){
  117. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Added new village <b><a href=\'admin.php?p=village&did=$wid\'>$wid</a></b> to user <b><a href=\'admin.php?p=player&uid=$uid\'>$uid</a></b>',".time().")");
  118. $database->setFieldTaken($wid);
  119. $database->addVillage($wid,$uid,'new village','0');
  120. $database->addResourceFields($wid,$database->getVillageType($wid));
  121. $database->addUnits($wid);
  122. $database->addTech($wid);
  123. $database->addABTech($wid);
  124. }
  125. }
  126. function Punish($post){
  127. global $database;
  128. $villages = $database->getProfileVillages($post['uid']);
  129. $admid = $post['admid'];
  130. $user = $database->getUserArray($post['uid'],1);
  131. for ($i = 0; $i <= count($villages)-1; $i++) {
  132. $vid = $villages[$i]['wref'];
  133. if($post['punish']){
  134. $popOld = $villages[$i]['pop'];
  135. $proc = 100-$post['punish'];
  136. $pop = floor(($popOld/100)*($proc));
  137. if($pop <= 1 ){$pop = 2;}
  138. $this->PunishBuilding($vid,$proc,$pop);
  139. }
  140. if($post['del_troop']){
  141. if($user['tribe'] == 1) {
  142. $unit = 1;
  143. }else if($user['tribe'] == 2) {
  144. $unit = 11;
  145. }else if($user['tribe'] == 3) {
  146. $unit = 21;
  147. }
  148. $this->DelUnits($villages[$i]['wref'],$unit);
  149. }
  150. if($post['clean_ware']){
  151. $time = time();
  152. $q = "UPDATE ".TB_PREFIX."vdata SET `wood` = '0', `clay` = '0', `iron` = '0', `crop` = '0', `lastupdate` = '$time' WHERE wref = $vid;";
  153. mysql_query($q, $this->connection);
  154. }
  155. }
  156. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Punished user: <a href=\'admin.php?p=player&uid=".$post['uid']."\'>".$post['uid']."</a> with <b>-".$post['punish']."%</b> population',".time().")");
  157. }
  158. function PunishBuilding($vid,$proc,$pop){
  159. global $database;
  160. $q = "UPDATE ".TB_PREFIX."vdata set pop = $pop where wref = $vid;";
  161. mysql_query($q, $this->connection);
  162. $fdata = $database->getResourceLevel($vid);
  163. for ($i = 1; $i <= 40; $i++) {
  164. if($fdata['f'.$i]>1){
  165. $zm = ($fdata['f'.$i]/100)*$proc;
  166. if($zm < 1){$zm = 1;}else{$zm = floor($zm);}
  167. $q = "UPDATE ".TB_PREFIX."fdata SET `f$i` = '$zm' WHERE `vref` = $vid;";
  168. mysql_query($q, $this->connection);
  169. }
  170. }
  171. }
  172. function DelUnits($vid,$unit){
  173. for ($i = $unit; $i <= 9+$unit; $i++) {
  174. $this->DelUnits2($vid,$unit);
  175. }
  176. }
  177. function DelUnits2($vid,$unit){
  178. $q = "UPDATE ".TB_PREFIX."units SET `u$unit` = '0' WHERE `vref` = $vid;";
  179. mysql_query($q, $this->connection);
  180. }
  181. function DelPlayer($uid,$pass){
  182. global $database;
  183. $ID = $_SESSION['id'];//$database->getUserField($_SESSION['username'],'id',1);
  184. if($this->CheckPass($pass,$ID)){
  185. $villages = $database->getProfileVillages($uid);
  186. for ($i = 0; $i <= count($villages)-1; $i++) {
  187. $this->DelVillage($villages[$i]['wref'], 1);
  188. }
  189. $q = "DELETE FROM ".TB_PREFIX."hero where uid = $uid";
  190. mysql_query($q, $this->connection);
  191. $name = $database->getUserField($uid,"username",0);
  192. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,$ID,'Deleted user <a>$name</a>',".time().")");
  193. $q = "DELETE FROM ".TB_PREFIX."users WHERE `id` = $uid;";
  194. mysql_query($q, $this->connection);
  195. }
  196. }
  197. function getUserActive() {
  198. $time = time() - (60*5);
  199. $q = "SELECT * FROM ".TB_PREFIX."users where timestamp > $time and username != 'support'";
  200. $result = mysql_query($q, $this->connection);
  201. return $this->mysql_fetch_all($result);
  202. }
  203. function CheckPass($password,$uid){
  204. $q = "SELECT password FROM ".TB_PREFIX."users where id = '$uid' and access = ".ADMIN;
  205. $result = mysql_query($q, $this->connection);
  206. $dbarray = mysql_fetch_array($result);
  207. if($dbarray['password'] == md5($password)) {
  208. return true;
  209. }else{
  210. return false;
  211. }
  212. }
  213. function DelVillage($wref, $mode=0){
  214. global $database;
  215. if($mode==0){
  216. $q = "SELECT * FROM ".TB_PREFIX."vdata WHERE `wref` = $wref and capital = 0";
  217. }else{
  218. $q = "SELECT * FROM ".TB_PREFIX."vdata WHERE `wref` = $wref";
  219. }
  220. $result = mysql_query($q, $this->connection);
  221. if(mysql_num_rows($result) > 0){
  222. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Deleted village <b>$wref</b>',".time().")");
  223. $database->clearExpansionSlot($wref);
  224. $q = "DELETE FROM ".TB_PREFIX."abdata where vref = $wref";
  225. mysql_query($q, $this->connection);
  226. $q = "DELETE FROM ".TB_PREFIX."bdata where wid = $wref";
  227. mysql_query($q, $this->connection);
  228. $q = "DELETE FROM ".TB_PREFIX."market where vref = $wref";
  229. mysql_query($q, $this->connection);
  230. $q = "DELETE FROM ".TB_PREFIX."odata where wref = $wref";
  231. mysql_query($q, $this->connection);
  232. $q = "DELETE FROM ".TB_PREFIX."research where vref = $wref";
  233. mysql_query($q, $this->connection);
  234. $q = "DELETE FROM ".TB_PREFIX."tdata where vref = $wref";
  235. mysql_query($q, $this->connection);
  236. $q = "DELETE FROM ".TB_PREFIX."fdata where vref = $wref";
  237. mysql_query($q, $this->connection);
  238. $q = "DELETE FROM ".TB_PREFIX."training where vref = $wref";
  239. mysql_query($q, $this->connection);
  240. $q = "DELETE FROM ".TB_PREFIX."units where vref = $wref";
  241. mysql_query($q, $this->connection);
  242. $q = "DELETE FROM ".TB_PREFIX."farmlist where wref = $wref";
  243. mysql_query($q, $this->connection);
  244. $q = "DELETE FROM ".TB_PREFIX."raidlist where towref = $wref";
  245. mysql_query($q, $this->connection);
  246. $q = "DELETE FROM ".TB_PREFIX."movement where `from` = $wref and proc=0";
  247. mysql_query($q, $this->connection);
  248. $getmovement = $database->getMovement(3,$wref,1);
  249. foreach($getmovement as $movedata) {
  250. $time = microtime(true);
  251. $time2 = $time - $movedata['starttime'];
  252. $database->setMovementProc($movedata['moveid']);
  253. $database->addMovement(4,$movedata['to'],$movedata['from'],$movedata['ref'],$time,$time+$time2);
  254. //$database->setMovementProc($movedata['moveid']);
  255. }
  256. //check return enforcement from del village
  257. $this->returnTroops($wref);
  258. $q = "DELETE FROM ".TB_PREFIX."vdata WHERE `wref` = $wref";
  259. mysql_query($q, $this->connection);
  260. if (mysql_affected_rows()>0) {
  261. $q = "UPDATE ".TB_PREFIX."wdata set occupied = 0 where id = $wref";
  262. mysql_query($q, $this->connection);
  263. $getprisoners = $database->getPrisoners($wref);
  264. foreach($getprisoners as $pris) {
  265. $troops = 0;
  266. for($i=1;$i<12;$i++){
  267. $troops += $pris['t'.$i];
  268. }
  269. $database->modifyUnit($pris['wref'],array("99o"),array($troops),array(0));
  270. $database->deletePrisoners($pris['id']);
  271. }
  272. $getprisoners = $database->getPrisoners3($wref);
  273. foreach($getprisoners as $pris) {
  274. $troops = 0;
  275. for($i=1;$i<12;$i++){
  276. $troops += $pris['t'.$i];
  277. }
  278. $database->modifyUnit($pris['wref'],array("99o"),array($troops),array(0));
  279. $database->deletePrisoners($pris['id']);
  280. }
  281. }
  282. }
  283. }
  284. function DelBan($uid,$id){
  285. global $database;
  286. $name = addslashes($database->getUserField($uid,"username",0));
  287. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Unbanned user <a href=\'admin.php?p=player&uid=$uid\'>$name</a>',".time().")");
  288. $q = "UPDATE ".TB_PREFIX."users SET `access` = '".USER."' WHERE `id` = $uid;";
  289. mysql_query($q, $this->connection);
  290. $q = "UPDATE ".TB_PREFIX."banlist SET `active` = '0' WHERE `id` = $id;";
  291. mysql_query($q, $this->connection);
  292. }
  293. function AddBan($uid,$end,$reason){
  294. global $database;
  295. $name = addslashes($database->getUserField($uid,"username",0));
  296. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Banned user <a href=\'admin.php?p=player&uid=$uid\'>$name</a>',".time().")");
  297. $q = "UPDATE ".TB_PREFIX."users SET `access` = '0' WHERE `id` = $uid;";
  298. mysql_query($q, $this->connection);
  299. $time = time();
  300. $admin = $_SESSION['id']; //$database->getUserField($_SESSION['username'],'id',1);
  301. $name = addslashes($database->getUserField($uid,'username',0));
  302. $q = "INSERT INTO ".TB_PREFIX."banlist (`uid`, `name`, `reason`, `time`, `end`, `admin`, `active`) VALUES ($uid, '$name' , '$reason', '$time', '$end', '$admin', '1');";
  303. mysql_query($q, $this->connection);
  304. }
  305. function search_player($player){
  306. $q = "SELECT id,username FROM ".TB_PREFIX."users WHERE `username` LIKE '%$player%' and username != 'support'";
  307. $result = mysql_query($q, $this->connection);
  308. return $this->mysql_fetch_all($result);
  309. }
  310. function search_email($email){
  311. $q = "SELECT id,email FROM ".TB_PREFIX."users WHERE `email` LIKE '%$email%' and username != 'support'";
  312. $result = mysql_query($q, $this->connection);
  313. return $this->mysql_fetch_all($result);
  314. }
  315. function search_village($village){
  316. $q = "SELECT * FROM ".TB_PREFIX."vdata WHERE `name` LIKE '%$village%' or `wref` LIKE '%$village%'";
  317. $result = mysql_query($q, $this->connection);
  318. return $this->mysql_fetch_all($result);
  319. }
  320. function search_alliance($alliance){
  321. $q = "SELECT * FROM ".TB_PREFIX."alidata WHERE `name` LIKE '%$alliance%' or `tag` LIKE '%$alliance%' or `id` LIKE '%$alliance%'";
  322. $result = mysql_query($q, $this->connection);
  323. return $this->mysql_fetch_all($result);
  324. }
  325. function search_ip($ip){
  326. $q = "SELECT * FROM ".TB_PREFIX."login_log WHERE `ip` LIKE '%$ip%'";
  327. $result = mysql_query($q, $this->connection);
  328. return $this->mysql_fetch_all($result);
  329. }
  330. function search_banned(){
  331. $q = "SELECT * FROM ".TB_PREFIX."banlist where active = '1'";
  332. $result = mysql_query($q, $this->connection);
  333. return $this->mysql_fetch_all($result);
  334. }
  335. function Del_banned(){
  336. //$q = "SELECT * FROM ".TB_PREFIX."banlist";
  337. $result = mysql_query($q, $this->connection);
  338. return $this->mysql_fetch_all($result);
  339. }
  340. /***************************
  341. Function to process MYSQLi->fetch_all (Only exist in MYSQL)
  342. References: Result
  343. ***************************/
  344. function mysql_fetch_all($result) {
  345. $all = array();
  346. if($result) {
  347. while ($row = mysql_fetch_assoc($result)){ $all[] = $row; }
  348. return $all;
  349. }
  350. }
  351. function query_return($q) {
  352. $result = mysql_query($q, $this->connection);
  353. return $this->mysql_fetch_all($result);
  354. }
  355. /***************************
  356. Function to do free query
  357. References: Query
  358. ***************************/
  359. function query($query) {
  360. return mysql_query($query, $this->connection);
  361. }
  362. public function getTypeLevel($tid,$vid) {
  363. global $village,$database;
  364. $keyholder = array();
  365. if($vid == 0) {
  366. $resourcearray = $village->resarray;
  367. } else {
  368. $resourcearray = $database->getResourceLevel($vid);
  369. }
  370. foreach(array_keys($resourcearray,$tid) as $key) {
  371. if(strpos($key,'t')) {
  372. $key = preg_replace("/[^0-9]/", '', $key);
  373. array_push($keyholder, $key);
  374. }
  375. }
  376. $element = count($keyholder);
  377. if($element >= 2) {
  378. if($tid <= 4) {
  379. $temparray = array();
  380. for($i=0;$i<=$element-1;$i++) {
  381. array_push($temparray,$resourcearray['f'.$keyholder[$i]]);
  382. }
  383. foreach ($temparray as $key => $val) {
  384. if ($val == max($temparray))
  385. $target = $key;
  386. }
  387. }
  388. else {
  389. $target = 0;
  390. for($i=1;$i<=$element-1;$i++) {
  391. if($resourcearray['f'.$keyholder[$i]] > $resourcearray['f'.$keyholder[$target]]) {
  392. $target = $i;
  393. }
  394. }
  395. }
  396. }
  397. else if($element == 1) {
  398. $target = 0;
  399. }
  400. else {
  401. return 0;
  402. }
  403. if($keyholder[$target] != "") {
  404. return $resourcearray['f'.$keyholder[$target]];
  405. }
  406. else {
  407. return 0;
  408. }
  409. }
  410. public function procDistanceTime($coor,$thiscoor,$ref,$vid) {
  411. global $bid28,$bid14;
  412. $xdistance = ABS($thiscoor['x'] - $coor['x']);
  413. if($xdistance > WORLD_MAX) {
  414. $xdistance = (2 * WORLD_MAX + 1) - $xdistance;
  415. }
  416. $ydistance = ABS($thiscoor['y'] - $coor['y']);
  417. if($ydistance > WORLD_MAX) {
  418. $ydistance = (2 * WORLD_MAX + 1) - $ydistance;
  419. }
  420. $distance = SQRT(POW($xdistance,2)+POW($ydistance,2));
  421. $speed = $ref;
  422. if($this->getTypeLevel(14,$vid) != 0 && $distance >= TS_THRESHOLD) {
  423. $speed = $speed * ($bid14[$this->getTypeLevel(14,$vid)]['attri']/100) ;
  424. }
  425. if($speed!=0){
  426. return round(($distance/$speed) * 3600 / INCREASE_SPEED);
  427. }else{
  428. return round($distance * 3600 / INCREASE_SPEED);
  429. }
  430. }
  431. public function returnTroops($wref) {
  432. global $database;
  433. $getenforce=$database->getEnforceVillage($wref,0);
  434. //if(($enforce['from']==$village->wid) || ($enforce['vref']==$village->wid)){
  435. foreach($getenforce as $enforce) {
  436. $to = $database->getVillage($enforce['from']);
  437. $Gtribe = "";
  438. if ($database->getUserField($to['owner'],'tribe',0) == '2'){ $Gtribe = "1"; }
  439. else if ($database->getUserField($to['owner'],'tribe',0) == '3'){ $Gtribe = "2"; }
  440. else if ($database->getUserField($to['owner'],'tribe',0) == '4'){ $Gtribe = "3"; }
  441. else if ($database->getUserField($to['owner'],'tribe',0) == '5'){ $Gtribe = "4"; }
  442. $start = ($database->getUserField($to['owner'],'tribe',0)-1)*10+1;
  443. $end = ($database->getUserField($to['owner'],'tribe',0)*10);
  444. $from = $database->getVillage($enforce['from']);
  445. $fromcoor = $database->getCoor($enforce['from']);
  446. $tocoor = $database->getCoor($enforce['vref']);
  447. $fromCor = array('x'=>$tocoor['x'], 'y'=>$tocoor['y']);
  448. $toCor = array('x'=>$fromcoor['x'], 'y'=>$fromcoor['y']);
  449. $speeds = array();
  450. //find slowest unit.
  451. for($i=$start;$i<=$end;$i++){
  452. if(intval($enforce['u'.$i]) > 0){
  453. if($unitarray) { reset($unitarray); }
  454. $unitarray = $GLOBALS["u".$i];
  455. $speeds[] = $unitarray['speed'];
  456. //echo print_r(array_keys($speeds))."unitspd\n".$i."trib\n";
  457. } else {
  458. $enforce['u'.$i]='0';
  459. }
  460. }
  461. if( intval($enforce['hero']) > 0){
  462. $q = "SELECT * FROM ".TB_PREFIX."hero WHERE uid = ".$from['owner']."";
  463. $result = mysql_query($q);
  464. $hero_f=mysql_fetch_array($result);
  465. $hero_unit=$hero_f['unit'];
  466. $speeds[] = $GLOBALS['u'.$hero_unit]['speed'];
  467. } else {
  468. $enforce['hero']='0';
  469. }
  470. $artefact = count($database->getOwnUniqueArtefactInfo2($from['owner'],2,3,0));
  471. $artefact1 = count($database->getOwnUniqueArtefactInfo2($enforce['from'],2,1,1));
  472. $artefact2 = count($database->getOwnUniqueArtefactInfo2($from['owner'],2,2,0));
  473. if($artefact > 0){
  474. $fastertroops = 3;
  475. }else if($artefact1 > 0){
  476. $fastertroops = 2;
  477. }else if($artefact2 > 0){
  478. $fastertroops = 1.5;
  479. }else{
  480. $fastertroops = 1;
  481. }
  482. $time = round($this->procDistanceTime($fromCor,$toCor,min($speeds),$enforce['from'])/$fastertroops);
  483. $foolartefact2 = $database->getFoolArtefactInfo(2,$enforce['from'],$from['owner']);
  484. if(count($foolartefact2) > 0){
  485. foreach($foolartefact2 as $arte){
  486. if($arte['bad_effect'] == 1){
  487. $time *= $arte['effect2'];
  488. }else{
  489. $time /= $arte['effect2'];
  490. $time = round($time);
  491. }
  492. }
  493. }
  494. $reference = $database->addAttack($enforce['from'],$enforce['u'.$start],$enforce['u'.($start+1)],$enforce['u'.($start+2)],$enforce['u'.($start+3)],$enforce['u'.($start+4)],$enforce['u'.($start+5)],$enforce['u'.($start+6)],$enforce['u'.($start+7)],$enforce['u'.($start+8)],$enforce['u'.($start+9)],$enforce['hero'],2,0,0,0,0);
  495. $database->addMovement(4,$wref,$enforce['from'],$reference,time(),($time+time()));
  496. $database->deleteReinf($enforce['id']);
  497. }
  498. }
  499. public function calculateProduction($wid,$uid,$b1,$b2,$b3,$b4,$fdata,$ocounter,$pop) {
  500. global $technology,$database;
  501. $normalA = $database->getOwnArtefactInfoByType($wid,4);
  502. $largeA = $database->getOwnUniqueArtefactInfo($uid,4,2);
  503. $uniqueA = $database->getOwnUniqueArtefactInfo($uid,4,3);
  504. $upkeep = $this->getUpkeep($this->getAllUnits($wid),0,$wid,$uid);
  505. $production=array();
  506. $production['wood'] = $this->getWoodProd($fdata, $ocounter,$b1);
  507. $production['clay'] = $this->getClayProd($fdata, $ocounter,$b2);
  508. $production['iron'] = $this->getIronProd($fdata, $ocounter,$b3);
  509. if ($uniqueA['size']==3 && $uniqueA['owner']==$uid){
  510. $production['crop'] = $this->getCropProd($fdata, $ocounter,$b4)-$pop-(($upkeep)-round($upkeep*0.50));
  511. }elseif ($normalA['type']==4 && $normalA['size']==1 && $normalA['owner']==$uid){
  512. $production['crop'] = $this->getCropProd($fdata, $ocounter,$b4)-$pop-(($upkeep)-round($upkeep*0.25));
  513. }else if ($largeA['size']==2 && $largeA['owner']==$uid){
  514. $production['crop'] = $this->getCropProd($fdata, $ocounter,$b4)-$pop-(($upkeep)-round($upkeep*0.25));
  515. }else{
  516. $production['crop'] = $this->getCropProd($fdata, $ocounter,$b4)-$pop-$upkeep;
  517. }
  518. return $production;
  519. }
  520. private function getWoodProd($fdata,$ocounter,$b1) {
  521. global $bid1,$bid5;
  522. $basewood = $sawmill = 0;
  523. $woodholder = array();
  524. for($i=1;$i<=38;$i++) {
  525. if($fdata['f'.$i.'t'] == 1) {
  526. array_push($woodholder,'f'.$i);
  527. }
  528. if($fdata['f'.$i.'t'] == 5) {
  529. $sawmill = $fdata['f'.$i];
  530. }
  531. }
  532. for($i=0;$i<=count($woodholder)-1;$i++) { $basewood+= $bid1[$fdata[$woodholder[$i]]]['prod']; }
  533. $wood = $basewood + $basewood * 0.25 * $ocounter[0];
  534. if($sawmill >= 1) {
  535. $wood += $basewood / 100 * $bid5[$sawmill]['attri'];
  536. }
  537. if($b1 > time()) {
  538. $wood *= 1.25;
  539. }
  540. $wood *= SPEED;
  541. return round($wood);
  542. }
  543. private function getClayProd($fdata,$ocounter,$b2) {
  544. global $bid2,$bid6,$session;
  545. $baseclay = $clay = $brick = 0;
  546. $clayholder = array();
  547. for($i=1;$i<=38;$i++) {
  548. if($fdata['f'.$i.'t'] == 2) {
  549. array_push($clayholder,'f'.$i);
  550. }
  551. if($fdata['f'.$i.'t'] == 6) {
  552. $brick = $fdata['f'.$i];
  553. }
  554. }
  555. for($i=0;$i<=count($clayholder)-1;$i++) { $baseclay+= $bid2[$fdata[$clayholder[$i]]]['prod']; }
  556. $clay = $baseclay + $baseclay * 0.25 * $ocounter[1];
  557. if($brick >= 1) {
  558. $clay += $baseclay / 100 * $bid6[$brick]['attri'];
  559. }
  560. if($b2 > time()) {
  561. $clay *= 1.25;
  562. }
  563. $clay *= SPEED;
  564. return round($clay);
  565. }
  566. private function getIronProd($fdata,$ocounter,$b3) {
  567. global $bid3,$bid7;
  568. $baseiron = $foundry = 0;
  569. $ironholder = array();
  570. for($i=1;$i<=38;$i++) {
  571. if($fdata['f'.$i.'t'] == 3) {
  572. array_push($ironholder,'f'.$i);
  573. }
  574. if($fdata['f'.$i.'t'] == 7) {
  575. $foundry = $fdata['f'.$i];
  576. }
  577. }
  578. for($i=0;$i<=count($ironholder)-1;$i++) { $baseiron+= $bid3[$fdata[$ironholder[$i]]]['prod']; }
  579. $iron = $baseiron + $baseiron * 0.25 * $ocounter[2];
  580. if($foundry >= 1) {
  581. $iron += $baseiron / 100 * $bid7[$foundry]['attri'];
  582. }
  583. if($b3 > time()) {
  584. $iron *= 1.25;
  585. }
  586. $iron *= SPEED;
  587. return round($iron);
  588. }
  589. private function getCropProd($fdata,$ocounter,$b4) {
  590. global $bid4,$bid8,$bid9;
  591. $basecrop = $grainmill = $bakery = 0;
  592. $cropholder = array();
  593. for($i=1;$i<=38;$i++) {
  594. if($fdata['f'.$i.'t'] == 4) {
  595. array_push($cropholder,'f'.$i);
  596. }
  597. if($fdata['f'.$i.'t'] == 8) {
  598. $grainmill = $fdata['f'.$i];
  599. }
  600. if($fdata['f'.$i.'t'] == 9) {
  601. $bakery = $fdata['f'.$i];
  602. }
  603. }
  604. for($i=0;$i<=count($cropholder)-1;$i++) { $basecrop+= $bid4[$fdata[$cropholder[$i]]]['prod']; }
  605. $crop = $basecrop + $basecrop * 0.25 * $ocounter[3];
  606. $jcrop=0;
  607. if($grainmill >= 1) $jcrop=$bid8[$grainmill]['attri'];
  608. if($bakery >= 1) $jcrop+=$bid9[$bakery]['attri'];
  609. $crop += $basecrop /100 * $jcrop;
  610. if($b4 > time()) {
  611. $crop *= 1.25;
  612. }
  613. $crop *= SPEED;
  614. return round($crop);
  615. }
  616. function getAllUnits($base,$InVillageOnly=False,$mode=0) {
  617. global $database;
  618. $ownunit = $database->getUnit($base);
  619. $ownunit['u99'] -= $ownunit['u99'];
  620. $ownunit['u99o'] -= $ownunit['u99o'];
  621. $enforcementarray = $database->getEnforceVillage($base,0);
  622. if(count($enforcementarray) > 0) {
  623. foreach($enforcementarray as $enforce) {
  624. for($i=1;$i<=50;$i++) {
  625. $ownunit['u'.$i] += $enforce['u'.$i];
  626. }
  627. $ownunit['hero'] += $enforce['hero'];
  628. }
  629. }
  630. if ($mode==0) {
  631. $enforceoasis=$database->getOasisEnforce($base,0);
  632. if(count($enforceoasis) > 0) {
  633. foreach($enforceoasis as $enforce) {
  634. for($i=1;$i<=50;$i++) {
  635. $ownunit['u'.$i] += $enforce['u'.$i];
  636. }
  637. $ownunit['hero'] += $enforce['hero'];
  638. }
  639. }
  640. //$enforcementarray = $database->getEnforceVillage($base,1);
  641. $enforceoasis1=$database->getOasisEnforce($base,1);
  642. if(count($enforceoasis1) > 0) {
  643. foreach($enforceoasis1 as $enforce) {
  644. for($i=1;$i<=50;$i++) {
  645. $ownunit['u'.$i] += $enforce['u'.$i];
  646. }
  647. $ownunit['hero'] += $enforce['hero'];
  648. }
  649. }
  650. $prisoners = $database->getPrisoners($base,1);
  651. if(!empty($prisoners)) {
  652. foreach($prisoners as $prisoner){
  653. $owner = $database->getVillageField($base,"owner");
  654. $ownertribe = $database->getUserField($owner,"tribe",0);
  655. $start = ($ownertribe-1)*10+1;
  656. $end = ($ownertribe*10);
  657. for($i=$start;$i<=$end;$i++) {
  658. $j = $i-$start+1;
  659. $ownunit['u'.$i] += $prisoner['t'.$j];
  660. }
  661. $ownunit['hero'] += $prisoner['t11'];
  662. }
  663. }
  664. }
  665. if(!$InVillageOnly) {
  666. $movement = $database->getVillageMovement($base);
  667. if(!empty($movement)) {
  668. for($i=1;$i<=50;$i++) {
  669. if (isset($movement['u'.$i])) {
  670. $ownunit['u'.$i] += $movement['u'.$i];
  671. }
  672. }
  673. $ownunit['hero'] += $movement['hero'];
  674. }
  675. }
  676. return $ownunit;
  677. }
  678. public function getUpkeep($array,$type,$vid,$uid,$prisoners=0) {
  679. global $database;
  680. $buildarray = array();
  681. $buildarray = $database->getResourceLevel($vid);
  682. $upkeep = 0;
  683. switch($type) {
  684. case 0:
  685. $start = 1;
  686. $end = 50;
  687. break;
  688. case 1:
  689. $start = 1;
  690. $end = 10;
  691. break;
  692. case 2:
  693. $start = 11;
  694. $end = 20;
  695. break;
  696. case 3:
  697. $start = 21;
  698. $end = 30;
  699. break;
  700. case 4:
  701. $start = 31;
  702. $end = 40;
  703. break;
  704. case 5:
  705. $start = 41;
  706. $end = 50;
  707. break;
  708. }
  709. for($i=$start;$i<=$end;$i++) {
  710. $k = $i-$start+1;
  711. $unit = "u".$i;
  712. $unit2 = "t".$k;
  713. global $$unit;
  714. $dataarray = $$unit;
  715. for($j=19;$j<=38;$j++) {
  716. if($buildarray['f'.$j.'t'] == 41) {
  717. $horsedrinking = $j;
  718. }
  719. }
  720. if($prisoners == 0){
  721. if(isset($horsedrinking)){
  722. if(($i==4 && $buildarray['f'.$horsedrinking] >= 10)
  723. || ($i==5 && $buildarray['f'.$horsedrinking] >= 15)
  724. || ($i==6 && $buildarray['f'.$horsedrinking] == 20)) {
  725. $upkeep += ($dataarray['pop']-1) * $array[$unit];
  726. } else {
  727. $upkeep += $dataarray['pop'] * $array[$unit];
  728. }}else{
  729. $upkeep += $dataarray['pop'] * $array[$unit];
  730. }
  731. }else{
  732. if(isset($horsedrinking)){
  733. if(($i==4 && $buildarray['f'.$horsedrinking] >= 10)
  734. || ($i==5 && $buildarray['f'.$horsedrinking] >= 15)
  735. || ($i==6 && $buildarray['f'.$horsedrinking] == 20)) {
  736. $upkeep += ($dataarray['pop']-1) * $array[$unit2];
  737. } else {
  738. $upkeep += $dataarray['pop'] * $array[$unit2];
  739. }}else{
  740. $upkeep += $dataarray['pop'] * $array[$unit2];
  741. }
  742. }
  743. }
  744. // $unit = "hero";
  745. // global $$unit;
  746. // $dataarray = $$unit;
  747. if($prisoners == 0){
  748. $upkeep += (isset($array['hero'])? $array['hero'] * 6:0);
  749. }else{
  750. $upkeep += (isset($array['t11'])? $array['t11'] * 6:0);
  751. }
  752. $artefact = count($database->getOwnUniqueArtefactInfo2($uid,4,3,0));
  753. $artefact1 = count($database->getOwnUniqueArtefactInfo2($vid,4,1,1));
  754. $artefact2 = count($database->getOwnUniqueArtefactInfo2($uid,4,2,0));
  755. if($artefact > 0){
  756. $upkeep /= 2;
  757. $upkeep = round($upkeep);
  758. }else if($artefact1 > 0){
  759. $upkeep /= 2;
  760. $upkeep = round($upkeep);
  761. }else if($artefact2 > 0){
  762. $upkeep /= 4;
  763. $upkeep = round($upkeep);
  764. $upkeep *= 3;
  765. }
  766. $foolartefact = $database->getFoolArtefactInfo(4,$vid,$uid);
  767. if(count($foolartefact) > 0){
  768. foreach($foolartefact as $arte){
  769. if($arte['bad_effect'] == 1){
  770. $upkeep *= $arte['effect2'];
  771. }else{
  772. $upkeep /= $arte['effect2'];
  773. $upkeep = round($upkeep);
  774. }
  775. }
  776. }
  777. return $upkeep;
  778. }
  779. };
  780. $admin = new adm_DB;
  781. include("function.php");
  782. ?>