Module:Quest reward

From Path of Exile Wiki
Revision as of 17:11, 28 April 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 = 'Invalid table selected. Valid choices are either quest or vendor',
    },
    messages = {
        storage = 'Attempted to store %s rows in "%s_rewards" table',
    },
}

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

local tables = {}

tables.quest_rewards = {
    table = 'quest_rewards',
    fields = {
        quest = {
            field = 'quest',
            type = 'String',
        },
        quest_id = {
            field = 'quest_id',
            type = 'Integer',
        },
        -- still needed?
        act = {
            field = 'act',
            type = 'Integer',
        },
        classes = {
            field = 'classes',
            type = 'String',
        },
        sockets = {
            field = 'sockets',
            type = 'Integer',
        },
        page = {
            field = 'page',
            type = 'Page',
        },
        item_level = {
            field = 'item_level',
            type = 'Integer',
        },
        rarity = {
            field = 'rarity',
            type = 'String',
        },
        reward = {
            field = 'reward',
            type = 'String',
        },
    },
}

tables.vendor_rewards = {
    table = 'vendor_rewards',
    fields = {
        quest = {
            field = 'quest',
            type = 'String',
        },
        quest_id = {
            field = 'quest_id',
            type = 'Integer',
        },
        act = {
            field = 'act',
            type = 'Integer',
        },
        reward = {
            field = 'reward',
            type = 'String',
        },
        npc = {
            field = 'npc',
            type = 'String',
        },
        classes = {
            field = 'classes',
            type = 'String',
        },
    }
}

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

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

local p = {}

p.table_quest_rewards = m_util.cargo.declare_factory{data=tables.quest_rewards}
p.table_vendor_rewards = m_util.cargo.declare_factory{data=tables.vendor_rewards}

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 .. '_rewards'] == nil then
        error(i18n.errors.invalid_table)
    end
    
    -- mw.loadData has some problems...
    local data = require(string.format('Module:Quest reward/data/%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 .. '_rewards'].table
        m_util.cargo.store(frame, row)
    end
    
    return string.format(i18n.messages.storage, #data, tpl_args.tbl)
end

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

return p