Module:ItemsByStat: Difference between revisions
Jump to navigation
Jump to search
SerpentofSet (talk | contribs) No edit summary |
SerpentofSet (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local AllItemData = mw.loadJsonData("Data:ItemData.json") | local AllItemData = mw.loadJsonData("Data:ItemData.json") | ||
-- I truly have no better idea of how to sort by cost within slot, so I'm gonna multiply the costs of slots I want lower in the table | |||
local multBySlot = {} | |||
multBySlot["Weapon"] = 1 | |||
multBySlot["Armor"] = 10000 | |||
multBySlot["Tech"] = 100000000 | |||
local selectByStat = function(stat) | local selectByStat = function(stat) | ||
Line 6: | Line 12: | ||
local inKeys = {"Name","Tier","Cost","Slot",stat,"Components"} | local inKeys = {"Name","Tier","Cost","Slot",stat,"Components"} | ||
local outData = {} | local outData = {} | ||
local i = 1 | local i = 1 | ||
for m,itemData in pairs(AllItemData) do | for m,itemData in pairs(AllItemData) do | ||
if itemData[stat] ~= nil and itemData[stat] ~= "0" then -- If Item has given stat in its keys, | if itemData[stat] ~= nil and itemData[stat] ~= "0" then -- If Item has given stat in its keys, | ||
outData[i] = {} | |||
for x = 1,#inKeys do | for x = 1,#inKeys do | ||
outData[ | outData[i][x] = itemData[inKeys[x]] -- grab stat and other values | ||
end | end | ||
i = i+1 | i = i+1 | ||
end | end | ||
end | end | ||
-- Sort by cost within slot | |||
table.sort(outData, function(a,b) return a[3]*multBySlot[a[4]]<b[3]*multBySlot[b[4]] end) | |||
return outData | return outData | ||
end | end | ||
Line 31: | Line 38: | ||
local tableData = selectByStat(stat) | local tableData = selectByStat(stat) | ||
local outString = '{| class="wikitable sortable"\n|-\n!Item!!Cost!!Value\n' | local outString = '{| class="wikitable sortable"\n|-\n!Item!!Cost!!Value\n' | ||
for i=1,#tableData | for i=1,#tableData do | ||
outString = outString.. | outString = outString.. | ||
'|-style="background:'..colorBySlot[tableData[ | '|-style="background:'..colorBySlot[tableData[i][4]]..'"\n|'.. | ||
frame:expandTemplate{title="Item", args={tableData[ | frame:expandTemplate{title="Item", args={tableData[i][1]}}.."||".. | ||
frame:expandTemplate{title="Souls", args={tableData[ | frame:expandTemplate{title="Souls", args={tableData[i][3]}}.."||".. | ||
tableData[ | tableData[i][5].."\n" | ||
end | end | ||
return outString.."|}" | return outString.."|}" |
Revision as of 21:26, 27 September 2024
Documentation for this module may be created at Module:ItemsByStat/doc
local p = {}
local AllItemData = mw.loadJsonData("Data:ItemData.json")
-- I truly have no better idea of how to sort by cost within slot, so I'm gonna multiply the costs of slots I want lower in the table
local multBySlot = {}
multBySlot["Weapon"] = 1
multBySlot["Armor"] = 10000
multBySlot["Tech"] = 100000000
local selectByStat = function(stat)
-- List properties to grab and set up data structure to hold values
local inKeys = {"Name","Tier","Cost","Slot",stat,"Components"}
local outData = {}
local i = 1
for m,itemData in pairs(AllItemData) do
if itemData[stat] ~= nil and itemData[stat] ~= "0" then -- If Item has given stat in its keys,
outData[i] = {}
for x = 1,#inKeys do
outData[i][x] = itemData[inKeys[x]] -- grab stat and other values
end
i = i+1
end
end
-- Sort by cost within slot
table.sort(outData, function(a,b) return a[3]*multBySlot[a[4]]<b[3]*multBySlot[b[4]] end)
return outData
end
local colorBySlot = {}
colorBySlot["Weapon"] = "#FCAC4D"
colorBySlot["Armor"] = "#86C921"
colorBySlot["Tech"] = "#DE9CFF"
p.tablefy = function(frame)
local stat = frame.args[1]
local tableData = selectByStat(stat)
local outString = '{| class="wikitable sortable"\n|-\n!Item!!Cost!!Value\n'
for i=1,#tableData do
outString = outString..
'|-style="background:'..colorBySlot[tableData[i][4]]..'"\n|'..
frame:expandTemplate{title="Item", args={tableData[i][1]}}.."||"..
frame:expandTemplate{title="Souls", args={tableData[i][3]}}.."||"..
tableData[i][5].."\n"
end
return outString.."|}"
end
return p