Module:Bestiary

From Path of Exile Wiki
Revision as of 08:31, 8 March 2018 by >OmegaK2
Jump to navigation Jump to search
--
-- Module for bestiary templates
--

local m_util = require('Module:Util')
local getArgs = require('Module:Arguments').getArgs

local p = {}

-- ----------------------------------------------------------------------------
-- Strings
-- ----------------------------------------------------------------------------

local i18n = {
    errors = {
        invalid_table = 'Table supplied is invalid',
    },
}

--mw.loadData('Module:QuestReward/vendor_reward_data')

-- ----------------------------------------------------------------------------
-- Helper functions and globals
-- ----------------------------------------------------------------------------

-- ----------------------------------------------------------------------------
-- Cargo
-- ----------------------------------------------------------------------------

local tables = {}

tables.recipes = {
    table = 'bestiary_recipes',
    fields = {
        id = {
            field = 'id',
            type = 'String',
        },
        header = {
            field = 'header',
            type = 'Text',
        },
        subheader = {
            field = 'subheader',
            type = 'Text',
        },
        notes = {
            field = 'notes',
            type = 'Text',
        },
    },
}

tables.components = {
    table = 'bestiary_components',
    fields = {
        id = {
            field = 'id',
            type = 'String',
        },
        min_level = {
            field = 'min_level',
            type = 'Intenger',
        },
        rarity = {
            field = 'rarity',
            type = 'String',
        },
        family = {
            field = 'family',
            type = 'String',
        },
        group = {
            field = 'group',
            type = 'String',
        },
        genus = {
            field = 'genus',
            type = 'String',
        },
        mod_id = {
            field = 'mod_id',
            type = 'String',
        },
        monster = {
            field = 'monster',
            type = 'String',
        },
    },
}

tables.recipe_components = {
    table = 'bestiary_recipe_components',
    fields = {
        recipe_id = {
            field = 'recipe_id',
            type = 'String',
        },
        component_id = {
            field = 'component_id',
            type = 'String',
        },
    },
}

-- ----------------------------------------------------------------------------
-- Page functions
-- ----------------------------------------------------------------------------

local p = {}

p.table_bestiary_recipes = m_util.cargo.declare_factory{data=tables.recipes}
p.table_bestiary_components = m_util.cargo.declare_factory{data=tables.components}
p.table_bestiary_recipe_components = m_util.cargo.declare_factory{data=tables.recipe_components}

function p.store_data(frame)
    -- Get args
    tpl_args = getArgs(frame, {
        parentFirst = true
    })
    frame = m_util.misc.get_frame(frame)
    
    if tables[tpl_args.tbl] == nil then
        error(i18n.errors.invalid_table)
    end
    
    -- mw.loadData has some problems...
    local data = require(string.format('Module:Bestiary/%s', tpl_args.tbl))
    for i=tpl_args.index_start or 1, tpl_args.index_end or #data do 
        local row = data[i]
        if row == nil then
            break
        end
        -- get full table name
        row._table = tables[tpl_args.tbl].table
        m_util.cargo.store(frame, row)
    end
    
    return string.format('Stored %s rows in "bestiary_%s" table', #data, tpl_args.tbl)
end

-- ----------------------------------------------------------------------------
-- End
-- ----------------------------------------------------------------------------

return p