Professor Layton Wiki
Advertisement
Professor Layton Wiki

This module handles the start of puzzle pages that are making use of {{Puzzle}}.

It will insert, in order:

  • {{PuzzleNav|<Name of previous puzzle>|<Name of next puzzle>|<Name of current puzzle>}}
  • {{PuzzleInfobox}} with all data that is available in the respective PuzzleData module (based on the game parameter).
  • Page introduction, including links to UK version if it is on a separate page.
  • Relevant categories for the puzzle, based on the PuzzleData.
  • {{DISPLAYTITLE:[<GAME><NUMBER>] <PUZZLE NAME>}}
  • If puzzle name starts with a number, then {{DEFAULTSORT:#<PUZZLE NAME>}} is added to sort these puzzles under "#" in categories.

This module depends on Module:Gamecodes


local p = {}
 
local function getCleanName(pageName)
	return pageName:match("[^%(]+")
end
 
local function parseSingleReward(reward)
	local m,r = reward:match("([%a ]+):(.+)")
		
	if m and r then
		return require('Module:Rewards/' .. mw.text.trim(m)).parse(mw.text.trim(r))
	end

	return '[[' .. reward:match("[^%(]+") .. '|' .. reward .. ']]'
end

local function getReward(puzzle)
	local r = puzzle.reward
	if not r then return 'None' end
	
	local s = mw.text.split(r,",")
	local ret = {}
	
	for i=1,#s do
		ret[#ret+1] = parseSingleReward(s[i])
	end
	
	if #ret < 2 then
		return ret[1] or "None"
	end
	
	return '* ' .. table.concat(ret,"\n* ")
end

local function getChapter(game, puzzle)
	if not puzzle.chapter then return "" end
	local chapters = mw.loadData('Module:Chapters/' .. game)
	
	local cName = chapters[puzzle.chapter]
	if not cName then return puzzle.chapter end
	
	local cShort = cName:match("[^:]+")
	
	return "[[" .. cName .. "|" .. cShort .. "]]"
end
 
function p.main(frame)
	local a = frame:getParent().args
	local game, num = a.game:upper(),tonumber(a.number)
	if not game then return '<b style="color:red;">Missing gamecode</b>' end
	if game == 'MJ' then game = 'MC' end
	
	if not num then
		if a.number == '???' then
			local hdnums = { DB = 156, UF = 170 }
			num = hdnums[game]
			if not num then return '<b style="color:red;">No Hidden Door puzzle in this game</b>' end
		else
			return '<b style="color:red;">Missing/invalid puzzle number "' .. (num or '') .. '"</b>'
		end
	end
 
	pData = mw.loadData('Module:PuzzleData/' .. game)
 
	local ret = {}
	local puzzle = pData[num]
	local aNum = num
 
	if a.uk == 'yes' then
		aNum = puzzle.uk
		puzzle = pData[aNum]
	elseif a.switch == 'yes' then
		aNum = puzzle.switch
		puzzle = pData[aNum]
	end
 
	if not puzzle or puzzle.name ~= mw.title.getCurrentTitle().text then
		ret[#ret+1] = '<b style="color:red;">Puzzle Name/Number Mismatch!' .. (puzzle and ' ('.. puzzle.number ..' should be "' .. puzzle.name .. '")' or '') .. '\n*Are you on the correct page?\n*Did you enter the correct number?\n*Note that names are capital sensitive!</b>[[Category:Puzzle Name Mismatch]]<br class="clear" />'	
	end
 
	------------------------------------------------------------
	-- Process PuzzleNav
	------------------------------------------------------------
	local pName = getCleanName(puzzle.name)
	if a.number ~= '???' then
		local pPrev = pData[aNum-1]
		local pNext = pData[aNum+1]
	 
		if (a.uk == 'yes' or a.switch == 'yes') then
			if tonumber(pPrev.number) ~= num-1 then
				pPrev = pData[num-1]
			end
			if not pNext or tonumber(pNext.number) ~= num+1 then
				pNext = pData[num+1]
			end
		else
			-- Prevent Next from being the first UK/Switch puzzle
			if pNext and (type(pNext.uk) == 'boolean' or type(pNext.switch) == 'boolean') then pNext = nil end
		end
	 
		local puzzleNav = mw.html.create('table'):addClass('puzzleNav')
		local navRow = puzzleNav:tag('tr')
	 
		if pPrev then
			local name = (a.uk == 'yes' and type(pPrev.uk) == 'string') and pPrev.uk or pPrev.name
			navRow
				:tag('td')
					:addClass('pn-outer')
					:wikitext('[[Puzzle:' .. pPrev.name .. '|&larr;]]')
					:done()
				:tag('td')
					:addClass('pn-inner')
					:css('width','32%')
					:wikitext('[[Puzzle:' .. pPrev.name .. '|' .. pPrev.number .. ' - ' .. getCleanName(name) .. ']]')
		else
			navRow
				:tag('td')
					:addClass('pn-inner')
					:css('width','33%')
					:wikitext('N/A')
		end
	
		navRow
			:tag('td')
				:addClass('pn-center')
				:wikitext(puzzle.number .. ' - ' .. pName)
	 
		if pNext then
			local name = (a.uk == 'yes' and type(pNext.uk) == 'string') and pNext.uk or pNext.name
			navRow
				:tag('td')
					:addClass('pn-inner')
					:css('width','32%')
					:wikitext('[[Puzzle:' .. pNext.name .. '|' .. pNext.number .. ' - ' .. getCleanName(name) .. ']]')
					:done()
				:tag('td')
					:addClass('pn-outer')
					:wikitext('[[Puzzle:' .. pNext.name .. '|&rarr;]]')
		else
			navRow
				:tag('td')
					:addClass('pn-inner')
					:css('width','33%')
					:wikitext('N/A')
		end
	 
		ret[#ret+1] = tostring(puzzleNav)
	end
 
	------------------------------------------------------------
	-- Process PuzzleInfobox
	------------------------------------------------------------
	ret[#ret+1] = mw.getCurrentFrame():expandTemplate({
		title = 'PuzzleInfobox',
		args = {
			name = pName,
			image = (not a.image or a.image == '') and (game .. puzzle.number .. (a.uk and 'UK' or '') .. (game == 'CV' and '.gif' or '.png')) or a.image,
			['game'] = game,
			['number'] = puzzle.number,
			['type'] = puzzle['type'],
			location = puzzle.location,
			givenby = puzzle.character,
			solvedby = puzzle.solver,
			picarats = puzzle.picarats,
			obligatory = puzzle.mandatory and 'Yes' or 'No',
			reward = getReward(puzzle),
			
			jpname = a.jpname,
			dename = a.dename,
			esname = a.esname,
			frname = a.frname,
			itname = a.itname,
			nlname = a.nlname,
			korname = a.korname,
		}
	})
 
	------------------------------------------------------------
	-- Intro
	------------------------------------------------------------ 
	local fullGame = require('Module:Gamecodes').expand(game)
	
	if type(puzzle.uk) == 'string' then
		-- Alt name
		ret[#ret+1] = "'''" .. pName .. "''' ('''"..puzzle.uk.."''' in the UK version) is a puzzle in ''[[" .. fullGame .. "]]''."
	elseif type(puzzle.uk) == 'boolean' then
		-- This is a UK puzzle, US has another.
		local ukGame = {
			DB = "''[[Professor Layton and Pandora's Box]]''",
			UF = "''[[Professor Layton and the Lost Future]]''",
			LS = "''[[Professor Layton and the Spectre's Call]]''",
		}
		local gName = ukGame[game] or "the European version of ''[[" .. fullGame .. "]]''" 
		ret[#ret+1] = "'''" .. pName .. "''' is a puzzle in " .. gName
		local usName = pData[num].name
		ret[#ret+1] = ". For the puzzle in the American version of the game, go [[Puzzle:" .. usName .. '|here]].'
	elseif type(puzzle.uk) == 'number' then
		-- This is a US puzzle, but UK has another.
		ret[#ret+1] = "'''" .. pName .. "''' is a puzzle in ''[[" .. fullGame .. "]]''"
		local ukName = pData[puzzle.uk].name
		ret[#ret+1] = ". For the puzzle in the European version of the game, go [[Puzzle:" .. ukName .. '|here]].'
	elseif type(puzzle.switch) == 'string' then
		-- The Switch version has another name
		ret[#ret+1] = "'''" .. pName .. "''' ('''"..puzzle.switch.."''' in the Switch version) is a puzzle in ''[[" .. fullGame .. "]]''."
	elseif type(puzzle.switch) == 'boolean' then
		-- This is a Switch puzzle, Mobile has another
		ret[#ret+1] = "'''" .. pName .. "''' is a puzzle in the Switch version of ''[[" .. fullGame .. "]]''" 
		local mobName = pData[num].name
		ret[#ret+1] = ". For the puzzle in the Mobile version of the game, go [[Puzzle:" .. mobName .. '|here]].'
	elseif type(puzzle.switch) == 'number' then
		-- This is a Mobile puzzle, Switch has another
		ret[#ret+1] = "'''" .. pName .. "''' is a puzzle in the Mobile version of ''[[" .. fullGame .. "]]''"
		local swName = pData[puzzle.switch].name
		ret[#ret+1] = ". For the puzzle in the Switch version of the game, go [[Puzzle:" .. swName .. '|here]].'
	else
		-- Just the same
		ret[#ret+1] = "'''" .. pName .. "''' is a puzzle in ''[[" .. fullGame .. "]]''."
	end
 
	if puzzle.mandatory then
		ret[#ret+1] = " The puzzle must be solved in order to progress the story."	
	end
 
	if pName:sub(1,1):match('%d') then
		ret[#ret+1] = mw.getCurrentFrame():preprocess("{{DEFAULTSORT:#" .. pName.."}}")	
	end
  
	return table.concat(ret)
end

return p
Advertisement