Update List to support Keys and clean up ObjectManager

main
jeffi 11 months ago
parent e35d19af3a
commit 250019a581
  1. 2
      src/ClassMagic/ClassMagic.lua
  2. 82
      src/List/List.lua
  3. 46
      src/ObjectManager/ObjectManager.lua
  4. 3
      src/Refreshable/Refreshable.lua
  5. 7
      src/UnitManager/UnitManager.lua

@ -8,7 +8,7 @@ local ClassMagic = {}
ClassMagic.__index = ClassMagic ClassMagic.__index = ClassMagic
---@param Class table ---@param Class table
---@param key string ---@param key string|number
---@return any ---@return any
function ClassMagic:Resolve(Class, key) function ClassMagic:Resolve(Class, key)
if Class[key] or Class[key] == false then if Class[key] or Class[key] == false then

@ -4,6 +4,8 @@ local Tinkr,
Bastion = ... Bastion = ...
---@class Bastion.List<I>: { _list: I[] } ---@class Bastion.List<I>: { _list: I[] }
---@field _list any[]
---@field indexes table<number, { key?: string|number, index: number}>
---@operator add(any): Bastion.List ---@operator add(any): Bastion.List
---@operator sub(any): Bastion.List ---@operator sub(any): Bastion.List
local List = { local List = {
@ -26,12 +28,27 @@ local List = {
self:remove(value) self:remove(value)
return self return self
end, end,
---@param a Bastion.List
---@param b Bastion.List
__concat = function(a, b) __concat = function(a, b)
return a:concat(b) return a:concat(b)
end, end,
---@param self Bastion.List
__tostring = function(self)
return self:toString()
end,
} }
List.__index = List
---@param k string|number
function List:__index(k)
if List[k] ~= nil then
return List[k]
end
local index = (self.indexes[k] and type(self.indexes[k].key) ~= "nil") and self.indexes[k].index or false
return index and self._list[index] or self._list[k]
end
---@generic I : table ---@generic I : table
---@param from? I[] ---@param from? I[]
@ -39,16 +56,25 @@ List.__index = List
function List:New(from) function List:New(from)
local self = setmetatable({}, List) local self = setmetatable({}, List)
self._list = from or {} self._list = from or {}
self.indexes = {}
return self return self
end end
---@param value any ---@param value any
function List:push(value) ---@param key? string
function List:push(value, key)
local index = #self._list + 1
table.insert(self._list, value) table.insert(self._list, value)
self.indexes[#self._list] = {
key = key,
index = index
}
end end
---@return any ---@return any
function List:pop() function List:pop()
self.indexes[#self._list] = nil
return table.remove(self._list) return table.remove(self._list)
end end
@ -65,9 +91,10 @@ end
---@return nil ---@return nil
function List:clear() function List:clear()
self._list = {} self._list = {}
self.indexes = {}
end end
---@generic I : any ---@generic I
---@param value I ---@param value I
---@return boolean ---@return boolean
function List:contains(value) function List:contains(value)
@ -81,15 +108,15 @@ end
---@generic I : any ---@generic I : any
---@param value I ---@param value I
---@return boolean
function List:remove(value) function List:remove(value)
local newList = List:New()
for i, v in ipairs(self._list) do for i, v in ipairs(self._list) do
if v == value then if v ~= value then
table.remove(self._list, i) newList:push(self._list,
return true type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil)
end end
end end
return false return newList
end end
---@generic I : any ---@generic I : any
@ -107,8 +134,8 @@ end
---@return Bastion.List<I> | Bastion.List ---@return Bastion.List<I> | Bastion.List
function List:map(callback) function List:map(callback)
local newList = List:New() local newList = List:New()
for _, v in ipairs(self._list) do for i, v in ipairs(self._list) do
newList:push(callback(v)) newList:push(callback(v), type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil)
end end
return newList return newList
end end
@ -118,9 +145,10 @@ end
---@return Bastion.List<I> | Bastion.List ---@return Bastion.List<I> | Bastion.List
function List:filter(callback) function List:filter(callback)
local newList = List:New() local newList = List:New()
for _, v in ipairs(self._list) do for i, v in ipairs(self._list) do
if callback(v) then if callback(v) then
newList:push(v) newList:push(v,
type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil)
end end
end end
return newList return newList
@ -167,6 +195,15 @@ function List:findIndex(callback)
return nil return nil
end end
---@param key number|string
function List:findByKey(key)
for i, v in ipairs(self._list) do
if self.indexes[i] and ((self.indexes[i].key and self.indexes[i].key == key) or (self.indexes[i].index == key)) then
return v
end
end
end
---@generic I : any ---@generic I : any
---@param callback fun(a: I, b: I): boolean ---@param callback fun(a: I, b: I): boolean
---@return nil ---@return nil
@ -177,15 +214,15 @@ end
function List:reverse() function List:reverse()
local newList = List:New() local newList = List:New()
for i = #self._list, 1, -1 do for i = #self._list, 1, -1 do
newList:push(self._list[i]) newList:push(self._list[i], type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil)
end end
return newList return newList
end end
function List:clone() function List:clone()
local newList = List:New() local newList = List:New()
for _, v in ipairs(self._list) do for i, v in ipairs(self._list) do
newList:push(v) newList:push(v, type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil)
end end
return newList return newList
end end
@ -193,11 +230,11 @@ end
---@param list Bastion.List ---@param list Bastion.List
function List:concat(list) function List:concat(list)
local newList = List:New() local newList = List:New()
for _, v in ipairs(self._list) do for i, v in ipairs(self._list) do
newList:push(v) newList:push(v, type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil)
end end
for _, v in ipairs(list._list) do for i, v in ipairs(list._list) do
newList:push(v) newList:push(v, type(self.indexes[i].key) == "string" and self.indexes[i].key --[[@as string]] or nil)
end end
return newList return newList
end end
@ -220,9 +257,4 @@ function List:toString()
return self:join(", ") return self:join(", ")
end end
---@return string
function List:__tostring()
return self:toString()
end
Bastion.List = List Bastion.List = List

@ -4,30 +4,22 @@ local Tinkr,
Bastion = ... Bastion = ...
---@class Bastion.ObjectManager ---@class Bastion.ObjectManager
---@field _lists table<string, { list: Bastion.List, cb: fun(object: TinkrObjectReference): false | Bastion.Unit }> ---@field _lists table<string, {list: Bastion.List, cb:fun(Object: WowGameObject): any}>
---@field afflicted Bastion.List
---@field enemies Bastion.List
---@field critters Bastion.List
---@field friends Bastion.List
---@field activeEnemies Bastion.List
---@field explosives Bastion.List
---@field incorporeal Bastion.List
---@field others Bastion.List
local ObjectManager = {} local ObjectManager = {}
ObjectManager.__index = ObjectManager ObjectManager.__index = ObjectManager
function ObjectManager:New() function ObjectManager:New()
---@class Bastion.ObjectManager
local self = setmetatable({}, ObjectManager) local self = setmetatable({}, ObjectManager)
self._lists = {} self._lists = {}
self.enemies = Bastion.List:New()
self.critters = Bastion.List:New()
self.friends = Bastion.List:New()
self.activeEnemies = Bastion.List:New() self.activeEnemies = Bastion.List:New()
self.afflicted = Bastion.List:New()
self.critters = Bastion.List:New()
self.enemies = Bastion.List:New()
self.explosives = Bastion.List:New() self.explosives = Bastion.List:New()
self.friends = Bastion.List:New()
self.incorporeal = Bastion.List:New() self.incorporeal = Bastion.List:New()
self.afflicted = Bastion.List:New()
self.others = Bastion.List:New() self.others = Bastion.List:New()
return self return self
@ -35,7 +27,7 @@ end
-- Register a custom list with a callback -- Register a custom list with a callback
---@param name string ---@param name string
---@param cb fun(object: TinkrObjectReference): false | Bastion.Unit ---@param cb fun(object: TinkrObjectReference): boolean | any
---@return Bastion.List | false ---@return Bastion.List | false
function ObjectManager:RegisterList(name, cb) function ObjectManager:RegisterList(name, cb)
if self._lists[name] then if self._lists[name] then
@ -58,8 +50,20 @@ function ObjectManager:ResetLists()
end end
end end
function ObjectManager:Reset()
self.activeEnemies:clear()
self.afflicted:clear()
self.critters:clear()
self.enemies:clear()
self.explosives:clear()
self.friends:clear()
self.incorporeal:clear()
self.others:clear()
self:ResetLists()
end
-- Refresh custom lists -- Refresh custom lists
---@param object table ---@param object WowGameObject
---@return nil ---@return nil
function ObjectManager:EnumLists(object) function ObjectManager:EnumLists(object)
for _, list in pairs(self._lists) do for _, list in pairs(self._lists) do
@ -80,15 +84,7 @@ end
-- Refresh all lists -- Refresh all lists
---@return nil ---@return nil
function ObjectManager:Refresh() function ObjectManager:Refresh()
self.afflicted:clear() self:Reset()
self.enemies:clear()
self.critters:clear()
self.friends:clear()
self.activeEnemies:clear()
self.explosives:clear()
self.incorporeal:clear()
self.others:clear()
self:ResetLists()
local objects = Objects() local objects = Objects()

@ -32,9 +32,10 @@ end
-- Create -- Create
---@generic V ---@generic V
---@generic R : Bastion.Refreshable
---@param value V ---@param value V
---@param cb fun(): V ---@param cb fun(): V
---@return V ---@return R | V
function Refreshable:New(value, cb) function Refreshable:New(value, cb)
local self = setmetatable({}, Refreshable) local self = setmetatable({}, Refreshable)

@ -27,7 +27,7 @@ function UnitManager:__index(k)
if k == "none" then if k == "none" then
return self:Get("none") return self:Get("none")
end end
if UnitManager[k] then if UnitManager[k] ~= nil then
return UnitManager[k] return UnitManager[k]
end end
@ -58,6 +58,10 @@ function UnitManager:__index(k)
if o then if o then
local unit = Unit:New(o) local unit = Unit:New(o)
self:SetObject(unit) self:SetObject(unit)
if self.objects[kguid] then
return self.objects[kguid]
end
end end
end end
@ -77,7 +81,6 @@ end
-- Get or create a unit -- Get or create a unit
---@param token UnitId ---@param token UnitId
---@return Bastion.Unit
function UnitManager:Get(token) function UnitManager:Get(token)
-- if not Validate(token) then -- if not Validate(token) then
-- error("UnitManager:Get - Invalid token: " .. token) -- error("UnitManager:Get - Invalid token: " .. token)

Loading…
Cancel
Save