Module:Kickstarter/Pack

local TableTools = require('Module:TableTools') local Utils = require('Module:Utils') local mw = mw

local string = string local table = table local sprintf = Utils.sprintf local startsWith = Utils.startsWith local addCommas = Utils.addCommas local flatten = Utils.flatten local tableIndex = Utils.tableIndex local sumArray = Utils.sumArray local shallowClone = TableTools.shallowClone

-- ----- MODULE FUNCTIONS --- -- local p = require("Module:BaseModule"):newModule local module_data = p:getModuleData("Module:Kickstarter/Pack/data")

local main_args = { parentFirst = true, wrappers = { "Template:Kickstarter/Pack" } }

p.main = p:makeInvokeFunc("_main", main_args)

-- ---- INTERNAL FUNCTIONS -- -- -- The pack's actual level local function getRealPackLevel(pack) local level = tableIndex(module_data['packs'], pack)

if level == nil then level = 0 end

return level end

-- Pack level is used to calculate previous rewards; any pack > 12 is considered to be 12 (Creator) local function getPackLevel(pack) local level = getRealPackLevel(pack)

if level > 12 then level = 12 end

return level end

-- For a given pack, merge all lower levels and return all rewards local function getPackData(pack) local level = getPackLevel(pack) if level == 0 then return end

local real_level = getRealPackLevel(pack)

local copy_keys = module_data['copy_keys'] local ret = shallowClone(module_data[pack]['base_data'])

-- Get packs up to our level local packs = {} local i = 1

for _, pack_name in ipairs(module_data['packs']) do       table.insert(packs, pack_name) if i == level then break end i = i + 1 end

-- Check for levels > Creator if real_level > level then table.insert(packs, pack) end

-- Merge all pack benefits up to our level for _, pack_name in ipairs(packs) do       for k, v in pairs(module_data[pack_name]) do            if tableIndex(copy_keys, k) then if ret[k] == nil then ret[k] = shallowClone(v) else ret[k] = flatten({ret[k], shallowClone(v)}) end end end end

-- Sum up the cumulative rewards for _, v in ipairs(module_data['sum_keys']) do       if ret[v] ~= nil then ret[v] = sumArray(ret[v]) end end

return ret end

-- -- PAGE FUNCTIONS -- function p._main(args, frame) local pack_name = args[1] local pack = getPackData(pack_name) if pack == nil then return end

local level = getPackLevel(pack_name) local myframe = frame:getParent or frame

local img_name if startsWith(pack_name, "Legend: ") then img_name = sprintf("File:Kickstarter_%s.png", string.gsub(pack_name, "Legend: ", "")) else img_name = sprintf("File:Kickstarter_%s.png", pack_name) end

-- Create HTML output local page = mw.html.create('div')

local right_content = mw.html.create('div') :cssText("width:30%;float:right; margin-left:25px;padding:0")

-- Right side image local img_div = right_content :tag('div') :cssText("height:92px; margin-bottom:8px")

if level > 1 then img_div:wikitext(sprintf('frameless|center|250px|link=', img_name)) end img_div:done

-- Right side "Uncategorized" table local basic_tbl = right_content :tag('table') :attr('class', 'wikitable') :cssText('width:100%') :tag('tr') :tag('th') :wikitext('Uncategorized Rewards*') :done :done for _, v in ipairs(pack['Basic']) do       basic_tbl :tag('tr') :tag('td') :wikitext(v) :done :done end basic_tbl :tag('caption') :cssText('caption-side:bottom;font-size:90%;text-align:left;margin-top:5px;') :wikitext('*all previous rewards already included') :done

-- Right side "Cumulative" table if level > 1 then local cumulative_tbl = right_content :tag('table') :attr('class', 'wikitable') :cssText('width:100%') :tag('tr') :tag('th') :attr('colspan', '2') :wikitext('Cumulative Rewards*') :done :done

for _, label in ipairs(module_data['sum_keys']) do           local val = pack[label] local retail = '' local suffix = ''

if val ~= nil then if label == 'Character slot' then label = 'Character slot (extra)' elseif label == 'Carpenter NPC' then label = 'Carpenter NPC contract' if val <= 1 then suffix = ' month' else if val < 100 then suffix = ' months' else val = 'Lifetime' end end else local amount if label == 'Dynamight token' then amount = (val/2000)*10 retail = sprintf(" (retail €%s)", addCommas(amount)) val = addCommas(val) else amount = val*10 retail = sprintf(" (retail €%s)", addCommas(amount))

if val <= 1 then suffix = ' month' else if val < 100 then suffix = ' months' else val = 'Lifetime' retail = '' end end end

label = sprintf("%s", label) end

cumulative_tbl :tag('tr') :tag('td') :wikitext(label) :done :tag('td') :wikitext(sprintf("%s%s%s", val, suffix, retail)) :done :done end end

cumulative_tbl :tag('caption') :cssText('caption-side:bottom;font-size:90%;text-align:left;margin-top:5px;') :wikitext('*all previous rewards already included') :done end right_content:done

-- Main page content local reg_backers_text = 'backers' local eb_cost_text = '' local eb_backers_text = ''

if pack['reg_backers'] == 1 then reg_backers_text = 'backer' end

if pack['eb_cost'] ~= nil then eb_cost_text = sprintf(" (€%s early bird)", addCommas(pack['eb_cost']))

if pack['eb_backers'] == 1 then eb_backers_text = sprintf(" and %s early bird backer", addCommas(pack['eb_backers'])) else eb_backers_text = sprintf(" and %s early bird backers", addCommas(pack['eb_backers'])) end end

page :node(right_content) :tag('div') :wikitext(sprintf( "The %s pack had a regular pledge of €%s or more%s with %s regular %s%s.", pack_name, addCommas(pack['reg_cost']), eb_cost_text, addCommas(pack['reg_backers']), reg_backers_text, eb_backers_text ))       :done

local sections = page :tag('div') :cssText('width:65%; padding-top:25px')

local section_count = 1 local max_sections = #module_data['sections']

for _, section in ipairs(module_data['sections']) do       if pack[section] ~= nil then if section_count == 1 then sections:newline end

sections :wikitext(myframe:preprocess(sprintf("== %s ==", section))) :newline

local section_item_count = 1 local max_section_items = #pack[section]

for _, text in ipairs(pack[section]) do               sections:wikitext(sprintf("* %s", text))

if section_count ~= max_sections then sections:newline else if section_item_count ~= (max_section_items - 1) then sections:newline end end

section_item_count = section_item_count + 1 end

section_count = section_count + 1 end end sections:done

return tostring(page:allDone) end

return p