Skip to content

Commit fb6c391

Browse files
committed
Player history and powergamers rewrite
1 parent 098d841 commit fb6c391

5 files changed

Lines changed: 449 additions & 85 deletions

File tree

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
-- Auto install tables if we dont got them yet (first install)
2+
db.query([[
3+
CREATE TABLE IF NOT EXISTS `player_history_skill` (
4+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
5+
`player_id` int(11) NOT NULL,
6+
`lastlogin` bigint(20) unsigned NOT NULL,
7+
`lastlogout` bigint(20) unsigned NOT NULL,
8+
`town_id` int(11) NOT NULL,
9+
`lastip` int(10) unsigned NOT NULL,
10+
`skull` tinyint(1) NOT NULL,
11+
`blessings` tinyint(2) NOT NULL,
12+
`onlinetime` int(11) NOT NULL,
13+
`balance` bigint(20) unsigned NOT NULL,
14+
`level` int(11) NOT NULL,
15+
`experience` bigint(20) NOT NULL,
16+
`maglevel` int(11) NOT NULL,
17+
`skill_fist` int(10) unsigned NOT NULL,
18+
`skill_club` int(10) unsigned NOT NULL,
19+
`skill_sword` int(10) unsigned NOT NULL,
20+
`skill_axe` int(10) unsigned NOT NULL,
21+
`skill_dist` int(10) unsigned NOT NULL,
22+
`skill_shielding` int(10) unsigned NOT NULL,
23+
`skill_fishing` int(10) unsigned NOT NULL,
24+
PRIMARY KEY (`id`),
25+
FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE
26+
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
27+
]])
28+
29+
30+
-- Auto populate table if it is empty
31+
local resultId = db.storeQuery("SELECT `id` FROM `player_history_skill` LIMIT 1;")
32+
if resultId == false then
33+
db.asyncQuery([[
34+
INSERT INTO `player_history_skill` (
35+
`player_id`,
36+
`lastlogin`,
37+
`lastlogout`,
38+
`town_id`,
39+
`lastip`,
40+
`skull`,
41+
`blessings`,
42+
`onlinetime`,
43+
`balance`,
44+
`level`,
45+
`experience`,
46+
`maglevel`,
47+
`skill_fist`,
48+
`skill_club`,
49+
`skill_sword`,
50+
`skill_axe`,
51+
`skill_dist`,
52+
`skill_shielding`,
53+
`skill_fishing`
54+
)
55+
SELECT
56+
`p`.`id` AS `player_id`,
57+
`zp`.`created` AS `lastlogin`,
58+
CASE WHEN `p`.`lastlogout` > 0
59+
THEN `p`.`lastlogout`
60+
ELSE `zp`.`created`
61+
END AS `lastlogout`,
62+
`p`.`town_id`,
63+
`p`.`lastip`,
64+
`p`.`skull`,
65+
`p`.`blessings`,
66+
`p`.`onlinetime`,
67+
`p`.`balance`,
68+
`p`.`level`,
69+
`p`.`experience`,
70+
`p`.`maglevel`,
71+
`p`.`skill_fist`,
72+
`p`.`skill_club`,
73+
`p`.`skill_sword`,
74+
`p`.`skill_axe`,
75+
`p`.`skill_dist`,
76+
`p`.`skill_shielding`,
77+
`p`.`skill_fishing`
78+
FROM `players` AS `p`
79+
INNER JOIN `znote_players` AS `zp`
80+
ON `p`.`id` = `zp`.`player_id`
81+
ORDER BY `zp`.`created`
82+
]])
83+
else
84+
result.free(resultId)
85+
end
86+
87+
88+
-- Logout event, triggered by logout, and death
89+
function historyLogoutEvent(player)
90+
local blessdec = 0
91+
local i = 0
92+
while player:hasBlessing(i+1) do
93+
blessdec = blessdec+2^i
94+
i = i+1
95+
end
96+
97+
local playerGuid = player:getGuid()
98+
db.query([[
99+
INSERT INTO `player_history_skill` (
100+
`player_id`,
101+
`lastlogin`,
102+
`lastlogout`,
103+
`town_id`,
104+
`lastip`,
105+
`skull`,
106+
`blessings`,
107+
`onlinetime`,
108+
`balance`,
109+
`level`,
110+
`experience`,
111+
`maglevel`,
112+
`skill_fist`,
113+
`skill_club`,
114+
`skill_sword`,
115+
`skill_axe`,
116+
`skill_dist`,
117+
`skill_shielding`,
118+
`skill_fishing`
119+
) VALUES (
120+
]]..table.concat({
121+
playerGuid,
122+
player:getLastLoginSaved(),
123+
os.time(),
124+
player:getTown():getId(),
125+
player:getIp(),
126+
player:getSkull(),
127+
blessdec,
128+
"(SELECT `onlinetime` FROM `players` WHERE `id`='"..playerGuid.."') + ".. os.time() - player:getLastLoginSaved(),
129+
player:getBankBalance(),
130+
player:getLevel(),
131+
player:getExperience(),
132+
player:getMagicLevel(),
133+
player:getSkillLevel(SKILL_FIST),
134+
player:getSkillLevel(SKILL_CLUB),
135+
player:getSkillLevel(SKILL_SWORD),
136+
player:getSkillLevel(SKILL_AXE),
137+
player:getSkillLevel(SKILL_DISTANCE),
138+
player:getSkillLevel(SKILL_SHIELD),
139+
player:getSkillLevel(SKILL_FISHING)
140+
}, ",")..[[
141+
);
142+
]])
143+
end
144+
145+
146+
-- Log player state on logout
147+
local player_history_skill = CreatureEvent("player_history_skill")
148+
function player_history_skill.onLogout(player)
149+
--print("2-logout["..player:getName().."]")
150+
historyLogoutEvent(player)
151+
return true
152+
end
153+
player_history_skill:register()
154+
155+
156+
-- And on death
157+
local player_history_skill_death = CreatureEvent("player_history_skill_death")
158+
function player_history_skill_death.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
159+
--print("3-death["..creature:getName().."]")
160+
historyLogoutEvent(Player(creature))
161+
end
162+
player_history_skill_death:register()
163+
164+
165+
-- If this is first login, insert current progress
166+
local player_history_skill_login = CreatureEvent("player_history_skill_login")
167+
function player_history_skill_login.onLogin(player)
168+
--print("1-login["..player:getName().."]")
169+
player:registerEvent("player_history_skill_death")
170+
171+
local playerGuid = player:getGuid()
172+
local resultId = db.storeQuery("SELECT `id` FROM `player_history_skill` WHERE `player_id`="..playerGuid.." LIMIT 1;")
173+
if resultId == false then
174+
db.query([[
175+
INSERT INTO `player_history_skill` (
176+
`player_id`,
177+
`lastlogin`,
178+
`lastlogout`,
179+
`town_id`,
180+
`lastip`,
181+
`skull`,
182+
`blessings`,
183+
`onlinetime`,
184+
`balance`,
185+
`level`,
186+
`experience`,
187+
`maglevel`,
188+
`skill_fist`,
189+
`skill_club`,
190+
`skill_sword`,
191+
`skill_axe`,
192+
`skill_dist`,
193+
`skill_shielding`,
194+
`skill_fishing`
195+
)
196+
SELECT
197+
`p`.`id` AS `player_id`,
198+
`zp`.`created` AS `lastlogin`,
199+
CASE WHEN `p`.`lastlogout` > 0
200+
THEN `p`.`lastlogout`
201+
ELSE `zp`.`created`
202+
END AS `lastlogout`,
203+
`p`.`town_id`,
204+
`p`.`lastip`,
205+
`p`.`skull`,
206+
`p`.`blessings`,
207+
`p`.`onlinetime`,
208+
`p`.`balance`,
209+
`p`.`level`,
210+
`p`.`experience`,
211+
`p`.`maglevel`,
212+
`p`.`skill_fist`,
213+
`p`.`skill_club`,
214+
`p`.`skill_sword`,
215+
`p`.`skill_axe`,
216+
`p`.`skill_dist`,
217+
`p`.`skill_shielding`,
218+
`p`.`skill_fishing`
219+
FROM `players` AS `p`
220+
INNER JOIN `znote_players` AS `zp`
221+
ON `p`.`id` = `zp`.`player_id`
222+
WHERE `p`.`id` = ]]..playerGuid..[[
223+
]])
224+
else
225+
result.free(resultId)
226+
end
227+
return true
228+
end
229+
player_history_skill_login:register()

layout/aside.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
include 'layout/widgets/charactersearch.php';
1212
include 'layout/widgets/topplayers.php';
1313
include 'layout/widgets/highscore.php';
14+
if ($config['powergamers']['enabled']) include 'layout/widgets/powergamers.php';
1415
include 'layout/widgets/serverinfo.php';
1516
if ($config['ServerEngine'] !== 'TFS_02') include 'layout/widgets/houses.php';
1617
if ($follow["enabled"]): ?>

layout/menu.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<li><a href="forum.php">Forum</a> </li>
2323
<li><a href="guilds.php">Guilds</a> </li>
2424
<li><a href="highscores.php">Highscores</a> </li>
25+
<?php if ($config['powergamers']['enabled']): ?><li><a href="powergamers.php">Powergamers</a> </li><?php endif; ?>
2526
<li><a href="houses.php">Houses</a> </li>
2627
<li><a href="killers.php">Killstatistics</a> </li>
2728
<li><a href="deaths.php">Latest deaths</a> </li>

layout/widgets/powergamers.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<div class="well widget">
2+
<div class="header">
3+
Top 5 Powergamers
4+
</div>
5+
<div class="body">
6+
<table>
7+
<?php
8+
$cache = new Cache('engine/cache/widget_powergamers');
9+
if ($cache->hasExpired()) {
10+
$players = mysql_select_multi("
11+
SELECT
12+
`h`.`player_id`,
13+
`p`.`name`,
14+
`p`.`level`,
15+
CAST(`p`.`experience` as signed) - CAST(`f`.`experience` as signed) AS `diff_experience`
16+
FROM (
17+
SELECT
18+
`i`.`player_id`,
19+
IFNULL(`o`.`id`, `i`.`id`) AS `from_id`
20+
FROM `player_history_skill` AS `i`
21+
LEFT JOIN (
22+
SELECT
23+
`x`.`player_id`,
24+
MAX(`x`.`id`) AS `id`
25+
FROM `player_history_skill` AS `x`
26+
WHERE
27+
`x`.`lastlogout` < UNIX_TIMESTAMP() - 7 * 24 * 60 * 60
28+
GROUP BY
29+
`x`.`player_id`
30+
) AS `o`
31+
ON `i`.`player_id` = `o`.`player_id`
32+
WHERE
33+
`i`.`lastlogout` >= UNIX_TIMESTAMP() - 7 * 24 * 60 * 60
34+
GROUP BY
35+
`i`.`player_id`
36+
) AS `h`
37+
INNER JOIN `player_history_skill` AS `f`
38+
ON `h`.`from_id` = `f`.`id`
39+
INNER JOIN `players` AS `p`
40+
ON `h`.`player_id` = `p`.`id`
41+
WHERE CAST(`p`.`experience` as signed) - CAST(`f`.`experience` as signed) > 0
42+
ORDER BY CAST(`p`.`experience` as signed) - CAST(`f`.`experience` as signed) DESC
43+
LIMIT 5
44+
");
45+
46+
$cache->setContent($players);
47+
$cache->save();
48+
} else {
49+
$players = $cache->load();
50+
}
51+
52+
if ($players) {
53+
foreach($players as $count => $player) {
54+
$nr = $count+1;
55+
$kexp = $player['diff_experience'] / 1000;
56+
$kexp = number_format($kexp, 0, '', ' ');
57+
echo "<tr><td>{$nr}</td><td><a href='characterprofile.php?name={$player['name']}'>{$player['name']}</a> ({$player['level']}) <span style='float: right;font-size:14px;'>{$kexp} K exp</span></td></tr>";
58+
}
59+
}
60+
?>
61+
</table>
62+
</div>
63+
</div>

0 commit comments

Comments
 (0)