Jump to content

Module:Event History/Draft

From SPS Wiki

This is the draft page for Module:Event History.

It implements the template {{Event History/Draft }}.


local p = {}

local util = require('Module:Module Util')
local wrapperTemplate = "Template:Event History/Draft"

--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 gallAttributes = {
	["hideaddbutton"] = "true",
	["widths"] = "75px",
	["bordersize"] = "none",
	["spacing"] = "small",
	["captionalign"] = "center",
	["captionsize"] = "medium",
	["position"] = "center",
	["navigation"] = "true",
	["mode"] = "packed"
}

local function constructTableWithHeader()
	local output = mw.html.create() --create html frame
	output = output:tag('table')
		:attr( 'class', 'article-table' )
		:css( 'width', '100%' )
		:css( 'align', 'center' )
	output:tag('tr')
		:tag('th')
			:attr( 'width', '35%' )
			:wikitext("Image")
		:tag('th')
			:attr( 'width', '35%' )
			:wikitext("Event Name")
		:tag('th')
			:attr( 'width', '30%' )
			:wikitext("Run Duration")
    return output
end

local function constructOutputTable(eventDetailsArray)
	local outputTable = constructTableWithHeader()
	--construct table content
	for _,event in ipairs(eventDetailsArray) do
		--if the event is ongoing we want to make the row green
		if util._isPast({event.endDate}) == false and util._isUpcoming({event.startDate}) == false then 
			outputTable
				:tag('tr')--create row
					:css( 'background', '#009150' )
					:css( 'color', 'white' )
					:tag('td')
						:wikitext("[[File:"..event.image.."|left|thumb|link="..event.link.."|220px]]")
					:tag('td')
						:wikitext("'''[["..event.link.."|"..event.name.."]]'''")
					:tag('td')
						:wikitext(util._presentWrittenDate(event.startDate, "no hours", true).." - "..util._presentWrittenDate(event.endDate, "no hours", true))
		else
			outputTable
				:tag('tr')--create row
					:tag('td')
						:wikitext("[[File:"..event.image.."|left|thumb|link="..event.link.."|220px]]")
					:tag('td')
						:wikitext("'''[["..event.link.."|"..event.name.."]]'''")
					:tag('td')
						:wikitext(util._presentWrittenDate(event.startDate, "no hours", true).." - "..util._presentWrittenDate(event.endDate, "no hours", true))
		end
	end
	return outputTable
end

local function getEventListStringForCategory(categoryString, frame)
	return frame:callParserFunction( "#dpl:category = "..categoryString, 
    	{
    		uses = "Template:Event",
    		include = "{Event}:name,{Event}:image,{Event}:time_start,{Event}:time_end",
    		ordermethod="titlewithoutnamespace",
    		allowcachedresults=true,
    		cacheperiod=604800, --cache period set to 604,800 seconds/7 days - don't need a high refresh on this
	    	listseparators = ",,,",
	    	secseparators = "%TITLE%||,||,"..
	    	",||,"..--image [3]
	    	",||,"..--time_start [4]
	    	",>,"--time_end [5]
    	} )
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()
	local pagename = args["name"] or frame.args["name"]
	local platform = args["platform"] or "pc"
	local category = (platform == "mobile") and "*Mobile LTE Events" or "*Event"
	
	--find name, image and dates of all events, and split into array
    local dpl = frame:callParserFunction( "#dpl:category = "..category, 
    	{
    		uses = "Template:Event",
    		titlematch = pagename.."%",
    		include = "{Event}:name,{Event}:image,{Event}:time_start,{Event}:time_end",
    		ordermethod="titlewithoutnamespace",
    		allowcachedresults=true,
    		cacheperiod=604800, --cache period set to 604,800 seconds/7 days - don't need a high refresh on this
	    	listseparators = ",,,",
	    	secseparators = "%TITLE%||,||,"..
	    	",||,"..--image [3]
	    	",||,"..--time_start [4]
	    	",>,"--time_end [5]
    	} )
    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>''We couldn't find any occurences of this event!''</center>[[Category:Events with no History]]")
		return emptyTable
	end
    local killLastCharacter = string.sub(dpl, 1, -2)
	local eventSplit = mw.text.split(killLastCharacter, '>', true)
	local detailsSplit = {}
	for k,v in ipairs(eventSplit) do --split event details
		local vArray = mw.text.split(v, '||', true) --split string on the bars
		local arrayV = {
			["link"] = vArray[1],
			["name"] = vArray[2],
			["image"] = vArray[3],
			["startDate"] = vArray[4],
			["endDate"] = vArray[5],
		}
		table.insert(detailsSplit, arrayV)
	end

	--sort the list
	table.sort(detailsSplit, function(a,b) return a.startDate < b.startDate end)
	
	return constructOutputTable(detailsSplit)
end

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

function p._fullWeeklyHistory(args, frame)
	frame = frame or mw.getCurrentFrame()
	local eventSet = args["platform"] or "pc"
	local searchYear = args["year"] or "0000"
	local eventCategory = (eventSet == "mobile") and "*Mobile LTE Events" or "Weekly Girl Events¦Weekly Pinup Events¦Weekly Outfit Events"

	--find name, image and dates of all events, and split into array
    local dpl = getEventListStringForCategory(eventCategory, frame)
    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>''We couldn't find any LTE events!!''</center>")
		return emptyTable
	end
    local killLastCharacter = string.sub(dpl, 1, -2)
	local eventSplit = mw.text.split(killLastCharacter, '>', true)
	local detailsSplit = {}
	for k,v in ipairs(eventSplit) do --split event details
		local vArray = mw.text.split(v, '||', true) --split string on the bars
		local arrayV = {
			["link"] = vArray[1],
			["name"] = vArray[2],
			["image"] = vArray[3],
			["startDate"] = vArray[4],
			["endDate"] = vArray[5],
		}
		
		if (starts_with(arrayV["startDate"], searchYear) == true or searchYear=="0000")  then
			table.insert(detailsSplit, arrayV)
		end
	end

	--sort the list
	table.sort(detailsSplit, function(a,b) return a.startDate < b.startDate end)

    return constructOutputTable(detailsSplit)--util.dumpTable(detailsSplit)
end

return p