Module:Companion

local p = {} local companions = mw.loadData('Module:Companion/data') local UNKNOWN_COMPANION = "Some Unknown Companion"

-- Toarray converts a table into an array. -- This is sometimes necessary to get table.concat to work. local function toarray(tbl) local a = {} for _, x in ipairs(tbl) do       table.insert(a, x)    end return a end

local function isempty(s) return s == nil or s == '' end

local function trim(s) local r, _ = s:gsub("^%s*(.-)%s*$", "%1") return r end

local function paramstring(s) if s == nil then return "" end return trim(tostring(s)) end

local function lookup(frame) return companions[p.key(frame)] end

local function get_attr(frame, key, fallback) local e = lookup(frame) if e == nil then return fallback end return tostring(e[key]) end

local function get_skill_attr(frame, key, fallback) local e = lookup(frame) if e == nil then return fallback end return tostring(e.skill[key] or "") end

function p.key(frame) local t = mw.title.getCurrentTitle local key = t.text if t:inNamespace("Module") then key = frame.pagename end key, _ = key:gsub(" %(Companion%)$", "") return key end

-- Currently key == name for all Companions, but this may change in the future -- (e.g. if something like Mighty/Zapper is released). function p.name(frame) return p.key(frame) end

function p.class(frame) return get_attr(frame, "class", "") end function p.skill_name(frame) return get_skill_attr(frame, "name", "") end function p.skill_icon(frame) return get_skill_attr(frame, "icon", "") end function p.skill_desc(frame) return get_skill_attr(frame, "desc", "") end function p.skill_target(frame) return get_skill_attr(frame, "target", "") end

function p.skill_condition(frame) local e = lookup(frame) if e == nil then return "" end if e.skill.emit == nil then return e.skill.condition or "" end return string.format('%s (%d%%)', e.skill.condition or "", e.skill.emit) end

function p.skill_effects(frame) local e = lookup(frame) if e == nil then return "" end return table.concat(toarray(e.skill.effects or {}), " ") end

function p.sortable_table local entity_icon = require("Module:Icon").entity_icon local class_order = {Z=1, SS=2, S=3, A=4, B=5, C=6, D=7}

local keys = {} for k in pairs(companions) do       table.insert(keys, k)    end table.sort(keys)

local s = {} table.insert(s, '{| class="wikitable sortable"') table.insert(s, '! class="unsortable"|Icon !! Name !! Class !! Skill') for _, key in ipairs(keys) do       local c = companions[key] table.insert(s, '|-') local icon = entity_icon{args={image=string.format('Companion_%s_icon.png', key)}} local class = string.format('data-sort-value=%d|%s', class_order[c.class] or 100, c.class) local skillicon = string.format('', c.skill.icon) table.insert(s, string.format('| %s || %s || %s || %s %s', icon, key, class, skillicon, c.skill.name)) end table.insert(s, '|}') return table.concat(s, "\n") end

function p._test_sortable_table if string.len(p.sortable_table) <= 500 then mw.log("sortable table is too short") end end

-- Run this before saving function p._test local testcases = { ["Bluebird"] = { name="Bluebird", class="S", skill_name="Magical Attack +5%, Equip", skill_condition="Equip (100%)", skill_effects="Magical Attack +5%.", },       ["Bluebird (Companion)"] = { name="Bluebird", class="S", skill_name="Magical Attack +5%, Equip", skill_condition="Equip (100%)", skill_effects="Magical Attack +5%.", },       [UNKNOWN_COMPANION] = { name=UNKNOWN_COMPANION, class="", skill_name="", skill_condition="", skill_effects="", },   }    for pname, tc in pairs(testcases) do        for fname, out in pairs(tc) do            local x = p[fname]{pagename=pname, args={}} if x ~= out then mw.log(string.format("%s %s is %s; expected %s", pname, fname, x, out)) end end end p._test_sortable_table end

return p