Documentation for this module may be created at Module:Rarity/doc
p = {}
colors = {[0] = '#595959', [1] = '#6bbf00', [2] = '#00b5f2', [3] = '#8733c6', [4] = '#f7cd0f', [5] = '#f47f3d', common ='#595959', uncommon ='#6bbf00', rare ='#00b5f2', epic='#8733c6', legendary='#f7cd0f', special='#f47f3d'}
reversecolors = {[0] = 'common', [1] = 'uncommon', [2] = 'rare', [3] = 'epic', [4] = 'legendary', [5] = 'special'}
item_directory = require('Module:ItemList')
item_categories = require('Module:ItemCategories')
p.getCategory = function (f)
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
local cat_name = args[1]
local all_imgs = ''
local internal_cats = require('Module:ItemCategories')
local image_list = {}
local link
assert(item_categories[cat_name] ~= nil, "No " .. cat_name .. " category in Module:ItemCategories")
if item_categories[cat_name].classes then
local isMatchingClassByName = {}
for _, class in pairs( item_categories[cat_name].classes ) do
isMatchingClassByName[class] = true
end
for key, value in pairs(item_directory) do
if isMatchingClassByName[value.class] then
table.insert(image_list, key)
end
end
elseif item_categories[cat_name].internal then
for key, value in pairs(item_directory) do
if value['class'] == cat_name then
table.insert(image_list, key)
end
end
else
image_list = item_categories[cat_name]["list"]
if args["category_links"] then
link = item_categories[cat_name]["link"]
end
end
local image_list_filtered = p.getFilteredImageList({
image_list = image_list,
args = args,
})
local image_list_sorted = p.sortByRarity({
image_list = image_list_filtered,
order_by_currency_amount = args.order_by_currency_amount,
})
for _, key in ipairs(image_list_sorted) do
all_imgs = all_imgs .. p.createImage({
[1] = key,
link = link,
ignore_description = args.ignore_description,
ignore_store = args.ignore_store,
ignore_event = args.ignore_event,
ignore_currency = args.ignore_currency,
ignore_bundle = args.ignore_bundle,
})
end
return all_imgs
end
p.getSingleItem = function (f)
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
local item_id = args[1]
local matching_item_directory_item = item_directory[item_id]
assert(matching_item_directory_item ~= nil, "No item with ID \"" .. item_id .. "\" in Module:ItemList")
return p.createImage({
[1] = item_id,
ignore_description = args.ignore_description,
ignore_store = args.ignore_store,
ignore_event = args.ignore_event,
ignore_currency = args.ignore_currency,
ignore_bundle = args.ignore_bundle,
})
end
p.getFilteredImageList = function(f)
local image_list = f.image_list
local args = f.args
local isAtLeastOneFilterPresent = (
(args.filter_store ~= nil)
or (args.filter_baseAnimal ~= nil)
or (args.filter_rarity ~= nil)
-- or (args.filter_something_else ~= nil)
)
if isAtLeastOneFilterPresent == false then
return image_list
end
local image_list_results = {}
for _, key in ipairs(image_list) do
local directoryItem = item_directory[key]
if directoryItem then
local directoryItemMatchesFilters = (
(
(args.filter_store == nil)
or (args.filter_store == directoryItem.store)
)
and (
(args.filter_baseAnimal == nil)
or (args.filter_baseAnimal == directoryItem.baseAnimal)
)
and (
(args.filter_rarity == nil)
or (args.filter_rarity == tostring(directoryItem.rarity))
)
--[[and (
(args.filter_something_else == nil)
or (args.filter_something_else == directoryItem.something_else)
)]]
)
if directoryItemMatchesFilters then
table.insert(image_list_results, key)
end
end
end
return image_list_results
end
p.sortByRarity = function (f)
-- Takes a table with list of item names, returns the items sorted by their rarity (common to special, additional sorting done via groups variable)
local image_list = f.image_list
local all_items = {}
local our_items = {}
local function has_value (tab, val)
for index, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
for item in pairs(item_directory) do
if has_value(image_list, item) then
table.insert(all_items, item)
end
end
local order_by_currency_amount = (f.order_by_currency_amount == '1')
table.sort(all_items, function(a, b)
local item_dir_a = item_directory[a]
local item_dir_b = item_directory[b]
-- sort by currency field (legacy)
if (
order_by_currency_amount
and item_dir_a.currency
and item_dir_b.currency
and (item_dir_a.currency.name == item_dir_b.currency.name)
) then
-- if the currency name is the same,
-- and the currency amount is different, lower currency amount goes first
if item_dir_a.currency.amount ~= item_dir_b.currency.amount then
return (item_dir_a.currency.amount < item_dir_b.currency.amount)
end
end
-- sort by first item in currencies field
if (
order_by_currency_amount
and item_dir_a.currencies
and item_dir_b.currencies
and item_dir_a.currencies[1]
and item_dir_b.currencies[1]
and (item_dir_a.currencies[1].name == item_dir_b.currencies[1].name)
) then
-- if the currency name is the same,
-- and the currency amount is different, lower currency amount goes first
if item_dir_a.currencies[1].amount ~= item_dir_b.currencies[1].amount then
return (item_dir_a.currencies[1].amount < item_dir_b.currencies[1].amount)
end
end
-- if the rarity is different, lower rarity goes first
if item_dir_a.rarity ~= item_dir_b.rarity then
return (item_dir_a.rarity < item_dir_b.rarity)
end
local item_group_a = item_dir_a.group or 0
local item_group_b = item_dir_b.group or 0
-- if the group is different, lower group goes first
if item_group_a ~= item_group_b then
return (item_group_a < item_group_b)
end
-- same rarity, group and currency amount, sort by name alphabetically
return (a < b)
end)
return all_items
end
p.createImage = function (f)
-- Input: {string, string, string} where each string is a name of item from ItemList
-- Optional named arguments can be specified however then these parameters change behavior of output for all item names
-- Optional parameters: file (to specify custom file name), append_before (to specify custom text before image box), amount (to specify amount of items), custom_name (to specify custom name for items), link (to specify link for an item), add_to_desc (to append text to description), ignore_description (to completely skip creation of description)
-- Main function which creates a box with item based on Module:ItemList (item_directory)
-- Returns HTML elements for all of those
-- =p.createImage({"Goat DNA"})
-- <div class="sar-rarity-itembox"><div class="sar-rarity-imagebox" style="background: #595959">[[File:DNA Goat.png|152px]]</div><p class="sar-rarity-itemtext"><span style="color:#595959;font-weight:bold">Common</span><br>Goat DNA</p></div>
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
local fullelements = ''
for index, itemname in ipairs(args) do
local item = item_directory[itemname]
if (item == nil) then
item = {rarity = 0, file = itemname .. '.png'} -- default values
elseif (item['file'] == nil) then
item['file'] = itemname .. '.png'
end
if not item["hidden"] then -- Wish I didn't have to do that to implement this feature, continue doesn't exist and goto is in newer Lua
if args['file'] then
item['file'] = args['file']
end
local color = reversecolors[item['rarity']]
if color == nil then
color = 'common'
end
if args['custom_name'] then -- custom name from the arg
itemname = args['custom_name']
elseif item['name'] then -- custom name from the table
itemname = item['name']
end
local mainbox = mw.html.create( 'div' )
mainbox:addClass('sar-rarity-itembox')
if args['append_before'] then
mainbox:node(args['append_before'])
end
local imagebox = mw.html.create( 'div' )
imagebox:attr('title', itemname) -- hover tooltip
local colorclass = 'sar-rarity-' .. color
if item['framed'] then
colorclass = colorclass .. '-framed'
end
imagebox:addClass(colorclass)
imagebox:addClass('sar-rarity-imagebox')
local imageWikitext = '[[File:' .. item['file'] .. '|152px|alt=' .. itemname
if args['image_link'] then
imageWikitext = imageWikitext .. '|link=' .. args['image_link']
imageWikitext = imageWikitext .. '|' .. itemname -- hover tooltip
end
imageWikitext = imageWikitext .. ']]'
imagebox:wikitext(imageWikitext)
if args['amount'] then
local amountbox = mw.html.create('div')
amountbox:addClass('sar-rarity-amount-div')
local amounttext = mw.html.create('p')
amounttext:addClass('sar-rarity-amount-p')
amounttext:wikitext('X' .. tostring(args['amount']))
amountbox:node(amounttext)
imagebox:node(amountbox)
end
mainbox:node( imagebox )
if not args['image_only'] then
local itemtext = mw.html.create( 'p' )
itemtext:addClass( "sar-rarity-itemtext" )
local itemNameWikitext = ""
local itemLinkPage = (
args.link -- custom argument
or item.link -- link field on the item
or (
item_categories[item.class]
and item_categories[item.class].link -- link field on the category
or nil
)
)
if itemLinkPage ~= nil then
local itemLinkAnchorOptional = ""
if itemLinkPage == "Animals" then
if item.baseAnimal ~= nil then
itemLinkAnchorOptional = "#" .. item.baseAnimal
end
end
itemNameWikitext =
'[['
.. itemLinkPage
.. itemLinkAnchorOptional
.. '|'
.. itemname
.. ']]'
else
itemNameWikitext = itemname
end
itemtext:wikitext( p.rarity({ item.rarity }) .. '<br>' .. itemNameWikitext )
if args['add_to_desc'] then
itemtext:wikitext(f:preprocess(args['add_to_desc']))
else
if item.description and not args.ignore_description then
itemtext:wikitext('<br>' .. f:preprocess(item.description))
end
if item.store and not args.ignore_store then
local storeNameFull = "Unknown Store"
if item.store == "saw" then
storeNameFull = "S.A.W. Shop"
elseif item.store == "carl" then
storeNameFull = "Cackling Carl's Cart"
end
local storeLinkAnchorOptional = ""
if args.link then
storeLinkAnchorOptional = '#' .. args.link
elseif item.link then
storeLinkAnchorOptional = '#' .. item.link
elseif (
item_categories[item.class]
and item_categories[item.class].link
) then
storeLinkAnchorOptional = '#' .. item_categories[item.class].link
end
itemtext:wikitext(f:preprocess('<br>' .. '<small>[[' .. storeNameFull .. storeLinkAnchorOptional .. '|' .. storeNameFull .. ']]</small>'))
end
if item.event and not args.ignore_event then
itemtext:wikitext(f:preprocess('<br>' .. '<small>[[' .. item.event .. ']]</small>'))
end
if item.currencies and not args.ignore_currency then
local currencyWikitext = '<br>'
local currencyIdxLast = #item.currencies
for currencyIdx, currency in ipairs(item.currencies) do
local currencyName = currency.name
if currencyName == 'DNA' then
currencyName = 'DNA ' .. (item.baseAnimal or 'Fox')
end
currencyWikitext =
currencyWikitext
.. '{{Currency|' .. currencyName .. '|' .. currency.amount .. '}}'
if currencyIdx < currencyIdxLast then
currencyWikitext = currencyWikitext .. ' '
end
end
itemtext:wikitext(f:preprocess(currencyWikitext))
elseif item.currency and not args.ignore_currency then
local currency = item.currency
itemtext:wikitext(f:preprocess('<br>' .. '{{Currency|' .. currency.name .. '|' .. currency.amount .. '}}'))
end
if item.bundle and not args.ignore_bundle then
itemtext:wikitext(f:preprocess('<br>' .. '\'\'<small>A part of the</small><br>[[S.A.W. Shop#' .. item.bundle .. '|' .. item.bundle .. ']]\'\''))
end
end
mainbox:node ( itemtext )
end
fullelements = fullelements .. tostring(mainbox)
end
end
return fullelements
end
p.rarity = function (f)
-- Input: {"uncommon"} or {4}
-- Used to create a span element with text indicating rarity of given item
-- Returns HTML of a span text for given rarity
-- =p.rarity({3})
-- <span style="color:#8733c6;font-weight:bold">Epic</span>
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
local span = mw.html.create( 'span' )
if type(args[1]) == "string" then
args[1] = string.lower(args[1])
end
if colors[args[1]] == nil then
args[1] = 'common'
end
local color = colors[args[1]]
if type(args[1]) == "number" then
args[1] = reversecolors[args[1]]
end
span:cssText('color:' .. color .. ';font-weight:bold')
local upper, _ = args[1]:gsub("^%l", string.upper)
span:wikitext(upper)
return tostring(span)
end
return p