forked from Relief156/Minecraft-Server-Status
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
568 lines (475 loc) · 23.1 KB
/
db.php
File metadata and controls
568 lines (475 loc) · 23.1 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
// 数据库连接文件
// 检查是否已安装
if (!file_exists('installed.lock')) {
die('系统尚未安装,请先完成安装设置');
}
require_once 'config.php';
class Database {
private $conn;
// 构造函数 - 建立数据库连接
public function __construct() {
$this->conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// 检查连接
if ($this->conn->connect_error) {
die("数据库连接失败: " . $this->conn->connect_error);
}
}
// 获取数据库连接
public function getConnection() {
return $this->conn;
}
// 获取所有服务器信息
// $sort_by: 排序字段,默认为sort_weight
// $sort_order: 排序顺序,默认为DESC(降序)
public function getAllServers($sort_by = 'sort_weight', $sort_order = 'DESC') {
// 验证排序字段和排序顺序的有效性
$valid_sort_fields = ['id', 'name', 'address', 'server_type', 'created_at', 'updated_at', 'sort_weight'];
$valid_sort_orders = ['ASC', 'DESC'];
// 如果排序字段不在有效列表中,使用默认值
if (!in_array($sort_by, $valid_sort_fields)) {
$sort_by = 'sort_weight';
}
// 如果排序顺序不在有效列表中,使用默认值
if (!in_array(strtoupper($sort_order), $valid_sort_orders)) {
$sort_order = 'DESC';
} else {
$sort_order = strtoupper($sort_order);
}
// 构建SQL查询语句
$sql = "SELECT * FROM servers ORDER BY $sort_by $sort_order";
$result = $this->conn->query($sql);
return $result;
}
// 获取单个服务器信息
public function getServerById($id) {
$sql = "SELECT * FROM servers WHERE id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
// 添加服务器
public function addServer($name, $address, $server_type = 'java', $sort_weight = 1000) {
// 先检查表结构是否包含server_type字段
$checkColumn = $this->conn->query("SHOW COLUMNS FROM servers LIKE 'server_type'");
// 检查是否包含sort_weight字段
$checkSortWeight = $this->conn->query("SHOW COLUMNS FROM servers LIKE 'sort_weight'");
if ($checkColumn && $checkColumn->num_rows > 0) {
// 表包含server_type字段
if ($checkSortWeight && $checkSortWeight->num_rows > 0) {
// 表也包含sort_weight字段
$sql = "INSERT INTO servers (name, address, server_type, sort_weight, created_at) VALUES (?, ?, ?, ?, NOW())";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
die("添加服务器prepare语句失败: " . $this->conn->error);
}
$stmt->bind_param("sssi", $name, $address, $server_type, $sort_weight);
} else {
// 表不包含sort_weight字段
$sql = "INSERT INTO servers (name, address, server_type, created_at) VALUES (?, ?, ?, NOW())";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
die("添加服务器prepare语句失败: " . $this->conn->error);
}
$stmt->bind_param("sss", $name, $address, $server_type);
}
$result = $stmt->execute();
$stmt->close();
return $result;
} else {
// 表不包含server_type字段,使用兼容旧结构的语句
$sql = "INSERT INTO servers (name, address, created_at) VALUES (?, ?, NOW())";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
die("添加服务器prepare语句失败: " . $this->conn->error);
}
$stmt->bind_param("ss", $name, $address);
$result = $stmt->execute();
$stmt->close();
return $result;
}
}
// 更新服务器
public function updateServer($id, $name, $address, $server_type = 'java', $sort_weight = null) {
// 先检查表结构是否包含server_type字段
$checkColumn = $this->conn->query("SHOW COLUMNS FROM servers LIKE 'server_type'");
// 检查是否包含sort_weight字段
$checkSortWeight = $this->conn->query("SHOW COLUMNS FROM servers LIKE 'sort_weight'");
if ($checkColumn && $checkColumn->num_rows > 0) {
// 表包含server_type字段
if ($checkSortWeight && $checkSortWeight->num_rows > 0) {
// 表也包含sort_weight字段
if ($sort_weight !== null) {
// 如果提供了排序权重,更新它
$sql = "UPDATE servers SET name = ?, address = ?, server_type = ?, sort_weight = ?, updated_at = NOW() WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
die("更新服务器prepare语句失败: " . $this->conn->error);
}
$stmt->bind_param("sssii", $name, $address, $server_type, $sort_weight, $id);
} else {
// 如果没有提供排序权重,不更新它
$sql = "UPDATE servers SET name = ?, address = ?, server_type = ?, updated_at = NOW() WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
die("更新服务器prepare语句失败: " . $this->conn->error);
}
$stmt->bind_param("sssi", $name, $address, $server_type, $id);
}
} else {
// 表不包含sort_weight字段
$sql = "UPDATE servers SET name = ?, address = ?, server_type = ?, updated_at = NOW() WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
die("更新服务器prepare语句失败: " . $this->conn->error);
}
$stmt->bind_param("sssi", $name, $address, $server_type, $id);
}
$result = $stmt->execute();
$stmt->close();
return $result;
} else {
// 表不包含server_type字段,使用兼容旧结构的语句
$sql = "UPDATE servers SET name = ?, address = ?, updated_at = NOW() WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
die("更新服务器prepare语句失败: " . $this->conn->error);
}
$stmt->bind_param("ssi", $name, $address, $id);
$result = $stmt->execute();
$stmt->close();
return $result;
}
}
// 删除服务器
public function deleteServer($id) {
$sql = "DELETE FROM servers WHERE id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param("i", $id);
return $stmt->execute();
}
// 析构函数 - 关闭数据库连接
public function __destruct() {
$this->conn->close();
}
// 检查并更新表结构,添加show_player_history、show_ip和ip_description字段
public function checkAndUpdateServerTable() {
// 检查servers表是否包含show_player_history字段
$checkColumn = $this->conn->query("SHOW COLUMNS FROM servers LIKE 'show_player_history'");
if (!$checkColumn || $checkColumn->num_rows === 0) {
// 如果不包含,添加show_player_history字段,默认为1(显示)
$alterTable = $this->conn->query("ALTER TABLE servers ADD COLUMN show_player_history TINYINT(1) DEFAULT 1 AFTER sort_weight");
if (!$alterTable) {
error_log("添加show_player_history字段失败: " . $this->conn->error);
return false;
}
}
// 检查是否包含show_ip字段
$checkShowIp = $this->conn->query("SHOW COLUMNS FROM servers LIKE 'show_ip'");
if (!$checkShowIp || $checkShowIp->num_rows === 0) {
// 如果不包含,添加show_ip字段,默认为1(显示IP)
$alterTable = $this->conn->query("ALTER TABLE servers ADD COLUMN show_ip TINYINT(1) DEFAULT 1 AFTER show_player_history");
if (!$alterTable) {
error_log("添加show_ip字段失败: " . $this->conn->error);
return false;
}
}
// 检查是否包含ip_description字段
$checkIpDesc = $this->conn->query("SHOW COLUMNS FROM servers LIKE 'ip_description'");
if (!$checkIpDesc || $checkIpDesc->num_rows === 0) {
// 如果不包含,添加ip_description字段,默认为空字符串
$alterTable = $this->conn->query("ALTER TABLE servers ADD COLUMN ip_description TEXT DEFAULT '' AFTER show_ip");
if (!$alterTable) {
error_log("添加ip_description字段失败: " . $this->conn->error);
return false;
}
}
return true;
}
// 设置服务器是否显示历史在线人数
public function setShowPlayerHistory($server_id, $show_history) {
// 确保字段存在
$this->checkAndUpdateServerTable();
$sql = "UPDATE servers SET show_player_history = ?, updated_at = NOW() WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
error_log("设置显示历史人数prepare语句失败: " . $this->conn->error);
return false;
}
$show_history = $show_history ? 1 : 0;
$stmt->bind_param("ii", $show_history, $server_id);
$result = $stmt->execute();
$stmt->close();
return $result;
}
// 设置服务器是否显示IP地址
public function setShowIp($server_id, $show_ip) {
// 确保字段存在
$this->checkAndUpdateServerTable();
$sql = "UPDATE servers SET show_ip = ?, updated_at = NOW() WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
error_log("设置显示IPprepare语句失败: " . $this->conn->error);
return false;
}
$show_ip = $show_ip ? 1 : 0;
$stmt->bind_param("ii", $show_ip, $server_id);
$result = $stmt->execute();
$stmt->close();
return $result;
}
// 获取服务器是否显示IP地址的设置
public function getShowIp($server_id) {
// 确保字段存在
$this->checkAndUpdateServerTable();
$sql = "SELECT show_ip FROM servers WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
error_log("获取显示IP设置prepare语句失败: " . $this->conn->error);
return true; // 默认显示
}
$stmt->bind_param("i", $server_id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();
return $row ? ($row['show_ip'] == 1) : true; // 默认显示
}
// 设置IP地址的替代描述文本
public function setIpDescription($server_id, $description) {
// 确保字段存在
$this->checkAndUpdateServerTable();
$sql = "UPDATE servers SET ip_description = ?, updated_at = NOW() WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
error_log("设置IP描述prepare语句失败: " . $this->conn->error);
return false;
}
$stmt->bind_param("si", $description, $server_id);
$result = $stmt->execute();
$stmt->close();
return $result;
}
// 获取IP地址的替代描述文本
public function getIpDescription($server_id) {
// 确保字段存在
$this->checkAndUpdateServerTable();
$sql = "SELECT ip_description FROM servers WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
error_log("获取IP描述prepare语句失败: " . $this->conn->error);
return ''; // 默认空字符串
}
$stmt->bind_param("i", $server_id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();
return $row ? $row['ip_description'] : ''; // 默认空字符串
}
// 获取服务器是否显示历史在线人数的设置
public function getShowPlayerHistory($server_id) {
// 确保字段存在
$this->checkAndUpdateServerTable();
$sql = "SELECT show_player_history FROM servers WHERE id = ?";
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
error_log("获取显示历史人数设置prepare语句失败: " . $this->conn->error);
return true; // 默认显示
}
$stmt->bind_param("i", $server_id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();
return $row ? ($row['show_player_history'] == 1) : true; // 默认显示
}
// 保存服务器在线人数历史数据
// 优化逻辑:如果当前人数与上一条记录相同,则更新记录时间;否则插入新记录
// 如果提供了玩家列表,当人数相同时还会比较玩家列表,如果列表不同也会插入新记录
public function savePlayerHistory($server_id, $players_online, $player_list = null) {
// 准备玩家列表JSON
$player_list_json = null;
if ($players_online > 0 && is_array($player_list) && count($player_list) > 0) {
$player_list_json = json_encode($player_list);
}
// 首先检查上一条记录
$sql_last = "SELECT id, players_online, player_list_json FROM player_history WHERE server_id = ? ORDER BY record_time DESC LIMIT 1";
$stmt_last = $this->conn->prepare($sql_last);
if ($stmt_last === false) {
error_log("查询最后一条玩家历史数据prepare语句失败: " . $this->conn->error);
return false;
}
$stmt_last->bind_param("i", $server_id);
$stmt_last->execute();
$result_last = $stmt_last->get_result();
$stmt_last->close();
if ($result_last && $result_last->num_rows > 0) {
// 有上一条记录,检查人数是否相同
$row_last = $result_last->fetch_assoc();
$last_id = $row_last['id'];
$last_players_online = $row_last['players_online'];
$last_player_list_json = $row_last['player_list_json'];
if ($last_players_online == $players_online) {
// 人数相同,检查玩家列表是否相同
if ($players_online > 0 && $player_list !== null) {
// 获取排序后的当前玩家列表
sort($player_list);
$current_player_list_sorted = $player_list;
// 获取排序后的历史玩家列表
$last_player_list_sorted = [];
if (!empty($last_player_list_json)) {
$last_player_list = json_decode($last_player_list_json, true);
if (is_array($last_player_list)) {
sort($last_player_list);
$last_player_list_sorted = $last_player_list;
}
}
// 比较玩家列表(确保顺序不影响比较结果)
if (count($current_player_list_sorted) == count($last_player_list_sorted) &&
$current_player_list_sorted === $last_player_list_sorted) {
// 玩家列表相同,更新记录时间
$sql_update = "UPDATE player_history SET record_time = NOW() WHERE id = ?";
$stmt_update_players = $this->conn->prepare($sql_update);
if ($stmt_update_players === false) {
error_log("更新玩家历史数据时间prepare语句失败: " . $this->conn->error);
return false;
}
$stmt_update_players->bind_param("i", $last_id);
$result_update = $stmt_update_players->execute();
$stmt_update_players->close();
return $result_update;
} else {
// 玩家列表不同,插入新记录
$sql_insert = "INSERT INTO player_history (server_id, players_online, player_list_json) VALUES (?, ?, ?)";
$stmt_insert = $this->conn->prepare($sql_insert);
if ($stmt_insert === false) {
error_log("保存玩家历史数据prepare语句失败: " . $this->conn->error);
return false;
}
$stmt_insert->bind_param("iis", $server_id, $players_online, $player_list_json);
$result_insert = $stmt_insert->execute();
$stmt_insert->close();
return $result_insert;
}
} else {
// 没有玩家在线,或没有提供玩家列表,直接更新记录时间
$sql_update = "UPDATE player_history SET record_time = NOW() WHERE id = ?";
$stmt_update_inner = $this->conn->prepare($sql_update);
if ($stmt_update_inner === false) {
error_log("更新玩家历史数据时间prepare语句失败: " . $this->conn->error);
return false;
}
$stmt_update_inner->bind_param("i", $last_id);
$result_update = $stmt_update_inner->execute();
$stmt_update_inner->close();
return $result_update;
}
} else {
// 人数不同,插入新记录
$sql_insert = "INSERT INTO player_history (server_id, players_online, player_list_json) VALUES (?, ?, ?)";
$stmt_insert = $this->conn->prepare($sql_insert);
if ($stmt_insert === false) {
error_log("保存玩家历史数据prepare语句失败: " . $this->conn->error);
return false;
}
$stmt_insert->bind_param("iis", $server_id, $players_online, $player_list_json);
$result_insert = $stmt_insert->execute();
$stmt_insert->close();
return $result_insert;
}
} else {
// 没有上一条记录,直接插入新记录
$sql_insert = "INSERT INTO player_history (server_id, players_online, player_list_json) VALUES (?, ?, ?)";
$stmt_insert = $this->conn->prepare($sql_insert);
if ($stmt_insert === false) {
error_log("保存玩家历史数据prepare语句失败: " . $this->conn->error);
return false;
}
$stmt_insert->bind_param("iis", $server_id, $players_online, $player_list_json);
$result_insert = $stmt_insert->execute();
$stmt_insert->close();
return $result_insert;
}
return false;
}
// 获取服务器的玩家历史数据(聚合数据)
// $days: 要获取的天数范围,默认为1天,设置为0表示查询所有记录
public function getPlayerHistory($server_id, $days = 1) {
// 根据天数选择合适的时间间隔分组
if ($days <= 0) {
// 查询所有记录,按天分组
$time_format = '%Y-%m-%d';
} elseif ($days <= 1) {
// 1天内,按小时分组
$time_format = '%Y-%m-%d %H:00:00';
} elseif ($days <= 7) {
// 1-7天,按2小时分组
$time_format = '%Y-%m-%d %H:00:00';
} else {
// 超过7天,按天分组
$time_format = '%Y-%m-%d';
}
// 设置SQL查询语句基础部分
$sql = "SELECT DATE_FORMAT(record_time, '$time_format') as time_label,
AVG(players_online) as avg_players
FROM player_history
WHERE server_id = ? ";
// 计算时间范围(如果不是查询所有记录)
if ($days > 0) {
$time_ago = date('Y-m-d H:i:s', strtotime("-$days days"));
$sql .= "AND record_time >= ? ";
}
// 完成SQL查询语句
$sql .= "GROUP BY time_label
ORDER BY time_label ASC";
// 准备预处理语句
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
error_log("获取玩家历史数据prepare语句失败: " . $this->conn->error);
return false;
}
// 绑定参数
$stmt->bind_param("is", $server_id, $time_ago);
// 执行查询
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
return $result;
}
// 获取服务器的原始玩家历史数据(包含玩家列表)
// $days: 要获取的天数范围,默认为1天,设置为0表示查询所有记录
public function getRawPlayerHistory($server_id, $days = 1) {
// 设置SQL查询语句基础部分
$sql = "SELECT record_time, players_online, player_list_json
FROM player_history
WHERE server_id = ? ";
// 计算时间范围(如果不是查询所有记录)
if ($days > 0) {
$time_ago = date('Y-m-d H:i:s', strtotime("-$days days"));
$sql .= "AND record_time >= ? ";
}
// 完成SQL查询语句
$sql .= "ORDER BY record_time ASC";
// 准备预处理语句
$stmt = $this->conn->prepare($sql);
if ($stmt === false) {
error_log("获取原始玩家历史数据prepare语句失败: " . $this->conn->error);
return false;
}
// 绑定参数
if ($days > 0) {
$stmt->bind_param("is", $server_id, $time_ago);
} else {
$stmt->bind_param("i", $server_id);
}
// 执行查询
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
return $result;
}
}
?>