TourGuide - Tweak to delayed update on turnin, might fix mikma's issues in his head.
[WoW-TourGuide.git] / TourGuide.lua
blobe65a883eef7bb91358e96fc6663095b7b69fffa7
2 local OptionHouse = LibStub("OptionHouse-1.1")
5 local myfaction = UnitFactionGroup("player")
7 TourGuide = DongleStub("Dongle-1.0"):New("TourGuide")
8 if tekDebug then TourGuide:EnableDebug(10, tekDebug:GetFrame("TourGuide")) end
9 TourGuide.guides = {}
10 TourGuide.guidelist = {}
11 TourGuide.nextzones = {}
14 TourGuide.icons = setmetatable({
15 ACCEPT = "Interface\\GossipFrame\\AvailableQuestIcon",
16 COMPLETE = "Interface\\Icons\\Ability_DualWield",
17 TURNIN = "Interface\\GossipFrame\\ActiveQuestIcon",
18 RUN = "Interface\\Icons\\Ability_Tracking",
19 MAP = "Interface\\Icons\\Ability_Spy",
20 FLY = "Interface\\Icons\\Ability_Druid_FlightForm",
21 TRAIN = "Interface\\GossipFrame\\trainerGossipIcon",
22 SETHEARTH = "Interface\\Icons\\Spell_Holy_ElunesGrace",
23 HEARTH = "Interface\\Icons\\INV_Misc_Rune_01",
24 NOTE = "Interface\\Icons\\INV_Misc_Note_01",
25 GRIND = "Interface\\Icons\\INV_Stone_GrindingStone_05",
26 USE = "Interface\\Icons\\INV_Misc_Bag_08",
27 BUY = "Interface\\Icons\\INV_Misc_Coin_01",
28 BOAT = "Interface\\Icons\\Spell_Frost_SummonWaterElemental",
29 GETFLIGHTPOINT = "Interface\\Icons\\Spell_Nature_GiftoftheWaterSpirit",
30 }, {__index = function() return "Interface\\Icons\\INV_Misc_QuestionMark" end})
33 local actiontypes = {
34 A = "ACCEPT",
35 C = "COMPLETE",
36 T = "TURNIN",
37 R = "RUN",
38 t = "TRAIN",
39 H = "HEARTH",
40 h = "SETHEARTH",
41 G = "GRIND",
42 F = "FLY",
43 f = "GETFLIGHTPOINT",
44 N = "NOTE",
45 B = "BUY",
46 b = "BOAT",
47 U = "USE",
51 function TourGuide:Initialize()
52 self.db = self:InitializeDB("TourGuideAlphaDB", {
53 char = {
54 turnedin = {},
55 cachedturnins = {},
58 self.turnedin = self.db.char.turnedin
59 self.cachedturnins = self.db.char.cachedturnins
61 self.db.char.currentguide = self.db.char.currentguide or self.guidelist[1]
62 self:LoadGuide(self.db.char.currentguide)
63 self:PositionStatusFrame()
64 end
67 function TourGuide:Enable()
68 local _, title = GetAddOnInfo("TourGuide")
69 local author, version = GetAddOnMetadata("TourGuide", "Author"), GetAddOnMetadata("TourGuide", "Version")
70 local oh = OptionHouse:RegisterAddOn("Tour Guide", title, author, version)
71 oh:RegisterCategory("Guides", TourGuide, "CreateGuidesPanel")
72 oh:RegisterCategory("Objectives", TourGuide, "CreateObjectivePanel")
74 for _,event in pairs(self.TrackEvents) do self:RegisterEvent(event) end
75 self:RegisterEvent("QUEST_COMPLETE", "UpdateStatusFrame")
76 self:RegisterEvent("QUEST_DETAIL", "UpdateStatusFrame")
77 self.TrackEvents = nil
78 self:UpdateStatusFrame()
79 end
82 function TourGuide:RegisterGuide(name, nextzone, faction, sequencefunc)
83 if faction ~= myfaction then return end
84 self.guides[name] = sequencefunc
85 self.nextzones[name] = nextzone
86 table.insert(self.guidelist, name)
87 end
90 function TourGuide:LoadGuide(name)
91 if not name then return end
93 self.db.char.currentguide = name
94 if not self.guides[name] then self.db.char.currentguide = self.guidelist[1] end
95 self:DebugF(1, "Loading guide: %s", name)
96 self:ParseObjectives(self.guides[self.db.char.currentguide]())
97 end
100 function TourGuide:LoadNextGuide()
101 local name = self.nextzones[self.db.char.currentguide]
102 if not name then return end
104 for i,quest in ipairs(self.quests) do self.turnedin[quest] = nil end -- Clean out old completed objectives, to avoid conflicts
106 self:LoadGuide(name)
107 self:UpdateGuidesPanel()
108 return true
112 function TourGuide:GetQuestLogIndexByName(name)
113 name = name or self.quests[self.current]
114 name = name:gsub("%s%(Part %d+%)", "")
115 for i=1,GetNumQuestLogEntries() do
116 if GetQuestLogTitle(i) == name then return i end
120 function TourGuide:GetQuestDetails(name)
121 if not name then return end
123 local i = self:GetQuestLogIndexByName(name)
124 local complete = i and select(7, GetQuestLogTitle(i)) == 1
125 return i, complete
129 function TourGuide:FindBagSlot(itemid)
130 for bag=0,4 do
131 for slot=1,GetContainerNumSlots(bag) do
132 local item = GetContainerItemLink(bag, slot)
133 if item and string.find(item, "item:"..itemid) then return bag, slot end
139 function TourGuide:GetCurrentObjectiveInfo()
140 return self:GetObjectiveInfo(self.current)
144 function TourGuide:GetObjectiveInfo(i)
145 local action, quest, note = self.actions[i], self.quests[i], self.notes[i]
146 if not action then return end
148 local logi, complete = self:GetQuestDetails(quest)
149 local hasitem = action == "ITEM" and self.questitems[i] and self:FindBagSlot(self.questitems[i])
150 local _, _, lootitem, lootqty = string.find(self.lootitems[i] or "", "(%d+)x?(%d*)")
151 lootqty = tonumber(lootqty) or 1
153 self:Debug(11, "GetObjectiveInfo", self.lootitems[i], lootitem, lootqty)
154 return action, quest:gsub("@.*@", ""), note, logi, complete, hasitem, self.turnedin[quest], quest, self.useitems[i], self.optional[i], lootitem, lootqty
158 local myclass = UnitClass("player")
159 local titlematches = {"For", "A", "The", "Or", "In", "Then", "From", "To", "A"}
160 local function ParseQuests(...)
161 local accepts, turnins, completes = {}, {}, {}
162 local uniqueid = 1
163 local actions, notes, quests, items, useitems, optionals, lootitems = {}, {}, {}, {}, {}, {}, {}
164 local i = 1
166 for j=1,select("#", ...) do
167 local text = select(j, ...)
168 local _, _, class = text:find("|C|([^|]+)|")
170 if text ~= "" and (not class or class == myclass) then
171 local _, _, action, quest = text:find("^(%a) ([^|]*)")
172 assert(actiontypes[action], "Unknown action: "..text)
173 quest = quest:trim()
174 if not (action == "A" or action =="C" or action =="T") then
175 quest = quest.."@"..uniqueid.."@"
176 uniqueid = uniqueid + 1
179 actions[i], quests[i] = actiontypes[action], quest
181 local _, _, note = string.find(text, "|N|([^|]+)|")
182 if note then notes[i] = note end
184 local _, _, item = string.find(text, "|I|(%d+)|")
185 if item then items[i] = item end
187 local _, _, useitem = string.find(text, "|U|(%d+)|")
188 if useitem then useitems[i] = useitem end
190 local _, _, lootitem = string.find(text, "|L|(%d+%s?%d*)|")
191 if lootitem then lootitems[i] = lootitem end
193 if string.find(text, "|O|") then optionals[i] = true end
195 i = i + 1
197 -- Debuggery
198 if action == "A" then accepts[quest] = true
199 elseif action == "T" then turnins[quest] = true
200 elseif action == "C" then completes[quest] = true end
202 if action == "A" or action == "C" or action == "T" then
203 -- Catch bad Title Case
204 for _,word in pairs(titlematches) do
205 if quest:find("[^:]%s"..word.."%s") or quest:find("[^:]%s"..word.."$") or quest:find("[^:]%s"..word.."@") then
206 TourGuide:DebugF(1, "%s %s -- Contains bad title case", action, quest)
210 local _, _, comment = string.find(text, "(|.|[^|]+)$")
211 if comment then TourGuide:Debug(1, "Unclosed comment: ".. comment) end
215 -- More debug
216 for quest in pairs(accepts) do if not turnins[quest] then TourGuide:DebugF(1, "Quest has no 'turnin' objective: %s", quest) end end
217 for quest in pairs(turnins) do if not accepts[quest] then TourGuide:DebugF(1, "Quest has no 'accept' objective: %s", quest) end end
218 for quest in pairs(completes) do if not accepts[quest] and not turnins[quest] then TourGuide:DebugF(1, "Quest has no 'accept' and 'turnin' objectives: %s", quest) end end
220 return actions, notes, quests, items, useitems, optionals, lootitems
224 function TourGuide:ParseObjectives(text)
225 self.actions, self.notes, self.quests, self.questitems, self.useitems, self.optional, self.lootitems = ParseQuests(string.split("\n", text))
229 function TourGuide:SetTurnedIn(i, value, noupdate)
230 if not i then
231 i = self.current
232 value = true
235 if value then value = true else value = nil end -- Cleanup to minimize savedvar data
237 self.turnedin[self.quests[i]] = value
238 self:DebugF(1, "Set turned in %q = %s", self.quests[i], tostring(value))
239 if not noupdate then self:UpdateStatusFrame()
240 else self.updatedelay = true end
244 function TourGuide:CompleteQuest(name, noupdate)
245 local i = self.current
246 repeat
247 action, quest, note, logi, complete, hasitem, turnedin, fullquestname = self:GetObjectiveInfo(i)
248 if action == "TURNIN" and not turnedin and name == quest:gsub("%s%(Part %d+%)", "") then
249 self:DebugF(1, "Saving early quest turnin %q", quest)
250 return self:SetTurnedIn(i, true, noupdate)
252 i = i + 1
253 until not action
254 self:DebugF(1, "Quest %q not found!", name)