Module:Icons

From Melvor Idle
Revision as of 20:40, 30 September 2021 by Falterfire (talk | contribs) (added Cyclops and Wizard to the list of ambiguous overrides)

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

--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 p = {}
--Extension overrides for items that have non-svg images
local extOverrides = {
  ["Crown of Rhaelyx"] = "png",
  ["Jewel of Rhaelyx"] = "png",
  ["Circlet of Rhaelyx"] = "png",
  ["Charge Stone of Rhaelyx"] = "png",
  ["Mysterious Stone"] = "png",
  ["Cloudburst Staff"] = "png",
  ["Earth Layered Shield"] = "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',
}

--Some overrides only apply for a specific type
local typeExtOverrides = {
	['item'] = {
			['Chicken'] = 'png'
		}
	}

--When calling for an icon of type Key, instead use type Value
local typeOverrides = {
  ['combat'] = 'combatArea',
  ['slayer'] = 'combatArea',
}
--When calling for an icon for Key, replace with image Value, and optionally override type also
local imgOverrides = {
  ['Alt Magic'] = 'Magic',
  ['Alt. Magic'] = 'Magic',
  ['Alternative Magic'] = 'Magic',
  ['Melee'] = 'Combat',
  ['Spider (lv. 51)'] = 'Spider',
  ['Spider (lv. 52)'] = 'Spider2',
  ['Basic Resupply'] = 'Lobster',
  ['Standard Resupply'] = 'Crab',
  ['Generous Resupply'] = 'Shark',
  ['Cooking Upgrade 1'] = {'Cooking', 'skill'},
  ['Cooking Upgrade 2'] = {'Cooking', 'skill'}
}
--When calling for an icon for Key, the link goes to Value
local linkOverrides = {
  ['Alt Magic'] = 'Alternative Magic',
  ['Alt. Magic'] = 'Alternative Magic',
  ['Spider'] = 'Spider (lv. 51)',
  ['Spider2'] = 'Spider (lv. 52)'
}
--If no other text override was specified for the given link, use these
local txtOverrides = {
  ['Spider'] = 'Spider (lv. 51)',
  ['Spider2'] = 'Spider (lv. 52)'
}
--Ambiguous overrides section
local ambiguousOverrides = {'Golbin', 'Chick', 'Chicken', 'Cyclops', 'Wizard'}

local Shared= require("Module:Shared")

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 or 'svg'
  local notext = args.notext ~= nil and args.notext ~= ''
  local nolink = args.nolink ~= nil and args.nolink ~= ''
  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 or link

  link = string.gsub(link, "%%27", "'")
  link = string.gsub(link, "'", "'")
  link = string.gsub(link, "'", "'")
  img = string.gsub(img, "%%27", "'")
  img = string.gsub(img, "'", "'")
  img = string.gsub(img, "'", "'")

  img = string.gsub(img, '#', '')
  link = string.gsub(link, '#', '')
  --MANUAL OVERRIDES
  if imgOverrides[link] ~= nil and img == link then
  	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
  if text == nil and txtOverrides[link] ~= nil then
    text = txtOverrides[link]
  end
  if linkOverrides[link] ~= nil then
    if text == nil 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 then text = link end
    link = link..' ('..iconType..')'
  end

  --Creating the image...
  local result = '[[File:'..img
  --Add in type if one is set
  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..']]'
  
  --Add Quantity to the front if requested
  if qty ~= nil and qty ~= '' then result = Shared.formatnum(qty)..' '..result end

  if not notext then 
    if nolink then
      if text ~= nil and text ~= '' then
        result = result..' '..text
      else
        result = result..' '..link
      end
    else
      result = result..' [['..link
      if text ~= nil and text ~= '' and text ~= link then
        result = result..'|'..text
      end
      result = result..']]'
    end
  end

  result = '<span style="display:inline-block">'..result..'</span>'

  if menu then
    result = '{| class="articletable" style="display:inline-block;vertical-align:middle;"\r\n|-\r\n|'..result
    result = result..'\r\n|}'
  end

  return result
end

function p._SkillReq(skill, level, showText)
  local result = p.Icon({skill, type='skill', notext='true'})
  if showText then
    result = result..' [['..skill..']]'
  end
  if level == nil then level = 'Unknown [[Category:Pages with script errors]]' end
  result = result.." Level "..level

  result = '<span style="display:inline-block">'..result..'</span>'
  return result
end

function p.SkillReq(frame)
  local args = frame.args ~= nil and frame.args or frame
  local skill = args[1]
  local level = tonumber(args[2])
  local showText = args.showText ~= nil and args.showText ~= '' and args.showText ~= 'false'
  return p._SkillReq(skill, level, showText)
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 result = 'Level '..level..' '
  result = result..p.Icon({linkname, img=iconname, type='item', notext = true})..p.Icon({'Mastery', notext=true})
  if showText  then result = result..'[['..linkname..']] [[Mastery]]' end
  result = '<span style="display:inline-block">'..result..'</span>'
  return result
end

function p.MasteryReq(frame)
  local args = frame.args ~= nil and frame.args or frame
  local itemName = args[1]
  local level = tonumber(args[2])
  local showText = args.showText ~= nil and args.showText ~= '' and args.showText ~= 'false'
  return p._MasteryReq(itemName, level, showText)
end

function p._Currency(fileName, link, amt, maxAmt)
  local amtText = Shared.formatnum(amt)
  if maxAmt ~= nil and maxAmt >= amt then
    amtText = amtText .. ' - ' .. Shared.formatnum(maxAmt)
  end

  local fileText = ''
  if fileName ~= nil then
    fileText = '[[File:' .. fileName .. '|25px'
    if link ~= nil then
      fileText = fileText .. '|link=' .. link
    end
    fileText = fileText .. ']]&nbsp;'
  end

  return '<span style="display:inline-block">' .. fileText .. amtText .. '</span>'
end

function p.GP(amt, maxamt)
  return p._Currency('Coins.svg', 'Coins', amt, maxamt)
end

function p.SC(amt, maxamt)
  return p._Currency('Slayer Coins.svg', 'Currency#Slayer Coins', amt, maxamt)
end

function p.RC(amt, maxamt)
  return p._Currency('Raid_Coins.svg', 'Currency#Raid Coins', amt, maxamt)
end

return p