Jump to content

Module:Outfit Table

From SPS Wiki

This is a basic module implementing a list of outfits for Crush Crush.


local p = {}

local util = require('Module:Module Util')
local wrapperTemplate = "Template:Outfit Table"

local outfitTypes = {
	"Deluxe", "Swimsuit", "School", "Holiday", "Bunny", "Fantasy", "Valentine", "Wedding", "Unique"
}

--Simple dirty function to print out tables/arrays
function p.dumpTable(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. p.dumpTable(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

--Function to determine if string is empty value 
local function isEmpty(s)
  return s == nil or s == ''
end

local function starts_with(str, start)
   return str:sub(1, #start) == start
end

local function get_index (val)
	for _, types in ipairs(outfitTypes) do
		tap = string.find(val, types)
		if tap ~= nil then
            return tap
        end
	end
    
    return nil
end

local function constructTableWithHeader()
	local output = mw.html.create() --create html frame
	output = output:tag('table')
		:attr( 'id', 'outfit-table' )
		:attr( 'class', 'wikitable sortable' )
		:css( 'width', '100%' )
		:css( 'align', 'center' )
	output:tag('tr')
		:tag('th')
			:attr( 'class', 'unsortable' )
			:attr( 'width', '25%' )
			:wikitext("Image")
		:tag('th')
			:attr( 'width', '35%' )
			:wikitext("Character")
		:tag('th')
			:attr( 'width', '40%' )
			:wikitext("Outfit Type")
    return output
end

local function constructOutputTable(outfitNameArray)
	local outputTable = constructTableWithHeader()
	--construct table content
	for _,outfit in ipairs(outfitNameArray) do
		local prefix = string.sub(outfit, 1, -22)
		local tap = get_index(prefix)
		local fitType = "Default"
		local chara = prefix
		if tap ~= nil then
			fitType = string.sub(prefix, tap)
			chara = string.sub(prefix, 1, tap-2)
		end
		outputTable
			:tag('tr')--create row
				:tag('td')
					:wikitext("[[File:"..outfit.."|left|link="..prefix.."|150px]]")
				:tag('td')
					:wikitext("'''"..chara.."'''")
				:tag('td')
					:wikitext(fitType)
	end
	return outputTable
end

--uses the template as a wrapper, grabbing any arguments passed to the template
function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrapper=wrapperTemplate
	})
	return p._main(args, frame)
end

function p._main(args, frame)
	frame = frame or mw.getCurrentFrame()
	
	--find name, image and dates of all events, and split into array
    local dpl = frame:callParserFunction( "#dpl:category = Sprite Images", 
    	{
    		namespace = "File",
    		titlematch = "%Sprite Likes You.png",
    		nottitlematch = "%Mobile%",
    		ordermethod="titlewithoutnamespace",
    		allowcachedresults=true,
    		cacheperiod=604800, --cache period set to 604,800 seconds/7 days - don't need a high refresh on this
	    	format = ",%TITLE%,>,",
    	} )
    if isEmpty(dpl) then
    	local emptyTable = constructTableWithHeader()
    	emptyTable
    		:tag('tr')--create row
				:tag('td')
				:attr( 'colspan', '3' )
					:wikitext("<center>'''CRUSHED!'''<br>[[File:Crushed Error.png|center|link=|220px]]<br>'''SOMETHING GOT CRUSHED!'''<br>''We couldn't find any outfits!''</center>[[Category:Pages requiring action]]")
		return emptyTable
	end
    local killLastCharacter = string.sub(dpl, 1, -2)
	local outfitSplit = mw.text.split(killLastCharacter, '>', true)
	return constructOutputTable(outfitSplit)
end

return p