Module:Utilities
From The Walkscape Walkthrough
Documentation for this module may be created at Module:Utilities/doc
local p = {}
function p.getName(frame)
return frame.args ~= nil and frame.args[1] or frame[1]
end
function p.getPageJson(pageNameOrFrame)
local pageName
if type(pageNameOrFrame) == "table" then
-- If it's a frame, get the page name from the frame arguments
pageName = pageNameOrFrame.args ~= nil and pageNameOrFrame.args[1] or pageNameOrFrame[1]
else
-- If it's a string, use it as the page name
pageName = pageNameOrFrame
end
local debug = pageNameOrFrame.args ~= nil and pageNameOrFrame.args.debug or nil
-- Pull the data from the page and strip html tags
local pageContent =
mw.title.new(pageName)
:getContent()
local noOpenTag = pageContent:gsub("<syntaxhighlight lang=\"json\">", "")
local noCloseTag = noOpenTag:gsub("</syntaxhighlight>", "")
local noInclude = noCloseTag:gsub("<noinclude>%[%[Category:Data%]%]</noinclude>", "")
-- Decode the json
local jsonData = mw.text.jsonDecode(noInclude)
-- If debug is true, convert the table into a JSON string
if debug == "true" then
return mw.text.jsonEncode(jsonData)
else
return jsonData
end
end
function p.getImageName(stringOrFrame)
local item
if type(stringOrFrame) == "table" then
-- If it's a frame, get the page name from the frame arguments
item = stringOrFrame.args ~= nil and stringOrFrame.args[1] or stringOrFrame[1]
else
-- If it's a string, use it as the page name
item = stringOrFrame
end
local name = item:match(".*/(.-)%..+")
if name then
return ((name:gsub("%s(%l)", function(s) return " " .. s:upper() end)):gsub("%s", "_")) .. ".svg"
else
return "Invalid item"
end
end
function p.getReadableName(stringOrFrame)
local item
if type(stringOrFrame) == "table" then
-- If it's a frame, get the page name from the frame arguments
item = stringOrFrame.args ~= nil and stringOrFrame.args[1] or stringOrFrame[1]
else
-- If it's a string, use it as the page name
item = stringOrFrame
end
local name = string.match(item, "%.([^%.]+)%.name$")
if name then
local spacedName = name:gsub("(%u)", " %1") -- Add a space before each capital letter
return (spacedName:gsub("^%l", string.upper)) -- Capitalize the first letter
else
return "Invalid item"
end
end
function p.formatValue(frame)
local value = frame.args ~= nil and frame.args[1] or frame[1]
if type(value) ~= "string" then
value = tostring(value)
end
local left,num,right = string.match(value,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
function p.getFormattedValue(frame)
local values = p.getPageJson('Data:Value')
local valueName = frame.args ~= nil and frame.args[1] or frame[1]
local multiplier = frame.args ~= nil and (frame.args.multiplier or frame.args[2]) or 1
for _, pair in pairs(values) do
if pair.Keyword == valueName then
-- return pair.Value .. ": " .. multiplier .. " : " .. pair.Value * multiplier
local rounded = math.ceil(pair.Value * multiplier)
return rounded
-- return p.formatValue(rounded)
end
end
end
function p.getRarityImage(rarity)
local images = {
Common = "Rarity_Common.svg",
Uncommon = "Rarity_Uncommon.svg",
Rare = "Rarity_Rare.svg",
Epic = "Rarity_Epic.svg",
Legendary = "Rarity_Legendary.svg",
Ethereal = "Rarity_Ethereal.svg",
Normal = "Quality_Normal.svg",
Good = "Quality_Good.svg",
Great = "Quality_Great.svg",
Excellent = "Quality_Excellent.svg",
Perfect = "Quality_Perfect.svg",
Eternal = "Quality_Eternal.svg",
Fine = "Quality_Fine.svg",
None = "Quality_None.svg"
}
local image = images[rarity]
if image then
return string.format("[[File:%s|frameless|100px|alt=Rank %s]]", image, rarity)
else
return "[[File:Unknown_item_svg.svg|frameless|25x25px]]"
end
end
return p