Module:UprisingComponents

local p = {} local argDefaults = {} local compData = mw.loadData('Module:UprisingComponents/data')

local testFrameArgs = { get = 'upgradecost', name = "Desh", tier = "2", level = "5", size = "small" }

local testParentFrameArgs = {}

-- test frame p.childFrame = { args = mw.clone(testFrameArgs), getParent = function local pArgs = testParentFrameArgs return { args = pArgs } end }

function p.childFrame:getParent local pArgs = testParentFrameArgs return { args = pArgs } end

function toLower(str) if str == nil then return nil end return mw.ustring.lower(tostring(str)) end

function toUpper(str) if str == nil then return nil end return mw.ustring.upper(tostring(str)) end

function keyExists(tbl, key, noCase) local lKey = toLower(key) ---(noCase == true && toLower(k) or k)   for k, v in pairs(compData) do        if (key == k or (noCase == true and lKey == toLower(k))) then return true end end return false end

function getParentFrame(frame) return frame:getParent end

function getParentFrame2(frame) return frame.getParent end

function Try_getParentFrame(frame) local success,result = pcall(getParentFrame, frame) if success == false or result == nil or result.args == nil then local success2,result2 = pcall(getParentFrame2, frame) if success2 == false or result2 == nil or result2.args == nil then return nil end return result2 end return result end

function _preprocess(frame, str) return frame:preprocess{text = str} end

function Try_preprocess(frame, str) local success, result = pcall(_preprocess, frame, str) if success == false then return str end return result end

function _getArgs(frame, deep) local args = {} local argNames = {} local tmp = {} local parentFrame = nil local parentFrame2 = nil

local processArgList = function(_args) for k,v in pairs(_args) do           local lKey = toLower(k) argNames[lKey] = k           if v ~= nil and mw.text.trim(tostring(v)) ~= '' then local isNum = mw.text.trim(tostring(v)):match('^%s*(%d+)%s*$') if isNum ~= nil then args[lKey] = tonumber(isNum) elseif type(v) == 'string' then args[lKey] = mw.text.trim(v) else args[lKey] = v               end end end end if frame then if frame.args ~= nil then processArgList(frame.args) end parentFrame = Try_getParentFrame(frame) end if parentFrame and parentFrame.args ~= nil then processArgList(parentFrame.args) if deep then parentFrame2 = Try_getParentFrame(parentFrame) if parentFrame2 and parentFrame2.args and parentFrame2.args.get ~= nil and type(parentFrame2.args.get) == 'string' then processArgList(parentFrame2.args) end end end

--if frame == mw.getCurrentFrame then local getValue = function(name, useDefault) local key = toLower(name) if not args[key] then if type(useDefault) == 'string' or type(useDefault) == 'number' then return useDefault elseif not useDefault then return nil else return argDefaults[key] end end return args[key] end return getValue, args, argNames --return p._main(frame, args, getValue, argNames) end

function p.getComponentByName(name) if name == nil then return nil end local lName = toLower(name) for k, v in pairs(compData) do       if mw.ustring.find(lName, toLower(k), 1, true) ~= nil then return v       end end return nil end

function p.getComponentTier(d, tier) local data = nil if type(d) == "string" then data = p.getComponentByName(d) elseif type(d) == "table" then data = d   end if data ~= nil then return (data["T" .. tostring(tier)] or data[toUpper(tier)]) end return nil end

function p.getComponentLevel(d, tier, level) local isNum = level ~= nil and mw.text.trim(tostring(level)):match('^%s*(%d+)%s*$') if type(d) == "string" then data = p.getComponentTier(d, tier) elseif type(d) == "table" then data = d       if level == nil then level = tier tier = nil isNum = level ~= nil and mw.text.trim(tostring(level)):match('^%s*(%d+)%s*$') end end if data ~= nil and data.prefixes ~= nil then if isNum ~= nil then local levelNum = tonumber(isNum) local prefixName = data.prefixes[tostring(levelNum)] if prefixName ~= nil then return (data[prefixName] or data[toLower(prefixName)]) end return nil end local lLevel = nil if level ~= nil then lLevel = toLower(level) if data[lLevel] ~= nil then return data[lLevel] end end for k, v in pairs(data) do           if mw.ustring.find(lLevel or (type(d) == "string" and d) or level or tier, toLower(k), 1, true) ~= nil then return v           end end end return nil end

function p.getComponentValueGetter(compName, tier, level)

local comp = p.getComponentByName(compName) local tierData = nil local levelData = nil if comp ~= nil then tierData = p.getComponentTier(comp, tier or 1) end if tierData ~= nil then levelData = p.getComponentLevel(tierData, level or compName) end local getValue = function(n) local lN = toLower(n) local success = false local result = nil if levelData ~= nil then result = (levelData[n] or levelData[lN]) success = ((result ~= nil and true) or keyExists(levelData, n, true)) if success == true then return result end end if lN == 'image' and comp ~= nil then return (comp.defaulticon or comp.defaultimage) end if tierData ~= nil then result = (tierData[n] or tierData[lN]) success = ((result ~= nil and true) or keyExists(tierData, n, true)) if success == true then return result end end if comp ~= nil then result = (comp[n] or comp[lN]) success = ((result ~= nil and true) or keyExists(comp, n, true)) if success == true then return result end end return nil end return getValue, comp, tierData, levelData end

function p.getComponentValue(n, compName, tier, level) local getValue = p.getComponentValueGetter(compName, tier, level) return getValue(n) end

function _getStarFilterColor(starLevel) if starLevel == nil or tostring(starLevel) == '' or tonumber(starLevel) <= 1 then return 'filter-darkgray' elseif tostring(starLevel) == '2' then return 'filter-gray' elseif tostring(starLevel) == '3' then return 'filter-green' elseif tostring(starLevel) == '4' then return 'filter-blue' elseif tostring(starLevel) == '5' then return 'filter-lite-purple2' elseif tostring(starLevel) == '6' or tonumber(starLevel) >= 6 then return 'filter-orange' end return '' end

function p.__genIconBorder(frame, args, getValue, getCompValue) -- Get Component Values local compFullName = (getCompValue('fullname') or getCompValue('name')) local tier = getCompValue('tier') or '1' local level = getCompValue('level') local image = getCompValue('image') local maxLevel = getCompValue('maxlevel') local subtype = getCompValue('subtype') if maxLevel == nil or tonumber(maxLevel) < 1 then mw.log('Invalid component level') end if maxLevel == nil or (tonumber(maxLevel) < (level ~= nil and tonumber(level) or 1)) then mw.log('Invalid component maxLevel') end local lElement = toLower(getCompValue('element')) local elementIconName = nil if lElement == 'toxic' then elementIconName = 'toxic-hexagon' elseif lElement == 'explosive' then elementIconName = 'explosive-hexagon' elseif lElement == 'disruption' then elementIconName = 'disruption-hexagon' elseif lElement == 'chemical' then elementIconName = 'chemical-hexagon' elseif lElement == 'omni' then elementIconName = 'filter-purple2 generic-hexagon' end -- Get Arg values local size = getValue('size', 'small') local iconHeight = 100 local iconWidth = 100 if size == 'medium' then iconHeight = 130 iconWidth = 130 elseif size == 'small' then iconHeight = 100 iconWidth = 100 elseif size == 'exsmall' then iconHeight = 65 iconWidth = 65 else size = 'small' end local out = '' local containerSpriteName = 'material-container-white' if (level and tonumber(level) >= 7) then containerSpriteName = 'material-container-gold' end local starFilterColor = _getStarFilterColor(level) local altText = 'T' .. tier .. ' ' .. compFullName .. ' &#10;&#x0a;' .. (level or '?') .. ' Star' .. (level and tonumber(level) > 1 and 's' or '') local starSpriteName = ((level and tonumber(level) >= 7) and 'star-gold' or ('star-silver ' .. starFilterColor)) local starSpriteClass = 'uprising-sprite ' .. starSpriteName .. (starSpriteName == 'star-gold' and 'margin-left--2px margin-top--2px margin-right-2px margin-bottom--2px' or '') .. ' tiny tiny class-style float-left' out = out .. '' out = out .. ' ' out = out .. ' ' if elementIconName ~= nil then out = out .. '  '        end out = out .. ' '           out = out .. ' '           out = out .. ' ' .. tostring(level) .. ' '       out = out .. ' '       out = out .. '' out = out .. ''       out = out .. ' '   out = out .. ' '   return ((frame ~= nil and getValue('nopreprocess', false) == nil and Try_preprocess(frame, out)) or out) end

function p._genIconBorder(frame, args, getValue) local compName = getValue('name', false) or getValue('fullname', false) or getValue('componentname', false) local tier = getValue('tier', '1') local level = getValue('level', false) if compName == nil then mw.log('Missing Component Name') return '' else local getCompValue = p.getComponentValueGetter(compName, tier, level) if getCompValue ~= nil then return p.__genIconBorder(frame, args, getValue, getCompValue) end end return '' end

function p.genIconBorder(frame) local getValue = nil local args = nil local argNames = nil getValue, args, argNames = _getArgs(frame) return p._genIconBorder(frame, args, getValue) end

function p._getValue(frame, args, getValue) local n = getValue('get', false) local compName = getValue('name', false) or getValue('fullname', false) or getValue('componentname', false) local tier = getValue('tier', '1') local level = getValue('level', false) local getCompValue = p.getComponentValueGetter(compName, tier, level) return (getCompValue and getCompValue(n) or '') end

function p.getValue(frame) local getValue = nil local args = nil local argNames = nil getValue, args, argNames = _getArgs(frame) return p._getValue(frame, args, getValue) end

function p._debug local result = p.getValue(p.childFrame) mw.log(result) return result end

function p._debug2 local result = p.genIconBorder(p.childFrame) mw.log(result) return result end

return p