Module:MagicItems

-- Purposes of this module: -- Displays lists and profiles for magic items -- Can generate a magic item card for any DMG or custom magic item

local p = {} local Shared = require( "Module:Shared" ) local MagicItemData = mw.loadData( 'Module:Data/magicitems' ) local cardFactor = 12 local cardS, cardM, cardL = 350, 700, 1200 local cardSTitle = 20

--========================================================== -- CALL FUNCTIONS --==========================================================

--A parent function; meant to be used in CSS. Displays all magic items function p.callAllMagicItems(frame) return p.displayMagicItems(MagicItemData) end

--A parent function; meant to be used in CSS. User asks for many items, separated by commas function p.callTheseMagicItems(frame) local Str = frame.args[1] or frame.args["name"] local List = p.getTheseMagicItems(Str) return p.displayMagicItems(List) end

function p.callItemBlock(frame) local Str = frame.args[1] or frame.args["name"] local List = p.getTheseMagicItems(Str) return p.displayItemBlock(List) end

function p.callItemCard(frame) local Str = frame.args[1] or frame.args["name"] return p.displayItemCard(Str) end

function p.callTypeIcon(frame) return "https://vignette.wikia.nocookie.net/ennorath/images/a/a5/Banging-gavel.png" end

--Returns a table of the magic items given in a string function p.getTheseMagicItems(ItemString) local stringList = {} local sortStringList = {} local sortedList = {} if(ItemString ~= nil) then stringList = Shared.splitString(ItemString, ",") else return nil end

for i=1, 500 do       if(MagicItemData[i] ~= nil) then for j,v in ipairs(stringList) do               local comparison = ""           --If the first character is a space, remove it                if(string.sub(stringList[j],1,1) == " ") then comparison = string.sub(stringList[j],2) else comparison = stringList[j] end if(string.lower(MagicItemData[i].Name) == string.lower(comparison)) then table.insert (sortedList, MagicItemData[i]) end end end end if (sortedList == {}) then return nil end return sortedList end

--Returns a table of the magic items given in a string function p.getThisMagicItem(ItemString) if(ItemString == nil) then return nil end

for i,v in ipairs(MagicItemData) do       if(MagicItemData[i] ~= nil) then local comparison = ""          --If the first character in ItemString is a space, remove it            if(string.sub(ItemString,1,1) == " ") then comparison = string.sub(ItemString,2) else comparison = ItemString end local comparison2 = ""          --If the first character in this item's Name is a space, remove it            if(string.sub(MagicItemData[i].Name,1,1) == " ") then comparison2 = string.sub(MagicItemData[i].Name,2) else comparison2 = MagicItemData[i].Name end if(string.lower(comparison) == string.lower(comparison2)) then local ItemReturn = MagicItemData[i] return ItemReturn end end end return 1 end

--========================================================== -- DISPLAY CODES --==========================================================

--Input a table of magic item data. --The function will display it as a condensed, abbreviated table with --columns showing various attributes for each item function p.displayMagicItems(List) local tablestart = "\n{| class=\"article-table\" style=\"font-size:70%; line-height:90%;\" \n|-\n!\n!Name\n!Rarity\n!Type\n!Subtype\n!Attune\n!Damage\n!WeaponMod\n!Price\n!EffectBrief\n" local body0 = "" tableend = "\n|\}" local itemK = {} for i=1,500 do       for j=1, 500 do            if (List[j] ~= nil and List[j].Index == i) then itemK = List[j] local newLink = itemK.Link --newLink = "[|"..itemK.Name.."]" local newRow = "" if (itemK.Name ~= nil) then newRow = newRow.."|-\n|\n|["..newLink.." "..itemK.Name.."]\n" end if (itemK.Rarity ~= nil) then newRow = newRow.."|"..itemK.Rarity.."\n" end if (itemK.Type ~= nil) then newRow = newRow.."|"..itemK.Type.."\n" end if (itemK.Subtype ~= nil) then newRow = newRow.."|"..itemK.Subtype.."\n" end if (itemK.Attune ~= nil) then newRow = newRow.."|"..itemK.Attune.."\n" end if (itemK.Damage ~= nil) then newRow = newRow.."|"..itemK.Damage.."\n" end if (itemK.WeaponMod ~= nil) then newRow = newRow.."|"..itemK.WeaponMod.."\n" end if (itemK.Price ~= nil) then newRow = newRow.."|"..itemK.Price.."\n" end if (itemK.EffectBrief ~= nil) then newRow = newRow.."|"..itemK.EffectBrief.."\n" end body0 = body0..newRow end end end return tablestart..body0..tableend end

---thumb|513x513px|none \n"               else displayText = displayText..""..itemT.Rarity..", "..itemT.Type..", "..itemT.Subtype..". \n RedDivider 01.png \n"                end                displayText = displayText.."=== Description ===\n"..itemT.Description.."\n"                if (itemT.Type == "Weapon") then displayText = displayText.."==== Damage ====\n"..itemT.Damage.."\n" end                displayText = displayText.."=== Properties ===\n"..itemT.Properties.."\n\n"                displayText = displayText..itemT.EffectLong.."\n"                if (itemT.Type == "Weapon") then displayText = displayText.."RedDivider 01.png \n=== Critical Effects ===\n"..itemT.Crit1.."\n\n"..itemT.Crit2.."\n"end            	body0 = body0..displayText    	end end    end    return ""..body0.."" end

--A function to determine what size a card should be --based on the length of its description or name function p.getCardSize(ItemString) local cardWidth, cardHeight = 0 local size = "s" local f = cardFactor local itemT = p.getTheseMagicItems(ItemString) if (itemT ~= nil) then itemT = itemT[1] end if (itemT == nil) or (itemT.Name == nil) then itemT = p.getTheseMagicItems("luckstone") itemT = itemT[1] end --adjust size based on Title if (string.len(itemT.Name) > cardSTitle) then size = "m" end --adjust based on Description if (string.len(itemT.DescrLong) > cardS) then size = "m" end if (string.len(itemT.DescrLong) > cardM) then size = "l" end if (string.len(itemT.DescrLong) > cardL) then size = "xl" end if (size == "s") then cardWidth, cardHeight = (25*f), (35*f) end if (size == "m") then cardWidth, cardHeight = (35*f), (50*f) end if (size == "l") then cardWidth, cardHeight = (35*f), (60*f) end if (size == "xl") then cardWidth, cardHeight = (40*f), (70*f) end

return cardWidth, cardHeight, size end

--A function to generate an item card using WikiText. function p.displayItemCard(ItemString) local itemT = p.getTheseMagicItems(ItemString) if (itemT ~= nil) then itemT = itemT[1] end if (itemT == nil) or (itemT.Name == nil) then itemT = p.getTheseMagicItems("luckstone") itemT = itemT[1] end local color = "" local cardWidth, cardHeight, _ = p.getCardSize(ItemString) --local cardWidth = 400 local subWidth = cardWidth - 35 --local cardHeight = 600 local icon = p.getItemIcon(ItemString) if (icon == nil) or (icon == "") then icon = "Banging-gavel.png" end local attune = "" if (itemT.Attune == "y") then attune = "(requires attunement)" end local tableStart = "{| class=\"cardTable1\" style=\"width:"..tostring(cardWidth).."px; height:"..tostring(cardHeight).."px; border-color:black; border-width:7px; text-align:left;\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\"\n|style=\"height:5px\"|\n{| width=\"100%\" border=\"0\" style=\"font-size:1.2; border-style:solid; border-width:2px; border-color:black; border-radius:8px; background-color:#636363;\"\n|-\n|" local tableTitle = " \n|"..itemT.Name.." \n| style=\"width:10px\"|\n|}" local tableSubtitle = "\n{| width=\""..tostring(subWidth).."px\" align=\"center\" border=\"0\" style=\"border-style:solid; text-align:left; border-top-style:none; border-width:2px; border-color:black; border-bottom-left-radius:8px; border-bottom-right-radius:8px; padding-left:10px; background-color:#ffffff; color:grey;\"\n|-\n|"..itemT.Subtype..", "..itemT.Rarity.." "..attune.."\n|}" local tableBody = "\n|-\n| style=\"padding-bottom:15px;\" |\n{| cellpadding=\"10px\" width=\""..tostring(subWidth).."px\" height=\"100%\" align=\"center\" border=\"0\" style=\"border-style:solid; line-height:1.3; height:100%; text-align:left; border-width:2px; border-color:black; background-color:#ffffff; color:black;\"\n|-\n| style=\"vertical-align:top;\"|"..itemT.DescrLong local tableEnd = "\n|}\n|}" return tableStart..tableTitle..tableSubtitle..tableBody..tableEnd end

--Returns a filename for an icon based on a given item's subtype function p.getItemIcon(ItemString) local itemT = p.getTheseMagicItems(ItemString) if (itemT ~= nil) then itemT = itemT[1] end if (itemT == nil) or (itemT.Subtype == nil) then itemT = p.getTheseMagicItems("luckstone") itemT = itemT[1] end local icon = ""

if (itemT.Subtype == "Armor") then     icon = "Shoulder-armor" end if (itemT.Subtype == "Boots") then     icon = "Leg-armor" end if (itemT.Subtype == "Cloak / Robe") then icon = "Cloak" end if (itemT.Subtype == "Headgear") then  icon = "Jewel-crown" end if (itemT.Subtype == "Necklace") then  icon = "Pearl-necklace" end if (itemT.Subtype == "Other") then     icon = "Cut-diamond" end if (itemT.Subtype == "Other Apparel") then icon = "Robe" end if (itemT.Subtype == "Potion") then    icon = "Standing-potion" end if (itemT.Subtype == "Ring") then      icon = "Ring" end if (itemT.Subtype == "Scroll") then    icon = "Tied-scroll" end if (itemT.Subtype == "Shield") then    icon = "Edged-shield" end if (itemT.Subtype == "Wand / Staff") then icon = "Orb-wand" end if (itemT.Subtype == "Weapon") then    icon = "Plain-dagger" end icon = icon..".png" return icon end

--======================================================== -- EXPERIMENTAL FUNCTIONS --========================================================

--A parent function; calls function p.callPrintItemCards(frame) local Str = frame.args[1] return p.printItemCards(Str) end

function p.printItemCards(iString) local itemT = p.getTheseMagicItems(iString)--"Cape of the Mountebank, Bag of Holding")   local smallTable = {}    local mediumTable = {}    local largeTable = {}    local xlargeTable = {}    local pageTable = {}    local smallRowSize = 2    local mediumRowSize = 2    local largeRowSize = 2    local xlargeRowSize = 1    for k,v in ipairs(itemT) do        local _, __, cSize = p.getCardSize(v.Name)        if(cSize == "s") then table.insert(smallTable, v) end        if(cSize == "m") then table.insert(mediumTable, v) end        if(cSize == "l") then table.insert(largeTable, v) end        if(cSize == "xl") then table.insert(xlargeTable, v) end    end    local smallN = Shared.tableCount(smallTable)    local mediumN = Shared.tableCount(mediumTable)    local largeN = Shared.tableCount(largeTable)    local xlargeN = Shared.tableCount(xlargeTable)    if(mediumN >= 4) then pageTable[1] = Shared.getTableElements(mediumTable, 4) end

local smallT = "" local mediumT = "" local largeT = "" local xlargeT = "" local SrowCount = 0 local MrowCount = 0 local LrowCount = 0 local XLrowCount = 0 for k,v in ipairs(itemT) do       local _, __, cSize = p.getCardSize(v.Name) if(cSize == "s") then smallT = smallT.."\n|\n"..p.displayItemCard(v.Name) SrowCount = SrowCount + 1 if(SrowCount == smallRowSize) then smallT = smallT.."\n|-" SrowCount = 0 end end if(cSize == "m") then mediumT = mediumT.."\n|\n"..p.displayItemCard(v.Name) MrowCount = MrowCount + 1 if(MrowCount == mediumRowSize) then mediumT = mediumT.."\n|-" MrowCount = 0 end end if(cSize == "l") then largeT = largeT.."\n|\n"..p.displayItemCard(v.Name) LrowCount = LrowCount + 1 if(LrowCount == largeRowSize) then largeT = largeT.."\n|-" LrowCount = 0 end end if(cSize == "xl") then xlargeT = xlargeT.."\n|\n"..p.displayItemCard(v.Name) XLrowCount = XLrowCount + 1 if(XLrowCount == xlargeRowSize) then xlargeT = xlargeT.."\n|-" XLrowCount = 0 end end end smallT = "{|"..smallT.."\n|}" mediumT = "{|"..mediumT.."\n|}" largeT = "{|"..largeT.."\n|}" xlargeT = "{|"..xlargeT.."\n|}" local tableStr = smallT.."\n"..mediumT.."\n"..largeT.."\n"..xlargeT return tableStr end

--========================================================

return p