Welcome to the Umamusume Wiki! If you want to contribute, please read the guidelines.

Module:Game/Skills/Data/Groups

From Umamusume Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Game/Skills/Data/Groups/doc

--[[
    !! THIS PAGE IS MANAGED BY GITLAB !!
    ANY EDITS TO PAGE CONTENT WILL BE OVERWRITTEN
    TO MAKE CHANGES, PLEASE SUBMIT A MERGE REQUEST AT https://gitlab.com/umamusume-wiki/lua-modules
]]

local Groups = {}
local Game = require("Module:Game")

---Get a list of skill IDs belonging to the given group IDs
---@param groupIds string[]
---@param ignoreNegative boolean|nil
---@return string[][] skillIds keyed by group ID
function Groups.getSkillGroups(groupIds, ignoreNegative)
    local groupIdsInsert = table.concat(groupIds, ",")
    local datas = Game.queryMaster {
        from = "skill_data",
        where = string.format("group_id IN (%s) %s", groupIdsInsert, ignoreNegative and "AND group_rate >= 0" or ""),
        order_by = "group_rate",
        data = {
            groupId = "group_id",
            id = "id"
        }
    }
    local ids = {}
    for _, data in ipairs(datas) do
        if ids[data.groupId] == nil then
            ids[data.groupId] = {}
        end
        table.insert(ids[data.groupId], data.id)
    end
    return ids
end

---Get a list of skill IDs and skill levels belonging to the given skill set IDs
---@param setIds string[]
---@return [string,number][][] skillIdLevelPairs keyed by set ID
function Groups.getSkillSets(setIds)
    local setIdsInsert = table.concat(setIds, ",")
    local datas = Game.queryMaster {
        from = "skill_set",
        where = string.format("id IN (%s)", setIdsInsert),
    }

    local idMap = {}
    for _, data in ipairs(datas) do
        local id = {}
        for i = 1, 20 do
            local skillId = data['skill_id' .. i]
            local skillLevel = data['skill_level' .. i]
            if skillId ~= nil and skillId ~= '0' then
                table.insert(id, { skillId, tonumber(skillLevel) or 0 })
            end
        end
        idMap[data.id] = id
    end
    return idMap
end

---Gets a the upgrade and downgrade for the given skill in the given group
---@param paired [string, string][] group ID and skill ID pair
---@return (string|nil)[] upgrade, (string|nil)[] downgrade keyed by skill ID
function Groups.getUpgradesAndDowngrades(paired)
    local groupIds = {}
    local skillIds = {}
    local skillIdGroups = {}
    for _, pair in ipairs(paired) do
        table.insert(groupIds, pair[1])
        table.insert(skillIds, pair[2])
        skillIdGroups[pair[2]] = pair[1]
    end

    local groupSkills = Groups.getSkillGroups(groupIds)

    local ups = {}
    local downs = {}
    for _, skillId in ipairs(skillIds) do
        local group = groupSkills[skillIdGroups[skillId]]

        local indexOfSkill = -1
        for i, v in ipairs(group) do
            if v == skillId then
                indexOfSkill = i
                break
            end
        end

        for i, v in ipairs(group) do
            if i == indexOfSkill + 1 then
                ups[skillId] = v
            elseif i == indexOfSkill - 1 then
                downs[skillId] = v
            end
        end
    end

    return ups, downs
end

---Gets a the cost for upgrading to the given skill in its chain
---@param paired [string, string][] group ID and skill ID pair
---@return (number|nil)[] costs keyed by skill ID
function Groups.getUpgradePathCosts(paired)
    local groupIds = {}
    local skillIds = {}
    local skillIdGroups = {}
    for _, pair in ipairs(paired) do
        table.insert(groupIds, pair[1])
        table.insert(skillIds, pair[2])
        skillIdGroups[pair[2]] = pair[1]
    end

    local upgradeChains = Groups.getSkillGroups(groupIds, true)

    -- just query all of them to save the hassle of deduping every chain
    local needPointDatas = Game.queryMaster {
        from = "single_mode_skill_need_point",
    }
    local pointMap = {}
    for _, needPointData in ipairs(needPointDatas) do
        pointMap[needPointData.id] = needPointData.need_skill_point
    end

    local addlCosts = {}
    for _, skillId in ipairs(skillIds) do
        local previousUpgrades = {}
        for _, v in ipairs(upgradeChains[skillIdGroups[skillId]] or {}) do
            -- skill group should be sorted from low to high
            if v == skillId then break end
            table.insert(previousUpgrades, v)
        end
        if #previousUpgrades > 0 then
            addlCosts[skillId] = 0
            for _, v in ipairs(previousUpgrades) do
                addlCosts[skillId] = addlCosts[skillId] + pointMap[v]
            end
        end
    end

    return addlCosts
end

---Gets the card ID of the card that this skill is an ult skill for
---@param skillIds string[]
---@return (string|nil)[] cardId keyed by skillId
function Groups.getUltimateForCards(skillIds)
    local skillIdsInsert = table.concat(skillIds, ",")
    local datas = Game.queryMaster {
        from = "card_rarity_data, skill_set",
        join_on = "card_rarity_data.skill_set = skill_set.id",
        where = string.format("skill_set.skill_id1 IN (%s)", skillIdsInsert),
        data = {
            skill_id = "skill_id1",
            card_id = "card_id"
        },
        limit = 1,
    }
    local ults = {}
    for _, data in ipairs(datas) do
        ults[data.skill_id] = data.card_id
    end
    return ults
end

---Gets the inherited skill version of the given unique skill IDs
---@param skillIds string[]
---@return string[] inheritSkillIds keyed by skill ID
function Groups.getInheritForSkills(skillIds)
    local skillIdsInsert = table.concat(skillIds, ",")
    local inheritSkillDatas = Game.queryMaster {
        from = "skill_data",
        where = string.format("unique_skill_id_1 IN (%s) OR unique_skill_id_2 IN (%s)", skillIdsInsert, skillIdsInsert),
    }
    local inheritMap = {}
    for _, inheritSkillData in ipairs(inheritSkillDatas) do
        inheritMap[inheritSkillData.unique_skill_id_1] = inheritSkillData.id
        inheritMap[inheritSkillData.unique_skill_id_2] = inheritSkillData.id
    end
    return inheritMap
end

return Groups