-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuilding.php
163 lines (126 loc) · 6.87 KB
/
building.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
defined('INSIDE') OR exit('No direct script access allowed');
class M_Building implements I_Model {
public static function build($planetID, $buildID, $toLvl, $metal, $crystal, $deuterium) {
global $debug;
// check if requirements are met
$req_met = true;
// check requirements
if (D_Units::getRequirements($buildID) !== []) {
$req = D_Units::getRequirements($buildID);
foreach ($req as $bID => $lvl) {
if (!$req_met) {
break;
}
// if requirement is a building
if ($bID < 100) {
$level = Loader::getBuildingList()[$buildID]->getLevel();
}
// if requirement is a research
if ($bID > 100 && $bID < 200) {
$level = Loader::getTechList()[$buildID]->getLevel();
}
if ($level < $lvl) {
throw new InvalidArgumentException('Requirements not met');
break;
}
}
}
if (Loader::getPlanet()->getBBuildingId() == 0 && Loader::getPlanet()->getBBuildingEndtime() == 0) {
if ($buildID > 0 && $metal >= 0 && $crystal >= 0 && $deuterium >= 0) {
try {
$buildTime = time() + 3600 * D_Units::getBuildTime(
Loader::getBuildingList()[$buildID],
Loader::getBuildingList()[D_Units::getUnitID('robotic_factory')]->getLevel(),
Loader::getBuildingList()[D_Units::getUnitID('shipyard')]->getLevel(),
Loader::getBuildingList()[D_Units::getUnitID('nanite_factory')]->getLevel()
);
$params = array(':b_building_id' => $buildID,
':b_building_endtime' => $buildTime,
':metal' => $metal,
':crystal' => $crystal,
':deuterium' => $deuterium,
':planetID' => $planetID
);
$dbConnection = new Database();
$stmt = $dbConnection->prepare('UPDATE planets SET b_building_id = :b_building_id, b_building_endtime = :b_building_endtime, metal = :metal, crystal = :crystal, deuterium = :deuterium WHERE planetID = :planetID;');
$stmt->execute($params);
Loader::getPlanet()->setBBuildingId($buildID);
Loader::getPlanet()->setBBuildingEndtime($buildTime);
Loader::getPlanet()->setMetal($metal);
Loader::getPlanet()->setCrystal($crystal);
Loader::getPlanet()->setDeuterium($deuterium);
} catch (PDOException $e) {
if (DEBUG) {
$debug->addLog(self::class, __FUNCTION__, __LINE__, get_class($e), $e->getMessage());
} else {
$debug->saveError(self::class, __FUNCTION__, __LINE__, get_class($e), $e->getMessage());
}
}
} else {
throw new InvalidArgumentException('Passed arguments are not valid');
}
} else {
throw new UnexpectedFalueException('a building is already in the buildingqueue');
}
}
public static function cancel($planetID, $metal, $crystal, $deuterium) {
global $debug;
$dbConnection = new Database();
if (Loader::getPlanet()->getBBuildingId() > 0 && Loader::getPlanet()->getBBuildingEndtime() > 0) {
if ($planetID > 0 && $metal >= 0 && $crystal >= 0 && $deuterium >= 0) {
try {
$params = array(':planetID' => $planetID,
':metal' => $metal,
':crystal' => $crystal,
':deuterium' => $deuterium
);
$stmt = $dbConnection->prepare('UPDATE planets SET b_building_id = 0, b_building_endtime = 0, metal = metal+:metal, crystal = crystal+:crystal, deuterium = deuterium+:deuterium WHERE planetID = :planetID;');
$stmt->execute($params);
Loader::getPlanet()->setBBuildingId(null);
Loader::getPlanet()->setBBuildingEndtime(null);
Loader::getPlanet()->setMetal(Loader::getPlanet()->getMetal() + $metal);
Loader::getPlanet()->setCrystal(Loader::getPlanet()->getCrystal() + $crystal);
Loader::getPlanet()->setDeuterium(Loader::getPlanet()->getDeuterium() + $deuterium);
} catch (PDOException $e) {
if (DEBUG) {
$debug->addLog(self::class, __FUNCTION__, __LINE__, get_class($e), $e->getMessage());
} else {
$debug->saveError(self::class, __FUNCTION__, __LINE__, get_class($e), $e->getMessage());
}
}
} else {
throw new InvalidArgumentException('Passed arguments are not valid');
}
} else {
throw new UnexpectedValueException('no building is currently in the buildingqueue');
}
}
/**
* loads the required language files
* @return array the loaded language-array
* @throws FileNotFoundException
*/
public function loadLanguage() {
global $lang;
$file = Config::$pathConfig['language'] . Config::$gameConfig['language'] . '/buildings.php';
if (file_exists($file)) {
require $file;
} else {
throw new FileNotFoundException('File \'' . $file . '\' not found');
}
$file = Config::$pathConfig['language'] . Config::$gameConfig['language'] . '/units.php';
if (file_exists($file)) {
require $file;
} else {
throw new FileNotFoundException('File \'' . $file . '\' not found');
}
$file = Config::$pathConfig['language'] . Config::$gameConfig['language'] . '/menu.php';
if (file_exists($file)) {
require $file;
} else {
throw new FileNotFoundException('File \'' . $file . '\' not found');
}
return $lang;
}
}