Skip to content

Commit 5a25f82

Browse files
author
LocalIdentity
committed
Use function to remove duplicate code
1 parent 38a9a7d commit 5a25f82

1 file changed

Lines changed: 30 additions & 46 deletions

File tree

src/Modules/CalcActiveSkill.lua

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -107,31 +107,38 @@ function calcs.createActiveSkill(activeEffect, supportList, actor, socketGroup,
107107
activeSkill.effectList = { activeEffect }
108108
local rejectedSupportsIndices = {}
109109

110-
for index, supportEffect in ipairs(supportList) do
111-
-- Loop through grantedEffectList until we find a support gem if the gem has an active and support component e.g. Autoexertion
110+
-- Return first compatible support grantedEffect plus a flag indicating the support has a support component
111+
local function getGrantedSupportEffect(supportEffect)
112+
local hasSupport = false
112113
if supportEffect.gemData then
113114
for _, grantedEffect in ipairs(supportEffect.gemData.grantedEffectList) do
114-
if grantedEffect.support then
115-
-- Pass 1: Add skill types from compatible supports
115+
if grantedEffect and grantedEffect.support then
116+
hasSupport = true
116117
if calcLib.canGrantedEffectSupportActiveSkill(grantedEffect, activeSkill) then
117-
for _, skillType in pairs(grantedEffect.addSkillTypes) do
118-
activeSkill.skillTypes[skillType] = true
119-
end
120-
else
121-
t_insert(rejectedSupportsIndices, index)
118+
return grantedEffect, true
122119
end
123120
end
124121
end
125-
else
126-
-- Skill with no GemData e.g. Item granted supports
122+
elseif supportEffect.grantedEffect then
123+
hasSupport = true
127124
if calcLib.canGrantedEffectSupportActiveSkill(supportEffect.grantedEffect, activeSkill) then
128-
for _, skillType in pairs(supportEffect.grantedEffect.addSkillTypes) do
129-
activeSkill.skillTypes[skillType] = true
130-
end
131-
else
132-
t_insert(rejectedSupportsIndices, index)
125+
return supportEffect.grantedEffect, true
133126
end
134127
end
128+
return nil, hasSupport
129+
end
130+
131+
for index, supportEffect in ipairs(supportList) do
132+
-- Loop through grantedEffectList until we find a support gem if the gem has an active and support component e.g. Autoexertion
133+
local grantedSupportEffect, hasSupport = getGrantedSupportEffect(supportEffect)
134+
if grantedSupportEffect then
135+
-- Pass 1: Add skill types from compatible supports
136+
for _, skillType in pairs(grantedSupportEffect.addSkillTypes) do
137+
activeSkill.skillTypes[skillType] = true
138+
end
139+
elseif hasSupport then
140+
t_insert(rejectedSupportsIndices, index)
141+
end
135142
end
136143

137144
-- loop over rejected supports until none are added.
@@ -141,43 +148,20 @@ function calcs.createActiveSkill(activeEffect, supportList, actor, socketGroup,
141148
notAddedNewSupport = true
142149
for index, supportEffectIndex in ipairs(rejectedSupportsIndices) do
143150
local supportEffect = supportList[supportEffectIndex]
144-
if supportEffect.gemData then
145-
for _, grantedEffect in ipairs(supportEffect.gemData.grantedEffectList) do
146-
if grantedEffect.support then
147-
if calcLib.canGrantedEffectSupportActiveSkill(grantedEffect, activeSkill) then
148-
notAddedNewSupport = false
149-
rejectedSupportsIndices[index] = nil
150-
for _, skillType in pairs(grantedEffect.addSkillTypes) do
151-
activeSkill.skillTypes[skillType] = true
152-
end
153-
end
154-
end
155-
end
156-
else
157-
if calcLib.canGrantedEffectSupportActiveSkill(supportEffect.grantedEffect, activeSkill) then
158-
notAddedNewSupport = false
159-
rejectedSupportsIndices[index] = nil
160-
for _, skillType in pairs(supportEffect.grantedEffect.addSkillTypes) do
161-
activeSkill.skillTypes[skillType] = true
162-
end
151+
local grantedSupportEffect = getGrantedSupportEffect(supportEffect)
152+
if grantedSupportEffect then
153+
notAddedNewSupport = false
154+
rejectedSupportsIndices[index] = nil
155+
for _, skillType in pairs(grantedSupportEffect.addSkillTypes) do
156+
activeSkill.skillTypes[skillType] = true
163157
end
164158
end
165159
end
166160
until (notAddedNewSupport)
167161

168162
for _, supportEffect in ipairs(supportList) do
169163
-- Pass 2: Add all compatible supports
170-
local grantedSupportEffect
171-
if supportEffect.gemData then
172-
for _, grantedEffect in ipairs(supportEffect.gemData.grantedEffectList) do
173-
if grantedEffect and grantedEffect.support and calcLib.canGrantedEffectSupportActiveSkill(grantedEffect, activeSkill) then
174-
grantedSupportEffect = grantedEffect
175-
break
176-
end
177-
end
178-
elseif calcLib.canGrantedEffectSupportActiveSkill(supportEffect.grantedEffect, activeSkill) then
179-
grantedSupportEffect = supportEffect.grantedEffect
180-
end
164+
local grantedSupportEffect = getGrantedSupportEffect(supportEffect)
181165
if grantedSupportEffect then
182166
t_insert(activeSkill.effectList, supportEffect)
183167
if supportEffect.isSupporting and activeEffect.srcInstance then

0 commit comments

Comments
 (0)