Module:ItemTooltip

--- ItemTooltip builds tooltips of Risk of Rain 2's items. --	--	@module		itemtooltip --	@alias		p --	@author		User:Paradoxzyx --	@require	Module:Items/Data --	@require	Module:Equipment/Data --	@release	stable -- local p = {}

local all_items = mw.loadData("Module:Items/Data").items local all_equipment = mw.loadData("Module:Equipment/Data").equipment

---	Gets the table entry for an item or equipment. --	@function		GetItemOrEquipment --	@param			{string} item_name Item name --	@return			{table} Item database entry --	@local function GetItemOrEquipment(item_name) assert(item_name ~= nil, 'Error: Name missing. 1st parameter is required.') local item = all_items[item_name] or all_equipment[item_name] assert(item ~= nil, 'Error: Incorrect name "' .. item_name .. '". Check capitalization.') return item end

---	Gets the item rarity. --	@function		p.GetRarity --	@param			{table} frame Frame object w/ first argument being the item name and 2nd argument being --							to use simple form of rarity (default false) --	@return			{string} Item rarity name function p.GetRarity(frame) local item_name = frame.args[1] local simple_form = frame.args[2] or false local item = GetItemOrEquipment(item_name) if type(item) == 'string' then return item end local rarity = item.Rarity if simple_form and rarity == 'Elite Equipment' then rarity = 'Equipment' elseif simple_form and rarity == 'Lunar Equipment' then rarity = 'Lunar' end return rarity end

---	Gets the item description. --	@function		p.GetDescription --	@param			{table} frame Frame object w/ first argument being the item name --	@param[opt]		{table} item Item database entry --	@return			{string} Item description function p.GetDescription(frame, item) local item_name = frame.args[1] item = item or GetItemOrEquipment(item_name) if type(item) == 'string' then return item else return frame:preprocess(item.Desc:gsub("\r\n", " ")) end end

---	Gets the item flavor text quote. --	@function		p.GetQuote --	@param			{table} frame Frame object w/ first argument being the item name --	@param[opt]		{table} item Item database entry --	@return			{string} Item quote function p.GetQuote(frame, item) local item_name = frame.args[1] item = item or GetItemOrEquipment(item_name) if type(item) == 'string' then return item else return frame:preprocess(item.Quote) end end

-- local GetItemBox local itembox_template = ' ' ..  ..                           ' ' ..                               ..                            ' ' ..                          ' '

---	Gets the item's tooltip image box. --	@function		GetItemBox --	@param			{table} frame Frame object --	@param			{string} name Item name --	@param			{number} size Width of item image in px --	@return			{string} Wikitext of tooltip image box --	@local local function GetItemBox(frame, name, size) frame.args[1] = name frame.args[2] = 'simple' local rarity = p.GetRarity(frame) local itemSize = math.floor(size * 0.9375) local offset = (size - itemSize) / 2 return string.format(itembox_template,     rarity,      size,      name,      offset,      offset,      name,      itemSize,      name  ) end

-- GetTooltip local tooltip_template = ' ' .. ' ' ..                             ' %s ' .. '%s' .. ' ' ..                           ' ' ..                              '%s %s   %s' .. ' ' ..                         ' '

---	Gets the item's tooltip. --	@function		p.GetTooltip --	@param			{table} frame Frame object w/ first argument being the item name --	@return			{string} Wikitext of tooltip function p.GetTooltip(frame) local item_name = frame.args[1] local item = GetItemOrEquipment(item_name) if type(item) == 'string' then return item end local item_box = GetItemBox(frame, item_name, 48) local quote = p.GetQuote(frame, item) local desc = p.GetDescription(frame, item) local cooldown = item.Cooldown and string.format(' %ss ', item.Cooldown) or '' local style = '' local function strip(txt) return txt:gsub('(.-) ', '%1'):gsub('+', '') end -- remove tags and + sign if strip(quote) == strip(desc) then quote = '' style = 'align-self: center;' end return string.format(tooltip_template,   item_box,    cooldown,    style,    item_name,    quote == '' and "" or (" " .. quote),   desc  ) end

return p --