1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** @noinspection SqlResolve */ |
4
|
|
|
|
5
|
|
|
namespace Unit; |
6
|
|
|
|
7
|
|
|
use _SnCacheInternal; |
8
|
|
|
use Exception; |
9
|
|
|
use mysqli_result; |
10
|
|
|
use Planet\DBStaticPlanet; |
11
|
|
|
use Planet\Planet; |
12
|
|
|
use SN; |
13
|
|
|
|
14
|
|
|
class DBStaticUnit { |
15
|
|
|
|
16
|
|
|
public static function db_unit_time_restrictions($date = SN_TIME_NOW) { |
17
|
|
|
$date = is_numeric($date) ? "FROM_UNIXTIME({$date})" : "'{$date}'"; |
18
|
|
|
|
19
|
|
|
return |
20
|
|
|
"(unit_time_start IS NULL OR unit_time_start <= {$date}) AND |
21
|
|
|
(unit_time_finish IS NULL OR unit_time_finish = '1970-01-01 03:00:00' OR unit_time_finish >= {$date})"; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function db_unit_by_id($unit_id) { |
25
|
|
|
$unit = SN::db_get_record_by_id(LOC_UNIT, $unit_id); |
|
|
|
|
26
|
|
|
if (is_array($unit)) { |
|
|
|
|
27
|
|
|
_SnCacheInternal::unit_linkLocatorToData($unit, $unit_id); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $unit; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function db_unit_by_location($user_id, $location_type, $location_id, $unit_snid = 0) { |
34
|
|
|
// apply time restrictions ???? |
35
|
|
|
SN::db_get_unit_list_by_location($user_id, $location_type, $location_id); |
36
|
|
|
|
37
|
|
|
return |
38
|
|
|
!$unit_snid |
39
|
|
|
? _SnCacheInternal::unit_locatorGetAllFromLocation($location_type, $location_id) |
40
|
|
|
: _SnCacheInternal::unit_locatorGetUnitFromLocation($location_type, $location_id, $unit_snid); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function db_unit_count_by_user_and_type_and_snid($user_id, $unit_type = 0, $unit_snid = 0) { |
44
|
|
|
$query = doquery( |
|
|
|
|
45
|
|
|
"SELECT unit_snid, sum(unit_level) as `qty` FROM {{unit}} WHERE `unit_player_id` = {$user_id} " . |
46
|
|
|
($unit_type ? "AND `unit_type` = {$unit_type} " : '') . |
47
|
|
|
($unit_snid ? "AND `unit_snid` = {$unit_snid} " : '') . |
48
|
|
|
'GROUP BY `unit_snid`' |
49
|
|
|
); |
50
|
|
|
$result = array(); |
51
|
|
|
while ($row = db_fetch($query)) { |
52
|
|
|
$result[$row['unit_snid']] = $row; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $result; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Used by UNIT_CAPTAIN module TODO |
59
|
|
|
public static function db_unit_in_fleet_by_user($user_id, $location_id, $unit_snid, $for_update) { |
60
|
|
|
return doquery( |
|
|
|
|
61
|
|
|
"SELECT * |
62
|
|
|
FROM {{fleets}} AS f |
63
|
|
|
JOIN {{unit}} AS u ON u.`unit_location_id` = f.fleet_id |
64
|
|
|
WHERE |
65
|
|
|
f.fleet_owner = {$user_id} AND |
66
|
|
|
(f.fleet_start_planet_id = {$location_id} OR f.fleet_end_planet_id = {$location_id}) |
67
|
|
|
AND u.unit_snid = {$unit_snid} AND u.`unit_location_type` = " . LOC_FLEET . " AND " . self::db_unit_time_restrictions() . |
68
|
|
|
" LIMIT 1" . |
69
|
|
|
($for_update ? ' FOR UPDATE' : '') |
70
|
|
|
, true); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
public static function db_unit_list_laboratories($user_id) { |
75
|
|
|
return doquery("SELECT DISTINCT unit_location_id AS `id` |
|
|
|
|
76
|
|
|
FROM {{unit}} |
77
|
|
|
WHERE unit_player_id = {$user_id} AND unit_location_type = " . LOC_PLANET . " AND unit_level > 0 AND unit_snid IN (" . STRUC_LABORATORY . ", " . STRUC_LABORATORY_NANO . ");"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function db_unit_set_by_id($unit_record_id, $set) { |
81
|
|
|
return SN::db_upd_record_by_id(LOC_UNIT, $unit_record_id, $set); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $set |
86
|
|
|
* |
87
|
|
|
* @return array|bool|false|mysqli_result|null |
88
|
|
|
*/ |
89
|
|
|
public static function db_unit_set_insert($set) { |
90
|
|
|
return SN::db_ins_record(LOC_UNIT, $set); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function db_unit_list_delete($user_id, $unit_location_type, $unit_location_id = 0, $unit_snid = 0) { |
94
|
|
|
return SN::db_del_record_list(LOC_UNIT, |
95
|
|
|
"`unit_location_type` = {$unit_location_type}" . |
96
|
|
|
($unit_location_id = idval($unit_location_id) ? " AND `unit_location_id` = {$unit_location_id}" : '') . |
|
|
|
|
97
|
|
|
($user_id = idval($user_id) ? " AND `unit_player_id` = {$user_id}" : '') . |
|
|
|
|
98
|
|
|
($unit_snid = idval($unit_snid) ? " AND `unit_snid` = {$unit_snid}" : '')); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function db_unit_list_stat_calculate() { |
102
|
|
|
return doquery( |
|
|
|
|
103
|
|
|
"SELECT unit_player_id, unit_type, unit_snid, unit_level, count(*) AS unit_amount |
104
|
|
|
FROM `{{unit}}` |
105
|
|
|
WHERE unit_level > 0 AND " . self::db_unit_time_restrictions() . |
106
|
|
|
" GROUP BY unit_player_id, unit_type, unit_snid, unit_level" |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
public static function db_unit_change_owner($location_type, $location_id, $new_owner_id) { |
112
|
|
|
doquery("UPDATE {{unit}} SET `unit_player_id` = {$new_owner_id} WHERE `unit_location_type` = {$location_type} AND `unit_location_id` = {$location_id}"); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
public static function db_unit_list_admin_delete_mercenaries_finished() { |
117
|
|
|
/** @noinspection SqlWithoutWhere */ |
118
|
|
|
return doquery("DELETE FROM {{unit}} WHERE unit_time_finish IS NOT NULL AND unit_time_finish < FROM_UNIXTIME(" . SN_TIME_NOW . ") AND unit_type = " . UNIT_MERCENARIES); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function db_unit_list_admin_set_mercenaries_expire_time($default_length) { |
122
|
|
|
return doquery( |
|
|
|
|
123
|
|
|
"UPDATE {{unit}} |
124
|
|
|
SET |
125
|
|
|
unit_time_start = FROM_UNIXTIME(" . SN_TIME_NOW . "), |
126
|
|
|
unit_time_finish = FROM_UNIXTIME(" . (SN_TIME_NOW + $default_length) . ") |
127
|
|
|
WHERE unit_type = " . UNIT_MERCENARIES |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Adjust unit amount |
133
|
|
|
* |
134
|
|
|
* @param int $playerId |
135
|
|
|
* @param int $planetId |
136
|
|
|
* @param int $unitSnId |
137
|
|
|
* @param float $amount |
138
|
|
|
* @param int $reason |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
* |
142
|
|
|
* @throws Exception |
143
|
|
|
*/ |
144
|
|
|
public static function dbChangeUnit($playerId, $planetId, $unitSnId, $amount, $reason = RPG_NONE) { |
145
|
|
|
$result = false; |
146
|
|
|
|
147
|
|
|
// TODO - Lock user |
148
|
|
|
$userArray = db_user_by_id($playerId); |
|
|
|
|
149
|
|
|
|
150
|
|
|
if ($unitSnId == RES_DARK_MATTER) { |
151
|
|
|
// Add dark matter to user |
152
|
|
|
$result = boolval(rpg_points_change($playerId, $reason, $amount)); |
153
|
|
|
} elseif (in_array($unitSnId, sn_get_groups(UNIT_RESOURCES_STR_LOOT))) { |
154
|
|
|
// Add resources to user's capital |
155
|
|
|
if ($userArray['user_as_ally'] == 1) { |
156
|
|
|
// TODO - If ally - adding resources to user record |
157
|
|
|
} else { |
158
|
|
|
// Adding resources to planet |
159
|
|
|
$planet = new Planet(); |
160
|
|
|
$planet->dbLoadRecord($planetId); |
161
|
|
|
$planet->changeResource($unitSnId, $amount); |
162
|
|
|
$planet->save(); |
163
|
|
|
|
164
|
|
|
$result = true; |
165
|
|
|
} |
166
|
|
|
} elseif (in_array($unitSnId, sn_get_groups(UNIT_ARTIFACTS_STR))) { |
167
|
|
|
// Add artifacts to player |
168
|
|
|
$result = self::dbAdd($playerId, 0, $unitSnId, $amount); |
169
|
|
|
} elseif (!empty($planetId) && in_array($unitSnId, sn_get_groups([UNIT_SHIPS_STR, UNIT_DEFENCE_STR,]))) { |
170
|
|
|
// Add fleet or defense to user's capital |
171
|
|
|
$result = self::dbAdd($playerId, $planetId, $unitSnId, $amount); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $result; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Add unit to player/planet |
180
|
|
|
* |
181
|
|
|
* Supports units. DOES NOT support resources |
182
|
|
|
* |
183
|
|
|
* DOES NOT autodetect location |
184
|
|
|
* |
185
|
|
|
* @param int $playerId |
186
|
|
|
* @param int $planetId 0 - for player units |
187
|
|
|
* @param int $unitSnId |
188
|
|
|
* @param float $amount |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
protected static function dbAdd($playerId, $planetId, $unitSnId, $amount) { |
193
|
|
|
if (!in_array($unitSnId, sn_get_groups([UNIT_SHIPS_STR, UNIT_DEFENCE_STR, UNIT_ARTIFACTS_STR,]))) { |
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ($planetId == 0) { |
198
|
|
|
$locationType = LOC_USER; |
199
|
|
|
$locationId = $playerId; |
200
|
|
|
} else { |
201
|
|
|
$locationType = LOC_PLANET; |
202
|
|
|
$locationId = $planetId; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$fields = [ |
206
|
|
|
'unit_player_id' => $playerId, |
207
|
|
|
'unit_location_type' => $locationType, |
208
|
|
|
'unit_location_id' => $locationId, |
209
|
|
|
'unit_snid' => $unitSnId, |
210
|
|
|
]; |
211
|
|
|
if (!($unitRecord = RecordUnit::findFirst($fields))) { |
212
|
|
|
if ($amount < 0) { |
213
|
|
|
return false; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// New unit |
217
|
|
|
// $unitRecord = RecordUnit::build([ |
218
|
|
|
// 'unit_player_id' => $playerId, |
219
|
|
|
// 'unit_location_type' => $locationType, |
220
|
|
|
// 'unit_location_id' => $locationId, |
221
|
|
|
// 'unit_snid' => $unitSnId, |
222
|
|
|
// 'unit_type' => get_unit_param($unitSnId, P_UNIT_TYPE), |
223
|
|
|
// 'unit_level' => $level, |
224
|
|
|
// ]); |
225
|
|
|
|
226
|
|
|
$fields += [ |
227
|
|
|
'unit_type' => get_unit_param($unitSnId, P_UNIT_TYPE), |
228
|
|
|
'unit_level' => $amount, |
229
|
|
|
]; |
230
|
|
|
$unitRecord = RecordUnit::build($fields); |
231
|
|
|
|
232
|
|
|
return $unitRecord->insert(); |
233
|
|
|
} else { |
234
|
|
|
if ($unitRecord->unit_level + $amount < 0) { |
235
|
|
|
// TODO - Log error or throw Exception |
236
|
|
|
return false; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$unitRecord->inc()->unit_level = $amount; |
240
|
|
|
|
241
|
|
|
return $unitRecord->update(); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public static function dbUserAdd($playerId, $unitSnId, $amount) { |
246
|
|
|
// if (!($unitRecord = RecordUnit::findFirst([ |
247
|
|
|
// 'unit_player_id' => $playerId, |
248
|
|
|
// 'unit_location_type' => LOC_USER, |
249
|
|
|
// 'unit_location_id' => $playerId, |
250
|
|
|
// 'unit_snid' => $unitSnId, |
251
|
|
|
// ]))) { |
252
|
|
|
// if ($level < 0) { |
253
|
|
|
// return false; |
254
|
|
|
// } |
255
|
|
|
// |
256
|
|
|
// // New unit |
257
|
|
|
// $unitRecord = RecordUnit::build([ |
258
|
|
|
// 'unit_player_id' => $playerId, |
259
|
|
|
// 'unit_location_type' => LOC_USER, |
260
|
|
|
// 'unit_location_id' => $playerId, |
261
|
|
|
// 'unit_type' => get_unit_param($unitSnId, P_UNIT_TYPE), |
262
|
|
|
// 'unit_snid' => $unitSnId, |
263
|
|
|
// 'unit_level' => $level, |
264
|
|
|
// ]); |
265
|
|
|
// |
266
|
|
|
//// var_dump($unitRecord);die(); |
267
|
|
|
// |
268
|
|
|
// return $unitRecord->insert(); |
269
|
|
|
// } else { |
270
|
|
|
// if ($unitRecord->unit_level + $level < 0) { |
271
|
|
|
// // TODO - Log error or throw Exception |
272
|
|
|
// return false; |
273
|
|
|
// } |
274
|
|
|
// |
275
|
|
|
// $unitRecord->inc()->unit_level = $level; |
276
|
|
|
// |
277
|
|
|
// return $unitRecord->update(); |
278
|
|
|
// } |
279
|
|
|
return self::dbAdd($playerId, 0, $unitSnId, $amount); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
} |
283
|
|
|
|