-
Notifications
You must be signed in to change notification settings - Fork 7
/
KillInfo.lua
294 lines (236 loc) · 8.98 KB
/
KillInfo.lua
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
-- KillInfo: The class with the data for showing a boss respawn timer.
-- Note that the timers themselves are GUI text labels.
local _, WBT = ...;
local Util = WBT.Util;
local Options = WBT.Options;
local KillInfo = {};
WBT.KillInfo = KillInfo;
KillInfo.CURRENT_VERSION = "v1.12";
KillInfo.UNKNOWN_SHARD = -1;
local RANDOM_DELIM = "-";
local ID_DELIM = ";";
local ID_PART_UNKNOWN = "_";
function KillInfo.CompareTo(t, a, b)
-- "if I'm comparing 'a' and 'b', return true when 'a' should come first"
local k1 = t[a];
local k2 = t[b];
if k1:IsExpired() and not k2:IsExpired() then
return false;
elseif not k1:IsExpired() and k2:IsExpired() then
return true;
end
return k1:GetSecondsUntilLatestRespawn() < k2:GetSecondsUntilLatestRespawn();
end
function KillInfo.IsValidID(id)
return KillInfo.ParseID(id) and true;
end
-- Returns nil if the parsing fails.
function KillInfo.ParseID(id)
local boss_name, shard_id, map_id = strsplit(ID_DELIM, id);
if not boss_name then
return nil;
end
return {
boss_name = boss_name,
shard_id = shard_id,
map_id = map_id,
};
end
function KillInfo.CreateID(boss_name, shard_id, map_id)
-- Unique ID used as key in the global table of tracked KillInfos and GUI labels.
--
-- Note on map_id: It's necessary to make Zandalari Warbringers unique.
if shard_id == nil or shard_id == KillInfo.UNKNOWN_SHARD then
shard_id = ID_PART_UNKNOWN;
end
local map_id = map_id or WBT.GetCurrentMapId();
return table.concat({boss_name, shard_id, map_id}, ID_DELIM);
end
function KillInfo:ID()
return self.CreateID(self.boss_name, self.shard_id, self.map_id);
end
function KillInfo:HasShardID()
return self.shard_id ~= nil;
end
-- Needed in order to verify that all fields are present.
function KillInfo:IsValidVersion()
return self.version and self.version == KillInfo.CURRENT_VERSION;
end
function KillInfo:SetInitialValues()
self.version = KillInfo.CURRENT_VERSION;
self.realm_name = GetRealmName(); -- Only use for printing!
self.realm_name_normalized = GetNormalizedRealmName();
self.map_id = WBT.GetCurrentMapId();
self.has_triggered_respawn = false;
end
-- NOTE:
-- This function is a reminder that the design is to update KillInfo.CURRENT_VERSION (and thereby clear
-- all user KillInfos), rather than try to upgrade existing ones. The reason is that it makes the code simpler,
-- and should not impact users too much if it only happens once in a while.
function KillInfo:Upgrade()
-- Don't implement this function.
end
function KillInfo:Print(indent)
print(indent .. "boss_name: " .. self.boss_name);
print(indent .. "version: " .. self.version);
print(indent .. "realm_name: " .. self.realm_name);
print(indent .. "realm_name_normalized: " .. self.realm_name_normalized);
print(indent .. "shard_id: " .. self.shard_id);
print(indent .. "map_id: " .. self.map_id);
print(indent .. "has_triggered_respawn: " .. tostring(self.has_triggered_respawn));
end
function KillInfo:SetNewDeath(t_death)
-- FIXME: It doesn't make sense to call this function from here. I think it's
-- a remnant from the time when the addon tried to upgrade KillInfos.
self:SetInitialValues();
self.t_death = t_death;
end
function KillInfo:New(boss_name, t_death, shard_id)
local ki = {};
setmetatable(ki, self);
self.__index = self;
ki.boss_name = boss_name;
ki.db = WBT.BossData.Get(boss_name); -- FIXME: Convert to fcn, and fix calls to WBT.BossData.Get(self.boss_name)
ki.shard_id = shard_id or KillInfo.UNKNOWN_SHARD;
ki:SetNewDeath(t_death);
return ki;
end
function KillInfo:Deserialize(serialized)
local ki = serialized;
setmetatable(ki, self);
self.__index = self;
-- Workaround: Update old timers with new db object in case BossData has been updated in new addon version.
-- FIXME: Don't save db object. See KillInfo.New.
ki.db = WBT.BossData.Get(ki.boss_name);
return ki;
end
function KillInfo:HasRandomSpawnTime()
return self.db.min_respawn ~= self.db.max_respawn;
end
function KillInfo:IsSafeToShare(error_msgs)
if not self:IsValidVersion() then
table.insert(error_msgs, "Timer was created with an old version of WBT and is now outdated.");
end
if self:IsExpired() then
table.insert(error_msgs, "Timer has expired.");
end
if self:HasUnknownShard() then
-- It's impossible to tell where it comes from. Player may have received it when server-jumping or
-- what not. To avoid complexity, just don't allow sharing it.
table.insert(error_msgs, "Timer doesn't have a shard ID. (This means that it was shared to you by a "
.. "player with an old version of WBT.)"); -- WBT v.1.9 or less.
else
local shard_id = WBT.GetCurrentShardID();
if WBT.IsUnknownShard(shard_id) then
table.insert(error_msgs, "Current shard ID is unknown. It will automatically be detected when "
.. "mousing over an NPC.");
elseif self.shard_id ~= shard_id then
table.insert(error_msgs, "Kill was made on shard ID " .. self.shard_id
.. ", but you are on " .. shard_id .. ".");
end
end
if Util.TableIsEmpty(error_msgs) then
return true;
end
return false;
end
function KillInfo:GetServerDeathTime()
return self.t_death;
end
function KillInfo:GetSecondsSinceDeath()
return GetServerTime() - self.t_death;
end
function KillInfo:GetEarliestRespawnTimePoint()
return self.t_death + self.db.min_respawn;
end
function KillInfo:GetLatestRespawnTimePoint()
return self.t_death + self.db.max_respawn;
end
function KillInfo:GetNumCycles()
local t = self:GetLatestRespawnTimePoint() - GetServerTime();
local n = 0;
while t < 0 do
t = t + self.db.max_respawn;
n = n + 1;
end
return n;
end
function KillInfo:GetSecondsUntilEarliestRespawn(opt_cyclic)
return self:GetSecondsUntilLatestRespawn(opt_cyclic) - (self.db.max_respawn - self.db.min_respawn);
end
function KillInfo:GetSecondsUntilLatestRespawn(opt_cyclic)
local t = self:GetLatestRespawnTimePoint() - GetServerTime();
if opt_cyclic and t < 0 then
t = t + (self:GetNumCycles() * self.db.max_respawn);
end
return t;
end
function KillInfo:GetSpawnTimeAsText()
if not self:IsValidVersion() then
return "--outdated--";
end
if self:HasRandomSpawnTime() then
local t_lower = self:GetSecondsUntilEarliestRespawn(true);
local t_upper = self:GetSecondsUntilLatestRespawn(true);
if t_lower == nil or t_upper == nil then
return "--invalid--";
elseif t_lower < 0 then
-- Just display 0 instead of 00:00 to make it easier to read
return "0" .. RANDOM_DELIM .. Util.FormatTimeSeconds(t_upper)
else
return Util.FormatTimeSeconds(t_lower) .. RANDOM_DELIM .. Util.FormatTimeSeconds(t_upper)
end
else
return Util.FormatTimeSeconds(self:GetSecondsUntilEarliestRespawn(true));
end
end
function KillInfo:InTimeWindow(from, to)
local t_now = GetServerTime();
return from <= t_now and t_now <= to;
end
function KillInfo:ShouldRespawnAlertPlayNow(offset)
local t_before_respawn = self:GetLatestRespawnTimePoint() - offset;
local trigger = self:InTimeWindow(t_before_respawn, t_before_respawn + 2)
and WBT.InZoneAndShardForTimer(self)
and WBT.PlayerIsInBossPerimiter(self.boss_name)
and self:IsValidVersion()
and not self:IsExpired()
and not self.has_triggered_respawn;
if trigger then
self.has_triggered_respawn = true;
end
return trigger;
end
function KillInfo:IsExpired()
return self:GetSecondsUntilLatestRespawn() < 0;
end
function KillInfo:IsCyclicExpired()
if not Options.cyclic.get() then
return self:IsExpired();
elseif Options.num_cycles_to_show.get() == Options.NUM_CYCLES_TO_SHOW_MAX then
-- Special case. Allows timers to always be shown.
return false;
end
return Options.num_cycles_to_show.get() < self:GetNumCycles();
end
function KillInfo:HasUnknownShard()
return self.shard_id == KillInfo.UNKNOWN_SHARD;
end
function KillInfo:IsOnCurrentShard()
if self:HasUnknownShard() then
return false;
elseif Options.assume_realm_keeps_shard.get() and (self.shard_id == WBT.GetSavedShardID(WBT.GetCurrentMapId())) then
return true;
else
return self.shard_id == WBT.GetCurrentShardID();
end
end
-- Returns true if the KillInfo comes from the last known shard for the
-- zone its boss belongs to.
function KillInfo:IsOnSavedRealmShard()
if self:HasUnknownShard() then
return false;
end
local zone_id = WBT.BossData.Get(self.boss_name).map_id;
return WBT.GetSavedShardID(zone_id) == self.shard_id;
end