Module:AwesomeSinkItemsTable

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

local format_int = function(number) local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')

-- reverse the int-string and append a separator to all blocks of 3 digits int = int:reverse:gsub("(%d%d%d)", "%1 ")

-- reverse the int-string back remove an optional separator and put the -- optional minus and fractional part back return minus .. int:reverse:gsub("^ ", "") .. fraction end

p.getItemsTable = function 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 tables = 'items' local fields = 'mDisplayName, mResourceSinkPoints' local args = { where = 'mResourceSinkPoints IS NOT NULL', orderBy = 'mResourceSinkPoints ASC, mDisplayName ASC' } local items = cargo.query(tables, fields, args) -- group all items with the same amount of points into one table row local currentPointsGroup = -1 local firstInTheGroup = true for i=1, #items, 1 do   -- if the next item has different sink points than the current group then create a new table row if items[i].mResourceSinkPoints ~= currentPointsGroup then txt = txt .. '\n|-\n| ' .. format_int(items[i].mResourceSinkPoints) .. ' || '     currentPointsGroup = items[i].mResourceSinkPoints firstInTheGroup = true end -- add item to the current group if not firstInTheGroup then txt = txt .. ', '   else firstInTheGroup = false end txt = txt ..  .. items[i].mDisplayName ..  end txt = txt .. '\n|}' return txt end

return p