Module:AwesomeSinkItemsTable

local p = {} local json = require('Module:Json') local sinkPoints = json.decode(mw.getCurrentFrame:expandTemplate{title='Template:ResourceSinkPoints.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

p.getItemsTable = function(frame) 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 if sinkPoints.Coupon ~= nil then txt = txt .. "\n|-\n| " .. sinkPoints.Coupon .. " || Coupon\n(For the first time, the Cyber Wagon is made available for purchase in the Shop and no points are given)" 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 if sortedKeys[i] ~= 'Coupon' then local points = sinkPoints[sortedKeys[i]] -- 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={[1] = 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