FANDOM


--[[
CHARACTER DATA
Fetches character data from their respective modules and formats them as needed.
In an MVC setup, this is the "Model" concept.
By: dragonjet
]]--
 
local CharacterData = {}
 
-- get data for a specific character
function CharacterData:get(characterName, formName)
    -- import the character data module
    local CharacterModule = require("Module:" .. characterName)
 
    -- default form name if not specified = "base" (non-release form)
    if not formName then formName = "base" end
 
    -- build and return the form data
    return CharacterData:form(CharacterModule, formName)
end
 
-- get data for a specific form of a character
function CharacterData:form(moduleData, formName)
    if formName == "base" then
        return mw.clone(moduleData.base)
    else
        -- base data of the character into a new variable
        local MergedForm = mw.clone(moduleData["base"])
 
        -- target form to get overrides from
        local TargetForm = mw.clone(moduleData[formName])
 
        -- check if target form even exists
        if TargetForm then
            -- override base info in favor of target form's info
            if TargetForm.info then
                for k,v in pairs(TargetForm.info) do
                    MergedForm.info[k] = v
                end
            end
 
            -- override base stats favor of target form's stats
            if TargetForm.stats then
                for k,v in pairs(TargetForm.stats) do
                    MergedForm.stats[k] = v
                end
            end
 
            -- override base wiki data in favor of target form's wiki
            if TargetForm.wiki then
                for k,v in pairs(TargetForm.wiki) do
                    MergedForm.wiki[k] = v
                end
            end
 
            -- override full CG list
            if TargetForm.cg then
                MergedForm.cg = mw.clone(TargetForm.cg)
            end
 
        else
            -- Target form does not exist in the character module
            error("[Arknights] " .. MergedForm.info.name .. " doesn't have data for " .. formName)
 
        end
 
        return MergedForm
    end
end
 
return CharacterData