zz_GarrisonToggle/core.lua

121 lines
3.4 KiB
Lua
Executable File

local _, addon = ...
local childName = addon['childName']
local child = addon[childName]
local events = child['events']
local db, dbp, GBM
child['gtypes'] = {}
local gtypes = child['gtypes']
LibStub("zzHelper"):Embed(child)
function child:GetPID(unit)
unit = unit or 'player'
local player, realm = UnitFullName(unit)
realm = realm or GetRealmName()
return format("%s-%s", player, realm)
end
function child:GetGBM()
return GBM
end
function child:SetGBM(value)
GBM = value
db['GBMStore'][child:GetPID()] = GBM
end
function child:ResetGBM()
for k, v in pairs(gtypes) do
local l = C_Garrison.GetGarrisonInfo(v)
if(l and l > 0) then
child:SetGBM(k)
GarrisonLandingPage.garrTypeID = gtypes[GBM]
break
end
end
return child:GetGBM()
end
function child:switchDisplay(value)
if(value) then
AlertFrame:UnregisterEvent("GARRISON_MISSION_FINISHED")
AlertFrame:UnregisterEvent("GARRISON_BUILDING_ACTIVATABLE")
else
AlertFrame:RegisterEvent("GARRISON_MISSION_FINISHED")
AlertFrame:RegisterEvent("GARRISON_BUILDING_ACTIVATABLE")
end
end
function child:findTypes()
for k, v in pairs(Enum.GarrisonType) do
local l = C_Garrison.GetGarrisonInfo(v)
if(l and tonumber(l) > 0) then
local _, ver, subver = strsplit('_', k)
local followerType = Enum.GarrisonFollowerType[format('FollowerType_%i_%i', ver, subver)]
if(followerType) then
dbp[followerType] = dbp[followerType] or {}
gtypes[followerType] = v
end
end
end
if(not gtypes[GBM]) then
child:ResetGBM()
end
end
local function prepareDB()
db = addon['db']['profile'][childName]
child['db'] = db
local pid = child:GetPID('player')
db['missions'] = db['missions'] or {}
db['missions'][pid] = db['missions'][pid] or {}
dbp = db['missions'][pid]
db['GBMStore'] = db['GBMStore'] or {}
db['GBMStore'][pid] = db['GBMStore'][pid] or Enum.GarrisonFollowerType.FollowerType_8_0
GBM = db['GBMStore'][pid]
addon['callbacks']:Fire(format("Init%s", childName))
end
local function GetMission(missionID)
for _, v in pairs(C_Garrison.GetInProgressMissions(GBM) or {}) do
if(missionID == v['missionID']) then
return v
end
end
end
local function AddDB(missionID)
local missionInfo = GetMission(missionID)
if(missionInfo) then
local mt = {C_Garrison.GetMissionTimes(missionID)}
dbp[GBM][missionID] = time() + mt[5]
end
end
function events:GARRISON_MISSION_STARTED(_, _, missionID)
AddDB(missionID)
end
function events:ADDON_LOADED(_, event, addonName)
if(addonName == 'Blizzard_GarrisonUI') then
child:findTypes()
child:UnregisterEvent(event)
end
end
function events:PLAYER_ENTERING_WORLD()
child:UnregisterEvent('PLAYER_ENTERING_WORLD')
if(not IsAddOnLoaded("Blizzard_GarrisonUI")) then
UIParentLoadAddOn("Blizzard_GarrisonUI")
end
if(db['hideStandard'] and ExpansionLandingPageMinimapButton) then
ExpansionLandingPageMinimapButton:UnregisterAllEvents()
ExpansionLandingPageMinimapButton:Hide()
ExpansionLandingPageMinimapButton:HookScript("OnShow", ExpansionLandingPageMinimapButton.Hide)
end
child:switchDisplay(db['garrisonHideCombat'])
end
child['specs'] = {
['name'] = childName,
['sv'] = true,
['cfg'] = true,
['cfgReset'] = prepareDB,
['ldb'] = child['ldb'],
['defaults'] = {
['profile'] = {
[childName] = {
['garrisonText'] = 'GS',
['garrisonHideCombat'] = false,
['hideStandard'] = true,
}
}
}
}
child:NewAddOn(...)