Clean up code.

main
ck 11 months ago
parent 3f0bc7cb4c
commit 4e697fa0a2
  1. 2
      src/Command/Command.lua
  2. 4
      src/Item/Item.lua
  3. 52
      src/Library/Library.lua
  4. 47
      src/Module/Module.lua
  5. 24
      src/MythicPlusUtils/MythicPlusUtils.lua
  6. 2
      src/NotificationList/NotificationList.lua
  7. 4
      src/Spell/Spell.lua
  8. 8
      src/TimeToDie/TimeToDie.lua
  9. 18
      src/Timer/Timer.lua
  10. 62
      src/Util/Util.lua
  11. 246
      src/_bastion.lua

@ -81,4 +81,6 @@ function Command:PrintHelp()
end end
end end
Bastion.Globals.Command = Command:New("bastion")
Bastion.Command = Command Bastion.Command = Command

@ -236,7 +236,7 @@ function Item:UseByID(unit)
RunMacroText(string.format("/use [@%s] item:%d", target, self:GetID())) RunMacroText(string.format("/use [@%s] item:%d", target, self:GetID()))
Bastion:Debug("Using by id", self) Bastion.Util:Debug("Using by id", self)
end end
-- Use the Item -- Use the Item
@ -268,7 +268,7 @@ function Item:Use(unit, condition)
-- Check if the mouse was looking -- Check if the mouse was looking
self.wasLooking = IsMouselooking() self.wasLooking = IsMouselooking()
UseItemByName(self.traits.use.byId and self:GetID() or self:GetName(), target:GetOMToken()) UseItemByName(self.traits.use.byId and self:GetID() or self:GetName(), target:GetOMToken())
Bastion:Debug("Using", self) Bastion.Util:Debug("Using", self)
-- Set the last Use time -- Set the last Use time
self.lastUseAttempt = GetTime() self.lastUseAttempt = GetTime()

@ -119,4 +119,56 @@ function Library:Import(library)
return lib:Resolve() return lib:Resolve()
end end
local LIBRARIES = {}
---@param library Bastion.Library
function Bastion:RegisterLibrary(library)
LIBRARIES[library.name] = library
end
---@param name string
function Bastion:GetLibrary(name)
if not LIBRARIES[name] then
error("Library " .. name .. " not found")
end
return LIBRARIES[name]
end
---@param library string
function Bastion:Import(library)
local lib = self:GetLibrary(library)
if not lib then
error("Library " .. library .. " not found")
end
return lib:Resolve()
end
function Bastion:CheckLibraryDependencies()
for k, v in pairs(LIBRARIES) do
if v.dependencies then
for i = 1, #v.dependencies do
local dep = v.dependencies[i]
if LIBRARIES[dep] then
if LIBRARIES[dep].dependencies then
for j = 1, #LIBRARIES[dep].dependencies do
if LIBRARIES[dep].dependencies[j] == v.name then
Bastion.Util:Print("Circular dependency detected between " .. v.name .. " and " .. dep)
return false
end
end
end
else
Bastion.Util:Print("Library " .. v.name .. " depends on " .. dep .. " but it's not registered")
return false
end
end
end
end
return true
end
Bastion.Library = Library Bastion.Library = Library

@ -92,3 +92,50 @@ function Module:Tick()
end end
Bastion.Module = Module Bastion.Module = Module
---@type Bastion.Module[]
local MODULES = {}
function Bastion:TickModules()
for i = 1, #MODULES do
MODULES[i]:Tick()
end
end
-- Find a module by name
---@param name string
function Bastion:FindModule(name)
for i = 1, #MODULES do
if MODULES[i].name == name then
return MODULES[i]
end
end
return false
end
---@param module Bastion.Module
function Bastion:Register(module)
local foundModule = Bastion:FindModule(module.name)
if not foundModule then
table.insert(MODULES, module)
Bastion.Util:Print("Registered", module)
else
Bastion.Util:Print("Module already registered", module)
return
end
end
---@param args { [2]: string }
Bastion.Globals.Command:Register("module", "Toggle a module on/off", function(args)
local module = Bastion:FindModule(args[2])
if module then
module:Toggle()
if module.enabled then
Bastion.Util:Print("Enabled", module.name)
else
Bastion.Util:Print("Disabled", module.name)
end
else
Bastion.Util:Print("Module not found")
end
end)

@ -1278,7 +1278,7 @@ end
function MythicPlusUtils:CastingCriticalKick(unit, percent) function MythicPlusUtils:CastingCriticalKick(unit, percent)
local castingSpell = unit:GetCastingOrChannelingSpell() local castingSpell = unit:GetCastingOrChannelingSpell()
if castingSpell then if castingSpell and type(castingSpell) == "table" then
local spellID = castingSpell:GetID() local spellID = castingSpell:GetID()
local kickEntry = self.kickList[spellID] local kickEntry = self.kickList[spellID]
if not kickEntry then if not kickEntry then
@ -1307,7 +1307,7 @@ end
function MythicPlusUtils:CastingCriticalStun(unit, percent) function MythicPlusUtils:CastingCriticalStun(unit, percent)
local castingSpell = unit:GetCastingOrChannelingSpell() local castingSpell = unit:GetCastingOrChannelingSpell()
if castingSpell then if castingSpell and type(castingSpell) == "table" then
local spellID = castingSpell:GetID() local spellID = castingSpell:GetID()
local kickEntry = self.kickList[spellID] local kickEntry = self.kickList[spellID]
if not kickEntry then if not kickEntry then
@ -1337,3 +1337,23 @@ function MythicPlusUtils:IsAOEBoss(unit)
end end
Bastion.MythicPlusUtils = MythicPlusUtils:New() Bastion.MythicPlusUtils = MythicPlusUtils:New()
Bastion.Globals.Command:Register("mplus", "Toggle m+ module on/off", function(args)
local cmd = args[2]
if cmd == "debuffs" then
Bastion.MythicPlusUtils:ToggleDebuffLogging()
Bastion.Util:Print("Debuff logging", Bastion.MythicPlusUtils.debuffLogging and "enabled" or "disabled")
return
end
if cmd == "casts" then
Bastion.MythicPlusUtils:ToggleCastLogging()
Bastion.Util:Print("Cast logging", Bastion.MythicPlusUtils.castLogging and "enabled" or "disabled")
return
end
Bastion.Util:Print("[MythicPlusUtils] Unknown command")
Bastion.Util:Print("Available commands:")
Bastion.Util:Print("debuffs")
Bastion.Util:Print("casts")
end)

@ -90,4 +90,6 @@ function NotificationList:RemoveAllNotifications()
end end
end end
Bastion.Globals.Notifications = NotificationList:New()
Bastion.NotificationList = NotificationList Bastion.NotificationList = NotificationList

@ -269,7 +269,7 @@ function Spell:Cast(unit, condition)
CastSpellByName(self:GetName(), u) CastSpellByName(self:GetName(), u)
SpellCancelQueuedSpell() SpellCancelQueuedSpell()
Bastion:Debug("Casting", self) Bastion.Util:Debug("Casting", self)
-- Set the last cast time -- Set the last cast time
self.lastCastAttempt = GetTime() self.lastCastAttempt = GetTime()
@ -300,7 +300,7 @@ function Spell:ForceCast(unit)
CastSpellByName(self:GetName(), u) CastSpellByName(self:GetName(), u)
SpellCancelQueuedSpell() SpellCancelQueuedSpell()
Bastion:Debug("Force Casting", self) Bastion.Util:Debug("Force Casting", self)
-- Set the last cast time -- Set the last cast time
self.lastCastAttempt = GetTime() self.lastCastAttempt = GetTime()

@ -44,6 +44,7 @@ local TimeToDie = {
PLAYER = -6, -- 25 PLAYER = -6, -- 25
DOES_NOT_EXIST = -7, DOES_NOT_EXIST = -7,
}, },
NextRefresh = 0
} }
function TimeToDie:IterableUnits() function TimeToDie:IterableUnits()
@ -52,6 +53,13 @@ end
function TimeToDie:Refresh() function TimeToDie:Refresh()
local currentTime = GetTime() local currentTime = GetTime()
if currentTime >= TimeToDie.NextRefresh then
TimeToDie.NextRefresh = currentTime + TimeToDie.Settings.Refresh
else
return
end
local historyCount = TimeToDie.Settings.HistoryCount local historyCount = TimeToDie.Settings.HistoryCount
local historyTime = TimeToDie.Settings.HistoryTime local historyTime = TimeToDie.Settings.HistoryTime
local ttdCache = TimeToDie.Cache local ttdCache = TimeToDie.Cache

@ -6,19 +6,25 @@ Bastion = ...
-- Create a new Timer class -- Create a new Timer class
---@class Bastion.Timer ---@class Bastion.Timer
---@field type string ---@field type string
---@field cb false | fun():boolean
local Timer = { local Timer = {
startTime = nil, startTime = nil,
resetAfterCombat = false, resetAfterCombat = false,
cb = false
} }
Timer.__index = Timer Timer.__index = Timer
-- Constructor -- Constructor
---@param type string ---@param type string
---@param cb? fun():boolean
---@return Bastion.Timer ---@return Bastion.Timer
function Timer:New(type) function Timer:New(type, cb)
local self = setmetatable({}, Timer) local self = setmetatable({}, Timer)
self.startTime = nil self.startTime = nil
self.type = type self.type = type
if cb then
self.cb = cb
end
return self return self
end end
@ -49,4 +55,14 @@ function Timer:Reset()
self.startTime = nil self.startTime = nil
end end
function Timer:Check()
if self.cb then
if not self:IsRunning() and self.cb() then
self:Start()
elseif self:IsRunning() and not self.cb() then
self:Reset()
end
end
end
Bastion.Timer = Timer Bastion.Timer = Timer

@ -25,4 +25,66 @@ local compareThisTable = {
function Util:CompareThis(operator, a, b) function Util:CompareThis(operator, a, b)
end end
local PrintEnabled = false
function Util:Print(...)
if not PrintEnabled then
return
end
local args = { ... }
local str = "|cFFDF362D[Bastion]|r |cFFFFFFFF"
for i = 1, #args do
str = str .. tostring(args[i]) .. " "
end
print(str)
end
function Util:TogglePrint()
PrintEnabled = not PrintEnabled
if PrintEnabled then
Util:Print("Enabled")
else
Util:Print("Disabled")
end
end
function Util:GetPrintMode()
return PrintEnabled
end
Bastion.Globals.Command:Register("toggle", "Toggle bastion on/off", function()
Util:TogglePrint()
end)
local DebugMode = false
function Util:ToggleDebug()
DebugMode = not DebugMode
if DebugMode then
Util:Print("Debug mode enabled")
else
Util:Print("Debug mode disabled")
end
end
function Util:GetDebugMode()
return DebugMode
end
function Util:Debug(...)
if not DebugMode then
return
end
local args = { ... }
local str = "|cFFDF6520[Bastion]|r |cFFFFFFFF"
for i = 1, #args do
str = str .. tostring(args[i]) .. " "
end
print(str)
end
Bastion.Globals.Command:Register("debug", "Toggle debug mode on/off", function()
Util:ToggleDebug()
end)
Bastion.Util = Util Bastion.Util = Util

@ -1,16 +1,21 @@
---@type Tinkr ---@type Tinkr
local Tinkr = ... local Tinkr = ...
---@class Bastion.Globals.SpellName : { [spellId]: string }
---@class Bastion ---@class Bastion
local Bastion = { local Bastion = {
DebugMode = false, Enabled = false,
Globals = {}, Globals = {
---@type Bastion.Globals.SpellName
SpellName = {}
},
Tick = GetGameTick()
} }
local TinkrScriptsBase = "scripts" local BastionBase = string.format("%s/%s", "scripts", "bastion")
local BastionBase = string.format("%s/%s", TinkrScriptsBase, "bastion")
local BastionScriptsBase = string.format("%s/%s", BastionBase, "scripts") local BastionScriptsBase = string.format("%s/%s", BastionBase, "scripts")
local ThirdPartyModulesBase = string.format("%s/%s", TinkrScriptsBase, "BastionScripts") local ThirdPartyModulesBase = string.format("%s/%s", "scripts", "BastionScripts")
Bastion.__index = Bastion Bastion.__index = Bastion
@ -149,13 +154,13 @@ end
local bastionFiles = { local bastionFiles = {
"~/src/ClassMagic/ClassMagic", "~/src/ClassMagic/ClassMagic",
"~/src/List/List", "~/src/List/List",
"~/src/Command/Command",
"~/src/Util/Util", "~/src/Util/Util",
"~/src/Library/Library", "~/src/Library/Library",
"~/src/Notification/Notification", "~/src/Notification/Notification",
"~/src/NotificationList/NotificationList", "~/src/NotificationList/NotificationList",
"~/src/Vector3/Vector3", "~/src/Vector3/Vector3",
"~/src/Sequencer/Sequencer", "~/src/Sequencer/Sequencer",
"~/src/Command/Command",
"~/src/Cache/Cache", "~/src/Cache/Cache",
"~/src/Cacheable/Cacheable", "~/src/Cacheable/Cacheable",
"~/src/Refreshable/Refreshable", "~/src/Refreshable/Refreshable",
@ -184,23 +189,15 @@ for i = 1, #bastionFiles do
Bastion:Require(bastionFiles[i]) Bastion:Require(bastionFiles[i])
end end
Bastion.Globals.CombatTimer = Bastion.Timer:New("combat") Bastion.Globals.CombatTimer = Bastion.Timer:New("combat", function()
Bastion.Globals.Notifications = Bastion.NotificationList:New() return UnitAffectingCombat("player")
end)
---@param unitTarget UnitId ---@param unitTarget UnitId
Bastion.Globals.EventManager:RegisterWoWEvent("UNIT_HEALTH", function(unitTarget) Bastion.Globals.EventManager:RegisterWoWEvent("UNIT_HEALTH", function(unitTarget)
--Bastion.UnitManager:Get(unitTarget):UpdateHealth() --Bastion.UnitManager:Get(unitTarget):UpdateHealth()
end) end)
---@type table<string, Bastion.Library>
local LIBRARIES = {}
---@type Bastion.Module[]
local MODULES = {}
Bastion.Enabled = false
---@param unit UnitToken ---@param unit UnitToken
---@param auras UnitAuraUpdateInfo ---@param auras UnitAuraUpdateInfo
Bastion.Globals.EventManager:RegisterWoWEvent("UNIT_AURA", function(unit, auras) Bastion.Globals.EventManager:RegisterWoWEvent("UNIT_AURA", function(unit, auras)
@ -229,9 +226,6 @@ end)
local playerGuid = UnitGUID("player") local playerGuid = UnitGUID("player")
local missed = {} local missed = {}
---@class Bastion.Globals.SpellName : { [spellId]: string }
Bastion.Globals.SpellName = {}
Bastion.Globals.EventManager:RegisterWoWEvent("COMBAT_LOG_EVENT_UNFILTERED", function() Bastion.Globals.EventManager:RegisterWoWEvent("COMBAT_LOG_EVENT_UNFILTERED", function()
local args = { CombatLogGetCurrentEventInfo() } local args = { CombatLogGetCurrentEventInfo() }
@ -246,30 +240,32 @@ Bastion.Globals.EventManager:RegisterWoWEvent("COMBAT_LOG_EVENT_UNFILTERED", fun
---@type string ---@type string
local spellName = args[13] local spellName = args[13]
if subEvent:find("SPELL") == 1 or subEvent:find("RANGE") == 1 then if subEvent == "SPELL_CAST_SUCCESS" then
if (not Bastion.Globals.SpellName[spellID] or Bastion.Globals.SpellName[spellID] ~= spellName) then if (not Bastion.Globals.SpellName[spellID] or Bastion.Globals.SpellName[spellID] ~= spellName) then
Bastion.Globals.SpellName[spellID] = spellName Bastion.Globals.SpellName[spellID] = spellName
end end
end end
---@type Bastion.Unit?
local u = Bastion.UnitManager[sourceGUID] local u = Bastion.UnitManager[sourceGUID]
---@type Bastion.Unit
local u2 = Bastion.UnitManager[destGUID] local u2 = Bastion.UnitManager[destGUID]
local t = GetTime() local t = GetTime()
if u then if type(u) ~= "nil" then
u:SetLastCombatTime(t) u:SetLastCombatTime(t)
end end
if u2 then if type(u2) ~= "nil" then
u2:SetLastCombatTime(t) u2:SetLastCombatTime(t)
if subEvent == "SPELL_MISSED" and sourceGUID == playerGuid and spellID == 408 then if subEvent == "SPELL_MISSED" and sourceGUID == playerGuid and spellID == 408 then
local missType = args[15] local missType = args[15]
if missType == "IMMUNE" then if type(u) ~= "nil" and missType == "IMMUNE" then
local castingSpell = u:GetCastingOrChannelingSpell() local castingSpell = u:GetCastingOrChannelingSpell()
if castingSpell then if castingSpell and type(castingSpell) == "table" then
if not missed[castingSpell:GetID()] then if not missed[castingSpell:GetID()] then
missed[castingSpell:GetID()] = true missed[castingSpell:GetID()] = true
end end
@ -279,97 +275,18 @@ Bastion.Globals.EventManager:RegisterWoWEvent("COMBAT_LOG_EVENT_UNFILTERED", fun
end end
end) end)
local Timer = {
TTD = 0
}
Bastion.Now = GetTime()
Bastion.Tick = GetGameTick()
Bastion.Ticker = C_Timer.NewTicker(0.1, function() Bastion.Ticker = C_Timer.NewTicker(0.1, function()
Bastion.Now = GetTime()
Bastion.Tick = GetGameTick() Bastion.Tick = GetGameTick()
Bastion.Globals.CombatTimer:Check()
if not Bastion.Globals.CombatTimer:IsRunning() and UnitAffectingCombat("player") then
Bastion.Globals.CombatTimer:Start()
elseif Bastion.Globals.CombatTimer:IsRunning() and not UnitAffectingCombat("player") then
Bastion.Globals.CombatTimer:Reset()
end
if Bastion.Enabled then if Bastion.Enabled then
Bastion.ObjectManager:Refresh() Bastion.ObjectManager:Refresh()
if Bastion.Now > Timer.TTD then Bastion.TimeToDie:Refresh()
Timer.TTD = Bastion.Now + Bastion.TimeToDie.Settings.Refresh Bastion:TickModules()
Bastion.TimeToDie:Refresh()
end
for i = 1, #MODULES do
MODULES[i]:Tick()
end
end end
end) end)
---@param module Bastion.Module Bastion.Globals.Command:Register("dumpspells", "Dump spells to a file", function()
function Bastion:Register(module)
table.insert(MODULES, module)
Bastion:Print("Registered", module)
end
-- Find a module by name
function Bastion:FindModule(name)
for i = 1, #MODULES do
if MODULES[i].name == name then
return MODULES[i]
end
end
end
Bastion.PrintEnabled = false
function Bastion:Print(...)
if not Bastion.PrintEnabled then
return
end
local args = { ... }
local str = "|cFFDF362D[Bastion]|r |cFFFFFFFF"
for i = 1, #args do
str = str .. tostring(args[i]) .. " "
end
print(str)
end
function Bastion:Debug(...)
if not Bastion.DebugMode then
return
end
local args = { ... }
local str = "|cFFDF6520[Bastion]|r |cFFFFFFFF"
for i = 1, #args do
str = str .. tostring(args[i]) .. " "
end
print(str)
end
local Command = Bastion.Command:New("bastion")
Command:Register("toggle", "Toggle bastion on/off", function()
Bastion.Enabled = not Bastion.Enabled
if Bastion.Enabled then
Bastion:Print("Enabled")
else
Bastion:Print("Disabled")
end
end)
Command:Register("debug", "Toggle debug mode on/off", function()
Bastion.DebugMode = not Bastion.DebugMode
if Bastion.DebugMode then
Bastion:Print("Debug mode enabled")
else
Bastion:Print("Debug mode disabled")
end
end)
Command:Register("dumpspells", "Dump spells to a file", function()
local i = 1 local i = 1
local rand = math.random(100000, 999999) local rand = math.random(100000, 999999)
while true do while true do
@ -392,121 +309,12 @@ Command:Register("dumpspells", "Dump spells to a file", function()
end end
end) end)
Command:Register("module", "Toggle a module on/off", function(args) Bastion.Globals.Command:Register("missed", "Dump the list of immune kidney shot spells", function()
local module = Bastion:FindModule(args[2])
if module then
module:Toggle()
if module.enabled then
Bastion:Print("Enabled", module.name)
else
Bastion:Print("Disabled", module.name)
end
else
Bastion:Print("Module not found")
end
end)
Command:Register("mplus", "Toggle m+ module on/off", function(args)
local cmd = args[2]
if cmd == "debuffs" then
Bastion.MythicPlusUtils:ToggleDebuffLogging()
Bastion:Print("Debuff logging", Bastion.MythicPlusUtils.debuffLogging and "enabled" or "disabled")
return
end
if cmd == "casts" then
Bastion.MythicPlusUtils:ToggleCastLogging()
Bastion:Print("Cast logging", Bastion.MythicPlusUtils.castLogging and "enabled" or "disabled")
return
end
Bastion:Print("[MythicPlusUtils] Unknown command")
Bastion:Print("Available commands:")
Bastion:Print("debuffs")
Bastion:Print("casts")
end)
Command:Register("missed", "Dump the list of immune kidney shot spells", function()
for k, v in pairs(missed) do for k, v in pairs(missed) do
Bastion:Print(k) Bastion.Util:Print(k)
end end
end) end)
---@param library Bastion.Library
function Bastion:RegisterLibrary(library)
LIBRARIES[library.name] = library
end
function Bastion:CheckLibraryDependencies()
for k, v in pairs(LIBRARIES) do
if v.dependencies then
for i = 1, #v.dependencies do
local dep = v.dependencies[i]
if LIBRARIES[dep] then
if LIBRARIES[dep].dependencies then
for j = 1, #LIBRARIES[dep].dependencies do
if LIBRARIES[dep].dependencies[j] == v.name then
Bastion:Print("Circular dependency detected between " .. v.name .. " and " .. dep)
return false
end
end
end
else
Bastion:Print("Library " .. v.name .. " depends on " .. dep .. " but it's not registered")
return false
end
end
end
end
return true
end
---@param library string
function Bastion:Import(library)
local lib = self:GetLibrary(library)
if not lib then
error("Library " .. library .. " not found")
end
return lib:Resolve()
end
---@param name string
function Bastion:GetLibrary(name)
if not LIBRARIES[name] then
error("Library " .. name .. " not found")
end
local library = LIBRARIES[name]
-- if library.dependencies then
-- for i = 1, #library.dependencies do
-- local dep = library.dependencies[i]
-- if LIBRARIES[dep] then
-- if LIBRARIES[dep].dependencies then
-- for j = 1, #LIBRARIES[dep].dependencies do
-- if LIBRARIES[dep].dependencies[j] == library.name then
-- Bastion:Print("Circular dependency detected between " .. library.name .. " and " .. dep)
-- return false
-- end
-- end
-- end
-- else
-- Bastion:Print("Library " .. v.name .. " depends on " .. dep .. " but it's not registered")
-- return false
-- end
-- end
-- end
return library
end
-- if not Bastion:CheckLibraryDependencies() then
-- return
-- end
Load("@Libraries/") Load("@Libraries/")
Load("@Modules/") Load("@Modules/")
Load("@") Load("@")

Loading…
Cancel
Save