Module:Icons: Difference between revisions

From Melvor Idle
(Add Astrology constellation overrides)
m (Fix extension override handling for skill reqs)
 
(174 intermediate revisions by 10 users not shown)
Line 1: Line 1:
--You can't generate Templates from within Lua due to the loading order, so instead copying the Icon functionality into a module so it can be pulled elsewhere.
--Should function very similarly to how Template:Icon works
local Shared = require("Module:Shared")
local Shared = require("Module:Shared")
local Num = require('Module:Number')
local Overrides = require('Module:Icons/Overrides')
-- Locally index some functions for performance
local sgsub, fixPagename, formatnum, tostring, type = string.gsub, Shared.fixPagename, Num.formatnum, tostring, type


local p = {}
local p = {}
--Extension overrides for items that have non-svg images
local extOverrides = {
  ["Adventure"] = "png",
  ["Crown of Rhaelyx"] = "png",
  ["Jewel of Rhaelyx"] = "png",
  ["Circlet of Rhaelyx"] = "png",
  ["Charge Stone of Rhaelyx"] = "png",
  ["Mysterious Stone"] = "png",
  ["Lemon"] = "jpg",
  ["Lemons"] = "jpg",
  ["Lemonade"] = "jpg",
  ["Miolite Sprig"] = "png",
  ["Miolite Trio"] = "png",
  ["Miolite Warden"] = "png",
  ["Miolite Monarch"] = "png",
  ["Wicked Greater Dragon"] = "png",
  ["Hunting Greater Dragon"] = "png",
  ["Chaotic Greater Dragon"] = "png",
  ["Beavis"] = "png",
  ["Pudding Duckie"] = "png",
  ["Pyro"] = "png",
  ["Snek"] = "png",
  ["Larry, the Lonely Lizard"] = "png",
  ["Caaarrrlll"] = "png",
  ["Gronk"] = "png",
  ["Finn, the Cat"] = "png",
  ["Marahute"] = "png",
  ["Monk-ey"] = "png",
  ["Asura"] = "png",
  ["Summoning Shard (Silver)"] = "png",
  ["Summoning Shard (Red)"] = "png",
  ["Summoning Shard (Green)"] = "png",
  ["Summoning Shard (Gold)"] = "png",
  ["Summoning Shard (Blue)"] = "png",
  ["Summoning Shard (Black)"] = "png",
  ['Infernal Core'] = 'png',
  ['Cool Glasses'] = 'png',
  ["Wizard's Sigil"] = 'png',
  ["Wizard's Scroll"] = 'png',
  ["Whetstone"] = 'png',
  ["Whale (Perfect)"] = 'png',
  ["Trout (Perfect)"] = 'png',
  ["Training Shield"] = 'png',
  ["Thiever's Cape"] = 'png',
  ["Thief's Moneysack"] = 'png',
  ["Swordfish (Perfect)"] = 'png',
  ["Strawberry Cupcake (Perfect)"] = 'png',
  ["Strawberry Cupcake"] = 'png',
  ["Strawberry Cake (Perfect)"] = 'png',
  ["Strawberry Cake"] = 'png',
  ["Stack of Bones"] = 'png',
  ["Sneak-Ers"] = 'png',
  ["Shrimp (Perfect)"] = 'png',
  ["Shipwheel"] = 'png',
  ["Shark (Perfect)"] = 'png',
  ["Seed Pouch"] = 'png',
  ["Seahorse (Perfect)"] = 'png',
  ["Sardine (Perfect)"] = 'png',
  ["Salmon (Perfect)"] = 'png',
  ["Sailor's Top"] = 'png',
  ["Runecrafting Pouch"] = 'png',
  ["Ring of Wealth"] = 'png',
  ["Raw Chicken"] = 'png',
  ["Raw Beef"] = 'png',
  ["Prayer Scroll"] = 'png',
  ["Plain Pizza Slice (Perfect)"] = 'png',
  ["Plain Pizza Slice"] = 'png',
  ["Pile of Ores"] = 'png',
  ["Pile of Logs"] = 'png',
  ["Miner's Helmet"] = 'png',
  ["Merchant's Permit"] = 'png',
  ["Meat Pizza Slice (Perfect)"] = 'png',
  ["Meat Pizza Slice"] = 'png',
  ["Marksman's Sigil"] = 'png',
  ["Manta Ray (Perfect)"] = 'png',
  ["Lumberjack's Top"] = 'png',
  ["Lobster (Perfect)"] = 'png',
  ["Lemon Cake (Perfect)"] = 'png',
  ["Lemon Cake"] = 'png',
  ["Knight's Sigil"] = 'png',
  ["Knight's Defender"] = 'png',
  ["Knight's Cape"] = 'png',
  ["Jeweled Necklace"] = 'png',
  ["Jester's Hat"] = 'png',
  ["Jadestone"] = 'png',
  ["Herring (Perfect)"] = 'png',
  ["Hearty Soup (Perfect)"] = 'png',
  ["Hearty Soup"] = 'png',
  ["Gold Crested Shield"] = 'png',
  ["Golbin Mask"] = 'png',
  ["Giant Club"] = 'png',
  ["Fishing Hook"] = 'png',
  ["Fine Coinpurse"] = 'png',
  ["Fanfish (Perfect)"] = 'png',
  ["Cream Corn Soup (Perfect)"] = 'png',
  ["Cream Corn Soup"] = 'png',
  ["Cream"] = 'png',
  ["Crate of Food"] = 'png',
  ["Crate of Basic Supplies"] = 'png',
  ["Crab (Perfect)"] = 'png',
  ["Cooking Apron"] = 'png',
  ["Chicken Soup (Perfect)"] = 'png',
  ["Chicken Soup"] = 'png',
  ["Chicken (Perfect)"] = 'png',
  ["Chest of Gems"] = 'png',
  ["Cherry Seeds"] = 'png',
  ["Cherry Cupcake (Perfect)"] = 'png',
  ["Cherry Cupcake"] = 'png',
  ["Cherry"] = 'png',
  ["Chef's Spoon"] = 'png',
  ["Chef's Hat"] = 'png',
  ["Cheese"] = 'png',
  ["Cave Giant Boots"] = 'png',
  ["Cave Fish (Perfect)"] = 'png',
  ["Carrot Cake (Perfect)"] = 'png',
  ["Carrot Cake"] = 'png',
  ["Carp (Perfect)"] = 'png',
  ["Bread (Perfect)"] = 'png',
  ["Bread"] = 'png',
  ["Book of Scholars"] = 'png',
  ["Bob's Gloves"] = 'png',
  ["Beef Pie (Perfect)"] = 'png',
  ["Beef Pie"] = 'png',
  ["Beef (Perfect)"] = 'png',
  ["Beef"] = 'png',
  ["Basic Soup (Perfect)"] = 'png',
  ["Basic Soup"] = 'png',
  ["Basic Bag"] = 'png',
  ["Bag of Flour"] = 'png',
  ["Apple Tree Seeds"] = 'png',
  ["Apple Pie (Perfect)"] = 'png',
  ["Apple Pie"] = 'png',
  ["Apple"] = 'png',
  ["Antique Vase"] = 'png',
  ["Anglerfish (Perfect)"] = 'png',
  ["Amulet of Incantation"] = 'png',
  ["Alchemist's Bag"] = 'png',
  ["Absorbing Shield"] = 'png',
  ["Shield of Melee Power"] = 'png',
  ["Shield of Ranged Power"] = 'png',
  ["Shield of Magic Power"] = 'png',
  ["Ring of Power"] = 'png',
  ["Beginning of the End"] = 'png',
  ["Goo"] = 'png',
  ["Wildflower"] = 'png',
  ["Nature's Call Staff Top"] = 'png',
  ["Nature's Call Staff Bottom"] = 'png',
  ["Fine Poison Powder"] = 'png',
  ["Poison Scales"] = 'png',
  ["Miolite Spore"] = 'png',
  ["Poison Essence"] = 'png',
  ["Worm Spike"] = 'png',
  ["Burning Essence"] = 'png',
  ["Cursed Hands"] = 'png',
  ["Stinging Essence"] = 'png',
  ["Tough Shell"] = 'png',
  ["Undead Essence"] = 'png',
  ["Desecrated Bones"] = 'png',
  ["War Axe"] = 'png',
  ["Warberd"] = 'png',
  ["Throwing Power Gloves"] = 'png',
  ["Slingshot"] = 'png',
  ["Elerine Longbow"] = 'png',
  ["Elerine Spear"] = 'png',
  ["Nature's Call Staff"] = 'png',
  ["Nature's Wrath Staff"] = 'png',
  ["Nature's Blessing Ring"] = 'png',
  ["Poisoned Shortbow"] = 'png',
  ["Poisoned Dagger"] = 'png',
  ["Scaled Shield"] = 'png',
  ["Poison Virulence Ring"] = 'png',
  ["Poison Virulence Gloves"] = 'png',
  ["Burning Madness Ring"] = 'png',
  ["Burning Madness Gloves"] = 'png',
  ["Spiked Shell Ring"] = 'png',
  ["Spiked Shell Gloves"] = 'png',
  ["Relentless Fury Ring"] = 'png',
  ["Relentless Fury Gloves"] = 'png',
  ["Ring of Blade Echoes"] = 'png',
  ["Ring of Barrage"] = 'png',
  ["Ring of Spirit Power"] = 'png',
  ["Hinder Potion I"] = 'png',
  ["Hinder Potion II"] = 'png',
  ["Hinder Potion III"] = 'png',
  ["Hinder Potion IV"] = 'png',
  ["Famished Potion I"] = 'png',
  ["Famished Potion II"] = 'png',
  ["Famished Potion III"] = 'png',
  ["Famished Potion IV"] = 'png',
  ["Lethal Toxins Potion I"] = 'png',
  ["Lethal Toxins Potion II"] = 'png',
  ["Lethal Toxins Potion III"] = 'png',
  ["Lethal Toxins Potion IV"] = 'png',
  ["Impending Darkness"] = 'png',
  ["Mastery Token (Astrology)"] = 'png',
  ["Stardust"] = 'png',
  ["Golden Stardust"] = 'png',
  ["Astrology Skillcape"] = 'png',
  ["Secret Stardust Potion I"] = 'png',
  ["Secret Stardust Potion II"] = 'png',
  ["Secret Stardust Potion III"] = 'png',
  ["Secret Stardust Potion IV"] = 'png',
  ["Noxious Serpent"] = 'png',
  ["Venomous Snake"] = 'png',
  ["Legaran Wurm"] = 'png',
  ["Spiked Red Claw"] = 'png',
  ["Greater Skeletal Dragon"] = 'png',
  ["Giant Moth"] = 'png',
  ["Elerine Mage"] = 'png',
  ["Elerine Warrior"] = 'png',
  ["Elerine Archer"] = 'png',
  ["Cursed Lich"] = 'png',
}


--Extension overrides for items that have non-png images
--Name level overrides take precedence over type level overrides
local extOverrides = Overrides.extOverrides()
--Some overrides only apply for a specific type
--Some overrides only apply for a specific type
local typeExtOverrides = {
local typeExtOverrides = Overrides.typeExtOverrides()
['item'] = {
['Chicken'] = 'png'
}
}
 
--When calling for an icon of type Key, instead use type Value
--When calling for an icon of type Key, instead use type Value
local typeOverrides = {
local typeOverrides = Overrides.typeOverrides()
  ['combat'] = 'combatArea',
  ['slayer'] = 'combatArea',
}
--When calling for an icon for Key, replace with image Value, and optionally override type also
--When calling for an icon for Key, replace with image Value, and optionally override type also
local imgOverrides = {
local imgOverrides = Overrides.imgOverrides()
  ['Alt Magic'] = 'Magic',
--When calling for an icon for Key + Type, replace with image Value, and optionally override type also
  ['Alt. Magic'] = 'Magic',
local typeImgOverrides = Overrides.typeImgOverrides()
  ['Alternative Magic'] = 'Magic',
  ['Item Alchemy'] = {'Item Alchemy III', 'spell'},
  ['Superheat'] = {'Superheat IV', 'spell'},
  ['Melee'] = 'Combat',
  ['Spider (lv. 51)'] = 'Spider',
  ['Spider (lv. 52)'] = 'Brown Spider',
  ['Basic Resupply'] = {'Lobster', 'item'},
  ['Standard Resupply'] = {'Crab', 'item'},
  ['Generous Resupply'] = {'Shark', 'item'},
  ['Cooking Upgrade 1'] = {'Cooking', 'skill'},
  ['Cooking Upgrade 2'] = {'Cooking', 'skill'},
  ['Loot Container Stacking'] = {'Amulet of Looting', 'item'},
  ['Bane, Instrument of Fear'] = {'Bane'},
  ['Mastery Token Astrology'] = {'Mastery Token (Astrology)'}
}
--When calling for an icon for Key, the link goes to Value
--When calling for an icon for Key, the link goes to Value
local linkOverrides = {
local linkOverrides = Overrides.linkOverrides()
  ['Alt Magic'] = 'Alternative Magic',
--When calling for an icon for Key + Type, the link goes to Value
  ['Alt. Magic'] = 'Alternative Magic',
local typeLinkOverrides = Overrides.typeLinkOverrides()
  ['Spider2'] = 'Brown Spider',
-- If no other text override was specified for the given link, use these
  ["Deedree"] = 'Astrology#Constellations',
local txtOverrides = Overrides.txtOverrides()
  ["Iridan"] = 'Astrology#Constellations',
-- Ambiguous overrides section
  ["Ameria"] = 'Astrology#Constellations',
local ambiguousOverrides = Overrides.ambiguousOverrides()
  ["Terra"] = 'Astrology#Constellations',
-- For ambiguous overrides, determines the type text to be used in the page link
  ["Vale"] = 'Astrology#Constellations',
local ambiguousTypeMap = Overrides.ambiguousTypeMap()
  ["Syllia"] = 'Astrology#Constellations',
local currencyFormatDefn = Overrides.currencyFormatDefn()
  ["Arachi"] = 'Astrology#Constellations',
 
  ["Ko"] = 'Astrology#Constellations',
-- Defines icon types for which a section or anchor on a static page should be linked to, rather
  ["Tellus"] = 'Astrology#Constellations',
-- than the usual page linking
  ["Hyden"] = 'Astrology#Constellations',
local sectionLinks = Overrides.sectionLinks()
  ["Qimican"] = 'Astrology#Constellations'
}
--If no other text override was specified for the given link, use these
local txtOverrides = {
  ['Spider2'] = 'Brown Spider'
}
--Ambiguous overrides section
local ambiguousOverrides = {'Golbin', 'Chick', 'Chicken', 'Cyclops', 'Wizard'}


function p.Icon(frame)
function p.Icon(frame)
  local args = frame.args ~= nil and frame.args or frame
local args = frame.args ~= nil and frame.args or frame
  local link = args[1]
local link = args[1]
  local text = args[2]
local text = args[2]
  local iconType = args.type
local iconType = args.type
  local ext = args.ext ~= nil and args.ext ~= '' and args.ext or 'svg'
local ext = args.ext ~= nil and args.ext ~= '' and args.ext or 'png'
  local notext = args.notext ~= nil and args.notext ~= ''
local notext = args.notext ~= nil and args.notext ~= ''
  local nolink = args.nolink ~= nil and args.nolink ~= ''
local nolink = args.nolink ~= nil and args.nolink ~= ''
  local noicon = args.noicon ~= nil and args.noicon ~= ''
local noicon = args.noicon ~= nil and args.noicon ~= ''
  local menu = args.menu ~= nil and args.menu ~= ''
local nospan = args.nospan ~= nil and args.nospan
  local imgSize = args.size ~= nil and args.size or 25
local menu = args.menu ~= nil and args.menu ~= ''
  local qty = args.qty
local imgSize = args.size ~= nil and args.size or 25
  local img = args.img ~= nil and args.img ~= '' and args.img or link
local qty = args.qty
local img = args.img ~= nil and args.img ~= '' and args.img or link
local class = args.class ~= nil and args.class ~= '' and args.class or ''
local altText = args.alt ~= nil and args.alt or (notext and (text or link)) or ''
local linkSection = args.section
local expIcon = args.expicon ~= nil and args.expicon or ''
 
link = fixPagename(link)
img = fixPagename(img)
 
img = sgsub(img, '#', '')
img = sgsub(img, '/Training', '')
img = sgsub(img, '/Guide', '')
img = sgsub(img, '/Boosts', '')
-- Remove '#' from links to facilitate linking to things with hashes in
-- their names (e.g. item 'Birthday Cake Piece #1'). Links to sections
-- of a page should instead be performed using the 'section' parameter.
link = sgsub(link, '#', '')
 
-- Handle types for which links should be to a section of a static page
local sl = sectionLinks[iconType]
if sl ~= nil and not sl.exclude[link] then
if text == nil or text == '' then
text = link
end
linkSection = string.gsub(link, ' ', '')
-- Link overwriting must occur after text & linkSection have been set
link = sl.link
if sl.img ~= nil then
img = sl.img[1]
if sl.img[2] ~= nil then
iconType = sl.img[2]
end
end
end
 
--MANUAL OVERRIDES
local origType = iconType
local ovrImg = imgOverrides[link]
local ovrTypeImg = typeImgOverrides[iconType]
if ovrTypeImg ~= nil then
ovrImg = ovrTypeImg ~= nil and ovrTypeImg[img] or ovrImg
end
local ovrTxt = txtOverrides[link]
local ovrLink = linkOverrides[link]
local ovrTypeLink = typeLinkOverrides[iconType]
if ovrTypeLink ~= nil and ovrTypeLink[img] ~= nil then
if ovrTypeLink[img] == '' then
nolink = true
else
ovrLink = ovrTypeLink[img]
end
end
local hasText = (text ~= nil and text ~= '')
if ovrImg ~= nil and img == link then
if type(ovrImg) == 'string' then
img = ovrImg
elseif type(ovrImg) == 'table' then
img = ovrImg[1]
if ovrImg[2] ~= nil then iconType = ovrImg[2] end
end
end
 
-- Type & extension overrides must be after adjustments have been made
-- for any image overrides
local ovrTypeExt, ovrTypeExtTable = nil, typeExtOverrides[iconType]
if ovrTypeExtTable ~= nil then
ovrTypeExt = ovrTypeExtTable[img]
end
local ovrType = typeOverrides[iconType]
local ovrExt = extOverrides.name[img]
if ovrExt == nil then
ovrExt = extOverrides.type[ovrType or iconType]
end
if not hasText and ovrTxt ~= nil then
text = ovrTxt
hasText = true
end
if ovrLink ~= nil then
if not hasText then
text = link
hasText = true
end
link = ovrLink
end
if ovrTypeExt ~= nil then
ext = ovrTypeExt
elseif ovrExt ~= nil then
ext = ovrExt
end
if ovrType ~= nil then
iconType = ovrType
end
 
-- There are a couple specific double overrides to be included that don't fit in the above lists
 
-- If the link requires disambiguation or a section, the modify the link accordingly
-- while preserving the displayed text
local isAmbig, hasSection = ambiguousOverrides[link], linkSection ~= nil and linkSection ~= ''
if isAmbig or hasSection then
if not hasText then
text = link
hasText = true
end
if isAmbig then
link = link .. ' (' .. (ambiguousTypeMap[origType] or origType) .. ')'
end
if hasSection then
link = link .. '#' .. linkSection
end
end


  link = Shared.fixPagename(link)
local resultText = ''
  img = Shared.fixPagename(img)
if not noicon then
-- Image size
local txtSize = tostring(imgSize)
-- Extension and type if one is set
local txtImg = (iconType ~= nil and iconType ~= '' and img .. '_(' .. iconType .. ')' or img) .. '.' .. ext
-- Include a link unless no link -and- no text was requested
local txtLink = '|link=' .. (nolink and '' or link)
resultText = '[[File:' .. txtImg .. '|' .. txtSize .. 'x' .. txtSize .. 'px|alt=' .. altText .. txtLink .. ']]'
if class ~= nil and class ~= '' then
resultText = '<span class="'..class..'">'..resultText..'</span>'
end
end


  img = string.gsub(img, '#', '')
if not notext then
  img = string.gsub(img, '/Training', '')
if nolink then
  img = string.gsub(img, '/Guide', '')
resultText = resultText .. (noicon and '' or ' ') .. (hasText and text or link)
  link = string.gsub(link, '#', '')
else
  --MANUAL OVERRIDES
resultText = resultText .. (noicon and '' or ' ') .. '[[' .. link .. (hasText and text ~= link and '|' .. text or '') .. ']]'
  if imgOverrides[link] ~= nil and img == link then
end
  local ovr = imgOverrides[link]
  if type(ovr) == 'string' then
  img = imgOverrides[link]
elseif type(ovr) == 'table' then
  img = ovr[1]
  if ovr[2] ~= nil then iconType = ovr[2] end
end
end
  end
  if (text == nil or text == '') and txtOverrides[link] ~= nil then
    text = txtOverrides[link]
  end
  if linkOverrides[link] ~= nil then
    if (text == nil or text == '') then text = link end
    link = linkOverrides[link]
  end
  if typeExtOverrides[iconType] ~= nil and typeExtOverrides[iconType][img] ~= nil then
  ext = typeExtOverrides[iconType][img]
  elseif extOverrides[img] ~= nil then
    ext = extOverrides[img]
  elseif iconType == 'mark' then
    ext = 'png'
  end
  if typeOverrides[iconType] ~= nil then
    iconType = typeOverrides[iconType]
  end
  --There are a couple specific double overrides I want to include that don't fit in the above lists
  if Shared.contains(ambiguousOverrides, link) then
    if (text == nil or text == '') then text = link end
    link = link..' ('..(iconType == 'mark' and 'item' or iconType)..')'
  end


  local result = ''
-- Add Quantity to the front if requested
  if not noicon then
--local resultQty = (qty ~= nil and qty ~= '' and formatnum(qty) .. ' ' or '')
--Creating the image...
if qty ~= nil and qty ~= '' then
result = '[[File:'..img
resultText = formatnum(qty) .. ' ' .. resultText
--Add in type if one is set
end
if iconType ~= nil and iconType ~= '' then result = result..'_('..iconType..')' end
--Add in extension and image size
result = result..'.'..ext..'|'..tostring(imgSize)..'x'..tostring(imgSize)..'px'  
--Include a link unless no link -and- no text was requested
if not (nolink and notext) then result = result..'|link='..link end
result = result..']]'
  end
 
  --Add Quantity to the front if requested
  if qty ~= nil and qty ~= '' then result = Shared.formatnum(qty)..' '..result end


  if not notext then
if menu then
  -- If the result is currently empty then there's no need to pad with whitespace
return '{| class="articletable img-text" style="vertical-align:middle;"\r\n|-\r\n|' .. expIcon .. resultText .. '\r\n|}'
  local padStr = (string.len(result) > 0 and ' ' or '')
elseif not noicon and not nospan then
    if nolink then
return '<span class="img-text">' .. expIcon .. resultText .. '</span>'
      if text ~= nil and text ~= '' then
else
        result = result..padStr..text
return resultText
      else
end
        result = result..padStr..link
end
      end
    else
      result = result..padStr..'[['..link
      if text ~= nil and text ~= '' and text ~= link then
        result = result..'|'..text
      end
      result = result..']]'
    end
  end
 
  if not noicon then
    result = '<span style="display:inline-block">'..result..'</span>'
  end


  if menu then
-- Returns a skill icon based on the realm.
    result = '{| class="articletable" style="display:inline-block;vertical-align:middle;"\r\n|-\r\n|'..result
function p._SkillRealmIcon(skill, realmID, size)
    result = result..'\r\n|}'
local realm = realmID or 'melvorD:Melvor'
  end
local link = fixPagename(skill)


  return result
local img = mw.html.create('span')
if realm == 'melvorItA:Abyssal' then
img:addClass('abyss-icon')
end
size = size or 25
img :wikitext('[[File:'):wikitext(link):wikitext('_(skill).png')
:wikitext('|'):wikitext(size):wikitext('x'):wikitext(size):wikitext('px')
:wikitext('|link='):wikitext(link):wikitext(']]')
return tostring(img)
end
end


-- Wrapper for p.Icon(), uses the same parameters but forces noicon = true
function p.SkillRealmIcon(frame)
function p.Link(frame)
local args = frame.args ~= nil and frame.args or frame
local args = frame.args ~= nil and frame.args or frame
args.noicon = true
local skill = args[1]
return p.Icon({['args'] = args})
local realmID = args.realm
local size = tonumber(args.size)
return p._SkillRealmIcon(skill, realmID, size)
end
end


function p._SkillReq(skill, level, showText)
function p._SkillReq(skill, level, showText, realmID)
  local result = p.Icon({skill, type='skill', notext='true'})
local realm = realmID or 'melvorD:Melvor'
  if showText then
local link = fixPagename(skill)
    result = result..' [['..skill..']]'
local altText = (showText and '') or skill
  end
if level == nil then  
  if level == nil then level = 'Unknown [[Category:Pages with script errors]]' end
level = 'Unknown [[Category:Pages with script errors]]'  
  result = result.." Level "..level
end


  result = '<span style="display:inline-block">'..result..'</span>'
-- Create skill image.
  return result
local img = mw.html.create()
if realm == 'melvorItA:Abyssal' then
img = img:tag('span')
:addClass('abyss-icon')
end
img :wikitext('[[File:'):wikitext(link):wikitext('_(skill).png')
:wikitext('|25x25px|link='):wikitext(link)
:wikitext('|alt='):wikitext(altText)
:wikitext(']]')
-- Create text and add image.
local text = mw.html.create('span')
:addClass('img-text')
:node(img)
if showText == true then
text:wikitext(' [[' .. link .. ']]')
end
--28/06/2024. Commented this out to try out red icons for abyssal levels
-- as opposed to the text below. Remove entire section if still commented out after
-- a week or two.
--if realm == 'melvorItA:Abyssal' then
-- text:wikitext(" Abyssal Level ")
--else
text:wikitext(" Level ")
--end
text:wikitext(level)
return tostring(text)
end
end


function p.SkillReq(frame)
function p._MasteryReq(itemName, level, showText)
  local args = frame.args ~= nil and frame.args or frame
local iconname = itemName
  local skill = args[1]
local linkname = itemName
  local level = tonumber(args[2])
--First, go with the lowest tier of potions if a potion is mentioned
  local showText = args.showText ~= nil and args.showText ~= '' and args.showText ~= 'false'
local s, e = string.find(itemName, 'Potion')
  return p._SkillReq(skill, level, showText)
if e ~= nil then
linkname = string.sub(itemName, 1, e)
iconname = linkname..' I'
end
local altTextItem = (showText and '') or linkname
local altTextMastery = (showText and '') or '&nbsp;Mastery'
 
    local result = mw.html.create('span')
    result
        :addClass('img-text')
        :wikitext('Level ' .. level .. ' ')
        :node(p.Icon({linkname, img=iconname, type='item', notext=true, nospan=true, alt=altTextItem}))
        :node('[[File:Mastery.svg|25x25px|alt=' .. altTextMastery .. ']]')
 
    if showText then
        result
            :wikitext('[[' .. linkname .. ']] ')
            :wikitext('[[Mastery]]')
    end
 
    return tostring(result)
end
end


function p._MasteryReq(itemName, level, showText)
function p._Currency(currencyID, amount, maxAmount)
  local iconname = itemName
local currencyDefn = currencyFormatDefn[currencyID]
  local linkname = itemName
if currencyDefn == nil then
  --First, go with the lowest tier of potions if a potion is mentioned
error('Invalid currency ID: ' .. currencyID, 2)
  local s, e = string.find(itemName, 'Potion')
end
  if e ~= nil then
 
    linkname = string.sub(itemName, 1, e)
local fileName, link, altText = currencyDefn.img, currencyDefn.name, currencyDefn.abbr
    iconname = linkname..' I'
 
  end
local function numColour(amount)
if tonumber(amount) < 0 then
return mw.html.create('span')
:css('color', 'red')
:wikitext(formatnum(amount))
:done()
else
return formatnum(amount)
end
end
local html = mw.html.create('span')
:addClass('img-text')
 
-- Currency amounts
if tonumber(amount) ~= nil then
local text = mw.html.create('span')
:css('margin-right', '0.2em')
:node(numColour(amount))
if (tonumber(maxAmount) ~= nil and maxAmount > amount) then
text:wikitext(' - ' .. numColour(maxAmount))
end
html:node(text:done())
end
-- Currency icon
if fileName ~= nil then
html:wikitext('[[File:'):wikitext(fileName):wikitext('|25px')
if link ~= nil then
html:wikitext('|link='):wikitext(link)
end
html:wikitext('|alt=')
:wikitext(((altText == nil and '') or altText))
:wikitext(']]')
end
html:done()
if ((amount ~= nil and tonumber(amount) == nil)
or (maxAmount ~= nil and tonumber(maxAmount) == nil)) then
html:wikitext('[[Category:Pages with non-numeric currency arguments]]')
end
return tostring(html)
end


  local result = 'Level '..level..' '
-- p.Currency() is called by various currency templates:
  result = result..p.Icon({linkname, img=iconname, type='item', notext = true})..p.Icon({'Mastery', notext=true})
-- [[Template:GP]], [[Template:SC]], [[Template:RC]]
  if showText  then result = result..'[['..linkname..']] [[Mastery]]' end
function p.Currency(frame)
  result = '<span style="display:inline-block">'..result..'</span>'
local args = frame.args ~= nil and frame.args or frame
  return result
local currID, minVal, maxVal, _
currID = args[1]
if args[2] ~= nil and args[2] ~= '' then
minVal, _ = string.gsub(args[2], ',', '')
end
if args[3] ~= nil and args[3] ~= '' then
maxVal, _ = string.gsub(args[3], ',', '')
end
return p._Currency((currID or 'nil'), minVal, maxVal)
end
end


function p.MasteryReq(frame)
--Adding a shortcut function for this
  local args = frame.args ~= nil and frame.args or frame
function p.TotH()
  local itemName = args[1]
return '<span class="img-text">[[File:TotH.svg|25x25px|link=Throne of the Herald Expansion]]</span>'
  local level = tonumber(args[2])
  local showText = args.showText ~= nil and args.showText ~= '' and args.showText ~= 'false'
  return p._MasteryReq(itemName, level, showText)
end
end


function p._Currency(fileName, link, amt, maxAmt)
function p.AoD()
  local amtText = Shared.formatnum(amt)
return '<span class="img-text">[[File:AoD.png|25x25px|link=Atlas of Discovery Expansion]]</span>'
  if maxAmt ~= nil and maxAmt >= amt then
end
    amtText = amtText .. ' - ' .. Shared.formatnum(maxAmt)
  end


  local fileText = ''
function p.ItA()
  if fileName ~= nil then
return '<span class="img-text">[[File:ItA.png|25x25px|link=Into the Abyss Expansion]]</span>'
    fileText = '[[File:' .. fileName .. '|25px'
end
    if link ~= nil then
      fileText = fileText .. '|link=' .. link
    end
    fileText = fileText .. ']]&nbsp;'
  end


  return '<span style="display:inline-block">' .. fileText .. amtText .. '</span>'
function p.Melvor()
return '<span class="img-text">[[File:Melvor_Logo.svg|25x25px|link=Full Version]]</span>'
end
end


function p.GP(amt, maxamt)
-- Function to retrieve the DLC id used for sorting columns that contain a DLC icon.
  return p._Currency('Coins.svg', 'Coins', amt, maxamt)
-- Passthrough from shared so that caller doesn't have to include Shared Module
function p.getExpansionID(id)
return Shared.getLocalID(id)
end
end


function p.SC(amt, maxamt)
function p.getExpansionIcon(id)
  return p._Currency('Slayer Coins.svg', 'Currency#Slayer Coins', amt, maxamt)
local ns, _ = Shared.getLocalID(id)
if ns == 'melvorTotH' then
return p.TotH()..' '
elseif ns == 'melvorAoD' then
return p.AoD()..' '
elseif ns == 'melvorItA' then
return p.ItA()..' '
else
return ''
end
end
end


function p.RC(amt, maxamt)
-- Special function that returns a Melvor idle icon
  return p._Currency('Raid_Coins.svg', 'Currency#Raid Coins', amt, maxamt)
-- Specifically used for a "DLC" column on tables.
-- Avoids every item from receiving the "base game icon"
function p.getDLCColumnIcon(id)
local icon = p.getExpansionIcon(id)
if icon == '' then
icon = p.Melvor() .. ' '
end
return icon
end
end


return p
return p

Latest revision as of 20:57, 26 August 2024

Documentation for this module may be created at Module:Icons/doc

local Shared = require("Module:Shared")
local Num = require('Module:Number')
local Overrides = require('Module:Icons/Overrides')
-- Locally index some functions for performance
local sgsub, fixPagename, formatnum, tostring, type = string.gsub, Shared.fixPagename, Num.formatnum, tostring, type

local p = {}

--Extension overrides for items that have non-png images
--Name level overrides take precedence over type level overrides
local extOverrides = Overrides.extOverrides()
--Some overrides only apply for a specific type
local typeExtOverrides = Overrides.typeExtOverrides()
--When calling for an icon of type Key, instead use type Value
local typeOverrides = Overrides.typeOverrides()
--When calling for an icon for Key, replace with image Value, and optionally override type also
local imgOverrides = Overrides.imgOverrides()
--When calling for an icon for Key + Type, replace with image Value, and optionally override type also
local typeImgOverrides = Overrides.typeImgOverrides()
--When calling for an icon for Key, the link goes to Value
local linkOverrides = Overrides.linkOverrides()
--When calling for an icon for Key + Type, the link goes to Value
local typeLinkOverrides = Overrides.typeLinkOverrides()
-- If no other text override was specified for the given link, use these
local txtOverrides = Overrides.txtOverrides()
-- Ambiguous overrides section
local ambiguousOverrides = Overrides.ambiguousOverrides()
-- For ambiguous overrides, determines the type text to be used in the page link
local ambiguousTypeMap = Overrides.ambiguousTypeMap()
local currencyFormatDefn = Overrides.currencyFormatDefn()

-- Defines icon types for which a section or anchor on a static page should be linked to, rather
-- than the usual page linking
local sectionLinks = Overrides.sectionLinks()

function p.Icon(frame)
	local args = frame.args ~= nil and frame.args or frame
	local link = args[1]
	local text = args[2]
	local iconType = args.type
	local ext = args.ext ~= nil and args.ext ~= '' and args.ext or 'png'
	local notext = args.notext ~= nil and args.notext ~= ''
	local nolink = args.nolink ~= nil and args.nolink ~= ''
	local noicon = args.noicon ~= nil and args.noicon ~= ''
	local nospan = args.nospan ~= nil and args.nospan
	local menu = args.menu ~= nil and args.menu ~= ''
	local imgSize = args.size ~= nil and args.size or 25
	local qty = args.qty
	local img = args.img ~= nil and args.img ~= '' and args.img or link
	local class = args.class ~= nil and args.class ~= '' and args.class or ''
	local altText = args.alt ~= nil and args.alt or (notext and (text or link)) or ''
	local linkSection = args.section
	local expIcon = args.expicon ~= nil and args.expicon or ''

	link = fixPagename(link)
	img = fixPagename(img)

	img = sgsub(img, '#', '')
	img = sgsub(img, '/Training', '')
	img = sgsub(img, '/Guide', '')
	img = sgsub(img, '/Boosts', '')
	-- Remove '#' from links to facilitate linking to things with hashes in
	-- their names (e.g. item 'Birthday Cake Piece #1'). Links to sections
	-- of a page should instead be performed using the 'section' parameter.
	link = sgsub(link, '#', '')

	-- Handle types for which links should be to a section of a static page
	local sl = sectionLinks[iconType]
	if sl ~= nil and not sl.exclude[link] then
		if text == nil or text == '' then
			text = link
		end
		linkSection = string.gsub(link, ' ', '')
		-- Link overwriting must occur after text & linkSection have been set
		link = sl.link
		if sl.img ~= nil then
			img = sl.img[1]
			if sl.img[2] ~= nil then
				iconType = sl.img[2]
			end
		end
	end

	--MANUAL OVERRIDES
	local origType = iconType
	local ovrImg = imgOverrides[link]
	local ovrTypeImg = typeImgOverrides[iconType]
	if ovrTypeImg ~= nil then
		ovrImg = ovrTypeImg ~= nil and ovrTypeImg[img] or ovrImg
	end
	local ovrTxt = txtOverrides[link]
	local ovrLink = linkOverrides[link]
	local ovrTypeLink = typeLinkOverrides[iconType]
	if ovrTypeLink ~= nil and ovrTypeLink[img] ~= nil then
		if ovrTypeLink[img] == '' then
			nolink = true
		else
			ovrLink = ovrTypeLink[img]
		end
	end
	local hasText = (text ~= nil and text ~= '')
	if ovrImg ~= nil and img == link then
		if type(ovrImg) == 'string' then
			img = ovrImg
		elseif type(ovrImg) == 'table' then
			img = ovrImg[1]
			if ovrImg[2] ~= nil then iconType = ovrImg[2] end
		end
	end

	-- Type & extension overrides must be after adjustments have been made
	-- for any image overrides
	local ovrTypeExt, ovrTypeExtTable = nil, typeExtOverrides[iconType]
	if ovrTypeExtTable ~= nil then
		ovrTypeExt = ovrTypeExtTable[img]
	end
	local ovrType = typeOverrides[iconType]
	local ovrExt = extOverrides.name[img]
	if ovrExt == nil then
		ovrExt = extOverrides.type[ovrType or iconType]
	end
	if not hasText and ovrTxt ~= nil then
		text = ovrTxt
		hasText = true
	end
	if ovrLink ~= nil then
		if not hasText then
			text = link
			hasText = true
		end
		link = ovrLink
	end
	if ovrTypeExt ~= nil then
		ext = ovrTypeExt
	elseif ovrExt ~= nil then
		ext = ovrExt
	end
	if ovrType ~= nil then
		iconType = ovrType
	end

	-- There are a couple specific double overrides to be included that don't fit in the above lists

	-- If the link requires disambiguation or a section, the modify the link accordingly
	-- while preserving the displayed text
	local isAmbig, hasSection = ambiguousOverrides[link], linkSection ~= nil and linkSection ~= ''
	if isAmbig or hasSection then
		if not hasText then
			text = link
			hasText = true
		end
		if isAmbig then
			link = link .. ' (' .. (ambiguousTypeMap[origType] or origType) .. ')'
		end
		if hasSection then
			link = link .. '#' .. linkSection
		end
	end

	local resultText = ''
	if not noicon then
		-- Image size
		local txtSize = tostring(imgSize)
		-- Extension and type if one is set
		local txtImg = (iconType ~= nil and iconType ~= '' and img .. '_(' .. iconType .. ')' or img) .. '.' .. ext
		-- Include a link unless no link -and- no text was requested
		local txtLink = '|link=' .. (nolink and '' or link)
		resultText = '[[File:' .. txtImg .. '|' .. txtSize .. 'x' .. txtSize .. 'px|alt=' .. altText .. txtLink .. ']]'
		if class ~= nil and class ~= '' then
			resultText = '<span class="'..class..'">'..resultText..'</span>'
		end
	end

	if not notext then
		if nolink then
			resultText = resultText .. (noicon and '' or ' ') .. (hasText and text or link)
		else
			resultText = resultText .. (noicon and '' or ' ') .. '[[' .. link .. (hasText and text ~= link and '|' .. text or '') .. ']]'
		end
	end

	-- Add Quantity to the front if requested
	--local resultQty = (qty ~= nil and qty ~= '' and formatnum(qty) .. ' ' or '')
	if qty ~= nil and qty ~= '' then
		resultText = formatnum(qty) .. ' ' .. resultText
	end

	if menu then
		return '{| class="articletable img-text" style="vertical-align:middle;"\r\n|-\r\n|' .. expIcon .. resultText .. '\r\n|}'
	elseif not noicon and not nospan then
		return '<span class="img-text">' .. expIcon .. resultText .. '</span>'
	else
		return resultText
	end
end

-- Returns a skill icon based on the realm.
function p._SkillRealmIcon(skill, realmID, size)
	local realm = realmID or 'melvorD:Melvor'
	local link = fixPagename(skill)

	local img = mw.html.create('span')
	if realm == 'melvorItA:Abyssal' then
		img:addClass('abyss-icon')
	end
	
	size = size or 25
	img :wikitext('[[File:'):wikitext(link):wikitext('_(skill).png')
		:wikitext('|'):wikitext(size):wikitext('x'):wikitext(size):wikitext('px')
		:wikitext('|link='):wikitext(link):wikitext(']]')
		
	return tostring(img)
end

function p.SkillRealmIcon(frame)
	local args = frame.args ~= nil and frame.args or frame
	local skill = args[1]
	local realmID = args.realm
	local size = tonumber(args.size)
	return p._SkillRealmIcon(skill, realmID, size)
end

function p._SkillReq(skill, level, showText, realmID)
	local realm = realmID or 'melvorD:Melvor'
	local link = fixPagename(skill)
	local altText = (showText and '') or skill
	if level == nil then 
		level = 'Unknown [[Category:Pages with script errors]]' 
	end

	-- Create skill image.
	local img = mw.html.create()
	
	if realm == 'melvorItA:Abyssal' then
		img = img:tag('span')
				 :addClass('abyss-icon')
	end
	
	img :wikitext('[[File:'):wikitext(link):wikitext('_(skill).png')
		:wikitext('|25x25px|link='):wikitext(link)
		:wikitext('|alt='):wikitext(altText)
		:wikitext(']]')
	
	-- Create text and add image.
	local text = mw.html.create('span')
		:addClass('img-text')
		:node(img)
	
	if showText == true then
		text:wikitext(' [[' .. link .. ']]')
	end
	
	--28/06/2024. Commented this out to try out red icons for abyssal levels
	-- as opposed to the text below. Remove entire section if still commented out after
	-- a week or two.
	--if realm == 'melvorItA:Abyssal' then
	--	text:wikitext(" Abyssal Level ")
	--else
	text:wikitext(" Level ")
	--end
	
	text:wikitext(level)
	return tostring(text)	
end

function p._MasteryReq(itemName, level, showText)
	local iconname = itemName
	local linkname = itemName
	--First, go with the lowest tier of potions if a potion is mentioned
	local s, e = string.find(itemName, 'Potion')
	if e ~= nil then
		linkname = string.sub(itemName, 1, e)
		iconname = linkname..' I'
	end
	local altTextItem = (showText and '') or linkname
	local altTextMastery = (showText and '') or '&nbsp;Mastery'

    local result = mw.html.create('span')
    result
        :addClass('img-text')
        :wikitext('Level ' .. level .. ' ')
        :node(p.Icon({linkname, img=iconname, type='item', notext=true, nospan=true, alt=altTextItem}))
        :node('[[File:Mastery.svg|25x25px|alt=' .. altTextMastery .. ']]')

    if showText then
        result
            :wikitext('[[' .. linkname .. ']] ')
            :wikitext('[[Mastery]]')
    end

    return tostring(result)
end

function p._Currency(currencyID, amount, maxAmount)
	local currencyDefn = currencyFormatDefn[currencyID]
	if currencyDefn == nil then
		error('Invalid currency ID: ' .. currencyID, 2)
	end

	local fileName, link, altText = currencyDefn.img, currencyDefn.name, currencyDefn.abbr

	local function numColour(amount)
		if tonumber(amount) < 0 then
			return mw.html.create('span')
				:css('color', 'red')
				:wikitext(formatnum(amount))
				:done()
		else
			return formatnum(amount)
		end
	end
	
	local html = mw.html.create('span')
		:addClass('img-text')

	-- Currency amounts
	if tonumber(amount) ~= nil then
		local text = mw.html.create('span')
			:css('margin-right', '0.2em')
			:node(numColour(amount))
		if (tonumber(maxAmount) ~= nil and maxAmount > amount) then
			text:wikitext(' - ' .. numColour(maxAmount))
		end
		html:node(text:done())
	end
	
	-- Currency icon
	if fileName ~= nil then
		html:wikitext('[[File:'):wikitext(fileName):wikitext('|25px')
		if link ~= nil then
			html:wikitext('|link='):wikitext(link)
		end
		html:wikitext('|alt=')
			:wikitext(((altText == nil and '') or altText))
			:wikitext(']]')
	end
	html:done()
	
	if ((amount ~= nil and tonumber(amount) == nil)
		or (maxAmount ~= nil and tonumber(maxAmount) == nil)) then
		html:wikitext('[[Category:Pages with non-numeric currency arguments]]')
	end
	
	return tostring(html)
end

-- p.Currency() is called by various currency templates:
--	[[Template:GP]], [[Template:SC]], [[Template:RC]]
function p.Currency(frame)
	local args = frame.args ~= nil and frame.args or frame
	local currID, minVal, maxVal, _
	currID = args[1]
	if args[2] ~= nil and args[2] ~= '' then
		minVal, _ = string.gsub(args[2], ',', '')
	end
	if args[3] ~= nil and args[3] ~= '' then
		maxVal, _ = string.gsub(args[3], ',', '')
	end
	
	return p._Currency((currID or 'nil'), minVal, maxVal)
end

--Adding a shortcut function for this
function p.TotH()
	return '<span class="img-text">[[File:TotH.svg|25x25px|link=Throne of the Herald Expansion]]</span>'
end

function p.AoD()
	return '<span class="img-text">[[File:AoD.png|25x25px|link=Atlas of Discovery Expansion]]</span>'
end

function p.ItA()
	return '<span class="img-text">[[File:ItA.png|25x25px|link=Into the Abyss Expansion]]</span>'
end

function p.Melvor()
	return '<span class="img-text">[[File:Melvor_Logo.svg|25x25px|link=Full Version]]</span>'
end

-- Function to retrieve the DLC id used for sorting columns that contain a DLC icon.
-- Passthrough from shared so that caller doesn't have to include Shared Module
function p.getExpansionID(id)
	return Shared.getLocalID(id)
end

function p.getExpansionIcon(id)
	local ns, _ = Shared.getLocalID(id)
	if ns == 'melvorTotH' then
		return p.TotH()..' '
	elseif ns == 'melvorAoD' then
		return p.AoD()..' '
	elseif ns == 'melvorItA' then
		return p.ItA()..' '
	else
		return ''
	end
end

-- Special function that returns a Melvor idle icon
-- Specifically used for a "DLC" column on tables.
-- Avoids every item from receiving the "base game icon"
function p.getDLCColumnIcon(id)
	local icon = p.getExpansionIcon(id)
	if icon == '' then
		icon = p.Melvor() .. ' '
	end
	return icon
end

return p