Module:Rarity

From Super Animal Royale Wiki
Jump to: navigation, search

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 all_imgs = ''
	local internal_cats = require('Module:ItemCategories')
	local image_list = {}
	local link
	if item_categories[args[1]]["internal"] then
		for key,value in pairs(item_directory) do
	    	if value['class'] == args[1] then
	    		table.insert(image_list, key)
	    	end
		end
	else
		image_list = item_categories[args[1]]["list"]
		if args["category_links"] then
			link = item_categories[args[1]]["link"]
		end
	end
	for _, key in ipairs(p.sortByRarity(image_list)) do
		all_imgs = all_imgs .. p.createImage({[1] = key, link = link})
	end
	return all_imgs
end

p.sortByRarity = function (f) -- Takes a table with list of item names, returns the items sorted by their rarity (common to special)
	local args = f
	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(args, item) then
			table.insert(all_items, item)
		end
	end
	table.sort(all_items, function(a, b) 
		-- rarity comaprison, if different, sort by rarity
		-- if both have no groups sort by name a < b
		-- if one of them have group but not the other treat group as 0?
		-- if both have a group then sort by group
		-- THIS IS BAD CODE THAT SHOULD BE OPTIMIZED LATER, TEMPORARY ONLY
		if item_directory[a]["rarity"] ~= item_directory[b]["rarity"] then
			return item_directory[a]["rarity"] < item_directory[b]["rarity"] 
		elseif item_directory[a]["group"] == nil and item_directory[b]["group"] == nil then
			return a < b
		elseif item_directory[a]["group"] == nil then
			return 0 < item_directory[b]["group"]
		elseif item_directory[b]["group"] == nil then
			return item_directory[a]["group"] < 0
		else
			return item_directory[a]["group"] < item_directory[b]["group"]
		end
	end)
	return all_items
end

p.createImage = function (f)
	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 args['file'] then
			item['file'] = args['file']
		end
		local color = colors[item['rarity']]
		if color == nil then
			color = 'common'
		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:cssText('background: ' .. color)
		imagebox:addClass('sar-rarity-imagebox')
		imagebox:wikitext( '[[File:' .. item['file'] .. '|152px]]' )
		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 )
		local itemtext = mw.html.create( 'p' )
		itemtext:addClass( "sar-rarity-itemtext" )
		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
		if args['link'] then  -- if custom argument, do this
			itemtext:wikitext(p.rarity({item['rarity']}) .. '<br>' .. '[[' .. args['link'] .. '|' .. itemname .. ']]')
		elseif item['link'] then -- if item has specific link in Module:ItemList
			itemtext:wikitext(p.rarity({item['rarity']}) .. '<br>' .. '[[' .. item['link'] .. '|' .. itemname .. ']]')
		elseif item_categories[item['class']] and item_categories[item['class']]['link'] then  -- else if the specific category is internal and has a link link to its main article
			itemtext:wikitext(p.rarity({item['rarity']}) .. '<br>' .. '[[' .. item_categories[item['class']]['link'] .. '|' .. itemname .. ']]')
		else  -- otherwise don't do links
			itemtext:wikitext(p.rarity({item['rarity']}) .. '<br>' .. itemname)
		end
		if args['add_to_desc'] then 
			itemtext:wikitext(f:preprocess(args['add_to_desc']))
		elseif item['description'] and not args['ignore_description'] then
			itemtext:wikitext(f:preprocess(item['description']))
		end
		mainbox:node ( itemtext )
		fullelements = fullelements .. tostring(mainbox)
	end
	return fullelements
end

p.rarity = function (f)
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
	else
		f = mw.getCurrentFrame()
	end
-- <span style="color:#6bbf00;font-weight:bold">Uncommon</span>
	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