Module:Moons

From Lethal Company Wiki

Documentation for this module may be created at Module:Moons/doc

-- AUTO PACTHED FILE, DO NOT EDIT!!

local p = {}

p['versions'] = { 35, 36, 37, 38, 40, 45, 47, 48, 49, 50, 55, 56, 61, 64 }

local GameVersion = require('Module:GameVersion')
local WikiAutomated = require('Module:WikiAutomated')

---@param version version? defaults to 'latest'
function p.getDataRoot(version)
    if version == nil then
        version = 'latest'
    end

    local ver = GameVersion.parseVersion(version, p['versions'])
    return mw.loadData('Module:Moons/Data/v' .. ver)
end

---@param version version? defaults to 'latest'
function p.getRawData(version)
    return p.getDataRoot(version)["data"]
end

---@param version version? defaults to 'latest'
function p.getData(version)
    local rawData = p.getRawData(version)

    local versionParsed = GameVersion.parseVersion(version, p.versions)
    local hasOverrideData, result = pcall(mw.loadData, 'Module:Moons/DataOverride')

    if hasOverrideData then
        local returnTable = rawData

        local defaultOverrideTable = result.default
        local versionOverrideTable = result[versionParsed]

        if defaultOverrideTable ~= nil then
            returnTable = WikiAutomated.tableExt.patch(returnTable, defaultOverrideTable)
        end

        if versionOverrideTable ~= nil then
            returnTable = WikiAutomated.tableExt.patch(returnTable, versionOverrideTable)
        end

        return returnTable
    else
        if not NO_OVERRIDE_DATA_WARNED then
            mw.log('Override data not found: ' .. result)
            NO_OVERRIDE_DATA_WARNED = true
        end

        return rawData
    end
end

MOON_ALIASES = {
    ExperimentationLevel = { "Exp", "Experimentation" },
    AssuranceLevel = { "Ass", "Assurance" },
    VowLevel = { "Vow" },

    OffenseLevel = { "Offense" },
    MarchLevel = { "March" },
    AdamanceLevel = { "Ada", "Adam", "Adamance" },

    EmbrionLevel = { "Emb", "Embrion" },

    RendLevel = { "Rend" },
    DineLevel = { "Dine" },
    TitanLevel = { "Titan" },
    ArtificeLevel = { "Art", "Artifice" },
    LiquidationLevel = { "Liq", "Liquid", "Liquidation" },

    CompanyBuildingLevel = { "Com", "Company", "Gordion" },
}
p.MOON_ALIASES = MOON_ALIASES

MOON_KEY_INDEX = {
    ExperimentationLevel = 1,
    AssuranceLevel = 2,
    VowLevel = 3,

    OffenseLevel = 10,
    MarchLevel = 11,
    AdamanceLevel = 12,

    EmbrionLevel = 20,

    RendLevel = 30,
    DineLevel = 31,
    TitanLevel = 32,
    ArtificeLevel = 33,
    LiquidationLevel = 34,

    CompanyBuildingLevel = 100,
}
p.MOON_KEY_INDEX = MOON_KEY_INDEX

MOON_KEY_INDEX_REVERSE = {}
for moonKey, idx in pairs(MOON_KEY_INDEX) do
    MOON_KEY_INDEX_REVERSE[idx] = moonKey
end
p.MOON_KEY_INDEX_REVERSE = MOON_KEY_INDEX_REVERSE

V35_LEVEL_KEY_ORDER = {
    ExperimentationLevel = 1,
    AssuranceLevel = 2,
    VowLevel = 3,
    CompanyBuildingLevel = 4,
    MarchLevel = 5,
    RendLevel = 6,
    DineLevel = 7,
    OffenseLevel = 8,
    TitanLevel = 9,

    AdamanceLevel = 100,
    ArtificeLevel = 101,
    LiquidationLevel = 102,
    EmbrionLevel = 103,
}
V50_LEVEL_KEY_ORDER = {
    ExperimentationLevel = 1,
    AssuranceLevel = 2,
    VowLevel = 3,
    CompanyBuildingLevel = 4,
    MarchLevel = 5,
    AdamanceLevel = 6,
    RendLevel = 7,
    DineLevel = 8,
    OffenseLevel = 9,
    TitanLevel = 10,
    ArtificeLevel = 11,
    LiquidationLevel = 12,
    EmbrionLevel = 13,
}
LEVEL_KEY_ORDERS = {
    [35] = V35_LEVEL_KEY_ORDER,
    [36] = V35_LEVEL_KEY_ORDER,
    [37] = V35_LEVEL_KEY_ORDER,
    [38] = V35_LEVEL_KEY_ORDER,
    [40] = V35_LEVEL_KEY_ORDER,
    [45] = V35_LEVEL_KEY_ORDER,
    [47] = V35_LEVEL_KEY_ORDER,
    [48] = V35_LEVEL_KEY_ORDER,
    [49] = V35_LEVEL_KEY_ORDER,

    [50] = V50_LEVEL_KEY_ORDER,
    [55] = V50_LEVEL_KEY_ORDER,
    [60] = V50_LEVEL_KEY_ORDER,
    [61] = V50_LEVEL_KEY_ORDER,
    [62] = V50_LEVEL_KEY_ORDER,
    [64] = V50_LEVEL_KEY_ORDER,
}
p.LEVEL_KEY_ORDERS = LEVEL_KEY_ORDERS
p.LEVEL_KEY_ORDER = LEVEL_KEY_ORDERS[50]

local function sortMoonKeys(key1, key2)
    return MOON_KEY_INDEX[key1] < MOON_KEY_INDEX[key2]
end
p.sortMoonKeys = sortMoonKeys

---@param version? integer
local function sortMoonKeysByLevel(key1, key2, version)
    local ver = GameVersion.parseVersion(version, p.versions)
    return LEVEL_KEY_ORDERS[ver][key1] < LEVEL_KEY_ORDERS[ver][key2]
end
p.sortMoonKeysByLevel = sortMoonKeysByLevel

--[[
get an index for a moon key

Note: The index is not extracted from game and may change anytime, so this key is
recommended for internal use only, e.g. a convenient sorting key for `table.sort()`.
]]
---@param moonKey string A moon key, e.g. ExperimentationLevel
---@return integer index A positive integer. Returns `-1` if moon key not found.
function p.getMoonKeyIndex(moonKey)
    local idx = MOON_KEY_INDEX[moonKey]
    if idx == nil then
        mw.log('Warning: Cannot find moon key index for moon key [' .. moonKey .. ']')
        return -1
    end
    return idx
end

--[[
map the input to the game internal key
e.g. art -> ArtificeLevel, Dine -> DineLevel
]]
---@param input string|number
function p.getMoonKey(input)
    if type(input) == "number" and input > -1 then
        return MOON_KEY_INDEX_REVERSE[input]
    end

    local inputLowered = string.lower(input)

    for key, aliases in pairs(MOON_ALIASES) do
        if key:lower() == inputLowered then
            return key
        end

        for _, alias in ipairs(aliases) do
            if alias:lower() == inputLowered then
                return key
            end
        end
    end

    -- keys and aliases all looked, no matching input
    error("Cannot parse moon input: " .. input)
end

function p.getMoonInfo(moon, version)
    local moonKey = p.getMoonKey(moon)
    local data = p.getData(version)

    return data[moonKey]
end

function p.isMoonExistInVersion(moon, version)
    return p.getMoonInfo(moon, version) ~= nil
end

local function getMoonList(version)
    local data = p.getData(version)
    local moons = {}
    for _, moonInfo in pairs(data) do
        table.insert(moons, moonInfo.mName)
    end
    table.sort(moons, sortMoonKeys)
    return moons
end
p.getMoonList = getMoonList

---@param filterFunc fun(moon: table): boolean
local function getMoonListWithFilter(version, filterFunc)
    local data = p.getData(version)
    local moons = {}
    for _, moonInfo in pairs(data) do
        if filterFunc(moonInfo) then
            table.insert(moons, moonInfo.mName)
        end
    end
    table.sort(moons, sortMoonKeys)
    return moons
end
p.getMoonListWithFilter = getMoonListWithFilter

local function getSpawnableMoonList(version)
    local data = p.getData(version)
    local moons = {}
    for _, moonInfo in pairs(data) do
        if moonInfo.spawnEnemiesAndScraps then
            table.insert(moons, moonInfo.mName)
        end
    end
    table.sort(moons, sortMoonKeys)
    return moons
end
p.getSpawnableMoonList = getSpawnableMoonList

local function getLevelList(version)
    local data = p.getData(version)
    local moons = {}
    for _, moonInfo in pairs(data) do
        table.insert(moons, moonInfo.mName)
    end
    table.sort(moons, function(a, b) return sortMoonKeysByLevel(a, b, version) end)
    return moons
end
p.getLevelList = getLevelList


return p