forked from fusionpit/WhatsTraining
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.lua
71 lines (60 loc) · 2.43 KB
/
Utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
--[[ file meta info
@file Utils.lua
@brief Utility functions. Filtering and table injections.
--]]
--[[
@brief Accessing the addons private table
@var _ addonName, thrown away
@var wt Global addonTable
--]]
local _, wt = ...
-- Invoking lua functions to local variables
local tinsert = tinsert
local ipairs = ipairs
-- Get player class
wt.currentClass = select(2, UnitClass("player"))
--[[
@brief Filtering function of spells table with custom predicate
@param spellsByLevel Invoking table of spells from <Class>.lua file
@param pred Custom function to use as filter
@return output Filtered output table
--]]
local function filter(spellsByLevel, pred)
local output = {} -- Local table init
for level, spells in pairs(spellsByLevel) do -- Looping through each level entry in spellsByLevel
output[level] = {} -- Sub-table init
for _, spell in ipairs(spells) do -- Looping through each spell entry at current level entry
if (pred(spell) == true) then -- If custom pred(spell) is true,
tinsert(output[level], spell) -- Insert spell at end of sub-table
end
end
end
return output
end
--[[
@brief Filter by player faction
@var playerFaction The player's current active faction
@param spellsByLevel Table of spells from <Class>.lua file
@return Returns output from filter()
--]]
local playerFaction = UnitFactionGroup("player")
function wt.FactionFilter(spellsByLevel)
return filter(spellsByLevel, function(spell) return spell.faction == nil or spell.faction == playerFaction end) -- Invoking filter() with custom function as @param pred
end
--[[
@brief When called from <Class>.lua file,
creates new sub-table in global scope table "wt" for spells that do not show multiple ranks in the spellbook
varargs is just a set of tables, where each table is a list of spell ids that
totally overwrite a previous rank of that ability ordered by rank.
Most warrior and rogue abilities are like this, as they cost the same amount
of resources but just last longer or do more damage.
--]]
function wt:AddOverriddenSpells(...)
local abilityMap = {} -- Local table init
for _, abilityIds in ipairs({...}) do -- Looping through each sub-table
for _, abilityId in ipairs(abilityIds) do -- Looping through each spell in table
abilityMap[abilityId] = abilityIds -- Add spell IDs to sub-table
end
end
self.overriddenSpellsMap = abilityMap -- Creates new table in global scope and insert data from local table
end