Module:Drops bullet display

From Little-Known Galaxy Wiki
Jump to navigation Jump to search

This module displays items from drop tables in a column bullet layout

Subpages

local argsUtil = require("Module:ArgsUtil")
local Icon = require("Module:Icon")
local cargoUtil = require("Module:CargoUtil")
local lootTableUtil = require("Module:LootTableUtil")

local p = {}

function p.main(frame)
    local args = argsUtil.merge(frame) -- Merge arguments from the current frame and its parent
    local pagename = mw.title.getCurrentTitle().text -- Get the current page name

    -- Determine the name to use for storing
    local storeName = args.name or pagename

    -- Store the input data to Cargo using lootTableUtil
    local storeOutput = lootTableUtil.processAndStoreInput(frame, storeName)

    -- Get all results using the utility module
    local allResults = lootTableUtil.getAllResults(args)

    -- Process individual items and add or combine them with existing results
    for i = 1, math.huge do
        local itemName = args["item" .. i]
        if not itemName then
            break
        end

        local percentChance = (tonumber(args["item" .. i .. "chance"]) or 0) * 100 -- Multiply by 100 to convert to percentage
        local yieldMin = tonumber(args["item" .. i .. "min"]) or 0
        local yieldMax = tonumber(args["item" .. i .. "max"]) or 0
        local yieldText = (yieldMin == yieldMax) and tostring(yieldMin) or (tostring(yieldMin) .. " - " .. tostring(yieldMax))

        local found = false
        for _, result in ipairs(allResults) do
            if result.itemName == itemName then
                result.adjustedChance = result.adjustedChance + percentChance
                found = true
                break
            end
        end

        if not found then
            table.insert(allResults, {
                itemName = itemName,
                yieldText = yieldText,
                adjustedChance = percentChance
            })
        end

        local data = {
            _table = "itemDrops",
            name = storeName,
            itemName = itemName,
            yieldMin = yieldMin,
            yieldMax = yieldMax,
            percentChance = percentChance
        }

        local status, result = pcall(function()
            return cargoUtil.storeData(data)
        end)

        if not status then
            storeOutput = storeOutput .. "Error storing data for item " .. itemName .. ": " .. result .. "<br>"
        end
    end

    -- Generate the output text
    local function generateOutput(results)
        if #results == 0 then
            return "'''No items found in the loot tables.'''"
        else
            local listText = "\n"  -- Add a newline here to ensure proper formatting
            for i, result in ipairs(results) do
                local listItem
                if result.itemName == "Nothing" then
                    listItem = "Nothing" .. string.format(" (%.2f%%)", result.adjustedChance)
                else
                    -- Generate the icon with yield text
                    local iconArgs = {
                        name = result.itemName,
                        amount = result.yieldText
                    }
                    local iconHtml = Icon._main(iconArgs)

                    -- Create the list item
                    listItem = tostring(iconHtml) .. string.format(" (%.2f%%)", result.adjustedChance)
                end
                listText = listText .. "* " .. listItem
                if i < #results then -- Add newline if this entry is not the last result
                    listText = listText .. "\n"
                end
            end
            return listText
        end
    end

    -- Generate the output text and return it
    local outputText = generateOutput(allResults)
    if #allResults > 0 then
        local columnStyle = #allResults < 8 and "" or "-moz-column-count: auto; -webkit-column-count: auto; column-count: auto; column-width: 220px;"
        outputText = '<div style="' .. columnStyle .. '">' .. outputText .. '</div>'
    end

    -- Combine store output and results output
    return storeOutput .. outputText
end

-- Updated processAndStoreInput to accept storeName
function lootTableUtil.processAndStoreInput(frame, storeName)
    local args = argsUtil.merge(frame)  -- Merge arguments from the current frame and its parent
    local output = mw.html.create('div')  -- Create an HTML div element for output

    -- Iterate over each table provided in the arguments
    for i = 1, math.huge do
        local tableID = args["table" .. i]
        if not tableID then 
            break 
        end

        -- Get the percent chance, yield min, and yield max from the arguments
        local percentChance = (tonumber(args["table" .. i .. "chance"]) or 0) * 100  -- Multiply by 100 to convert to percentage
        local yieldMin = tonumber(args["table" .. i .. "min"]) or 0
        local yieldMax = tonumber(args["table" .. i .. "max"]) or 0

        -- Prepare the data to be stored
        local data = {
            _table = "itemDrops",
            name = storeName, -- Use storeName as the name
            tableID = tableID, -- Store the tableID
            yieldMin = yieldMin,
            yieldMax = yieldMax,
            percentChance = percentChance
        }

        -- Attempt to store the data and capture the result
        local status, result = pcall(function()
            return cargoUtil.storeData(data)
        end)

        -- Output the result of the storage operation only if there was an error
        if not status then
            output:wikitext("Error storing data for table " .. tableID .. ": " .. result .. "<br>")
        end
    end

    return tostring(output)
end

return p