Module:Sandbox/User:Californ1a/T/Quests

From Brighter Shores Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/User:Californ1a/T/Quests/doc

require('strict')
require('Module:Mw.html extension')
local pt = require('Module:Paramtest')
local yn = require('Module:Yesno')
local p = {}

local yes = '✔'
local no = '✘'
local icon_size = 20

-- Template entrypoint
function p.main(frame)
	return p._main(frame:getParent().args)
end

function p._main(args)
	local other = {
		free = yn(pt.default_to(args.free, false), false),
		collapsed = yn(pt.default_to(args.collapsed, false), false),
		right = yn(pt.default_to(args.right, false), false)
	}
	
	local prem = '+'
	if other.free then
		prem = 'false'
	end
	
	local episodes = mw.smw.ask{ 
		'[[Infobox::Episode]]',
		'[[Release Date::+]]',
		'[[Premium::'..prem..']]',
		'?=name',
		'?Name#-=plain_name',
		'?Premium=prem',
		'?Episode sequence number=ep_order',
		'order=asc',
		'sort=Episode sequence number'
	}
    
    if episodes == nil or episodes[1] == nil then
		return 'No data found for table'
    end

	for i,v in ipairs(episodes) do
		local name = episodes[i].plain_name
		episodes[i].icon = mw.ustring.format('[[File:%s episode icon.png|link=%s|%spx]]', name, name, icon_size)
	end
	
	--local debug_str = '<pre>'..mw.text.jsonEncode(episodes, mw.text.JSON_PRETTY)..'</pre>'

	return tostring(p.create_table(episodes, other, args))--..debug_str
end

function p.add_episode_quests(tbl, episode, quests, args)
	-- Main story row
	local main_story_ep_param = 'main story '..mw.ustring.lower(episode.plain_name)
	local main_story_done = yn(args[main_story_ep_param], false)
	tbl:tag('tr')
		:tag('td')
			:attr({colspan=2})
			:addClass('table-bg-grey')
			:wikitext(mw.ustring.format('%s [[Quests|Main Story]]', episode.icon))
		:done()
		:tag('td')
			:IF(main_story_done)
				:addClass('table-bg-green')
				:css({['text-align']='center'})
				:wikitext(yes)
			:ELSE()
				:addClass('table-bg-red')
				:css({['text-align']='center'})
				:wikitext(no)
			:END()
		:done()
	:done()
	
	-- Individual quest rows for episode
	for i,v in ipairs(quests) do
		local quest = quests[i]
		if ((not (main_story_done and quest.type == 'Main')) or quest.type == 'Side') then
			local quest_param = mw.ustring.lower(quest.plain_name)
			local quest_done = yn(args[quest_param], false)
			tbl:tag('tr')
				:IF(quest.type == 'Main')
					:tag('td')
						:wikitext('-')
					:done()
				:END()
				:tag('td')
					:IF(quest.type == 'Side')
						:attr({colspan=2})
					:END()
					:wikitext(mw.ustring.format('%s %s', episode.icon, quest.name))
				:done()
				:tag('td')
					:IF(quest_done)
						:addClass('table-bg-green')
						:css({['text-align']='center'})
						:wikitext(yes)
					:ELSE()
						:addClass('table-bg-red')
						:css({['text-align']='center'})
						:wikitext(no)
					:END()
				:done()
			:done()
		end
	end

	return tbl:done()
end

function p.create_table(episodes, other, args)
	-- Table header
	local out = mw.html.create('table')
		:addClass('wikitable')
		:addClass('mw-collapsible')
		:IF(other.collapsed)
			:addClass('mw-collapsed')
		:END()
		:tag('caption')
			:wikitext('Quests')
		:done()
		:tag('tr')
			:tag('th')
				:attr({colspan=2})
				:wikitext('Quest')
			:done()
			:tag('th')
				:addClass('align-center')
				:wikitext('Done')
			:done()
	
	-- Get quests per-episode
	for i,v in ipairs(episodes) do
		local ep_name = episodes[i].plain_name
		local quests = mw.smw.ask({
			'[[Infobox::Quest]]',
			mw.ustring.format('[[Episode::%s]]', ep_name),
			'?=name',
			'?Name#-=plain_name',
			'?Episode=episode',
			'?Episode nth=order',
			'?Quest type=type',
			'?Difficulty=difficulty',
			'sort=Quest type,Episode nth,Difficulty'
		})
		
		out = p.add_episode_quests(out, episodes[i], quests, args)
	end
	
	out:allDone()
	
	local div = out
	if other.right then
		div = mw.html.create('div')
			:css({float='right'})
			:node(out)
		:done()
	end
	
	return div
end

return p