Jump to content

Module:Memory Album

From SPS Wiki

This module creates the Memory Album gallery used on the girl pages. Uses DPL3 to check for the existence of optional encounter images. Used on {{Memory Album }}.


local p = {}

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

local function urlEncode(str)
  str = string.gsub(str,
  	"%s",
  	"_")
  return str
end

local function errorImage(message, name, fileName)
	local caption = message or "No image found!"
	return "Crushed Error.png|"..caption.." [https://wiki.sadpandastudios.com/w/index.php?title=Special:Upload&wpDestFile="..urlEncode(name).."_"..urlEncode(fileName)..".png Upload?]\n"
end

local function contains(searchString, pattern)
	return string.match( searchString, pattern ) ~= nil
end

local gallAttributes = {
	["hideaddbutton"] = "true",
	["mode"] = "packed"
}

local function createGalleryString(listOfImages, name)
  local str = ''
  if contains( listOfImages, name.." Encounter 0.png" ) then str = str..name.." Encounter 0.png|Encounter\n" end
  if contains( listOfImages, name.." Encounter 1.png" ) then str = str..name.." Encounter 1.png|CRUSHED!\n" end
  if contains( listOfImages, "Encounter" )
  then
  	str = str..name.." Encounter.png|Encounter\n"
  else
  	str = str..errorImage("No encounter image found!", name, "Encounter")
  end
  if contains( listOfImages, name.." Encounter 2.png" ) then str = str..name.." Encounter 2.png|Encounter\n" end
  if contains( listOfImages, "Friendzoned" )
  then
  	str = str..name.." Friendzoned.png|Friendzoned\n"
  else
  	str = str..errorImage("No friendzoned image found!", name, "Friendzoned")
  end
  if contains( listOfImages, "Sweetheart" )
  then
  	str = str..name.." Sweetheart.png|Sweetheart\n"
  else
  	str = str..errorImage("No sweetheart image found!", name, "Sweetheart")
  end
  if contains( listOfImages, "Lover" )
  then
  	str = str..name.." Lover.png|Lover\n"
  else
  	str = str..errorImage("No lover image found!", name, "Lover")
  end
  str = str..name.." Moonlight Stroll.png|Moonlight Stroll Date\n"
  str = str..name.." Beach.png|Beach Date\n"
  str = str..name.." Sightseeing.png|Sightseeing Date\n"
  str = str..name.." Movie Theater.png|Movie Theater Date\n"
  if contains( listOfImages, name.." Human Moonlight Stroll.png" ) then str = str..name.." Human Moonlight Stroll.png|Moonlight Stroll Date (Human)\n" end
  if contains( listOfImages, name.." Human Beach.png" ) then str = str..name.." Human Beach.png|Beach Date (Human)\n" end
  if contains( listOfImages, name.." Human Sightseeing.png" ) then str = str..name.." Human Sightseeing.png|Sightseeing Date (Human)\n" end
  if contains( listOfImages, name.." Human Movie Theater.png" ) then str = str..name.." Human Movie Theater.png|Movie Theater Date (Human)\n" end
  return str
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="Template:Memory Album"
	})
	return p._main(args, frame)
end

function p._main(args, frame)
	frame = frame or mw.getCurrentFrame()
	local name = args["name"] or frame.args["name"]
	
	if args['spacing'] ~= '0' then gallAttributes["spacing"] = args['spacing'] end
	--Do a DPL call so we don't have to check for existence of each file individually
    local galleryFiles = frame:callParserFunction( "#dpl:category = Date Images|Relationship Images", 
    	{
    		titlematch = "%"..name.."%",
    		format = ",%TITLE%,||,>",
    		redirects = "include", --need to include redirects for chars sharing the same image (looking at you Sutra)
    		cacheperiod=604800 --cache period set to 604,800 seconds/7 days - don't need a high refresh on this
    	} )
    local killLastCharacters = string.sub(galleryFiles, 1, -4)
    local gallery = createGalleryString(killLastCharacters, name)
	return frame:extensionTag( 'gallery', gallery, gallAttributes )
end

return p