Module:Item

local loader = require('Module:Loader') local getArgs = require('Module:Arguments').getArgs

local string, table, yesno, raritytier, currency, linkmodule, color = loader.require('String', 'Table', 'Yesno', 'RarityTier', 'Currency', 'Link', 'Color') local aliases, alltooltips = loader.loadData('Item/Aliases', 'Inventory slot/Aliases')

local addThousandSep = string._formatNum local makeRarity = raritytier._link local makeCurrency = currency._currency local makeEnchant = linkmodule._enchantmentsLink local setColor = color._colorTemplates

local lang = mw.getContentLanguage

local p = {} --- -- Template: Resource Display -- -- Displays item and its amount, alongside its image, drop chance, rarity, enchant, etc --- function p.resourceDisplay(frame) local args = getArgs(frame) local str = args[1] local img = yesno(args["image"] or args["img"] or args["i"], true) local nolink = yesno(args["nolink"] or args["nl"], false) local noErr = yesno(args["noerror"] or args["ne"], false) local ignoreOdds = yesno(args["ignoreodds"], false)

return string.pcall(p._item, str, img, nolink, noErr, false, ignoreOdds) end --- -- Template: Item Display -- -- Displays only item, alongside its image, drop chance, rarity, enchant, etc --- function p.itemDisplay(frame) local args = getArgs(frame) local str = args[1] local img = yesno(args["image"] or args["img"] or args["i"], true) local nolink = yesno(args["nolink"] or args["nl"], false) local noErr = yesno(args["noerror"] or args["ne"], false) local ignoreOdds = yesno(args["ignoreodds"], false)

return string.pcall(p._item, str, img, nolink, noErr, true, ignoreOdds) end --- -- Functions for use in other modules --- function p._resourceDisplay(str, img, nolink, noErr, ignoreOdds) return p._item(str, yesno(img, true), nolink, noErr, false, ignoreOdds) end function p._itemDisplay(str, img, nolink, noErrm, ignoreOdds) return p._item(str, yesno(img, true), nolink, noErr, true, ignoreOdds) end

--Module Access Point function p._item(str, img, nolink, noErr, ignoreAmount, ignoreOdds) local num, num2 mw.log(str) --remove commas, they break stuff str = str:gsub(',', ''):gsub('&percnt;', '%%'):gsub('&amp;', '&') --determine whether or not the hover text should be displayed local no_hover = str:match('%d+%-+%d+') and true or false if str:match('%s*[^\\];%s*(.-)$') then str, after = str:match('^(.+)%s*;%s*(.-)$') end --separate item and quantity + handle invalid syntax if not ignoreAmount then if str:match('^([%d,%-%?]+%.?[%d,%-]*x? ?or ?[%d,%-%?]+%.?[%d,%-]*x?) (.*)') then num, item = str:match('^([%d,%-%?]+%.?[%d,%-]*x? ?or ?[%d,%-%?]+%.?[%d,%-]*x?) (.*)') elseif str:match('^([%d,%-%?]+%.?[%d,%-]*x?) (.*)') then num, item = str:match('^([%d,%-%?]+%.?[%d,%-]*x?) (.*)') elseif noErr then return str else item = str no_hover = true -- return string.error('Invalid Resource list syntax "%s"', str) end if num and num:match('or') then local tempStr = num num, num2 = tempStr:match('(.+) ?or ?(.+)') end --handle colors (green = standard, red = only when number is "?") text_color = "Green" if (num and num:match("%?")) or (num2 and num2:match('%?')) then no_hover = true text_color = "Red" end --remove unnecessary characters from number num = num and num:gsub('[x,]', '') num2 = num2 and num2:gsub('[x,]', '') else if str:match('^[%d,%-%?]+%.?[%d,%-]*x? ?or ?[%d,%-%?]+%.?[%d,%-]*x? (.*)') then item = str:match('^[%d,%-%?]+%.?[%d,%-]*x? ?or ?[%d,%-%?]+%.?[%d,%-]*x? (.*)') elseif str:match('^[%d,%-%?]+%.?[%d,%-]*x? (.*)') then item = str:match('^[%d,%-%?]+%.?[%d,%-]*x? (.*)') else item = str end end --remove unnecessary characters from item item = item:gsub('[%[%]]', '') --unpack and process aliases item = ' '..item..' ' for pattern, replace in pairs(aliases) do		if item:match(pattern) then item = item:gsub(pattern, replace) end end item = mw.text.trim(item) local alt = item -- if item:match('s$') or item:match("[Bb]locks") then -- 	alt = item -- 	item = item:gsub('s$', ''):gsub("[Bb]locks", "Block") -- end if item:match("[Bb]locks") then alt = item item = item:gsub("[Bb]locks", "Block") end --generate alternate text if item:match('%|') then item, alt = item:match('(.*)%|(.*)') elseif item:match('[^%%]%!') then item, alt = item:match('(.*)%!(.*)') end --determine whether or not odds should be displayed local odds = nil if item:match("%s%%.+%%") then odds = item:match(".*%s%%(.+)%%") item = item:gsub('(.*)%s%%.+%%(.*)', '%1%2') end if alt:match("%s%%.+%%") then odds = alt:match(".*%s%%(.+)%%") alt = alt:gsub('(.*)%s%%.+%%(.*)', '%1%2') end --determine whether or not enchant should be displayed local enchant = nil if item:match("%s&.+&") then enchant = item:match(".*%s&(.+)&") item = item:gsub('(.*)%s&.+&(.*)', '%1%2') end if alt:match("%s&.+&") then enchant = alt:match(".*%s&(.+)&") alt = alt:gsub('(.*)%s&.+&(.*)', '%1%2') end --change the $rarity$ to actual rarity template alt = alt:gsub(		"(.*)$(.+)$(.*)", 		function(a, b, c) 			return table.concat{				a, 				makeRarity(b, nil, nil, true), 				c,			} 		end	) item = item:gsub("(.*)%s?$.+$%s?(.*)", "%1%2")

item = mw.text.trim(item) --convert armor piece string to functional string, pick the proper image (helmet) local item_img = item if item:match('(.*) [Pp][Ii][Ee][Cc][Ee]') then local foo = item:match('(.*) [Pp][Ii][Ee][Cc][Ee]') if foo:match('(.*) [Aa][Rr][Mm][Oo][Rr]') then item_img = foo:match('(.*) [Aa][Rr][Mm][Oo][Rr]')..' Helmet' else item_img = foo..' Helmet' end elseif item:match('(.+) [Aa][Rr][Mm][Oo][Rr]') then local foo = item:match('(.+) [Aa][Rr][Mm][Oo][Rr]') item_img = foo..' Helmet' end --convert minion string to functional string, pick the proper image (tier I minion head) if item:match('Minion [IVXivx]+') then item = item:gsub('(Minion )[IVXivx]+', '%1') elseif item:match('Minion') then item_img = item..' I'	end

local title, title2 --handle the calculations performed on the number, then format it	if not ignoreAmount then if not no_hover then --get amount of stacks and items stacks = math.floor(tonumber(num) / 64) items = tonumber(num) % 64 --add thousand separator to the quantity num = addThousandSep(num) end title = nil if not no_hover then --add hover info about amount in stacks and items if stacks + items * 0.01 > 1 then local str_stacks = lang:plural( stacks, 'stack', 'stacks' ) local str_items = lang:plural( items, 'item', 'items' ) if items > 0 then title = ('%s %s plus %s (%s in total)') :format(addThousandSep(stacks), str_stacks, items, num) else title = ('%s %s (%s items total)') :format(addThousandSep(stacks), str_stacks, num) end end end --same thing as above, but for second number if not no_hover and num2 then --get amount of stacks and items stacks2 = math.floor(tonumber(num2) / 64) items2 = tonumber(num2) % 64 --add thousand separator to the quantity num2 = addThousandSep(num2) end title2 = nil if not no_hover and num2 then --add hover info about amount in stacks and items if stacks2 + items2 * 0.01 > 1 then local str_stacks = lang:plural(stacks2, 'stack', 'stacks') local str_items = lang:plural(items2, 'item', 'items') if items2 > 0 then title2 = ('%s %s plus %s (%s in total)') :format(addThousandSep(stacks2), str_stacks, items2, num2) else title2 = ('%s %s (%s items total)') :format(addThousandSep(stacks2), str_stacks, num2) end end end end local function _getImage(img_, nolink_, item_) local imgexist = (not not alltooltips[img_]) or mw.title.makeTitle(6, img_..'.png').file.exists return imgexist and string.makeImage(img_, { size=21, link=nolink_ and nil or item_ })..' ' or string.makeImage('Blank Icon.png', { size=21, link='' })..' ' end --apply formatting local ret = {} if ignoreAmount then ret = { (img and _getImage(item_img, nolink,item) or ''), nolink and (alt and alt or item) or string.makeLink(item, alt), (enchant and (" (%s)"):format(makeEnchant(enchant)) or ''), ((odds and not ignoreOdds) and ("(%s)"):format(odds) or ''), after and ' '..after or '', }	elseif item:lower:find('^%s*coins?%s*$') then ret = { tostring(makeCurrency(ignoreAmount and nil or num, nil, false, false, img, nolink, true, nil, 'coins')) }	elseif item:lower:find('^%s*bits?%s*$') then ret = { tostring(makeCurrency(ignoreAmount and nil or num, nil, false, false, img, nolink, true, nil, 'bits')) }	elseif item:lower:find('^%s*gems?%s*$') then ret = { tostring(makeCurrency(ignoreAmount and nil or num, nil, false, false, img, nolink, true, nil, 'gems')) }	elseif item:lower:find('^%s*pelts?%s*$') then ret = { tostring(makeCurrency(ignoreAmount and nil or num, nil, false, false, false, nolink, true, nil, 'pelts')) }	elseif num2 then ret = { img and _getImage(item_img, nolink, item) or '', string.makeTitle(				setColor(text_color, num), 				title, 				{ ignoreTitleNil = true }			), --returns first param if title is nil ' ', setColor(text_color, 'or'), ' ', string.makeTitle(setColor(text_color, num2..'x'), title2, {ignoreTitleNil = true}), --returns first param if title2 is nil ' ',			nolink and (alt and alt or item) or string.makeLink(item, alt), enchant and (" (%s)"):format(makeEnchant(enchant)) or '', (odds and not ignoreOdds) and ("(%s)"):format(odds) or '', after and ' '..after or '', }	else ret = { (img and _getImage(item_img, nolink,item) or ''), string.makeTitle(num and setColor(text_color, num..'x') or '', title, {ignoreTitleNil = true}), --returns first param if title is nil ' ',			nolink and (alt and alt or item) or string.makeLink(item, alt), (enchant and (" (%s)"):format(makeEnchant(enchant)) or ''), ((odds and not ignoreOdds) and ("(%s)"):format(odds) or ''), after and ' '..after or '', }	end return (table.concat(ret)) --return (table.concat(ret):gsub('%%([^%%])', '%1')) end

--Finish Module return p