public
Description: WoW Addon - Echos quest objective completion to your party
Homepage: http://www.tekkub.net
Clone URL: git://github.com/tekkub/quecho.git
Click here to lend your support to: quecho and make a donation at www.pledgie.com !
quecho / Quecho.lua
100644 127 lines (90 sloc) 3.205 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
-------------------------------------
-- Namespace Declaration --
-------------------------------------
 
Quecho = {}
 
 
Quecho.quests = setmetatable({}, {__index = function (t,i)
  local v = {}
  rawset(t, i, v)
  return v
end})
 
 
local f = CreateFrame("Frame")
f:SetScript("OnEvent", function(frame, event, ...) if Quecho[event] then return Quecho[event](Quecho, event, ...) end end)
f:RegisterEvent("ADDON_LOADED")
 
 
function Quecho:ADDON_LOADED(event, addon)
  if addon ~= "Quecho" then return end
 
  self:QUEST_LOG_UPDATE()
 
  f:RegisterEvent("UI_INFO_MESSAGE")
  f:RegisterEvent("CHAT_MSG_ADDON")
  f:RegisterEvent("QUEST_LOG_UPDATE")
 
  f:UnregisterEvent("ADDON_LOADED")
  self.ADDON_LOADED = nil
end
 
 
function Quecho:PrintF(...) ChatFrame1:AddMessage(string.format(...)) end
 
 
---------------------------
-- Reset timer --
---------------------------
 
local DELAY = 60 * 5
local sendtimes, nextpurge = {}
local function OnUpdate(f)
  if not nextpurge then f:SetScript("OnUpdate", nil) end
 
  local now = GetTime()
  if now >= (nextpurge + DELAY) then
    local next2
    for sender,objectives in pairs(Quecho.quests) do
      for objective in pairs(objectives) do
        local t = sendtimes[sender..objective]
        if (t + DELAY) <= now then
          sendtimes[sender..objective] = nil
          Quecho.quests[sender][objective] = nil
        elseif not next2 or t < next2 then next2 = t end
      end
    end
 
    Quecho:UpdateTracker()
    if not next2 then f:SetScript("OnUpdate", nil) end
    nextpurge = next2
  end
end
 
 
------------------------------
-- Event Handlers --
------------------------------
 
function Quecho:UI_INFO_MESSAGE(event, msg)
  if not msg or not msg:find("(.+): (%d+/%d+)") then return end
  SendAddonMessage("Quecho", msg, "PARTY")
end
 
 
local myname = UnitName("player")
function Quecho:CHAT_MSG_ADDON(event, prefix, msg, channel, sender)
  if sender == myname then return end
 
  if prefix == "Quecho" then
    local _, _, objective, progress = msg:find("(.+): (%d+/%d+)")
 
    sendtimes[sender..objective] = GetTime()
    if not nextpurge then
      nextpurge = GetTime()
      f:SetScript("OnUpdate", OnUpdate)
    end
    self.quests[sender][objective] = progress
 
    self:UpdateTracker()
 
  elseif prefix == "Quecho2" then self:PrintF("%s turned in %s ", sender, msg)
  elseif prefix == "Quecho3" then self:PrintF("%s accepted %s ", sender, msg)
  elseif prefix == "Quecho4" then self:PrintF("%s abandoned %s ", sender, msg) end
end
 
 
local currentquests, oldquests, firstscan, abandoning = {}, {}, true
function Quecho:QUEST_LOG_UPDATE()
  currentquests, oldquests = oldquests, currentquests
  for i in pairs(currentquests) do currentquests[i] = nil end
 
  for i=1,GetNumQuestLogEntries() do
    local link = GetQuestLink(i)
    if link then currentquests[link] = true end
  end
 
  if firstscan then
    firstscan = nil
    return
  end
 
  for link in pairs(oldquests) do if not currentquests[link] then SendAddonMessage(abandoning and "Quecho4" or "Quecho2", link, "PARTY") end end
  for link in pairs(currentquests) do if not oldquests[link] then SendAddonMessage("Quecho3", link, "PARTY") end end
 
  abandoning = nil
end
 
 
local orig = AbandonQuest
function AbandonQuest(...)
  abandoning = true
  return orig(...)
end