ATTENTION! The process of updating WiKi to version Eco 10.x has begun. Those wishing to participate can find out more Information on our ECO Contribution Wiki Discord.

Module:Utils: Difference between revisions

From Eco - English Wiki
[checked revision][unchecked revision]
No edit summary
m (Use tabs for indentation, as per MediaWiki coding conventions.)
Line 3: Line 3:
-- Trims and parses the args into a table, then returns the table
-- Trims and parses the args into a table, then returns the table
function p.normaliseArgs(frame)
function p.normaliseArgs(frame)
    local origArgs = frame:getParent().args
local origArgs = frame:getParent().args
    local args = {}
local args = {}


    for k, v in pairs(origArgs) do
for k, v in pairs(origArgs) do
        v = mw.text.trim(tostring(v))
v = mw.text.trim(tostring(v))
        if v ~= '' then
if v ~= '' then
            args[k] = v
args[k] = v
        end
end
    end
end


    return args
return args
end
end


function p.checkImage(name, too_expensive)
function p.checkImage(name, too_expensive)
    local icon = name:gsub('%s+', '') .. '_Icon.png'
local icon = name:gsub('%s+', '') .. '_Icon.png'
    if too_expensive then
if too_expensive then
        return icon
return icon
    end
end


    if mw.title.makeTitle('File', icon).file.exists then
if mw.title.makeTitle('File', icon).file.exists then
        return icon
return icon
    else
else
        return 'NoImage.png'
return 'NoImage.png'
    end
end
end
end


local function in_array(item, array)
local function in_array(item, array)
    -- Should only use on short arrays
-- Should only use on short arrays
    local set = {}
local set = {}
    for _, l in ipairs(array) do
for _, l in ipairs(array) do
        set[l] = true
set[l] = true
    end
end
    return set[item] ~= nil
return set[item] ~= nil
end
end


Line 44: Line 44:
---@param too_expensive boolean|nil
---@param too_expensive boolean|nil
function p.build_icon(name, link, size, bg, border, too_expensive)
function p.build_icon(name, link, size, bg, border, too_expensive)
    -- Size options are iconNormal or iconRecipe - 64px or 44px - defaults to iconNormal
-- Size options are iconNormal or iconRecipe - 64px or 44px - defaults to iconNormal


    local L = require('Module:Localization') -- local import
local L = require('Module:Localization') -- local import


    if not size then
if not size then
        size = 'iconNormal'
size = 'iconNormal'
    end
end
    local icon_bg
local icon_bg
    if bg then
if bg then
        icon_bg = bg
icon_bg = bg
    end
end
    local icon_border
local icon_border
    if border then
if border then
        icon_border = border
icon_border = border
    end
end


    local item_data = mw.loadData('Module:ItemData')
local item_data = mw.loadData('Module:ItemData')
    local item = item_data.items[name]
local item = item_data.items[name]
    local image
local image
    if item then
if item then
        if item['group'] == L.t('Skill Books') then
if item['group'] == L.t('Skill Books') then
            image = 'SkillBook.png'
image = 'SkillBook.png'
            icon_bg = 'iconGold'
icon_bg = 'iconGold'
        elseif item['group'] == L.t('Skill Scrolls') then
elseif item['group'] == L.t('Skill Scrolls') then
            image = 'Skill Scroll'
image = 'Skill Scroll'
            icon_bg = 'iconGold'
icon_bg = 'iconGold'
            -- Attempt to generate skill page
-- Attempt to generate skill page
        elseif in_array(L.t('Basic Research'), item['tagGroups']) then
elseif in_array(L.t('Basic Research'), item['tagGroups']) then
            image = string.sub(item['untranslated'], 1, -7):gsub('%s+', '') .. '_Icon.png'
image = string.sub(item['untranslated'], 1, -7):gsub('%s+', '') .. '_Icon.png'
            icon_bg = 'paperBasic'
icon_bg = 'paperBasic'
        elseif in_array(L.t('Advanced Research'), item['tagGroups']) then
elseif in_array(L.t('Advanced Research'), item['tagGroups']) then
            image = string.sub(item['untranslated'], 1, -10):gsub('%s+', '') .. '_Icon.png'
image = string.sub(item['untranslated'], 1, -10):gsub('%s+', '') .. '_Icon.png'
            icon_bg = 'paperAdvanced'
icon_bg = 'paperAdvanced'
        elseif in_array(L.t('Modern Research'), item['tagGroups']) then
elseif in_array(L.t('Modern Research'), item['tagGroups']) then
            image = string.sub(item['untranslated'], 1, -8):gsub('%s+', '') .. '_Icon.png'
image = string.sub(item['untranslated'], 1, -8):gsub('%s+', '') .. '_Icon.png'
            icon_bg = 'paperModern'
icon_bg = 'paperModern'
        else
else
            image = p.checkImage(item['untranslated'], too_expensive)
image = p.checkImage(item['untranslated'], too_expensive)
        end
end
        if not icon_bg then
if not icon_bg then
            if item['group'] == L.t('Food') then
if item['group'] == L.t('Food') then
                icon_bg = 'iconGreen'
icon_bg = 'iconGreen'
            elseif item['carried'] == L.t('Hands') then
elseif item['carried'] == L.t('Hands') then
                icon_bg = 'iconBrown'
icon_bg = 'iconBrown'
            end
end
        end
end
    else
else
        image = p.checkImage(name, too_expensive)
image = p.checkImage(name, too_expensive)
    end
end


    if not icon_bg then
if not icon_bg then
        icon_bg = 'iconBlue'
icon_bg = 'iconBlue'
    end
end


    if border then
if border then
        icon_border = border
icon_border = border
    else
else
        icon_border = 'borderBlue'
icon_border = 'borderBlue'
    end
end


    if size == 'iconNormal' then
if size == 'iconNormal' then
        icon_container = 'iconContainer'
icon_container = 'iconContainer'
    else
else
        icon_container = 'iconContainerSmall'
icon_container = 'iconContainerSmall'
    end
end


    if not link then
if not link then
        link = ''
link = ''
    else
else
        link = '|link='..link
link = '|link='..link
    end
end


    local file = '[[File:' .. image .. '|frameless|class=' .. size .. ' ' .. icon_bg .. link ..']]'
local file = '[[File:' .. image .. '|frameless|class=' .. size .. ' ' .. icon_bg .. link ..']]'
    return '<div class="' .. icon_container .. '"><div class="iconStack">' .. file .. '</div><div class="iconBorder ' .. icon_border .. '" style="position:absolute;"></div></div>'
return '<div class="' .. icon_container .. '"><div class="iconStack">' .. file .. '</div><div class="iconBorder ' .. icon_border .. '" style="position:absolute;"></div></div>'
end
end


function p.Icon(frame)
function p.Icon(frame)
    args = p.normaliseArgs(frame)
args = p.normaliseArgs(frame)
    return p.build_icon(args.name, args.link, args.size, args.bg, args.border, args.too_expensive)
return p.build_icon(args.name, args.link, args.size, args.bg, args.border, args.too_expensive)
end
end


-- mw.LoadData prevents #table from working correctly
-- mw.LoadData prevents #table from working correctly
function p.tableLen(tbl)
function p.tableLen(tbl)
    local count = 0
local count = 0
    for _, v in ipairs(tbl) do
for _, v in ipairs(tbl) do
        if v == nil then
if v == nil then
            return count
return count
        end
end
        count = count + 1
count = count + 1
    end
end
    return count
return count
end
end


return p
return p

Revision as of 16:50, 22 February 2022

This module provides utility functions used from other modules.

Usage

Add the following line of code at the top of your file.

local Utils = require("Module:Utils")

-- You may then call functions from this module in your script. For example:
local tableLength = Utils.tableLen(myTable)

local p = {}

-- Trims and parses the args into a table, then returns the table
function p.normaliseArgs(frame)
	local origArgs = frame:getParent().args
	local args = {}

	for k, v in pairs(origArgs) do
		v = mw.text.trim(tostring(v))
		if v ~= '' then
			args[k] = v
		end
	end

	return args
end

function p.checkImage(name, too_expensive)
	local icon = name:gsub('%s+', '') .. '_Icon.png'
	if too_expensive then
		return icon
	end

	if mw.title.makeTitle('File', icon).file.exists then
		return icon
	else
		return 'NoImage.png'
	end
end

local function in_array(item, array)
	-- Should only use on short arrays
	local set = {}
	for _, l in ipairs(array) do
		set[l] = true
	end
	return set[item] ~= nil
end

---@param name string
---@param size string|nil
---@param bg string|nil
---@param border string|nil
---@param too_expensive boolean|nil
function p.build_icon(name, link, size, bg, border, too_expensive)
	-- Size options are iconNormal or iconRecipe - 64px or 44px - defaults to iconNormal

	local L = require('Module:Localization') -- local import

	if not size then
		size = 'iconNormal'
	end
	local icon_bg
	if bg then
		icon_bg = bg
	end
	local icon_border
	if border then
		icon_border = border
	end

	local item_data = mw.loadData('Module:ItemData')
	local item = item_data.items[name]
	local image
	if item then
		if item['group'] == L.t('Skill Books') then
			image = 'SkillBook.png'
			icon_bg = 'iconGold'
		elseif item['group'] == L.t('Skill Scrolls') then
			image = 'Skill Scroll'
			icon_bg = 'iconGold'
			-- Attempt to generate skill page
		elseif in_array(L.t('Basic Research'), item['tagGroups']) then
			image = string.sub(item['untranslated'], 1, -7):gsub('%s+', '') .. '_Icon.png'
			icon_bg = 'paperBasic'
		elseif in_array(L.t('Advanced Research'), item['tagGroups']) then
			image = string.sub(item['untranslated'], 1, -10):gsub('%s+', '') .. '_Icon.png'
			icon_bg = 'paperAdvanced'
		elseif in_array(L.t('Modern Research'), item['tagGroups']) then
			image = string.sub(item['untranslated'], 1, -8):gsub('%s+', '') .. '_Icon.png'
			icon_bg = 'paperModern'
		else
			image = p.checkImage(item['untranslated'], too_expensive)
		end
		if not icon_bg then
			if item['group'] == L.t('Food') then
				icon_bg = 'iconGreen'
			elseif item['carried'] == L.t('Hands') then
				icon_bg = 'iconBrown'
			end
		end
	else
		image = p.checkImage(name, too_expensive)
	end

	if not icon_bg then
		icon_bg = 'iconBlue'
	end

	if border then
		icon_border = border
	else
		icon_border = 'borderBlue'
	end

	if size == 'iconNormal' then
		icon_container = 'iconContainer'
	else
		icon_container = 'iconContainerSmall'
	end

	if not link then
		link = ''
	else
		link = '|link='..link
	end

	local file = '[[File:' .. image .. '|frameless|class=' .. size .. ' ' .. icon_bg .. link ..']]'
	return '<div class="' .. icon_container .. '"><div class="iconStack">' .. file .. '</div><div class="iconBorder ' .. icon_border .. '" style="position:absolute;"></div></div>'
end

function p.Icon(frame)
	args = p.normaliseArgs(frame)
	return p.build_icon(args.name, args.link, args.size, args.bg, args.border, args.too_expensive)
end

-- mw.LoadData prevents #table from working correctly
function p.tableLen(tbl)
	local count = 0
	for _, v in ipairs(tbl) do
		if v == nil then
			return count
		end
		count = count + 1
	end
	return count
end

return p