1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Unit; |
4
|
|
|
use _SnCacheInternal; |
5
|
|
|
use mysqli_result; |
6
|
|
|
use SN; |
7
|
|
|
|
8
|
|
|
class DBStaticUnit { |
9
|
|
|
|
10
|
|
|
public static function db_unit_time_restrictions($date = SN_TIME_NOW) { |
11
|
|
|
$date = is_numeric($date) ? "FROM_UNIXTIME({$date})" : "'{$date}'"; |
12
|
|
|
|
13
|
|
|
return |
14
|
|
|
"(unit_time_start IS NULL OR unit_time_start <= {$date}) AND |
15
|
|
|
(unit_time_finish IS NULL OR unit_time_finish = '1970-01-01 03:00:00' OR unit_time_finish >= {$date})"; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public static function db_unit_by_id($unit_id) { |
19
|
|
|
$unit = SN::db_get_record_by_id(LOC_UNIT, $unit_id); |
|
|
|
|
20
|
|
|
if (is_array($unit)) { |
|
|
|
|
21
|
|
|
_SnCacheInternal::unit_linkLocatorToData($unit, $unit_id); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $unit; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function db_unit_by_location($user_id = 0, $location_type, $location_id, $unit_snid = 0) { |
28
|
|
|
// apply time restrictions ???? |
|
|
|
|
29
|
|
|
SN::db_get_unit_list_by_location($user_id, $location_type, $location_id); |
30
|
|
|
|
31
|
|
|
return |
32
|
|
|
!$unit_snid |
33
|
|
|
? _SnCacheInternal::unit_locatorGetAllFromLocation($location_type, $location_id) |
34
|
|
|
: _SnCacheInternal::unit_locatorGetUnitFromLocation($location_type, $location_id, $unit_snid); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function db_unit_count_by_user_and_type_and_snid($user_id, $unit_type = 0, $unit_snid = 0) { |
38
|
|
|
$query = doquery( |
39
|
|
|
"SELECT unit_snid, sum(unit_level) as `qty` FROM {{unit}} WHERE `unit_player_id` = {$user_id} " . |
40
|
|
|
($unit_type ? "AND `unit_type` = {$unit_type} " : '') . |
41
|
|
|
($unit_snid ? "AND `unit_snid` = {$unit_snid} " : '') . |
42
|
|
|
'GROUP BY `unit_snid`' |
43
|
|
|
); |
44
|
|
|
$result = array(); |
45
|
|
|
while ($row = db_fetch($query)) { |
46
|
|
|
$result[$row['unit_snid']] = $row; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $result; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Used by UNIT_CAPTAIN module TODO |
53
|
|
|
public static function db_unit_in_fleet_by_user($user_id, $location_id, $unit_snid, $for_update) { |
54
|
|
|
return doquery( |
55
|
|
|
"SELECT * |
56
|
|
|
FROM {{fleets}} AS f |
57
|
|
|
JOIN {{unit}} AS u ON u.`unit_location_id` = f.fleet_id |
58
|
|
|
WHERE |
59
|
|
|
f.fleet_owner = {$user_id} AND |
60
|
|
|
(f.fleet_start_planet_id = {$location_id} OR f.fleet_end_planet_id = {$location_id}) |
61
|
|
|
AND u.unit_snid = {$unit_snid} AND u.`unit_location_type` = " . LOC_FLEET . " AND " . self::db_unit_time_restrictions() . |
62
|
|
|
" LIMIT 1" . |
63
|
|
|
($for_update ? ' FOR UPDATE' : '') |
64
|
|
|
, true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
public static function db_unit_list_laboratories($user_id) { |
69
|
|
|
return doquery("SELECT DISTINCT unit_location_id AS `id` |
70
|
|
|
FROM {{unit}} |
71
|
|
|
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 . ");"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function db_unit_set_by_id($unit_record_id, $set) { |
75
|
|
|
return SN::db_upd_record_by_id(LOC_UNIT, $unit_record_id, $set); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $set |
80
|
|
|
* |
81
|
|
|
* @return array|bool|false|mysqli_result|null |
82
|
|
|
*/ |
83
|
|
|
public static function db_unit_set_insert($set) { |
84
|
|
|
return SN::db_ins_record(LOC_UNIT, $set); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function db_unit_list_delete($user_id = 0, $unit_location_type, $unit_location_id = 0, $unit_snid = 0) { |
88
|
|
|
return SN::db_del_record_list(LOC_UNIT, |
89
|
|
|
"`unit_location_type` = {$unit_location_type}" . |
90
|
|
|
($unit_location_id = idval($unit_location_id) ? " AND `unit_location_id` = {$unit_location_id}" : '') . |
|
|
|
|
91
|
|
|
($user_id = idval($user_id) ? " AND `unit_player_id` = {$user_id}" : '') . |
|
|
|
|
92
|
|
|
($unit_snid = idval($unit_snid) ? " AND `unit_snid` = {$unit_snid}" : '')); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public static function db_unit_list_stat_calculate() { |
96
|
|
|
return doquery( |
97
|
|
|
"SELECT unit_player_id, unit_type, unit_snid, unit_level, count(*) AS unit_amount |
98
|
|
|
FROM `{{unit}}` |
99
|
|
|
WHERE unit_level > 0 AND " . self::db_unit_time_restrictions() . |
100
|
|
|
" GROUP BY unit_player_id, unit_type, unit_snid, unit_level" |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
public static function db_unit_change_owner($location_type, $location_id, $new_owner_id) { |
106
|
|
|
doquery("UPDATE {{unit}} SET `unit_player_id` = {$new_owner_id} WHERE `unit_location_type` = {$location_type} AND `unit_location_id` = {$location_id}"); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
public static function db_unit_list_admin_delete_mercenaries_finished() { |
111
|
|
|
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); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public static function db_unit_list_admin_set_mercenaries_expire_time($default_length) { |
115
|
|
|
return doquery( |
116
|
|
|
"UPDATE {{unit}} |
117
|
|
|
SET |
118
|
|
|
unit_time_start = FROM_UNIXTIME(" . SN_TIME_NOW . "), |
119
|
|
|
unit_time_finish = FROM_UNIXTIME(" . (SN_TIME_NOW + $default_length) . ") |
120
|
|
|
WHERE unit_type = " . UNIT_MERCENARIES |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public static function dbUserAdd($playerId, $unitSnId, $level) { |
125
|
|
|
if (!($unitRecord = \Unit\RecordUnit::findFirst([ |
126
|
|
|
'unit_player_id' => $playerId, |
127
|
|
|
'unit_location_type' => LOC_USER, |
128
|
|
|
'unit_location_id' => $playerId, |
129
|
|
|
'unit_snid' => $unitSnId, |
130
|
|
|
]))) { |
131
|
|
|
if ($level < 0) { |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// New unit |
136
|
|
|
$unitRecord = \Unit\RecordUnit::build([ |
137
|
|
|
'unit_player_id' => $playerId, |
138
|
|
|
'unit_location_type' => LOC_USER, |
139
|
|
|
'unit_location_id' => $playerId, |
140
|
|
|
'unit_type' => get_unit_param($unitSnId, P_UNIT_TYPE), |
141
|
|
|
'unit_snid' => $unitSnId, |
142
|
|
|
'unit_level' => $level, |
143
|
|
|
]); |
144
|
|
|
|
145
|
|
|
// var_dump($unitRecord);die(); |
|
|
|
|
146
|
|
|
|
147
|
|
|
return $unitRecord->insert(); |
148
|
|
|
} else { |
149
|
|
|
if ($unitRecord->unit_level + $level < 0) { |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$unitRecord->inc()->unit_level = $level; |
154
|
|
|
|
155
|
|
|
return $unitRecord->update(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|