Module:Data

local getArgs = require('Dev:Arguments').getArgs

local p = {} function p.BuildTable( frame ) local args = getArgs(frame) return p._buildtable(frame, args) end

function p.LoadTemplate( frame ) local args = getArgs(frame) --return tableToString(args) return p._loadtemplate(frame, args) end

function p._buildtable(frame, args) local gamedef = {} if not pcall(function gamedef = mw.loadData("Module:Data/" .. args["DataSource"]) end ) then return "Error: Failed to find Data/" .. args["DataSource"] .. " Params: " .. printTable(args) end if (args["SelectVal"] and string.find(args["SelectVal"], "Default_")) then args["SelectVal"] = "Deekin" end

local data = GetData(gamedef, args["SelectProp"], args["SelectVal"]) local headers = GetHeaders(gamedef) if not data then return "Error: Failed to find Filter Data Params: " .. tablelength(gamedef.Values) end

local sTable = mw.html.create('table') sTable:addClass('wikitable') sTable:addClass('sortable') sTable:addClass("MyTable") local trHeader = AddChild(sTable, "tr") --Add Header Row for k,v in pairs(headers) do       local th = AddChild(trHeader, "th", v.HeaderText) if (v.HeaderText == "Icon") then th:addClass("unsortable") end end

--Add Rows for k,oItem in pairs(data) do       local tr = AddChild(sTable, "tr") --Iterate over headers and --place values from oItem into TDs for k2, v2 in pairs(headers) do           local PName = v2.PName local td = AddChild(tr, "td") if (oItem[PName]) then if (oItem[PName].Type == "Image") then LoadImage(td, oItem[PName]) --if (v.Rarity == "1") then --   oDiv:css("border", "solid 1px white") --elseif (v.Rarity == "2") then --   oDiv:css("border", "solid 1px green") --elseif (v.Rarity == "3") then --   oDiv:css("border", "solid 1px blue") --elseif (v.Rarity == "4") then -- oDiv:css("border", "solid 1px purple") --else --  oDiv:css("border", "solid 1px white") --end elseif (oItem[PName].Type == "Text") then if (oItem[PName].SortValue) then td:attr("data-sort-value",oItem[PName].SortValue) end if (oItem[PName].Link) then local sText = "" .. oItem[PName].Text .. "" td:wikitext(sText) else td:wikitext(oItem[PName].Text) end elseif ( type(oItem[PName]) == "table") then td:wikitext("[Object]") else td:wikitext(oItem[PName]) end else td:wikitext("---") end end end return sTable end

function p._loadtemplate(frame, args) local gamedef = {} local sTemplateName = args["TemplateName"] if not pcall(function gamedef = mw.loadData("Module:Data/" .. args["DataSource"]) end ) then return "Error: Failed to find Data/" .. args["DataSource"] .. " Params: " .. printTable(args) end

if (string.find(args["SelectVal"], "Default")) then args["SelectVal"] = "Deekin" end

local data = GetData(gamedef, args["SelectProp"], args["SelectVal"]) if not data then return "Error: Failed to find Filter Data Params: " .. tablelength(gamedef.Values) end local s = getTemplate(frame, data, sTemplateName ) if (type(s) == "string" and string.find(s, "ERROR:") ) then return s .. printTable(args) end return s end

function LoadImage(target, data) local sText = "" if (data["ImageFile"]) then sText = "" local oDivImage = AddChild(target, "div") oDivImage:wikitext(sText) end if (data["Text"]) then if (data["Link"]) then sText = "" .. data["Text"]           sText = sText .. "" else sText = data["Text"] end local oDivText = AddChild(target, "div") oDivText:css("text-align", "center") oDivText:wikitext(sText) end return end

function getTemplate(frame, data, sTemplateName ) local loadType = "Expand" --local loadType = "PreProcess" local sArgs = buildTemplateArgs(data, loadType)

if (type(sArgs) == "string" and string.find(sArgs, "ERROR:") ) then return sArgs end local s = frame:expandTemplate{title = sTemplateName, args = sArgs } --local s = frame:preprocess("") return s end

function buildTemplateArgs( data, sType ) local oData = data[1] if not oData then return "ERROR: Data Empty" end --for frame:expandTemplate need a Table if (sType == "Expand") then local vals = {} for k,v in pairs(oData) do           AddValueToTable(vals, k, v, "") end return vals --for frame:preprocess need a WikiString elseif (sType == "PreProcess") then local s = "" for k,v in pairs(oData) do           local sType = type(v) if sType == "string" or sType == "number" then s = s .. "|" .. k .. "=" .. v           end end return s   end return "Missing Type" end

function AddValueToTable(oTable, sKey, oValue, sParentKey) local sType = type(oValue) if (sType == "table") then if (#sParentKey == 0) then sParentKey = sKey .. "."       else sParentKey = sParentKey .. sKey .. "."       end for k2, v2 in pairs(oValue) do           AddValueToTable(oTable, k2, v2, sParentKey) end else oTable[sParentKey .. sKey] = oValue end return end

function GetData(data, pName, pValue) local vals = {} --filter data by pValue --for example getting data where Name == Bruenor --example use: getting Bruenor's Gear Items if (pValue) then for k,v in pairs(data.Values) do           if v[pName] == pValue then table.insert(vals, v)           end end --return all the values --example use building a top level level -- Champion List else for k,v in pairs(data.Values) do           table.insert(vals, v)        end end return vals end

function GetHeaders(data) local vals = {} for k,v in pairs(data.Headers) do       table.insert(vals, v)    end

return vals end

function AddChild(parent, elementtype, sValue) local ele = parent:tag(elementtype) if (sValue) then ele:wikitext(sValue) end return ele end

function tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end

function printTable(T) local s = "" for k,v in pairs(T) do       if (k) then local sType = type(v) if sType == "string" or sType == "number" then s = s .. "|Key: " .. k .. ", Value:" .. v .. " "           else s = s .. "|Key: " .. k .. ", Value: [object] " end end end return s end

function tableToString(t) local key local value local result result = '' for key, value in pairs(t) do       if (tonumber(key) ~= nil) then result = result .. ':table[' .. key .. '] is ' .. value .. ' (key is int) \n' else result = result .. ':table[\'' .. key .. '\'] is ' .. value .. '\n' end end return result end

return p