Added Donns Fix for gain mill and bakery
[travianx.git] / GameEngine / Building_bak.php
blob08dcbe51dfab2fbdf01bac7a672e1f7ef60a2020
1 <?php
3 #################################################################################
4 ## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
5 ## --------------------------------------------------------------------------- ##
6 ## Filename Buildng.php ##
7 ## Developed by: Dzoki & Dixie ##
8 ## License: TravianX Project ##
9 ## Copyright: TravianX (c) 2010-2011. All rights reserved. ##
10 ## ##
11 #################################################################################
14 class Building {
16 public $NewBuilding = false;
17 private $maxConcurrent;
18 private $allocated;
19 private $basic,$inner,$plus = 0;
20 public $buildArray = array();
22 public function Building() {
23 global $session;
24 $this->maxConcurrent = BASIC_MAX;
25 if(ALLOW_ALL_TRIBE || $session->tribe == 1) {
26 $this->maxConcurrent += INNER_MAX;
28 if($session->plus) {
29 $this->maxConcurrent += PLUS_MAX;
31 $this->LoadBuilding();
35 public function procBuild($get) { global $session;
36 if(isset($get['a']) && $get['c'] == $session->checker && !isset($get['id'])) {
37 if($get['a'] == 0) {
38 $this->removeBuilding($get['d']);
40 else {
41 $session->changeChecker();
42 $this->upgradeBuilding($get['a']);
45 if(isset($get['a']) && $get['c'] == $session->checker && isset($get['id'])) {
46 $session->changeChecker();
47 $this->constructBuilding($get['id'],$get['a']);
49 if(isset($get['buildingFinish'])) {
50 if($session->gold >= 2) {
51 $this->finishAll();
56 public function canBuild($id,$tid) {
57 global $village,$session;
58 if($this->isMax($tid,$id)) {
59 return 1;
61 else {
62 if($this->isCurrent($id)) {
63 if($this->isLoop($id)) {
64 return 3;
66 else {
67 return 2;
70 else {
71 if($this->allocated <= $this->maxConcurrent) {
72 if($village->getProd("crop") == 2) {
73 return 4;
75 else {
76 switch($this->checkResource($tid,$id)) {
77 case 1:
78 return 5;
79 break;
80 case 2:
81 return 6;
82 break;
83 case 3:
84 return 7;
85 break;
86 case 4:
87 if($id >= 19) {
88 if($session->tribe == 1 || ALLOW_ALL_TRIBE) {
89 if($this->inner == 0) {
90 return 8;
92 else {
93 if($session->plus) {
94 if($this->plus == 0) {
95 return 9;
97 else {
98 return 2;
101 else {
102 return 2;
106 else {
107 if($this->basic == 0) {
108 return 8;
110 else {
111 if($session->plus) {
112 if($this->plus == 0) {
113 return 9;
115 else {
116 return 2;
119 else {
120 return 2;
125 else {
126 if($this->basic == 1) {
127 if($session->plus && $this->plus == 0) {
128 return 9;
130 else {
131 return 2;
134 else {
135 return 8;
138 break;
142 else {
143 return 2;
149 public function walling() {
150 global $session;
151 $wall = array(31,32,33);
152 foreach($this->buildArray as $job) {
153 if(in_array($job['type'],$wall)) {
154 return "3".$session->tribe;
157 return false;
160 public function rallying() {
161 foreach($this->buildArray as $job) {
162 if($job['type'] == 16) {
163 return true;
166 return false;
169 public function procResType($ref) {
170 global $session;
171 switch($ref) {
172 case 1: $build = "Woodcutter"; break;
173 case 2: $build = "Clay Pit"; break;
174 case 3: $build = "Iron Mine"; break;
175 case 4: $build = "Cropland"; break;
176 case 5: $build = "Sawmill"; break;
177 case 6: $build = "Brickyard"; break;
178 case 7: $build = "Iron Foundry"; break;
179 case 8: $build = "Grain Mill"; break;
180 case 9: $build = "Bakery"; break;
181 case 10: $build = "Warehouse"; break;
182 case 11: $build = "Granary"; break;
183 case 12: $build = "Blacksmith"; break;
184 case 13: $build = "Armoury"; break;
185 case 14: $build = "Tournament Square"; break;
186 case 15: $build = "Main Building"; break;
187 case 16: $build = "Rally Point"; break;
188 case 17: $build = "Marketplace"; break;
189 case 18: $build = "Embassy"; break;
190 case 19: $build = "Barracks"; break;
191 case 20: $build = "Stable"; break;
192 case 21: $build = "Workshop"; break;
193 case 22: $build = "Academy"; break;
194 case 23: $build = "Cranny"; break;
195 case 24: $build = "Town Hall"; break;
196 case 25: $build = "Residence"; break;
197 case 26: $build = "Palace"; break;
198 case 27: $build = "Treasury"; break;
199 case 28: $build = "Trade Office"; break;
200 case 29: $build = "Great Barracks"; break;
201 case 30: $build = "Great Stable"; break;
202 case 31: $build = "City Wall"; break;
203 case 32: $build = "Earth Wall"; break;
204 case 33: $build = "Palisade"; break;
205 case 34: $build = "Stonemason's Lodge"; break;
206 case 35: $build = "Brewery"; break;
207 case 36: $build = "Trapper"; break;
208 case 37: $build = "Hero's Mansion"; break;
209 case 38: $build = "Great Warehouse"; break;
210 case 39: $build = "Great Granary"; break;
211 case 40: $build = "Wonder of the World"; break;
212 case 41: $build = "Horse Drinking Trough"; break;
213 default: $build = "Error"; break;
215 return $build;
218 private function loadBuilding() {
219 global $database,$village,$session;
220 $this->buildArray = $database->getJobs($village->wid);
221 $this->allocated = count($this->buildArray);
222 if($this->allocated > 0) {
223 foreach($this->buildArray as $build) {
224 if($build['loopcon'] == 1) {
225 $this->plus = 1;
227 else {
228 if($build['field'] <= 18) {
229 $this->basic += 1;
231 else {
232 if($session->tribe == 1 || ALLOW_ALL_TRIBE) {
233 $this->inner += 1;
235 else {
236 $this->basic += 1;
241 $this->NewBuilding = true;
245 private function removeBuilding($d) {
246 global $database,$village;
247 foreach($this->buildArray as $jobs) {
248 if($jobs['id'] == $d) {
249 $uprequire = $this->resourceRequired($jobs['field'],$jobs['type']);
250 if($database->removeBuilding($d)) {
251 $database->modifyResource($village->wid,$uprequire['wood'],$uprequire['clay'],$uprequire['iron'],$uprequire['crop'],1);
252 if($jobs['field'] >= 19) {
253 header("Location: dorf2.php");
255 else {
256 header("Location: dorf1.php");
263 private function upgradeBuilding($id) {
264 global $database,$village,$session,$logging,$ranking;
265 if($this->allocated < $this->maxConcurrent) {
266 $uprequire = $this->resourceRequired($id,$village->resarray['f'.$id.'t']);
267 $time = time() + $uprequire['time'];
268 $bindicate = $this->canBuild($id,$village->resarray['f'.$id.'t']);
269 $loop = ($bindicate == 9 ? 1 : 0);
270 $loopsame = 0;
271 if($loop == 1) {
272 foreach($this->buildArray as $build) {
273 if($build['field']==$id) {
274 $loopsame += 1;
275 $uprequire = $this->resourceRequired($id,$village->resarray['f'.$id.'t'],($loopsame>0?2:1));
278 if($session->tribe == 1 || ALLOW_ALL_TRIBE) {
279 if($id >= 19) {
280 foreach($this->buildArray as $build) {
281 if($build['field'] >= 19) {
282 $time = $build['timestamp'] + $uprequire['time'];
286 else {
287 foreach($this->buildArray as $build) {
288 if($build['field'] <= 18) {
289 $time = $build['timestamp'] + $uprequire['time'];
294 else {
295 $time = $this->buildArray[0]['timestamp'] + $uprequire['time'];
298 if($database->addBuilding($village->wid,$id,$village->resarray['f'.$id.'t'],$loop,$time)) {
299 $database->modifyResource($village->wid,$uprequire['wood'],$uprequire['clay'],$uprequire['iron'],$uprequire['crop'],0);
300 //$ranking->procRankReq($session->uid);
301 $playerarray = $database->getUserArray($session->uid,1);
302 $oldrank = $ranking->searchRank($session->username,"username");
303 $database->updateoldrank($session->uid,$oldrank);
304 $database->addclimberpop($session->uid,$uprequire['pop']);
306 $logging->addBuildLog($village->wid,$this->procResType($village->resarray['f'.$id.'t']),($village->resarray['f'.$id]+($loopsame>0?2:1)),0);
307 if($id >= 19) {
308 header("Location: dorf2.php");
310 else {
311 header("Location: dorf1.php");
317 private function downgradeBuilding($id) {
318 global $database,$village,$session,$logging;
319 if($this->allocated < $this->maxConcurrent) {
320 $name = "bid".$village->resarray['f'.$id.'t'];
321 global $$name;
322 $dataarray = $$name;
323 $time = time() + round($dataarray[$village->resarray['f'.$id]-1]['time'] / 4);
324 $loop = 0;
325 if($this->inner == 1 || $this->basic == 1) {
326 if($session->plus && $this->plus == 0) {
327 $loop = 1;
330 if($loop == 1) {
331 if($session->tribe == 1 || ALLOW_ALL_TRIBE) {
332 if($id >= 19) {
333 foreach($this->buildArray as $build) {
334 if($build['field'] >= 19) {
335 $time = $build['timestamp'] + round($dataarray[$village->resarray['f'.$id]-1]['time'] / 4);
340 else {
341 $time = $this->buildArray[0]['timestamp'] + round($dataarray[$village->resarray['f'.$id]-1]['time'] / 4);
344 if($database->addBuilding($village->wid,$id,$village->resarray['f'.$id.'t'],$loop,$time,0)) {
345 $logging->addBuildLog($village->wid,$this->procResType($village->resarray['f'.$id.'t']),($village->resarray['f'.$id]-1),2);
346 header("Location: dorf2.php");
353 private function constructBuilding($id,$tid) {
354 global $database,$village,$session,$logging;
355 if($this->allocated < $this->maxConcurrent) {
356 if($tid == 16) {
357 $id = 39;
359 else if($tid == 31 || $tid == 32 || $tid == 33) {
360 $id = 40;
362 $uprequire = $this->resourceRequired($id,$tid);
363 $time = time() + $uprequire['time'];
364 $bindicate = $this->canBuild($id,$village->resarray['f'.$id.'t']);
365 $loop = ($bindicate == 9 ? 1 : 0);
366 if($loop == 1) {
367 foreach($this->buildArray as $build) {
368 if($build['field'] >= 19) {
369 $time = $build['timestamp'] + $uprequire['time'];
373 if($this->meetRequirement($tid)) {
374 if($database->addBuilding($village->wid,$id,$tid,$loop,$time)) {
375 $logging->addBuildLog($village->wid,$this->procResType($tid),($village->resarray['f'.$id]+1),1);
376 $database->modifyResource($village->wid,$uprequire['wood'],$uprequire['clay'],$uprequire['iron'],$uprequire['crop'],0);
377 $database->addclimberpop($session->uid,$uprequire['pop']);
379 header("Location: dorf2.php");
385 private function meetRequirement($id) {
386 switch($id) {
387 case 1:
388 case 2:
389 case 3:
390 case 4:
391 case 11:
392 case 15:
393 case 16:
394 case 18:
395 case 23:
396 case 31:
397 case 32:
398 case 33:
399 return true;
400 break;
401 case 10:
402 case 20:
403 return ($this->getTypeLevel(15) >= 1)? true : false;
404 break;
405 case 5:
406 if($this->getTypeLevel(1) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
407 break;
408 case 6:
409 if($this->getTypeLevel(2) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
410 break;
411 case 7:
412 if($this->getTypeLevel(3) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
413 break;
414 case 8:
415 if($this->getTypeLevel(4) >= 5) { return true; } else { return false; }
416 break;
417 case 9:
418 if($this->getTypeLevel(15) >= 5 && $this->getTypeLevel(4) >= 10 && $this->getTypeLevel(8) >= 5) { return true; } else { return false; }
419 break;
420 case 12:
421 if($this->getTypeLevel(22) >= 3 && $this->getTypeLevel(15) >= 3) { return true; } else { return false; }
422 break;
423 case 13:
424 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(22) >= 1) { return true; } else { return false; }
425 break;
426 case 14:
427 if($this->getTypeLevel(16) >= 15) { return true; } else { return false; }
428 break;
429 case 17:
430 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(10) >= 1 && $this->getTypeLevel(11) >= 1) { return true; } else { return false; }
431 break;
432 case 19:
433 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1) { return true; } else { return false; }
434 break;
435 case 20:
436 if($this->getTypeLevel(12) >= 3 && $this->getTypeLevel(22) >= 5) { return true; } else { return false; }
437 break;
438 case 21:
439 if($this->getTypeLevel(22) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
440 break;
441 case 22:
442 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1) { return true; } else { return false; }
443 break;
444 case 24:
445 if($this->getTypeLevel(22) >= 10 && $this->getTypeLevel(15) >= 10) { return true; } else { return false; }
446 break;
447 case 25:
448 if($this->getTypeLevel(15) >= 5) { return true; } else { return false; }
449 break;
450 case 26:
451 if($this->getTypeLevel(18) >= 1 && $this->getTypeLevel(15) >= 5 && $this->getTypeLevel(25) == 0) { return true; } else { return false; }
452 break;
453 case 27:
454 if($this->getTypeLevel(15) >= 10) { return true; } else { return false; }
455 break;
456 case 28:
457 if($this->getTypeLevel(17) == 20 && $this->getTypeLevel(20) >= 10) { return true; } else { return false; }
458 break;
459 case 29:
460 if($this->getTypeLevel(16) == 20) { return true; } else { return false; }
461 break;
462 case 30:
463 if($this->getTypeLevel(20) == 20) { return true; } else { return false; }
464 break;
465 case 34:
466 if($this->getTypeLevel(26) >= 3 && $this->getTypeLevel(15) >= 5 && $this->getTypeLevel(25) == 0) { return true; } else { return false; }
467 break;
468 case 35:
469 if($this->getTypeLevel(16) >= 10 && $this->getTypeLevel(11) == 20) { return true; } else { return false; }
470 break;
471 case 36:
472 if($this->getTypeLevel(16) >= 1) { return true; } else { return false; }
473 break;
474 case 37:
475 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1) { return true; } else { return false; }
476 break;
477 case 38:
478 case 39:
479 if($this->getTypeLevel(15) >= 10) { return true; } else { return false; }
480 break;
481 case 40:
482 return false; //not implemented
483 break;
484 case 41:
485 if($this->getTypeLevel(16) >= 10 && $this->getTypeLevel(20) == 20) { return true; } else { return false; }
486 break;
490 private function checkResource($tid,$id) {
491 $name = "bid".$tid;
492 global $village,$$name;
493 $dataarray = $$name;
494 $wood = $dataarray[$village->resarray['f'.$id]+1]['wood'];
495 $clay = $dataarray[$village->resarray['f'.$id]+1]['clay'];
496 $iron = $dataarray[$village->resarray['f'.$id]+1]['iron'];
497 $crop = $dataarray[$village->resarray['f'.$id]+1]['crop'];
498 if($wood > $village->maxstore || $clay > $village->maxstore || $iron > $village->maxstore) {
499 return 1;
501 else {
502 if($crop > $village->maxcrop) {
503 return 2;
505 else {
506 if($wood > $village->awood || $clay > $village->aclay || $iron > $village->airon || $crop > $village->acrop) {
507 return 3;
509 else {
510 if($village->awood-$wood > 0 && $village->aclay-$clay > 0 && $village->airon-$iron > 0 && $village->acrop-$crop >0){
511 return 4;
513 else {
514 return 3;
521 public function isMax($id,$field) {
522 $name = "bid".$id;
523 global $$name,$village;
524 $dataarray = $$name;
525 if($id <= 4) {
526 if($village->capital == 1) {
527 return ($village->resarray['f'.$field] == (count($dataarray) - 1));
529 else {
530 return ($village->resarray['f'.$field] == (count($dataarray) - 11));
533 else {
534 return ($village->resarray['f'.$field] == count($dataarray));
538 public function getTypeLevel($tid) {
539 global $village;
540 $keyholder = array();
541 foreach(array_keys($village->resarray,$tid) as $key) {
542 if(strpos($key,'t')) {
543 $key = preg_replace("/[^0-9]/", '', $key);
544 array_push($keyholder, $key);
547 $element = count($keyholder);
548 if($element >= 2) {
549 if($tid <= 4) {
550 $temparray = array();
551 for($i=0;$i<=$element-1;$i++) {
552 array_push($temparray,$village->resarray['f'.$keyholder[$i]]);
554 foreach ($temparray as $key => $val) {
555 if ($val == max($temparray))
556 $target = $key;
559 else {
560 for($i=0;$i<=$element-1;$i++) {
561 if($village->resarray['f'.$keyholder[$i]] != $keyholder[0]) {
562 $target = $i;
567 else if($element == 1) {
568 $target = 0;
570 else {
571 return 0;
573 if($keyholder[$target] != "") {
574 return $village->resarray['f'.$keyholder[$target]];
576 else {
577 return 0;
582 private function isCurrent($id) {
583 foreach($this->buildArray as $build) {
584 if($build['field'] < $id && $build['field'] > $id) {
585 return true;
590 private function isLoop($id) {
591 foreach($this->buildArray as $build) {
592 if($build['field'] == $id && $build['loopcon'] == 1) {
593 return true;
596 return false;
599 private function finishAll() {
600 global $database,$session,$logging,$village,$bid18,$bid10,$bid11,$technology;
601 foreach($this->buildArray as $jobs) {
602 $level = $database->getFieldLevel($jobs['wid'],$jobs['field']);
603 $level = ($level == -1) ? 0 : $level;
604 if($jobs['type'] != 25 AND $jobs['type'] != 26 AND $jobs['type'] != 40) {
605 $resource = $this->resourceRequired($jobs['field'],$jobs['type']);
606 $q = "UPDATE ".TB_PREFIX."fdata set f".$jobs['field']." = f".$jobs['field']." + 1, f".$jobs['field']."t = ".$jobs['type']." where vref = ".$jobs['wid'];
607 if($database->query($q)) {
609 $database->modifyPop($jobs['wid'],$resource['pop'],0);
610 $database->addCP($jobs['wid'],$resource['cp']);
612 $q = "DELETE FROM ".TB_PREFIX."bdata where id = ".$jobs['id'];
613 $database->query($q);
614 if($jobs['type'] == 18) {
615 $owner = $database->getVillageField($jobs['wid'],"owner");
616 $max = $bid18[$level]['attri'];
617 $q = "UPDATE ".TB_PREFIX."alidata set max = $max where leader = $owner";
618 $database->query($q);
620 if($jobs['type'] == 10) {
621 $max=$database->getVillageField($jobs['wid'],"maxstore");
622 if($level=='0'){ $max-=800; }
623 $max-=$bid10[$level]['attri'];
624 $max+=$bid10[$level+1]['attri'];
625 $database->setVillageField($jobs['wid'],"maxstore",$max);
628 if($jobs['type'] == 11) {
629 $max=$database->getVillageField($jobs['wid'],"maxcrop");
630 if($level=='0'){ $max-=800; }
631 $max-=$bid11[$level]['attri'];
632 $max+=$bid11[$level+1]['attri'];
633 $database->setVillageField($jobs['wid'],"maxcrop",$max);
639 $technology->finishTech();
640 $logging->goldFinLog($village->wid);
641 $database->modifyGold($session->uid,0,0);
642 header("Location: ".$session->referrer);
645 public function resourceRequired($id,$tid,$plus=1) {
646 $name = "bid".$tid;
647 global $$name,$village,$bid15;
648 $dataarray = $$name;
649 $wood = $dataarray[$village->resarray['f'.$id]+$plus]['wood'];
650 $clay = $dataarray[$village->resarray['f'.$id]+$plus]['clay'];
651 $iron = $dataarray[$village->resarray['f'.$id]+$plus]['iron'];
652 $crop = $dataarray[$village->resarray['f'.$id]+$plus]['crop'];
653 $pop = $dataarray[$village->resarray['f'.$id]+$plus]['pop'];
654 if ($tid == 15) {
655 if($this->getTypeLevel(15) == 0) {
656 $time = round($dataarray[$village->resarray['f'.$id]+$plus]['time']/ SPEED *5);
658 else {
659 $time = round($dataarray[$village->resarray['f'.$id]+$plus]['time'] / SPEED);
662 else {
663 if($this->getTypeLevel(15) != 0) {
664 $time = round($dataarray[$village->resarray['f'.$id]+$plus]['time'] * ($bid15[$this->getTypeLevel(15)]['attri']/100) / SPEED);
666 else {
667 $time = round($dataarray[$village->resarray['f'.$id]+$plus]['time']*5 / SPEED);
670 $cp = $dataarray[$village->resarray['f'.$id]+$plus]['cp'];
671 return array("wood"=>$wood,"clay"=>$clay,"iron"=>$iron,"crop"=>$crop,"pop"=>$pop,"time"=>$time,"cp"=>$cp);
674 public function getTypeField($type) {
675 global $village;
676 for($i=19;$i<=40;$i++) {
677 if($village->resarray['f'.$i.'t'] == $type) {
678 return $i;
683 public function calculateAvaliable($id,$tid) {
684 global $village,$generator;
685 $uprequire = $this->resourceRequired($id,$tid);
686 $rwood = $uprequire['wood']-$village->awood;
687 $rclay = $uprequire['clay']-$village->aclay;
688 $rcrop = $uprequire['crop']-$village->acrop;
689 $riron = $uprequire['iron']-$village->airon;
690 $rwtime = $rwood / $village->getProd("wood") * 3600;
691 $rcltime = $rclay / $village->getProd("clay")* 3600;
692 $rctime = $rcrop / $village->getProd("crop")* 3600;
693 $ritime = $riron / $village->getProd("iron")* 3600;
694 $reqtime = max($rwtime,$rctime,$rcltime,$ritime);
695 $reqtime += time();
696 return $generator->procMtime($reqtime);