|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class DBStaticUser |
|
5
|
|
|
*/ |
|
6
|
|
|
class DBStaticUser extends DBStaticRecord { |
|
7
|
|
|
|
|
8
|
|
|
public static $_table = 'users'; |
|
9
|
|
|
public static $_idField = 'id'; |
|
10
|
|
|
|
|
11
|
|
|
protected static function whereNotAlly() { |
|
12
|
|
|
|
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
// TODO - это вообще-то надо хранить в конфигурации |
|
16
|
|
|
/** |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function getLastRegisteredUserName() { |
|
20
|
|
|
$query = |
|
21
|
|
|
static::buildDBQ() |
|
22
|
|
|
->field('username') |
|
23
|
|
|
->where('`user_as_ally` IS NULL') |
|
|
|
|
|
|
24
|
|
|
->orderBy(array('`id` DESC')); |
|
25
|
|
|
|
|
26
|
|
|
return (string)$query->selectValue(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return DbResultIterator |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function db_player_list_export_blitz_info() { |
|
33
|
|
|
return |
|
34
|
|
|
static::buildDBQ() |
|
35
|
|
|
->fields(array('id', 'username', 'total_rank', 'total_points', 'onlinetime',)) |
|
36
|
|
|
->where('`user_as_ally` IS NULL') |
|
|
|
|
|
|
37
|
|
|
->orderBy(array('`id`')) |
|
38
|
|
|
->selectIterator(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return DbResultIterator |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function db_user_list_non_bots() { |
|
45
|
|
|
// $query = doquery("SELECT `id` FROM {{users}} WHERE `user_as_ally` IS NULL AND `user_bot` = " . USER_BOT_PLAYER . " FOR UPDATE;"); |
|
46
|
|
|
|
|
47
|
|
|
$query = |
|
48
|
|
|
static::buildDBQ() |
|
49
|
|
|
->field('id') |
|
50
|
|
|
->where("`user_as_ally` IS NULL") |
|
|
|
|
|
|
51
|
|
|
->where("`user_bot` = " . USER_BOT_PLAYER) |
|
|
|
|
|
|
52
|
|
|
->setForUpdate(); |
|
53
|
|
|
|
|
54
|
|
|
return $query->selectIterator(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function db_user_lock_with_target_owner_and_acs($user, $planet = array()) { |
|
58
|
|
|
$query = "SELECT 1 FROM `{{users}}` WHERE `id` = " . idval($user['id']) . |
|
59
|
|
|
(!empty($planet['id_owner']) ? ' OR `id` = ' . idval($planet['id_owner']) : '') |
|
60
|
|
|
. " FOR UPDATE"; |
|
61
|
|
|
|
|
62
|
|
|
static::getDb()->doSelect($query); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param bool $online |
|
67
|
|
|
* |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function db_user_count($online = false) { |
|
71
|
|
|
return intval(static::getDb()->doSelectFetchValue( |
|
72
|
|
|
"SELECT COUNT(`id`) AS `user_count` |
|
73
|
|
|
FROM `{{users}}` |
|
74
|
|
|
WHERE |
|
75
|
|
|
`user_as_ally` IS NULL" . |
|
76
|
|
|
($online ? ' AND `onlinetime` > ' . (SN_TIME_NOW - classSupernova::$config->game_users_online_timeout) : '') |
|
|
|
|
|
|
77
|
|
|
)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public static function db_user_list_admin_sorted($sort, $online = false) { |
|
81
|
|
|
// $query = "SELECT |
|
|
|
|
|
|
82
|
|
|
// u.*, COUNT(r.id) AS referral_count, SUM(r.dark_matter) AS referral_dm |
|
83
|
|
|
// FROM |
|
84
|
|
|
// {{users}} as u |
|
85
|
|
|
// LEFT JOIN |
|
86
|
|
|
// {{referrals}} as r on r.id_partner = u.id |
|
87
|
|
|
// WHERE " . |
|
88
|
|
|
// ($online ? "`onlinetime` >= :onlineTime" : 'user_as_ally IS NULL') . |
|
89
|
|
|
// " GROUP BY u.id |
|
90
|
|
|
// ORDER BY user_as_ally, {$sort} ASC"; |
|
91
|
|
|
|
|
92
|
|
|
$query = static::buildDBQ() |
|
93
|
|
|
->setAlias('u') |
|
94
|
|
|
->field('u.*') |
|
95
|
|
|
->fieldCount('r.id', 'referral_count') |
|
96
|
|
|
->fieldSingleFunction('sum', 'r.dark_matter', 'referral_dm') |
|
97
|
|
|
->join('LEFT JOIN {{referrals}} as r on r.id_partner = u.id') |
|
98
|
|
|
->where($online ? "`onlinetime` >= " . intval(SN_TIME_NOW - classSupernova::$config->game_users_online_timeout) : 'user_as_ally IS NULL') |
|
|
|
|
|
|
99
|
|
|
->groupBy('u.id') |
|
|
|
|
|
|
100
|
|
|
->orderBy("user_as_ally, {$sort} ASC"); |
|
101
|
|
|
|
|
102
|
|
|
$result = $query->selectIterator(); |
|
103
|
|
|
|
|
104
|
|
|
return $result; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public static function db_user_list_to_celebrate($config_user_birthday_range) { |
|
108
|
|
|
$query = static::buildDBQ() |
|
109
|
|
|
->field('id', 'username', 'user_birthday', 'user_birthday_celebrated') |
|
110
|
|
|
->fieldLiteral('CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, \'-%m-%d\')) AS `current_birthday`') |
|
111
|
|
|
->fieldLiteral('DATEDIFF(CURRENT_DATE, CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, \'-%m-%d\'))) AS `days_after_birthday`') |
|
112
|
|
|
->where('`user_birthday` IS NOT NULL') |
|
|
|
|
|
|
113
|
|
|
->where('(`user_birthday_celebrated` IS NULL OR DATE_ADD(`user_birthday_celebrated`, INTERVAL 1 YEAR) < CURRENT_DATE)') |
|
|
|
|
|
|
114
|
|
|
->where('`user_as_ally` IS NULL') |
|
|
|
|
|
|
115
|
|
|
->having('`days_after_birthday` >= 0') |
|
116
|
|
|
->having('`days_after_birthday` < ' . intval($config_user_birthday_range)) |
|
117
|
|
|
->setForUpdate(); |
|
118
|
|
|
|
|
119
|
|
|
$result = $query->selectIterator(); |
|
120
|
|
|
// |
|
|
|
|
|
|
121
|
|
|
// $query = "SELECT |
|
122
|
|
|
// `id`, `username`, `user_birthday`, `user_birthday_celebrated`, |
|
123
|
|
|
// CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, '-%m-%d')) AS `current_birthday`, |
|
124
|
|
|
// DATEDIFF(CURRENT_DATE, CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, '-%m-%d'))) AS `days_after_birthday` |
|
125
|
|
|
// FROM |
|
126
|
|
|
// `{{users}}` |
|
127
|
|
|
// WHERE |
|
128
|
|
|
// `user_birthday` IS NOT NULL |
|
129
|
|
|
// AND `user_as_ally` IS NULL |
|
130
|
|
|
// AND (`user_birthday_celebrated` IS NULL OR DATE_ADD(`user_birthday_celebrated`, INTERVAL 1 YEAR) < CURRENT_DATE) |
|
131
|
|
|
// HAVING |
|
132
|
|
|
// `days_after_birthday` >= 0 AND `days_after_birthday` < {$config_user_birthday_range} FOR UPDATE"; |
|
133
|
|
|
// |
|
134
|
|
|
// $result = static::$dbStatic->doQueryIterator($query); |
|
135
|
|
|
|
|
136
|
|
|
return $result; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return DbEmptyIterator|DbMysqliResultIterator |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function db_user_list_admin_multiaccounts() { |
|
143
|
|
|
$query = "SELECT COUNT(*) AS `ip_count`, `user_lastip` |
|
144
|
|
|
FROM `{{users}}` |
|
145
|
|
|
WHERE `user_as_ally` IS NULL |
|
146
|
|
|
GROUP BY `user_lastip` |
|
147
|
|
|
HAVING COUNT(*) > 1"; |
|
148
|
|
|
|
|
149
|
|
|
return static::getDb()->doSelectIterator($query); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public static function db_player_list_blitz_delete_players() { |
|
153
|
|
|
classSupernova::$db->doDeleteDanger( |
|
|
|
|
|
|
154
|
|
|
TABLE_USERS, |
|
155
|
|
|
array(), |
|
156
|
|
|
array( |
|
157
|
|
|
"`username` LIKE 'Игрок%'" |
|
158
|
|
|
) |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @deprecated - NEVER change DM amount directly w/o logging! |
|
164
|
|
|
*/ |
|
165
|
|
|
public static function db_player_list_blitz_set_50k_dm() { |
|
166
|
|
|
classSupernova::$db->doUpdateTableSet(TABLE_USERS, |
|
167
|
|
|
array( |
|
168
|
|
|
'dark_matter' => 50000, |
|
169
|
|
|
'dark_matter_total' => 50000, |
|
170
|
|
|
) |
|
171
|
|
|
); |
|
172
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Выбирает записи игроков по списку их ID |
|
178
|
|
|
* |
|
179
|
|
|
* @param $user_id_list |
|
180
|
|
|
* |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
public static function db_user_list_by_id($user_id_list) { |
|
184
|
|
|
!is_array($user_id_list) ? $user_id_list = array($user_id_list) : false; |
|
185
|
|
|
|
|
186
|
|
|
$user_list = array(); |
|
187
|
|
|
foreach ($user_id_list as $user_id_unsafe) { |
|
188
|
|
|
$user = DBStaticUser::db_user_by_id($user_id_unsafe); |
|
189
|
|
|
!empty($user) ? $user_list[$user_id_unsafe] = $user : false; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $user_list; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
public static function db_user_by_username($username_unsafe, $for_update = false, $fields = '*', $player = null, $like = false) { |
|
|
|
|
|
|
197
|
|
|
// TODO Проверить, кстати - а везде ли нужно выбирать юзеров или где-то все-таки ищутся Альянсы ? |
|
198
|
|
|
if (!($username_unsafe = trim($username_unsafe))) { |
|
199
|
|
|
return false; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$user = null; |
|
203
|
|
|
if (classSupernova::$gc->snCache->isArrayLocation(LOC_USER)) { |
|
|
|
|
|
|
204
|
|
|
foreach (classSupernova::$gc->snCache->getData(LOC_USER) as $user_id => $user_data) { |
|
|
|
|
|
|
205
|
|
|
if (is_array($user_data) && isset($user_data['username'])) { |
|
206
|
|
|
// проверяем поле |
|
207
|
|
|
// TODO Возможно есть смысл всегда искать по strtolower - но может игрок захочет переименоваться с другим регистром? Проверить! |
|
208
|
|
|
if ((!$like && $user_data['username'] == $username_unsafe) || ($like && strtolower($user_data['username']) == strtolower($username_unsafe))) { |
|
209
|
|
|
// $user_as_ally = intval($user_data['user_as_ally']); |
|
|
|
|
|
|
210
|
|
|
$user_as_ally = idval($user_data['user_as_ally']); |
|
211
|
|
|
if ($player === null || ($player === true && !$user_as_ally) || ($player === false && $user_as_ally)) { |
|
212
|
|
|
$user = $user_data; |
|
213
|
|
|
break; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if ($user === null) { |
|
221
|
|
|
// Вытаскиваем запись |
|
222
|
|
|
$username_safe = db_escape($like ? strtolower($username_unsafe) : $username_unsafe); // тут на самом деле strtolower() лишняя, но пусть будет |
|
223
|
|
|
|
|
224
|
|
|
$user = classSupernova::$db->doSelectFetch( |
|
225
|
|
|
"SELECT * FROM {{users}} WHERE `username` " . ($like ? 'LIKE' : '=') . " '{$username_safe}'" |
|
226
|
|
|
. " FOR UPDATE" |
|
227
|
|
|
); |
|
228
|
|
|
classSupernova::$gc->snCache->cache_set(LOC_USER, $user); // В кэш-юзер так же заполнять индексы |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $user; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public static function db_user_list($user_filter = '', $for_update = false, $fields = '*') { |
|
|
|
|
|
|
235
|
|
|
return classSupernova::$gc->cacheOperator->db_get_record_list(LOC_USER, $user_filter); |
|
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @param $user_id |
|
240
|
|
|
* @param array $set |
|
241
|
|
|
* |
|
242
|
|
|
* @return array|bool|mysqli_result|null |
|
243
|
|
|
*/ |
|
244
|
|
|
public static function db_user_set_by_id($user_id, $set) { |
|
245
|
|
|
return classSupernova::$gc->cacheOperator->db_upd_record_by_id(LOC_USER, $user_id, $set, array()); |
|
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param $user_id |
|
250
|
|
|
* @param array $set |
|
|
|
|
|
|
251
|
|
|
* @param array $adjust |
|
252
|
|
|
* |
|
253
|
|
|
* @return array|bool|mysqli_result|null |
|
254
|
|
|
*/ |
|
255
|
|
|
public static function db_user_adjust_by_id($user_id, $adjust) { |
|
256
|
|
|
return classSupernova::$gc->cacheOperator->db_upd_record_by_id(LOC_USER, $user_id, array(), $adjust); |
|
|
|
|
|
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Возвращает информацию о пользователе по его ID |
|
261
|
|
|
* |
|
262
|
|
|
* @param int|array $user_id_unsafe |
|
263
|
|
|
* <p>int - ID пользователя</p> |
|
264
|
|
|
* <p>array - запись пользователя с установленным полем ['id']</p> |
|
265
|
|
|
* @param bool $for_update @deprecated |
|
266
|
|
|
* @param string $fields @deprecated список полей или '*'/'' для всех полей |
|
267
|
|
|
* @param null $player |
|
268
|
|
|
* @param bool|null $player Признак выбора записи пользователь типа "игрок" |
|
269
|
|
|
* <p>null - Можно выбрать запись любого типа</p> |
|
270
|
|
|
* <p>true - Выбирается только запись типа "игрок"</p> |
|
271
|
|
|
* <p>false - Выбирается только запись типа "альянс"</p> |
|
272
|
|
|
* |
|
273
|
|
|
* @return array|false |
|
274
|
|
|
* <p>false - Нет записи с указанным ID и $player</p> |
|
275
|
|
|
* <p>array - запись типа $user</p> |
|
276
|
|
|
*/ |
|
277
|
|
|
public static function db_user_by_id($user_id_unsafe, $for_update = false, $fields = '*', $player = null) { |
|
278
|
|
|
$user = classSupernova::$gc->cacheOperator->db_get_record_by_id(LOC_USER, $user_id_unsafe, $for_update, $fields); |
|
|
|
|
|
|
279
|
|
|
|
|
280
|
|
|
return (is_array($user) && |
|
281
|
|
|
( |
|
282
|
|
|
$player === null |
|
283
|
|
|
|| |
|
284
|
|
|
($player === true && !$user['user_as_ally']) |
|
285
|
|
|
|| |
|
286
|
|
|
($player === false && $user['user_as_ally']) |
|
287
|
|
|
)) ? $user : false; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* @param $ally_id |
|
293
|
|
|
* @param $ally_rank_id |
|
294
|
|
|
* @param array $set |
|
295
|
|
|
* @param array $adjust |
|
296
|
|
|
*/ |
|
297
|
|
|
public static function db_user_list_set_by_ally_and_rank($ally_id, $ally_rank_id, $set, $adjust) { |
|
298
|
|
|
classSupernova::$gc->cacheOperator->db_upd_record_list_DANGER( |
|
|
|
|
|
|
299
|
|
|
LOC_USER, |
|
300
|
|
|
$set, |
|
301
|
|
|
$adjust, |
|
302
|
|
|
array( |
|
303
|
|
|
'ally_id' => $ally_id, |
|
304
|
|
|
), |
|
305
|
|
|
array( |
|
306
|
|
|
// TODO - DANGER !!! |
|
307
|
|
|
"`ally_rank_id` >= {$ally_rank_id}" |
|
308
|
|
|
) |
|
309
|
|
|
); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @param array $playerArray |
|
314
|
|
|
*/ |
|
315
|
|
|
public static function renderNameAndCoordinates($playerArray) { |
|
316
|
|
|
return "{$playerArray['username']} " . uni_render_coordinates($playerArray); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @param mixed $user |
|
321
|
|
|
*/ |
|
322
|
|
|
public static function validateUserRecord($user) { |
|
323
|
|
|
if (!is_array($user)) { |
|
324
|
|
|
// TODO - remove later |
|
325
|
|
|
print('<h1>СООБЩИТЕ ЭТО АДМИНУ: sn_db_unit_changeset_prepare() - USER is not ARRAY</h1>'); |
|
326
|
|
|
pdump(debug_backtrace()); |
|
327
|
|
|
die('USER is not ARRAY'); |
|
328
|
|
|
} |
|
329
|
|
|
if (!isset($user['id']) || !$user['id']) { |
|
330
|
|
|
// TODO - remove later |
|
331
|
|
|
print('<h1>СООБЩИТЕ ЭТО АДМИНУ: sn_db_unit_changeset_prepare() - USER[id] пустой</h1>'); |
|
332
|
|
|
pdump($user); |
|
333
|
|
|
pdump(debug_backtrace()); |
|
334
|
|
|
die('USER[id] пустой'); |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* @param array $playerRowFieldChanges - array of $resourceId => $amount |
|
340
|
|
|
* @param int $userId |
|
341
|
|
|
* |
|
342
|
|
|
* @see DBStaticPlanet::db_planet_update_resources |
|
343
|
|
|
* // TODO - DEDUPLICATE |
|
344
|
|
|
*/ |
|
345
|
|
View Code Duplication |
public static function db_user_update_resources($playerRowFieldChanges, $userId) { |
|
|
|
|
|
|
346
|
|
|
$fields = array(); |
|
347
|
|
|
foreach ($playerRowFieldChanges as $resourceId => $value) { |
|
348
|
|
|
$fields[pname_resource_name($resourceId)] = $value; |
|
349
|
|
|
} |
|
350
|
|
|
if(!empty($fields)) { |
|
351
|
|
|
classSupernova::$gc->db->doUpdateRowAdjust( |
|
|
|
|
|
|
352
|
|
|
TABLE_USERS, |
|
353
|
|
|
array(), |
|
354
|
|
|
$fields, |
|
355
|
|
|
array( |
|
356
|
|
|
'id' => $userId |
|
357
|
|
|
) |
|
358
|
|
|
); |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
} |
|
363
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: