Super Animal Royale Wiki
Advertisement

此模块的文档可以在Module:Testmodule/doc创建

p = {}

function merge(a, b)
    if type(a) == 'table' and type(b) == 'table' then
        for k,v in pairs(b) do if type(v)=='table' and type(a[k] or false)=='table' then merge(a[k],v) else a[k]=v end end
    end
    return a
end

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] = '普通', [1] = '罕見', [2] = '稀有', [3] = '史詩', [4] =  '傳奇', [5] =  '特殊'}
item_directory = require('Module:AnimalsList')
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 = '普通'
		end
		
		local mainbox = mw.html.create( 'div' )
		mainbox:cssText('display: inline-block;margin-left: 5px;vertical-align: text-top;line-height: 20px;text-align:center;width:152px;margin:0px 4px 0px 5px')
		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..';height:152px;width:152px;display: block;line-height: 147px;text-align: center;')
		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
		itemtext:wikitext(p.rarity({item['rarity']})..'<br>')
		

		if args['link'] then  -- if custom argument, do this
			itemtext:wikitext('[[' .. args['link'] .. '|' .. itemname .. ']]')
		elseif item['link'] then -- if item has specific link in Module:ItemList
			itemtext:wikitext( '[[' .. 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('[[' .. item_categories[item['class']]['link'] .. '|' .. itemname .. ']]')
		else  -- otherwise don't do links
			itemtext:wikitext(itemname)
		end
		
		if not item['free'] then
			if item['DNA'] == 0 then
				itemtext:wikitext("<p style='vertical-align:text-top;color:#6bbf00'>'''免費'''</p>")
			else
				itemtext:wikitext("<br>[[File:DNA "..item['animal']..".png|37px]]'''"..item['DNA'].."''' [[File:Super serum.png|40px]]'''"..item['Serum'].."'''")
			end
		end
		if args['add_to_desc'] then 
			itemtext:wikitext(args['add_to_desc'])
		elseif item['description'] and not args['ignore_description'] then
			itemtext:wikitext(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 colors[args[1]] == nil then
		args[1] = '普通'
	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
Advertisement