-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLocalization.lua
More file actions
251 lines (244 loc) · 8.47 KB
/
Copy pathEventLocalization.lua
File metadata and controls
251 lines (244 loc) · 8.47 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
-- Receiver-side presentation only. Original text remains in every ANN packet for
-- older peers; the optional trailing field contains facts, never executable text.
local QT = _G.QuestTogether
local L = QT.Translate
local locales = {
enUS = true,
deDE = true,
frFR = true,
esES = true,
esMX = true,
ptBR = true,
ruRU = true,
itIT = true,
koKR = true,
zhCN = true,
zhTW = true,
}
local aliases = { enGB = "enUS" }
local prefixes = {
QUEST_ACCEPTED = "Quest Accepted: ",
QUEST_COMPLETED = "Quest Completed: ",
QUEST_READY_TO_TURN_IN = "Ready to Turn In: ",
QUEST_REMOVED = "Quest Removed: ",
WORLD_QUEST_ENTERED = "World Quest Entered: ",
WORLD_QUEST_LEFT = "Left World Quest: ",
WORLD_QUEST_COMPLETED = "World Quest Completed: ",
BONUS_OBJECTIVE_ENTERED = "Bonus Objective Entered: ",
BONUS_OBJECTIVE_LEFT = "Left Bonus Objective: ",
BONUS_OBJECTIVE_COMPLETED = "Bonus Objective Completed: ",
}
local progressEvents = { QUEST_PROGRESS = true, WORLD_QUEST_PROGRESS = true, BONUS_OBJECTIVE_PROGRESS = true }
local function Integer(addon, value, minimum, maximum)
local n = addon:SafeToNumber(value)
if n and n >= minimum and n <= maximum and n == math.floor(n) then
return n
end
end
function QT:GetEventLocale()
local locale = self.localizationTestLocale or self.locale or "enUS"
locale = aliases[locale] or locale
return locales[locale] and locale or "enUS"
end
function QT:GetLocalizedEventPrefix(eventType)
local prefix = prefixes[eventType]
return prefix and self.TranslateForLocale(prefix, self:GetEventLocale()) or nil
end
function QT:DecodeAnnouncementFacts(value)
if not self:CanAccessValue(value) or type(value) ~= "string" or #value > 80 then
return nil
end
local locale, kind, index, current, required = value:match("^1:([a-zA-Z]+):([qcplo]):(%d*):(%d*):(%d*)$")
if not locales[locale] then
return nil
end
local fact = { locale = locale, kind = kind }
if kind == "q" then
if index ~= "" or current ~= "" or required ~= "" then
return nil
end
elseif kind == "l" then
fact.current = Integer(self, current, 1, 1000)
if index ~= "" or required ~= "" or not fact.current then
return nil
end
else
fact.index = Integer(self, index, 1, 100)
fact.current = Integer(self, current, 0, kind == "p" and 100 or (kind == "o" and 1 or 1000000000))
if not fact.index or not fact.current then
return nil
end
if required ~= "" then
if kind ~= "c" then
return nil
end
fact.required = Integer(self, required, 1, 1000000000)
if not fact.required or fact.current > fact.required then
return nil
end
end
end
return fact
end
function QT:BuildAnnouncementFacts(eventType, index, objectiveType, finished, current, required)
local kind = prefixes[eventType] and "q" or nil
if eventType == "PLAYER_LEVEL_UP" then
kind, current, index, required = "l", Integer(self, current, 1, 1000), nil, nil
if not current then
return nil
end
elseif progressEvents[eventType] then
index = Integer(self, index, 1, 100)
if not index then
return nil
end
current = Integer(self, current, 0, 1000000000)
required = Integer(self, required, 1, 1000000000)
if self:CanAccessValue(objectiveType) and objectiveType == "progressbar" then
if not current or current > 100 then
return nil
end
kind, required = "p", nil
elseif current then
kind = "c"
if required and current > required then
required = nil
end
else
-- Text-only legacy objectives cannot safely yield numeric facts.
-- Their completion state still has a localized representation.
if not self:CanAccessValue(finished) or type(finished) ~= "boolean" then
return nil
end
kind, current, required = "o", finished and 1 or 0, nil
end
end
if not kind then
return nil
end
return table.concat({ "1", self:GetEventLocale(), kind, index or "", current or "", required or "" }, ":")
end
function QT:GetLocalizedQuestTitle(questID)
local id = Integer(self, questID, 1, 1000000000)
if not id then
return nil
end
-- These snapshots are addon-owned and already hold the client's local title.
local snapshot = self.GetQuestSnapshot and self:GetQuestSnapshot(id)
local title = snapshot and self:SafeTrimString(snapshot.title, "") or ""
if title ~= "" and not self:IsPlaceholderQuestTitle(id, title) then
return title
end
local api = self.API or {}
if type(api.GetLocalizedQuestTitle) ~= "function" then
return nil
end
local now = self:SafeToNumber(api.GetTime and api.GetTime()) or 0
local cache = rawget(self, "localizedQuestTitles")
if not cache then
cache = { entries = {}, order = {}, requests = {} }
self.localizedQuestTitles = cache
end
local entry = cache.entries[id]
if entry and now >= entry.time and now - entry.time < (entry.title and 300 or 5) then
return entry.title
end
if self:IsWorkBlocked("quest_snapshot_refresh") then
return nil
end
local ok, rawTitle = pcall(api.GetLocalizedQuestTitle, id)
title = ok and self:SafeTrimString(rawTitle, "") or ""
if #title > 512 or self:IsPlaceholderQuestTitle(id, title) then
title = ""
end
if not entry then
if #cache.order >= 256 then
cache.entries[table.remove(cache.order, 1)] = nil
end
cache.order[#cache.order + 1] = id
entry = {}
cache.entries[id] = entry
end
entry.time, entry.title = now, title ~= "" and title or nil
if title == "" and type(api.RequestLocalizedQuestTitle) == "function" then
-- A bounded rolling window prevents bursts at fixed-window boundaries
-- and retains per-ID throttling even if the display cache evicts an entry.
local requested = false
for i = #cache.requests, 1, -1 do
local request = cache.requests[i]
if now < request.time or now - request.time >= 30 then
table.remove(cache.requests, i)
elseif request.id == id then
requested = true
end
end
if #cache.requests < 10 and not requested then
cache.requests[#cache.requests + 1] = { id = id, time = now }
pcall(api.RequestLocalizedQuestTitle, id)
end
end
return entry.title
end
function QT:LocalizeAnnouncementEvent(event)
if event.eventType == "LOOKING_FOR_QUEST_PARTNERS" then
return L("Looking for questing partners") .. " :)"
end
local facts = self:DecodeAnnouncementFacts(event.eventFacts)
local prefix = prefixes[event.eventType]
if facts and facts.locale == self:GetEventLocale() then
return event.text
end
if event.eventType == "PLAYER_LEVEL_UP" and facts and facts.kind == "l" then
return L("Level ") .. tostring(facts.current)
end
local id = Integer(self, event.questId, 1, 1000000000)
if not id or (not prefix and not progressEvents[event.eventType]) then
return event.text
end
if prefix and facts and facts.kind ~= "q" then
return event.text
end
if prefix then
-- Event type already identifies these labels. Optional facts can be
-- omitted by older senders or packet fitting; recognize only exact known
-- prefixes, never guess a title by splitting arbitrary translated prose.
local sourceTitle
local text = self:SafeTrimString(event.text, "")
for locale in pairs(locales) do
if not facts or facts.locale == locale then
local sourcePrefix = self.TranslateForLocale(prefix, locale)
if text:sub(1, #sourcePrefix) == sourcePrefix and #text > #sourcePrefix then
sourceTitle = text:sub(#sourcePrefix + 1)
break
end
end
end
if not facts and not sourceTitle then return event.text end
local title = self:GetLocalizedQuestTitle(id) or sourceTitle
return title and (self.TranslateForLocale(prefix, self:GetEventLocale()) .. title) or event.text
end
if not facts or (facts.kind ~= "c" and facts.kind ~= "p" and facts.kind ~= "o") then
return event.text
end
local title = self:GetLocalizedQuestTitle(id)
-- Progress prose cannot safely be split into a quest title and objective.
-- Preserve readable source text when local data cannot supply the title.
if not title then
return event.text
end
-- Objective indexes are not stable identities across stages. Do not borrow
-- another player's objective text or counters, or replace numbers in prose.
local progress
if facts.kind == "p" then
progress = string.format(L("Objective %d: %d%%"), facts.index, facts.current)
elseif facts.kind == "c" and facts.required then
progress = string.format(L("Objective %d: %d/%d"), facts.index, facts.current, facts.required)
elseif facts.kind == "c" then
progress = string.format(L("Objective %d: %d"), facts.index, facts.current)
elseif facts.current == 1 then
progress = string.format(L("Objective %d: Complete"), facts.index)
else
progress = string.format(L("Objective %d: Progress updated"), facts.index)
end
return title .. " — " .. progress
end