player_history.lua

main 230 lines · 5.7 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1-- Auto install tables if we dont got them yet (first install)
2db.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
31local resultId = db.storeQuery("SELECT `id` FROM `player_history_skill` LIMIT 1;")
32if 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 ]])
83else
84 result.free(resultId)
85end
86
87
88-- Logout event, triggered by logout, and death
89function 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 ]])
143end
144
145
146-- Log player state on logout
147local player_history_skill = CreatureEvent("player_history_skill")
148function player_history_skill.onLogout(player)
149 --print("2-logout["..player:getName().."]")
150 historyLogoutEvent(player)
151 return true
152end
153player_history_skill:register()
154
155
156-- And on death
157local player_history_skill_death = CreatureEvent("player_history_skill_death")
158function player_history_skill_death.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
159 --print("3-death["..creature:getName().."]")
160 historyLogoutEvent(Player(creature))
161end
162player_history_skill_death:register()
163
164
165-- If this is first login, insert current progress
166local player_history_skill_login = CreatureEvent("player_history_skill_login")
167function 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
228end
229player_history_skill_login:register()
230
Top