-
Notifications
You must be signed in to change notification settings - Fork 39
/
webtop10_example.php
114 lines (108 loc) · 3.53 KB
/
webtop10_example.php
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
<?php
// Change to your database setup.
$dbhost = "localhost"; // The IP of your mysql server
$dbuser = "youruser"; // The mysql user
$dbpw = "yourpassword"; // The mysql password
$dbname = "smrpg"; // The database name
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SM:RPG Top 10</title>
<style>
.error {
color: red;
font-size: 20pt;
border: 5px solid black;
}
</style>
</head>
<body>
<h1>SM:RPG Top 10</h1>
<?php
$error = "";
try {
$db = new mysqli($dbhost, $dbuser, $dbpw, $dbname);
if ($db->connect_errno)
throw new Exception("Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error);
$db->set_charset('utf8mb4');
// Get top 10 players
$q = $db->query('SELECT player_id, name, (cast(\'76561197960265728\' as unsigned) + steamid) as steamid64, level, experience, credits, lastseen, lastreset FROM players ORDER BY level DESC, experience DESC LIMIT 10');
$players = array();
while ($player = $q->fetch_object()) {
// fetch upgrade info of upgrades the player bought.
$player->upgrades = array();
$upgr_res = $db->query('SELECT u.shortname, pu.purchasedlevel, pu.selectedlevel FROM player_upgrades pu INNER JOIN upgrades u ON u.upgrade_id = pu.upgrade_id WHERE pu.player_id = ' . (int) $player->player_id . ' AND pu.purchasedlevel > 0 ORDER BY u.shortname');
while ($upgrade = $upgr_res->fetch_object()) {
$player->upgrades[] = $upgrade;
}
$players[] = $player;
}
$q->close();
// Get last reset time and reason
$q = $db->query('SELECT setting, value FROM settings WHERE setting = "last_reset" OR setting = "reset_reason"');
$last_reset = array();
while ($setting = $q->fetch_object()) {
$last_reset[$setting->setting] = $setting->value;
}
$q->close();
}
catch(Exception $e) {
$error = $e->getMessage();
}
?>
<p>Listing the top 10 RPG players on the server.</p>
<?php if (!empty($error)): ?>
<span class="error">Database error: <?php echo $error; ?></span>
<?php else: ?>
<table border="1">
<tr>
<th></th>
<th>Name</th>
<th>Level</th>
<th>Experience</th>
<th>Credits</th>
<th>Last seen</th>
<th>Last reset</th>
</tr>
<?php foreach($players as $index => $player): ?>
<tr>
<td><?php echo $index+1; ?>.</td>
<td><a href="http://steamcommunity.com/profiles/<?php echo $player->steamid64; ?>" title="Steam Profile"><?php echo htmlentities($player->name, ENT_QUOTES, "UTF-8"); ?></a></td>
<td><?php echo $player->level; ?></td>
<td><?php echo $player->experience; ?></td>
<td><?php echo $player->credits; ?></td>
<td><?php echo strftime("%d.%m.%Y %H:%M:%S", $player->lastseen); ?></td>
<td><?php echo strftime("%d.%m.%Y %H:%M:%S", $player->lastreset); ?></td>
</tr>
<tr>
<td></td>
<td colspan="5">
<table border="1">
<tr>
<th>Upgrade shortname</th>
<th>Purchased level</th>
<th>Selected level</th>
</tr>
<?php foreach($player->upgrades as $upgrade): ?>
<tr>
<td><?php echo $upgrade->shortname; ?></td>
<td><?php echo $upgrade->purchasedlevel; ?></td>
<td><?php echo $upgrade->selectedlevel; ?></td>
</tr>
<?php endforeach; ?>
</table>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php if(isset($last_reset["last_reset"])): ?>
Server was last reset on <?php echo strftime("%d.%m.%Y %H:%M:%S", $last_reset["last_reset"]); ?>.<br />
<?php endif; ?>
<?php if(isset($last_reset["reset_reason"])): ?>
Reason: <?php echo $last_reset["reset_reason"]; ?>
<?php endif; ?>
<?php endif; ?>
</body>
</html>