forked from cybertk/ConcentrationRecharge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.lua
More file actions
executable file
·193 lines (150 loc) · 4.61 KB
/
Character.lua
File metadata and controls
executable file
·193 lines (150 loc) · 4.61 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
local _, ns = ...
local Util = ns.Util
local Concentration = {
skillLineToCurrencyCache = {},
skillLinesTWW = {
[171] = 2871, -- Alchemy
-- [794] = 278910, -- Archaeology
[164] = 2872, -- Blacksmithing
-- [185] = 2873, -- Cooking
[333] = 2874, -- Enchanting
[202] = 2875, -- Engineering
-- [356] = 2876, -- Fishing
-- [182] = 2877, -- Herbalism
[773] = 2878, -- Inscription
[755] = 2879, -- Jewelcrafting
[165] = 2880, -- Leatherworking
-- [186] = 2881, -- Mining
-- [393] = 2882, -- Skinning
[197] = 2883, -- Tailoring
},
skillLinesMN = {
[171] = 2906, -- Alchemy
[164] = 2907, -- Blacksmithing
[333] = 2909, -- Enchanting
[202] = 2910, -- Engineering
[773] = 2913, -- Inscription
[755] = 2914, -- Jewelcrafting
[165] = 2915, -- Leatherworking
[197] = 2918, -- Tailoring
},
}
Concentration.__index = Concentration
local CONCENTRATION_MAX = 1000
local CONCENTRATION_RECHARGE_RATE_IN_SECONDS = 250 / 24 / 3600
function Concentration.__eq(a, b)
return a.fullTime == b.fullTime
end
function Concentration.__lt(a, b)
return a.fullTime < b.fullTime
end
function Concentration:Create(name, skillLine, spelloffset, buttonIndex)
if not self:IsValidSkillLine(skillLine) then
Util:Debug("Unsupported profession:", name)
return
end
local itemType, actionID, spellID = C_SpellBook.GetSpellBookItemType(1 + spelloffset, Enum.SpellBookSpellBank.Player)
local o = {}
o.i = buttonIndex
o.skillLine = skillLine
o.spell = spellID
setmetatable(o, self)
Util:Debug("Concentration Init:", name, skillLine)
return o
end
function Concentration:Update()
local currencyID = self.skillLineToCurrencyCache[self.skillLine]
if not currencyID then
currencyID = C_TradeSkillUI.GetConcentrationCurrencyID(self.skillLinesMN[self.skillLine])
if not currencyID then
return false
end
self.skillLineToCurrencyCache[self.skillLine] = currencyID
end
local currency = C_CurrencyInfo.GetCurrencyInfo(currencyID)
if not currency then
return false
end
self.v = currency.quantity
self.fullTime = GetServerTime() + (CONCENTRATION_MAX - self.v) / CONCENTRATION_RECHARGE_RATE_IN_SECONDS
Util:Debug("Concentration Updated:", self.skillLine, self.v)
return true
end
function Concentration:IsValidSkillLine(skillLine)
return self.skillLinesMN[skillLine] ~= nil
end
function Concentration:GetLatestV()
if self.v == CONCENTRATION_MAX then
return CONCENTRATION_MAX, true
end
local now = GetServerTime()
if now >= self.fullTime then
return CONCENTRATION_MAX, true
end
return math.floor(0.5 + CONCENTRATION_MAX - (self.fullTime - now) * CONCENTRATION_RECHARGE_RATE_IN_SECONDS), false
end
function Concentration:IsFull()
return select(2, self:GetLatestV())
end
function Concentration:IsRecharging()
return self.v and not select(2, self:GetLatestV())
end
function Concentration:SecondsToFull()
return self.fullTime - GetServerTime()
end
function Concentration:SecondsOfRecharge()
return CONCENTRATION_MAX / CONCENTRATION_RECHARGE_RATE_IN_SECONDS
end
function Concentration:SecondsRecharged()
return self.v / CONCENTRATION_RECHARGE_RATE_IN_SECONDS
end
ns.Concentration = Concentration
local Character = {}
function Character:New(o)
o = o or {}
self.__index = self
setmetatable(o, self)
if next(o) == nil then
Character._Init(o)
end
for _, concentration in pairs(o.concentration) do
setmetatable(concentration, Concentration)
end
return o
end
function Character:_Init()
local _localizedClassName, classFile, _classID = UnitClass("player")
local _englishFactionName, localizedFactionName = UnitFactionGroup("player")
self.name = UnitName("player")
self.GUID = UnitGUID("player")
self.realmName = GetRealmName()
self.level = UnitLevel("player")
self.factionName = localizedFactionName
self.class = classFile
self.concentration = {}
self.updatedAt = GetServerTime()
Util:Debug("Initialized new character:", self.name)
end
function Character:Update()
local indices = { GetProfessions() }
for i = 1, 2 do
local tabIndex = indices[i]
if tabIndex then
local name, icon, skillLevel, maxSkillLevel, numAbilities, spelloffset, skillLine, _ = GetProfessionInfo(tabIndex)
local concentration = self.concentration[skillLine]
if concentration == nil then
concentration = Concentration:Create(name, skillLine, spelloffset, i)
self.concentration[skillLine] = concentration
elseif not Concentration:IsValidSkillLine(skillLine) then
concentration = nil
self.concentration[skillLine] = nil
Util:Debug("Removed unsupported prefession:", skillLine)
end
if concentration then
concentration:Update()
end
end
end
self.updatedAt = GetServerTime()
end
ns.Character = Character