Module:Utils: Difference between revisions

From Eco - English Wiki
[checked revision][checked revision]
No edit summary
No edit summary
Line 126: Line 126:


function p.SpecialtyXP(Tier)
function p.SpecialtyXP(Tier)
local WikiText =  ''
WikiText =  WikiText ..'<table class="wikitable"><th>Specialty Level</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><tr>'
WikiText =  WikiText ..'<table class="wikitable"><th>Specialty Level</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><tr>'
for i = 1, 6 do
for i = 1, 6 do

Revision as of 15:00, 25 July 2025

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.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

function p.checkPage(pagename)
	local pagetitle = mw.title.new(pagename)
	if pagetitle and pagetitle.exists then return "Y" else return "N" end
end


function p.SkillSearch(PageName)
	local SkillName = ''
	local skillData = require( "Module:SkillData" )
    local skillList = skillData.skills
    local Lang = Utils.getLanguageName()
    	for Sname,Sdata in pairs(skillList) do
    		if (Sdata.Name[Lang] == PageName) then SkillName = Sname end
    	end
    return SkillName
end

function p.ItemSearch(PageName)
	local ItemName = 'None'
	local ItemData = require( "Module:ItemData" )
    local ItemList = ItemData.items
    local Lang = p.getLanguageName()
    	for Iname,Idata in pairs(ItemList) do
    		if (Idata.Name[Lang] == PageName) then ItemName = Iname end
    	end
    return ItemName
end

function p.TagSearch(PageName)
	local TagName = 'None'
	local TagData = require( "Module:TagData" )
    local TagList = TagData.tags
    local Lang = p.getLanguageName()
    	for Tname,Tdata in pairs(TagList) do
    		if (Tdata.Name[Lang] == PageName) then TagName = Tname end
    	end
    return TagName
end

function p.SpecialtyXP(Tier)
	local WikiText =  ''
	WikiText =  WikiText ..'<table class="wikitable"><th>Specialty Level</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><tr>'
	for i = 1, 6 do
		local XP = Tier.tonumber * ( 25 * i) ^ 2
		WikiText =  WikiText .. '<td>' .. XP .. '</td>'
	end
	WikiText =  WikiText ..'</tr></table>'
    return WikiText
end

return p