-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathresearch.php
151 lines (115 loc) · 5.93 KB
/
research.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
<?php
defined('INSIDE') OR exit('No direct script access allowed');
class M_Research implements I_Model {
public static function build($planetID, $buildID, $toLvl, $metal, $crystal, $deuterium) {
global $debug;
$dbConnection = new Database();
//echo $key . " - " . $v . "<br />";
$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()[$bID])->getLevel();
}
// if requirement is a research
if ($bID > 100 && $bID < 200) {
$level = Loader::getTechList()[$bID]->getLevel();
}
if ($level < $lvl) {
throw new InvalidArgumentException('Requirements not met');
break;
}
}
}
if (Loader::getPlanet()->getBTechID() == 0 && Loader::getPlanet()->getBTechEndtime() == 0) {
if ($buildID > 0 && $metal >= 0 && $crystal >= 0 && $deuterium >= 0) {
try {
$price = D_Units::getPriceList($buildID);
// preis * faktor ^ level
$buildTime = time() + ($price["metal"] * pow($price["factor"],
$toLvl - 1) + $price["crystal"] * pow($price["factor"],
$toLvl - 1)) / (1000 * (1 + Loader::getBuildingList()['research_lab'])) * 3600;
$params = array(':b_tech_id' => $buildID,
':b_tech_endtime' => $buildTime,
':metal' => $metal,
':crystal' => $crystal,
':deuterium' => $deuterium,
':planetID' => $planetID
);
$stmt = $dbConnection->prepare('UPDATE planets SET b_tech_id = :b_tech_id, b_tech_endtime = :b_tech_endtime, metal = :metal, crystal = :crystal, deuterium = :deuterium WHERE planetID = :planetID;');
$stmt->execute($params);
} 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('a building is already in the buildingqueue');
}
}
public static function cancel($planetID, $metal, $crystal, $deuterium) {
global $debug;
$dbConnection = new Database();
if (Loader::getPlanet()->getBTechID() > 0 && Loader::getPlanet()->getBTechEndtime() > 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_tech_id = 0, b_tech_endtime = 0, metal = metal+:metal, crystal = crystal+:crystal, deuterium = deuterium+:deuterium WHERE planetID = :planetID;');
$stmt->execute($params);
} 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'] . '/research.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;
}
}