Module:AwesomeSinkItemsTable

local p = {} local json = require('Module:Json') local sinkPoints = json.decode(mw.getCurrentFrame:expandTemplate{title='Template:ResourceSinkPoints.json'}) local sinkPointsExperimental = json.decode(mw.getCurrentFrame:expandTemplate{title='Template:ResourceSinkPointsExperimental.json'})

local getKeysSortedByValue = function(tbl, sortFunction) -- create default comparator if none was provided if sortFunction == nil then sortFunction = function(a, b) return a < b end end

local keys = {} for key in pairs(tbl) do		table.insert(keys, key) end

table.sort(keys, function(a, b) return sortFunction(tbl[a], tbl[b]) end)

return keys end

function p.getItemsTable(frame) return p.buildItemsTable(sinkPoints, frame) end

function p.getItemsTableExperimental(frame) return p.buildItemsTable(sinkPointsExperimental, frame) end

function p.buildItemsTable(sinkPoints, frame) frame = frame or mw.getCurrentFrame;

local txt = '{| class="wikitable"\n|-\n! Points   !! Items'

-- manually add the coupon because it's not in the docs.json, therefore not in the database do local couponName if sinkPoints["FICSIT Coupon"] ~= nil then couponName = "FICSIT Coupon" elseif sinkPoints["Coupon"] ~= nil then couponName = "Coupon" end

if couponName ~= nil then txt = txt .. "\n|-\n| " .. sinkPoints[couponName] .. " || " .. frame:expandTemplate{ title = 'ItemLink', args = { couponName } } .. " (For the first time, the Cyber Wagon is made available for purchase in the Shop and no points are given)" end end

local sortedKeys = getKeysSortedByValue(sinkPoints)

-- group all items with the same amount of points into one table row local currentPointsGroup = -1 local firstInTheGroup = true

for i=1, #sortedKeys, 1 do		-- coupon was already added manually above local key = sortedKeys[i]; if key ~= 'FICSIT Coupon' and key ~= 'Coupon' then local points = sinkPoints[key] -- if the next item has different sink points than the current group then create a new table row if points ~= currentPointsGroup then txt = txt .. '\n|-\n| ' .. mw.getCurrentFrame:expandTemplate{ title = 'FormatNumber', args = { points } } .. ' || '				currentPointsGroup = points firstInTheGroup = true end

-- add item to the current group if not firstInTheGroup then txt = txt .. ', '			else firstInTheGroup = false end txt = txt .. frame:expandTemplate{ title = 'ItemLink', args = { sortedKeys[i] } } end end

txt = txt .. '\n|}' return txt end

return p