|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DBStatic; |
|
4
|
|
|
|
|
5
|
|
|
use classSupernova; |
|
6
|
|
|
use Common\V2Location; |
|
7
|
|
|
use mysqli_result; |
|
8
|
|
|
|
|
9
|
|
|
class DBStaticUnit { |
|
10
|
|
|
|
|
11
|
|
|
public static function db_unit_time_restrictions($date = SN_TIME_NOW) { |
|
12
|
|
|
$date = is_numeric($date) ? "FROM_UNIXTIME({$date})" : "'{$date}'"; |
|
13
|
|
|
|
|
14
|
|
|
return |
|
15
|
|
|
"(unit_time_start IS NULL OR unit_time_start <= {$date}) AND |
|
16
|
|
|
(unit_time_finish IS NULL OR unit_time_finish = '1970-01-01 03:00:00' OR unit_time_finish >= {$date})"; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
public static function getUnitListByV2Location(V2Location $v2Location) { |
|
21
|
|
|
return static::db_get_unit_by_location($v2Location->getLocationPlayerId(), $v2Location->getLocationType(), $v2Location->getLocationId()); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param int $user_id |
|
26
|
|
|
* @param int $location_type |
|
27
|
|
|
* @param int $location_id |
|
28
|
|
|
* |
|
29
|
|
|
* @return array|bool |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function db_get_unit_list_by_location($user_id = 0, $location_type, $location_id) { |
|
|
|
|
|
|
32
|
|
|
if (!($location_type = idval($location_type)) || !($location_id = idval($location_id))) { |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (classSupernova::$gc->snCache->isUnitLocatorNotSet($location_type, $location_id)) { |
|
|
|
|
|
|
37
|
|
|
$got_data = classSupernova::$gc->cacheOperator->db_get_record_list(LOC_UNIT, "unit_location_type = {$location_type} AND unit_location_id = {$location_id} AND " . DBStaticUnit::db_unit_time_restrictions()); |
|
|
|
|
|
|
38
|
|
|
if (!empty($got_data) && is_array($got_data)) { |
|
39
|
|
|
foreach ($got_data as $unit_id => $unit_data) { |
|
40
|
|
|
classSupernova::$gc->snCache->setUnitLocatorByLocationAndIDs($location_type, $location_id, $unit_data); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$result = false; |
|
46
|
|
|
foreach (classSupernova::$gc->snCache->getUnitLocatorByFullLocation($location_type, $location_id) as $key => $value) { |
|
|
|
|
|
|
47
|
|
|
$result[$key] = $value; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $result; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param int $user_id |
|
56
|
|
|
* @param $location_type |
|
57
|
|
|
* @param $location_id |
|
58
|
|
|
* @param int $unit_snid |
|
59
|
|
|
* @param bool $for_update |
|
60
|
|
|
* @param string $fields |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function db_get_unit_by_location($user_id = 0, $location_type, $location_id, $unit_snid = 0, $for_update = false, $fields = '*') { |
|
|
|
|
|
|
65
|
|
|
DBStaticUnit::db_get_unit_list_by_location($user_id, $location_type, $location_id); |
|
66
|
|
|
|
|
67
|
|
|
return classSupernova::$gc->snCache->getUnitLocator($location_type, $location_id, $unit_snid); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public static function db_unit_count_by_user_and_type_and_snid($user_id, $unit_type = 0, $unit_snid = 0) { |
|
71
|
|
|
$query = classSupernova::$db->doSelect( |
|
72
|
|
|
"SELECT unit_snid, sum(unit_level) as `qty` FROM {{unit}} WHERE `unit_player_id` = {$user_id} " . |
|
73
|
|
|
($unit_type ? "AND `unit_type` = {$unit_type} " : '') . |
|
74
|
|
|
($unit_snid ? "AND `unit_snid` = {$unit_snid} " : '') . |
|
75
|
|
|
'GROUP BY `unit_snid`' |
|
76
|
|
|
); |
|
77
|
|
|
$result = array(); |
|
78
|
|
|
while ($row = db_fetch($query)) { |
|
79
|
|
|
$result[$row['unit_snid']] = $row; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// Used by UNIT_CAPTAIN module TODO |
|
86
|
|
|
public static function db_unit_in_fleet_by_user($user_id, $location_id, $unit_snid, $for_update) { |
|
87
|
|
|
return classSupernova::$db->doSelectFetch( |
|
88
|
|
|
"SELECT * |
|
89
|
|
|
FROM {{fleets}} AS f |
|
90
|
|
|
JOIN {{unit}} AS u ON u.`unit_location_id` = f.fleet_id |
|
91
|
|
|
WHERE |
|
92
|
|
|
f.fleet_owner = {$user_id} AND |
|
93
|
|
|
(f.fleet_start_planet_id = {$location_id} OR f.fleet_end_planet_id = {$location_id}) |
|
94
|
|
|
AND u.unit_snid = {$unit_snid} AND u.`unit_location_type` = " . LOC_FLEET . " AND " . self::db_unit_time_restrictions() . |
|
95
|
|
|
" LIMIT 1" . |
|
96
|
|
|
($for_update ? ' FOR UPDATE' : '')); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
public static function db_unit_list_laboratories($user_id) { |
|
101
|
|
|
return classSupernova::$db->doSelect("SELECT DISTINCT unit_location_id AS `id` |
|
102
|
|
|
FROM {{unit}} |
|
103
|
|
|
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 . ");"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param $unit_id |
|
108
|
|
|
* @param array $set - SET fields |
|
109
|
|
|
* @param array $adjust - ADJUST fields |
|
110
|
|
|
* |
|
111
|
|
|
* @return array|bool|mysqli_result|null |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function db_unit_set_by_id($unit_id, $set, $adjust) { |
|
114
|
|
|
return classSupernova::$gc->cacheOperator->db_upd_record_by_id( |
|
|
|
|
|
|
115
|
|
|
LOC_UNIT, |
|
116
|
|
|
$unit_id, |
|
117
|
|
|
$set, |
|
118
|
|
|
$adjust |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param array $set |
|
124
|
|
|
* |
|
125
|
|
|
* @return array|bool|false|mysqli_result|null |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function db_unit_set_insert($set) { |
|
128
|
|
|
return classSupernova::$gc->cacheOperator->db_ins_record(LOC_UNIT, $set); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public static function db_unit_list_delete($user_id = 0, $unit_location_type, $unit_location_id = 0, $unit_snid = 0) { |
|
132
|
|
|
$where = array('unit_location_type' => $unit_location_type); |
|
133
|
|
|
($unit_location_id = idval($unit_location_id)) ? $where['unit_location_id'] = $unit_location_id : false; |
|
134
|
|
|
($user_id = idval($user_id)) ? $where['unit_player_id'] = $user_id : false; |
|
135
|
|
|
($unit_snid = idval($unit_snid)) ? $where['unit_snid'] = $unit_snid : false; |
|
136
|
|
|
|
|
137
|
|
|
return classSupernova::$gc->cacheOperator->db_del_record_list(LOC_UNIT, $where); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public static function db_unit_list_stat_calculate() { |
|
141
|
|
|
return classSupernova::$db->doSelect( |
|
142
|
|
|
"SELECT unit_player_id, unit_type, unit_snid, unit_level, count(*) AS unit_amount |
|
143
|
|
|
FROM `{{unit}}` |
|
144
|
|
|
WHERE unit_level > 0 AND " . self::db_unit_time_restrictions() . |
|
145
|
|
|
" GROUP BY unit_player_id, unit_type, unit_snid, unit_level" |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
public static function db_unit_change_owner($location_type, $location_id, $new_owner_id) { |
|
151
|
|
|
classSupernova::$db->doUpdateTableSet( |
|
152
|
|
|
TABLE_UNIT, |
|
153
|
|
|
array( |
|
154
|
|
|
'unit_player_id' => $new_owner_id, |
|
155
|
|
|
), |
|
156
|
|
|
array( |
|
157
|
|
|
'unit_location_type' => $location_type, |
|
158
|
|
|
'unit_location_id' => $location_id, |
|
159
|
|
|
) |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
public static function db_unit_list_admin_delete_mercenaries_finished() { |
|
165
|
|
|
classSupernova::$db->doDeleteDanger( |
|
|
|
|
|
|
166
|
|
|
TABLE_UNIT, |
|
167
|
|
|
array( |
|
168
|
|
|
'unit_type' => UNIT_MERCENARIES, |
|
169
|
|
|
), |
|
170
|
|
|
array( |
|
171
|
|
|
'unit_time_finish IS NOT NULL', |
|
172
|
|
|
"unit_time_finish < FROM_UNIXTIME(" . SN_TIME_NOW . ")", |
|
173
|
|
|
) |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public static function db_unit_list_admin_set_mercenaries_expire_time($default_length) { |
|
178
|
|
|
return |
|
179
|
|
|
classSupernova::$db->doUpdateTableSet( |
|
180
|
|
|
TABLE_UNIT, |
|
181
|
|
|
array( |
|
182
|
|
|
'unit_time_start' => date(FMT_DATE_TIME_SQL, SN_TIME_NOW), |
|
183
|
|
|
'unit_time_finish' => date(FMT_DATE_TIME_SQL, SN_TIME_NOW + $default_length), |
|
184
|
|
|
), |
|
185
|
|
|
array( |
|
186
|
|
|
'unit_type' => UNIT_MERCENARIES, |
|
187
|
|
|
) |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param $unit_id |
|
194
|
|
|
* @param $unit_value |
|
195
|
|
|
* @param $user |
|
196
|
|
|
* @param null|array|mixed $planet_id |
|
197
|
|
|
* |
|
198
|
|
|
* @return bool |
|
199
|
|
|
*/ |
|
200
|
|
|
public static function dbUpdateOrInsertUnit($unit_id, $unit_value, $user, $planet_id = null) { |
|
201
|
|
|
DBStaticUser::validateUserRecord($user); |
|
202
|
|
|
|
|
203
|
|
|
$planet_id = !empty($planet_id['id']) ? $planet_id['id'] : $planet_id; |
|
204
|
|
|
|
|
205
|
|
|
$unit_location = sys_get_unit_location($user, array(), $unit_id); |
|
206
|
|
|
$location_id = $unit_location == LOC_USER ? $user['id'] : $planet_id; |
|
207
|
|
|
$location_id = $location_id ? $location_id : 'NULL'; |
|
208
|
|
|
|
|
209
|
|
|
$temp = DBStaticUnit::db_get_unit_by_location($user['id'], $unit_location, $location_id, $unit_id, true, 'unit_id'); |
|
210
|
|
|
if (!empty($temp['unit_id'])) { |
|
211
|
|
|
$result = (bool)classSupernova::$gc->cacheOperator->db_upd_record_by_id( |
|
|
|
|
|
|
212
|
|
|
LOC_UNIT, |
|
213
|
|
|
$temp['unit_id'], |
|
214
|
|
|
array(), |
|
215
|
|
|
array( |
|
216
|
|
|
'unit_level' => +$unit_value |
|
217
|
|
|
) |
|
218
|
|
|
); |
|
219
|
|
|
} else { |
|
220
|
|
|
$locationIdRendered = $unit_location == LOC_USER ? $user['id'] : $planet_id; |
|
221
|
|
|
$unitType = get_unit_param($unit_id, P_UNIT_TYPE); |
|
222
|
|
|
$result = (bool)classSupernova::$gc->cacheOperator->db_ins_record( |
|
|
|
|
|
|
223
|
|
|
LOC_UNIT, |
|
224
|
|
|
array( |
|
225
|
|
|
'unit_player_id' => $user['id'], |
|
226
|
|
|
'unit_location_type' => (int)$unit_location, |
|
227
|
|
|
'unit_location_id' => $locationIdRendered, |
|
228
|
|
|
'unit_type' => (int)$unitType, |
|
229
|
|
|
'unit_snid' => (int)$unit_id, |
|
230
|
|
|
'unit_level' => (float)$unit_value, |
|
231
|
|
|
) |
|
232
|
|
|
); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return $result; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.