Module:Sandbox

From Path of Exile 2 Wiki
Revision as of 15:34, 2 July 2015 by >OmegaK2
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]


This page is not an actual Scribunto module. It exists to provide editors a place to create experimental modules.

Naming your modules

To keep things tidy, please use the following format to name your experimental modules:

Module:Sandbox/Your username/Module name

Cleaning up unused modules

Experimental modules may be deleted by admins upon request or after a long period of inactivity.

List of modules in this area

For a list of the experimental modules under Module:Sandbox, see Special:PrefixIndex/Module:Sandbox/.

local p = {}
local getArgs

-- for testing
local quest_data = {
    {
        reward="Ground Slam",
        reward_type="skill",
        class="Marauder",
        difficulty="Normal",
        quest="Enemy at the Gate",
    },
    {
        reward="Molten Strike",
        reward_type="skill",
        class="Marauder",
        difficulty="Normal",
        quest="Enemy at the Gate",
    },
}

local poe_classes = {'Marauder', 'Templar', 'Witch', 'Shadow', 'Ranger', 'Duelist', 'Scion'}

function p.table(frame)
    if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
    local args = getArgs(frame, {
		parentFirst = true
	})
    
    args.reward = args.reward or args.Reward
    args.reward_type = args.reward_type or args.rewardType or args.RewardType
    args.class = args.class or args.Class
    args.difficulty = args.difficulty or args.Difficulty
    args.quest = args.quest or args.Quest
    args.display_style = args.display_style or args.displayStyle or args.DisplayStyle or 'full'
    
    -- parser
    
    local return_data = {}
    
    
    for key, row in ipairs(quest_data) do
        (function()
            for k, v in ipairs({"reward", "reward_type", "class", "quest", "difficulty"}) do
                if args[v] ~= nil and row[v] ~= args[v] then return end
            end
            
            table.insert(return_data, row)
        end)()
    end
    
    local outstr
    if args.display_style == 'full' then
        outstr = display_full(return_data)
    end
    
    return outstr
end

function display_full(return_data)
    local tbl = mw.html.create('table')
    local tblrow = tab:tag('tr')
    
    tbl
        :addClass('wikitable')
        
    tblrow
        :tag('th')
            :wikitext('Class')
    for garbage, value in ipairs(poe_classes) do
        tblrow
            :tag('th')
                :attr('rowspan', 2)
                :wikitext('[[' .. value .. ']]')
    end
    tbl
        :tag('tr')
            :tag('th')
                :wikitext('Quest')
    
    local format_data = {}
    
    -- reformat to make proper lists
    -- quest_diff: class: [rowA, rowB]
    for garbage, row in ipairs(return_data) do
        key = row['quest'] .. '_' .. row['difficulty']
        if format_data[key] == nil then
            --format_data[key] = {row['class'] = {row}}
            format_data[key] = {}
        elseif format_data[key][row['class']] == nil then
            format_data[key][row['class']] = {row}
        else
            table.insert(format_data[key][row['class']], row)
        end
    end
    
    -- lastly add to table
    for key, row in ipairs(format_data) do
        local tblrow = tbl:tag('tr')
        if row['All'] ~= nil then
            tblrow
                :tag('td')
                    :attr('colspan', 7)
                    :wikitext(format_reward(row['All']))
        else
            for garbage, class in ipairs(poe_classes) do
                local txt = ''
                if row['class'] ~= nil then
                    txt = format_reward(row[class])
                end
                tblrow
                    :tag('td')
                        :wikitext(txt)
            end
        end
        
    end
    
    return tostring(tbl)
end

function format_reward(row)
    -- Take a row and format it accordingly
    local output
    if row['reward_type'] == 'skill' then
        output = frame:expandTemplate{title='sl', args={row['reward']}}
    elseif row['reward_type'] == 'item' then
        output = frame:expandTemplate{title='il', args={row['reward']}}
    else
        output = row['reward']
    end
     
    if row['display'] ~= nil then
        output = replace_vars{row[display], reward=output}
    end
    
    return output
end


function replace_vars(str, vars)
  -- Allow replace_vars{str, vars} syntax as well as replace_vars(str, {vars})
  if not vars then
    vars = str
    str = vars[1]
  end
  return (string_gsub(str, "({([^}]+)})",
    function(whole,i)
      return vars[i] or whole
    end))
end

return p