Module:MoveDataParser ROA

From Mizuumi Wiki
Jump to navigation Jump to search

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

local p = {}
local cargo = mw.ext.cargo
local images = {
	-- buttons
	["A"]      = "[[File:roa_input_xbox_a.png]]",
	["B"]      = "[[File:roa_input_xbox_b.png]]",
	["X"]      = "[[File:roa_input_xbox_x.png]]",
	["Y"]      = "[[File:roa_input_xbox_y.png]]",
	-- control stick directions
	["6"]      = "[[File:roa_input_right_soft.png]]",
	["9"]      = "[[File:roa_input_upright_soft.png]]",
	["8"]      = "[[File:roa_input_up_soft.png]]",
	["7"]      = "[[File:roa_input_upleft_soft.png]]",
	["4"]      = "[[File:roa_input_left_soft.png]]",
	["1"]      = "[[File:roa_input_downleft_soft.png]]",
	["2"]      = "[[File:roa_input_down_soft.png]]",
	["3"]      = "[[File:roa_input_downright_soft.png]]",
	["6-"]     = "[[File:roa_input_right_hard.png]]",
	["8-"]     = "[[File:roa_input_up_hard.png]]",
	["4-"]     = "[[File:roa_input_left_hard.png]]",
	["2-"]     = "[[File:roa_input_down_hard.png]]",
	-- charge input
	["C"]      = "[[File:roa_input_charge.png]]",
	-- triggers
	-- others
	["+"]      = "[[File:roa_input_plus.png]]",
	[">"]      = "[[File:roa_input_next.png]]",
	[" "]      = "[[File:roa_input_next.png]]"
}

-- Color frame advantage text based on value (positive or negative)
function p.colorizeFrameAdvantage(frame)
	-- Set the string to the first argument given
	local str = frame.args[1]
	
	-- Surrond all plus values with a span with style color green
	str = string.gsub(str, "(%+%d+)", "<span style=\"color:green;\">%1</span>")
	-- Surrond all negative values with a span with style color green
	str = string.gsub(str, "(%-%d+)", "<span style=\"color:red;\">%1</span>")
	
	-- Return the string
	return str
end

-- Split string 's' at every 'delimiter' into an array
function splitKeepDelimiter(s, delimiter)
	-- Array to return
	local result = {}
	
	-- Boolean to check if this is the firstSplit (only applicable when keeping delimiter)
	local firstSplit = true
	
	-- Iterate through the string finding every section split with delimiter
	for match in (s..delimiter):gmatch("(.-)"..delimiter) do
		-- Don't add the delimiter until after the first split
		if firstSplit then
			firstSplit = false
		else
			table.insert(result, delimiter)
		end
		
		-- Remove a '%' character if it's leftover from a delimiter
		local matchWithEscapedRemoved = match
		if string.find(string.sub(match,-1, string.len(match)),"%%") then
			mw.log(match)
			matchWithEscapedRemoved = string.sub(match, 1, -2)
		end
		
		-- Add the match to the result array
		table.insert(result, matchWithEscapedRemoved)
	end
	
	-- Return the result
	return result
end

-- Create a wikitext hyperlink that links to the frame data page section of a move
-- Assumes the section header is the same as moveInput
function p.createFrameDataLink(frame)
	-- Target cargo table to query
	local cargoTable = frame.args['cargoTable']
	-- MoveId to query
	local move_id = frame.args['move_id']
	-- Field in to return in query
	local fields = "input"
	-- Args for query
	local args = { where = "move_id = '" .. move_id .. "'"}
	
	-- Beginning of wikitext to return
	local wikitext = "[[/Data#"
	
	-- Cargo query for moveInput
	local results = cargo.query( cargoTable, fields, args )
	local moveInput = results[1]['input']
	
	-- Replaces any left and right square brackets with their ascii code to make it wikitext compatible
	moveInput = string.gsub(moveInput, '%[', "&#91;")
	moveInput = string.gsub(moveInput, "%]", "&#93;")
	
	-- TODO: Maybe add alternate text instead of it just saying '/Data#moveInput'?
	-- Concatenates 'wikitext' with 'moveInput' and ending square brackets
	wikitext = wikitext .. moveInput .. "]]"
	
	-- Returns the wikitext
	return wikitext 
end

-- Change a string of characters to a list of images in wikitext format (reference above table)
function p.inputsToIcons(frame)
	-- Get string
	local inputText = frame.args[1]
	local ret = ""
	
	-- Check if current inputText is matching with any images
	local checkInputText = type(images[inputText]) == "function" and images[inputText]() or images[inputText] or nil
	
	-- If it doesn't match, then keep checking down
	if checkInputText==nil then
		-- If text contains spaces, split it down
		if string.find(inputText, " ") then
			local inputTextAsList = splitKeepDelimiter(inputText, " ")
			for i,v in ipairs(inputTextAsList) do
				ret = ret..p.inputsToIcons({args={v}})
			end
		-- If text contains '~', split it down
		elseif string.find(inputText,"~") then
			local inputTextAsList = splitKeepDelimiter(inputText, "~")
			for i,v in ipairs(inputTextAsList) do
				ret = ret..p.inputsToIcons({args={v}})
			end
		-- If text contains '/', split it down
		elseif string.find(inputText,"+") then
			local inputTextAsList = splitKeepDelimiter(inputText, "+")
			for i,v in ipairs(inputTextAsList) do
				ret = ret..p.inputsToIcons({args={v}})
			end
		-- If text contain a digit then a letter, then split at that point
		elseif string.find(inputText,"%d%a") then
			local frontEnd,backEnd = string.find(inputText,"%d%a")
			ret = ret..p.inputsToIcons({args={string.sub(inputText,1,frontEnd)}})..p.inputsToIcons({args={string.sub(inputText,backEnd)}})
		-- If text contain a digit then a left square bracket, then split at that point
		elseif string.find(inputText,"%d%[") then
			local frontEnd,backEnd = string.find(inputText,"%d%[")
			ret = ret..p.inputsToIcons({args={string.sub(inputText,1,frontEnd)}})..p.inputsToIcons({args={string.sub(inputText,backEnd)}})
		else
			ret = inputText
		end
	else
		ret = checkInputText
	end
	
	-- Return the result
	return ret
end

-- Cargo queries a single image by 3 unnamed arguments
--- 1 - name of target cargo table
--- 2 - moveId from that cargo table
--- 3 - image index of the move
function p.getMoveImage(frame)
	-- Target cargo table to query
	local cargoTable = frame.args[1]
	-- Target move to query
	local move_id = frame.args[2]
	-- Target image index (is default 1 from template MoveDataCargoImage)
	local imageIndex = frame.args[3]
	-- Boolean to use image or hitbox image
	local useHitbox = mw.text.trim(frame.args[4])
	-- Maximum height of the image or hitbox image, used to limit edge cases
	local imageHeight = mw.text.trim(frame.args[5])
	-- Wikitext to return after function is completed
	local wikitext = ""
	
	-- 'fields' part of the cargo query
	-- We need to grab only the first instance of each image "sorted" by moveId
	local fields = ""
	if useHitbox == "yes" then
		fields = "img_hitbox"
	else
		fields = "img"
	end
	
	-- 'args' part of the Lua cargo query
	local args = {
		-- 'whereQuery' from above which goes by moveIds
		where = "move_id = '" .. mw.text.trim(move_id) .. "'"
	}
	
	local result
	
	-- Cargo query
	if useHitbox == "yes" then
		result = cargo.query( cargoTable, fields, args )[1].img_hitbox
	else
		result = cargo.query( cargoTable, fields, args )[1].img
	end
	
	-- return nothing if there is no image for the move
	if result == nil then
		return
	end
	
	-- Turn query into list split by ','
	local listOfImages = mw.text.split(result,',')
	
	-- Get target image based on imageIndex
	local targetImage = listOfImages[tonumber(imageIndex)]
	
	-- Return the image as wikitext
	if imageHeight == "no" then
		wikitext = "[[File:" .. targetImage .. "|175px]]"
	else
		wikitext = "[[File:" .. targetImage .. "|x" .. imageHeight .. "]]"
	end
	
	return wikitext
end

return p