Module:Creep mission table

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

For the {{Creep mission table}} template.

Subpages

local cargoUtil = require("Module:CargoUtil")

local p = {}

local planetsOrder = {
    "Grey Planet", "Blue Reef", "Desert Dunes", "Iceladus", "Lava Lakes", "Viridis"
}

local killRanges = {
    easy = { 4, 12 },
    hard = { 2, 5 },
    special = { 4, 8 }
}

-- Function to calculate the reward range based on the kill range
local function calculateRewardRange(lowerBound, upperBound)
    local lowerReward = 20 * lowerBound * 1.5
    local upperReward = 20 * upperBound * 1.5
    return lowerReward .. "-" .. upperReward
end

function p.main(frame)
    local queryOptions = {
        tables = "Creeps, creepLocation",
        fields = "Creeps.name, Creeps.difficulty, creepLocation.planet, creepLocation.enemyName",
        join = "Creeps.name=creepLocation.enemyName",
        where = "creepLocation.planet IN ('Grey Planet', 'Blue Reef', 'Desert Dunes', 'Iceladus', 'Lava Lakes', 'Viridis')",
        groupBy = "", -- Group by if needed
        limit = 100,
        orderBy = "creepLocation.planet" -- Order by planet to ensure correct order
    }

    local results = cargoUtil.queryData(queryOptions)

    local resultsTable = {}

    -- Process results
    for _, planet in ipairs(planetsOrder) do
        for _, row in ipairs(results) do
            local creepName = row["Creeps.name"]
            local creepDifficulty = row["Creeps.difficulty"]
            local enemyPlanet = row["creepLocation.planet"]
            local enemyName = row["creepLocation.enemyName"]

            -- Check if enemyPlanet is in planetsOrder
            if enemyPlanet == planet then
                local difficulty = string.lower(creepDifficulty)
                local killRange = killRanges[difficulty]
                if killRange then
                    local rewardRange = calculateRewardRange(killRange[1], killRange[2])

                    table.insert(resultsTable, {
                        planet = enemyPlanet,
                        planetImage = "[[File:" .. enemyPlanet:gsub(" ", "_") .. ".png|link=" .. enemyPlanet:gsub(" ", "_") .. "|50px]]",
                        name = "[[File:" .. enemyName .. ".png|link=|" .. enemyName .. "|40px]] [[" .. creepName .. "]]",
                        killRange = table.concat(killRange, "-"),
                        rewardRange = rewardRange
                    })
                else
                    -- Handle case where creepDifficulty does not match any key in killRanges
                    mw.log("Invalid difficulty for creep: " .. creepDifficulty)
                end
            end
        end
    end

    -- Generate HTML table
    local html = mw.html.create('table'):addClass('lkg-table mw-collapsible mw-collapsed tablemedium tdc1 tdc2 tdc3 tdc4')
    
    -- Row for "Potential Requests" spanning all columns
    local potentialRequestsRow = mw.html.create('tr')
    potentialRequestsRow:tag('td')
        :attr('colspan', 4)
        :wikitext('Potential Requests')
    html:node(potentialRequestsRow)

    -- Table headers
    local headerRow = mw.html.create('tr')
    headerRow:tag('th'):wikitext('Planet'):done()
    headerRow:tag('th'):wikitext('Creep Name'):done()
    headerRow:tag('th'):wikitext('Kill Range'):done()
    headerRow:tag('th'):wikitext('Reward Range'):done()
    html:node(headerRow)

    -- Populate the table with results
    for _, result in ipairs(resultsTable) do
        local dataRow = mw.html.create('tr')
        dataRow:tag('td'):wikitext(result.planetImage):done()
        dataRow:tag('td'):wikitext(result.name):done()
        dataRow:tag('td'):wikitext(result.killRange):done()
        dataRow:tag('td')
            :wikitext(result.rewardRange)
            :wikitext(' [[File:Credits.png|link=|20px]]') -- Adding the icon after the reward range
            :done()
        html:node(dataRow)
    end

    return tostring(html)
end

return p