Module:Sandbox: Difference between revisions

From Path of Exile 2 Wiki
Jump to navigation Jump to search
>OmegaK2
No edit summary
>OmegaK2
No edit summary
Line 123: Line 123:
                 if row_data['classes'][class] ~= nil then
                 if row_data['classes'][class] ~= nil then
                     for garbage2, row in ipairs(row_data['classes'][class]) do
                     for garbage2, row in ipairs(row_data['classes'][class]) do
                         txt = format_reward(row) .. '<br>'
                         txt = txt .. format_reward(row) .. '<br>'
                     end
                     end
                 end
                 end

Revision as of 16:59, 2 July 2015

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 g_frame
local getArgs = require('Module:Arguments').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
	})
    g_frame = frame
    
    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 tblhead = tbl:tag('tr')
    
    tbl
        :addClass('wikitable')
        
    tblhead
        :tag('th')
            :wikitext('Class')
    for garbage, value in ipairs(poe_classes) do
        tblhead
            :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
        local key = row['quest'] .. '_' .. row['difficulty']
        local sub = format_data[key]

        if sub == nil then
            format_data[key] = {
                quest=row['quest'], 
                difficulty=row['difficulty'],
                classes={}
            }
            format_data[key]['classes'][row['class']] = {row}
            
        elseif sub['classes'][row['class']] == nil then
            sub['classes'][row['class']] = {row}
        else
            table.insert(sub['classes'][row['class']], row)
        end
    end
    for quest_diff, row_data in pairs(format_data) do
        mw.log(quest_diff, row_data)
        local tblrow = tbl:tag('tr')
        tblrow
            :tag('td')
                :wikitext('[[' .. row_data['quest'] .. ']]')
        
        if row_data['classes']['All'] ~= nil then
            tblrow
                :tag('td')
                    :attr('colspan', 7)
                    :wikitext(format_reward(row_data['classes']['All']))
        else
            for garbage, class in ipairs(poe_classes) do
                mw.log(class, row_data['classes'][class])
                local txt = ''
                if row_data['classes'][class] ~= nil then
                    for garbage2, row in ipairs(row_data['classes'][class]) do
                        txt = txt .. format_reward(row) .. '<br>'
                    end
                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 = g_frame:expandTemplate{title='sl', args={row['reward']}}
    elseif row['reward_type'] == 'item' then
        output = g_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