-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainBarracks.php
101 lines (80 loc) · 3.61 KB
/
trainBarracks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
session_start();
if (!isset($_SESSION['username'])){
header('location: /login');
}
require_once($_SERVER['DOCUMENT_ROOT'].'/connect.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/troopInfoLookup.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/getCurrentResources.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/getCurrentTroops.php');
$tribe = "teuton";
if(isset($_GET['vbid'])){
$vbid=(int)mysqli_real_escape_string($connection, $_GET['vbid']);
}
else{
header('location: /village');
}
$currentTime = time();
$villageID = $_SESSION['idPlayer'];
$troopsToTrainNum = array();
$troopsToTrainID = array();
for($i = 1; $i < 11; $i++){
if(isset($_GET['unit'.$i])){
if($_GET['unit'.$i] > 0){
array_push($troopsToTrainNum, (int)mysqli_real_escape_string($connection, $_GET['unit'.$i]));
array_push($troopsToTrainID, $i);
}
}
}
for($i = 0; $i < count($troopsToTrainID); $i++){
$trainTime[$i] = TroopInfo::getTrainTime($tribe,$troopsToTrainID[$i]);
$trainCost = TroopInfo::getTrainCost($tribe,$troopsToTrainID[$i]);
$troopName[$i] = TroopInfo::getTroopName($tribe,$troopsToTrainID[$i]);
$combinedTrainTime[$i] = $trainTime[$i] * $troopsToTrainNum[$i];
$combinedTrainCost = [];
foreach ($trainCost as $cost) {
$combinedTrainCost[] = $cost * $troopsToTrainNum[$i];
}
//Subtract the resources that are needed to train
$newWood -= $combinedTrainCost[0];
$newClay -= $combinedTrainCost[1];
$newIron -= $combinedTrainCost[2];
$newCrop -= $combinedTrainCost[3];
}
$timeStarted = $currentTime;
$timeCompleted = $currentTime;
$combinedTrainTimeAll = 0;
if($troopProduction){
//Queue after the last troop production time
$getAllTroopProduction = $connection->prepare('SELECT * FROM barracksproduction WHERE idVillage= ?');
$getAllTroopProduction->bind_param('i', $villageID);
$getAllTroopProduction->execute();
$resultAllTroopProduction = $getAllTroopProduction->get_result()->fetch_all(MYSQLI_NUM);
$getAllTroopProduction->close();
$lastTroopProduction = $resultAllTroopProduction[count($resultAllTroopProduction)-1];
$timeStarted = $lastTroopProduction[6];
$timeCompleted = $lastTroopProduction[6];
}
if($newWood>=0 && $newClay>=0 && $newIron>=0 && $newCrop>=0){
for($i = 0; $i < count($troopsToTrainID); $i++){
$timeStarted = $timeCompleted;
$timeCompleted = $timeStarted + (int)$combinedTrainTime[$i];
$combinedTrainTimeAll += (int)$combinedTrainTime[$i];
//Insert into barracks production
$barracksProduction = $connection->prepare("INSERT INTO barracksproduction (idvillage,unitname,unitid,unitcount,unitprodtime,timestarted,timecompleted,lastupdate) VALUES (?,?,?,?,?,?,?,?)");
$barracksProduction->bind_param("isiidiii", $villageID, $troopName[$i], $troopsToTrainID[$i], $troopsToTrainNum[$i], $trainTime[$i], $timeStarted, $timeCompleted, $timeStarted);
$barracksProduction->execute();
$barracksProduction->close();
$updateCurrentRes = $connection->prepare("UPDATE villageresources SET currentWood=?,currentClay=?,currentIron=?,currentCrop=?,lastUpdate=? WHERE idVillage = ?");
$updateCurrentRes->bind_param("ddddii", $newWood,$newClay,$newIron,$newCrop,$currentTime,$villageID);
$updateCurrentRes->execute();
$updateCurrentRes->close();
}
header('location: /village/villageBuilding/?vbid='.$vbid);
//header('location: /resources');
}
//If there arent enough resources redirect to barracks
else{
header('location: /village/villageBuilding/?vbid='.$vbid);
}
?>