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
---@param Class table
---@param key string
---@param key string|number
---@return any
function ClassMagic:Resolve(Class, key)
if Class[key] or Class[key] == false then

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

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

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

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

Loading…
Cancel
Save