Модуль:Utils: различия между версиями

Материал из Eco - Русская Wiki
Перейти к:навигация, поиск
[непроверенная версия][досмотренная версия]
Нет описания правки
Нет описания правки
Строка 1: Строка 1:
local p = {}
local p = {}


-- 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.normalise(args)
    local origArgs = frame:getParent().args
    local args = {}


    for k, v in pairs(origArgs) do
for k, v in pairs(args) 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)
--- Trims and parses the args into a table, then returns the table
    local icon = name:gsub('%s+', '') .. '_Icon.png'
--- @author User:Avaren
    if too_expensive then
function p.normaliseArgs(frame)
        return icon
local origArgs = frame:getParent().args
    end
local args = {}


    if mw.title.makeTitle('File', icon).file.exists then
for k, v in pairs(origArgs) do
        return icon
v = mw.text.trim(tostring(v))
    else
if v ~= '' then
        return 'NoImage.png'
args[k] = v
    end
end
end
end


local function in_array(item, array)
return args
    -- Should only use on short arrays
    local set = {}
    for _, l in ipairs(array) do
        set[l] = true
    end
    return set[item] ~= nil
end
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
function p.CheckId(name)
        icon_bg = 'iconBlue'
local itemData = mw.loadData("Module:ItemData")
     end
     local itemTable = itemData.items[name]
 
     if itemTable == nil then return 'NoItem' end
    if border then
     local IconName = itemTable.ID
        icon_border = border
    else
return IconName
        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
end


function p.Icon(frame)
--- Check if item is in given array.
    args = p.normaliseArgs(frame)
--- @author User:Avaren
    return p.build_icon(args.name, args.link, args.size, args.bg, args.border, args.too_expensive)
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
end


-- mw.LoadData prevents #table from working correctly
--- Calculate the length of a table by iterating over every item in it.
--- @author User:Avaren
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


Строка 143: Строка 70:
end
end


function p.getLanguageName(lang)
function p.getLanguageName()
   local languageName = "English"
   local languageName = "English"
   if lang.args[1] == "ru" then languageName = "Russian" end
  local language = mw.language.getContentLanguage()
   if lang.args[1] == "de" then languageName = "German" end
  local languageCode = language:getCode()
   if lang.args[1] == "fr" then languageName = "French" end
   if languageCode == "ru" then languageName = "Russian" end
   if languageCode == "de" then languageName = "German" end
   if languageCode == "fr" then languageName = "French" end
   return languageName
   return languageName
end
function p.checkImage(filename)
if filename then
if mw.title.makeTitle('Media', filename).file.exists then return "Y" else return "N" end
else return "Error name" end
end
end


return p
return p

Версия от 06:50, 2 июля 2025

Для документации этого модуля может быть создана страница Модуль:Utils/doc

local p = {}

--- Trims and parses the args into a table, then returns the table
function p.normalise(args)

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

	return args
end

--- Trims and parses the args into a table, then returns the table
--- @author User:Avaren
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.CheckId(name)
	local itemData = mw.loadData("Module:ItemData")
    local itemTable = itemData.items[name]
    if itemTable == nil then return 'NoItem' end
    local IconName = itemTable.ID
	
	return IconName
end

--- Check if item is in given array.
--- @author User:Avaren
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

--- Calculate the length of a table by iterating over every item in it.
--- @author User:Avaren
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

function p.getLanguageCode()
  local language = mw.language.getContentLanguage()
  local languageCode = language:getCode()
  return languageCode
end

function p.getLanguageName()
  local languageName = "English"
  local	language = mw.language.getContentLanguage()
  local languageCode = language:getCode()
  if languageCode == "ru" then languageName = "Russian" end
  if languageCode == "de" then languageName = "German" end
  if languageCode == "fr" then languageName = "French" end
  return languageName
end

function p.checkImage(filename)
	if filename then
		if mw.title.makeTitle('Media', filename).file.exists then return "Y" else return "N" end
	else return "Error name" end
end

return p