Module:MoveTutorTable

From Pokemon Revolution Online Wiki
Revision as of 12:13, 31 July 2026 by Cae (talk | contribs) (created page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:MoveTutorTable/doc

local p = {}

local DATA_FORMAT = 'CSV with header'

local function nonEmpty(s)
	if s == nil then return nil end
	s = mw.text.trim(tostring(s))
	if s == '' then return nil end
	return s
end

local moveRowsCache = nil
local function moveRows()
	if moveRowsCache then return moveRowsCache end
	local csvData = mw.ext.externalData.getExternalData{
		url = 'https://wiki.pokemonrevolution.net/index.php?title=Special:GetData/RawMovesList',
		format = DATA_FORMAT,
		data = {
			name = 'Name', type = 'Type', category = 'Category', power = 'Power',
			accuracy = 'Accuracy', pp = 'PP', priority = 'Priority',
		},
	}
	moveRowsCache = {}
	if csvData then
		for _, row in ipairs(csvData) do
			if nonEmpty(row.name) then
				moveRowsCache[row.name] = row
			end
		end
	end
	return moveRowsCache
end

local typeColorCache = {}
local function typeColor(frame, pokeType)
	pokeType = nonEmpty(pokeType) or 'Normal'
	if not typeColorCache[pokeType] then
		typeColorCache[pokeType] = {
			light = frame:expandTemplate{ title = pokeType .. ' color light' },
			base = frame:expandTemplate{ title = pokeType .. ' color' },
			dark = frame:expandTemplate{ title = pokeType .. ' color dark' },
		}
	end
	return typeColorCache[pokeType]
end

local categoryColorCache = {}
local function categoryColor(frame, category)
	category = nonEmpty(category) or 'Status'
	if not categoryColorCache[category] then
		categoryColorCache[category] = {
			base = frame:expandTemplate{ title = category .. ' color' },
			dark = frame:expandTemplate{ title = category .. ' color dark' },
		}
	end
	return categoryColorCache[category]
end

function p.main(frame)
	local parent = frame:getParent()
	local args = (parent and parent.args) or frame.args

	local moveName = nonEmpty(args.Move)
	if not moveName then
		return "<span style='color:red'>Usage: {{MoveTutorTable|Move=MoveName|Contents=...}}</span>"
	end

	local row = moveRows()[moveName]
	if not row then
		return "<span style='color:red'>Data load error: No move data found for " .. moveName .. "</span>"
	end

	local moveType = nonEmpty(row.type) or 'Normal'
	local category = nonEmpty(row.category) or 'Status'
	local colors = typeColor(frame, moveType)
	local catColors = categoryColor(frame, category)

	local power = nonEmpty(row.power) or '—'
	local accuracy = nonEmpty(row.accuracy) or ''
	local pp = nonEmpty(row.pp) or ''
	local priorityNum = tonumber(row.priority) or 0
	local priorityText = (priorityNum > 0 and '+' or '') .. tostring(priorityNum)

	local contents = mw.ustring.gsub(mw.text.trim(args.Contents or ''), '\n', '')

	local function valueBox(text)
		return '<div style="background-color:#FFFFFF; border: 1px solid #' .. colors.dark ..
			'; border-radius: 25px; width: 30%; margin: auto auto; color:#000000; font-weight: normal;">' ..
			text .. '</div>'
	end

	local buf = {}
	buf[#buf + 1] = '<table style="width:73%; background-color:#' .. colors.base .. '; border: 5px solid #' ..
		colors.dark .. '; font-weight: bold; color: #' .. colors.dark .. '; border-radius: 25px; padding: 4px; ' ..
		'text-align: center;">'

	buf[#buf + 1] = '<tr style="background-color:#' .. colors.light .. ';"><td colspan="4" style="' ..
		'border-radius: 25px; border: 3px solid #' .. colors.dark .. ';">[[' .. moveName ..
		'|<span style="color:#' .. colors.dark .. ';">' .. moveName .. '</span>]]</td></tr>'

	buf[#buf + 1] = '<tr style="background-color:#' .. colors.light .. ';">' ..
		'<td style="border-radius: 25px 1px 1px 25px; border: 3px solid #' .. colors.dark ..
		'; padding: 4px;">Type</td>' ..
		'<td style="border-radius: 1px 25px 25px 1px; border: 3px solid #' .. colors.dark ..
		'; padding: 4px; background-color:#' .. colors.dark .. ';">[[' .. moveType ..
		'_(type)|<span style="color:#FFFFFF; font-weight: normal;">' .. moveType .. '</span>]]</td>' ..
		'<td style="border-radius: 25px 1px 1px 25px; border: 3px solid #' .. colors.dark ..
		'; padding: 4px;">Category</td>' ..
		'<td style="border-radius: 1px 25px 25px 1px; border: 3px solid #' .. catColors.dark ..
		'; padding: 4px; background-color:#' .. catColors.base .. ';">[[' .. category ..
		'_Moves|<span style="color:#FFFFFF; font-weight: normal;">' .. category .. '</span>]]</td></tr>'

	buf[#buf + 1] = '<tr style="background-color:#' .. colors.light .. ';">' ..
		'<td style="border-radius: 25px 1px 1px 25px; border: 3px solid #' .. colors.dark ..
		'; padding: 4px;">Base power' .. valueBox(power) .. '</td>' ..
		'<td style="border: 3px solid #' .. colors.dark .. '; padding: 4px;">Accuracy' .. valueBox(accuracy) ..
		'</td>' ..
		'<td style="border: 3px solid #' .. colors.dark .. '; padding: 4px;">[[Priority Moves|<span style="color:#' ..
		colors.dark .. ';">Priority</span>]]' .. valueBox(priorityText) .. '</td>' ..
		'<td style="border-radius: 1px 25px 25px 1px; border: 3px solid #' .. colors.dark ..
		'; padding: 4px;">PP' .. valueBox(pp) .. '</td></tr>'

	buf[#buf + 1] = '<tr><td colspan="4">' ..
		'<table style="border-radius: 25px; border: 3px solid #' .. colors.dark .. '; background-color:#' ..
		colors.light .. '; padding: 6px; width: 100%;">' ..
		'<tr><td colspan="3">Tutor locations</td></tr>' ..
		'<tr><td><table cellpadding="5" class="collapsible collapsed" style="width: 100%; padding: 3px;">' ..
		'<tr style="background-color: #' .. colors.dark .. '; color: #FFFFFF;">' ..
		'<th style="width: 28%; border: 1px solid #' .. colors.base .. '; position: relative;">' ..
		'<div style="position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-50%); ' ..
		'text-align: center; pointer-events: none;">Location</div></th>' ..
		'<th width="9%" style="border: 1px solid #' .. colors.base .. ';">Price</th>' ..
		'<th style="width: 70%; border: 1px solid #' .. colors.base .. ';">Prerequisites</th></tr>' ..
		contents ..
		'</table></td></tr></table></td></tr>'

	buf[#buf + 1] = '</table>'

	return table.concat(buf)
end

return p