Module:Desynthesis results

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
Documentation for Module:Desynthesis results does not exist yet [create] (How does this work?)
local icon = require("Module:Icon")

function renderQty(min, max)
    if min == max then
        return tostring(min)
    end
    return ("%d–%d"):format(min, max)
end

local p = {}

function p.renderTable(id)
    id = tonumber(id)

	-- Desynthesis data is huge, so it's split up into multiple JSON files
    -- that we need to be able to handle. On the wiki these are under
    -- Module:Desynthesis with subpages that group every 1000 item IDs where
    -- IDs 1 through 1000 go in 001000.json, 1001-2000 go in 002000.json, etc.
	--
	-- Attempt to load the appropriate JSON file; if there is no such file, or
	-- the file doesn't contain data for this item, return an error.
    local name = ("Module:Desynthesis/%03d000.json"):format(math.floor((id - 1) / 1000) + 1)
    local success, dataOrError = pcall(
		mw.loadJsonData,
        name
    )
	local sourceData
	if success then
		sourceData = dataOrError.Sources[id]
	end -- otherwise, leave sourceData nil

	if not sourceData then
        return ("<span class=error>Error: No data for desynthesis source %d</span>"):format(id)
    end

    local rewards = sourceData.Rewards
    -- sort by chance of obtaining, descending
    table.sort(rewards, function(a, b) return a.Pct > b.Pct end)

    local result = "{| class=\"table crafting sortable\"\n! Item\n! Quantity\n! Chance\n"
    for _, reward in ipairs(rewards) do
        local iconWithLabel = icon.gameIconFromQuery({
            query = ("[[Has context::Item]][[Has internal ID::%d]]"):format(reward.Id)
        })
        result = result .. ("|-\n| %s\n| %s\n| %.2f%%\n"):format(
            iconWithLabel,
            renderQty(reward.Min, reward.Max),
            reward.Pct * 100
        )
    end

    result = result .. ("|- class=sortbottom\n! align=center colspan=3 | Based on %d desynth attempts &bull; Data provided by [https://gacha.infi.ovh/desynth?source=%s gacha.infi.ovh]\n|}"):format(sourceData.Records, id)
    return result
end

function p.main(frame)
    local id = frame.args.id
    if not id or id == "" then
        return "<span class=error>Error: no item ID provided.</span>"
    end
    return p.renderTable(id)
end

return p