Module:Script-MBTL

From Mizuumi Wiki
Revision as of 07:18, 29 July 2023 by Hexeaktivitat (talk | contribs) (continued debugging)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Script-MBTL/doc

local p = {}

--custom script for the IS wiki, feel free to make a copy of it or take any part of the code for other wikis. ohoho I DID!!!!! 
--MBTL wiki fork

--split multiple properties passed as a single param to a template call and return the converted property text


--local functions used by p.gameInput()

local function getColor(btn)

	--recognized buttons
	switch = {
		["IAD"] = "#000000",
		["MD"] = "#000000",
		["RB"] = "#000000",
		["A"] = "#E53995",
		["B"] = "#FF831E",
		["C"] = "#5BCD91",
		["D"] = "#44A9E3",
		["Shield"] = "#44A9E3",
		["X"] = "#686868",
	}
	
	btn = btn:gsub('+','')
	return switch[btn]
end

local function colorFormat(button, text)
	
	if text == '' then return '' end
	if button == nil then return text end
	local clr = getColor(button)
	if clr == nil then return text end
	local start = "<span style=\"color:"
	local mid = ";\">"
	local endstr = "</span>"
	local ctext = start .. clr .. mid .. text .. endstr
	
	return ctext
end

--actual method functions
	
function p.multiprop(frame)
	
	local str = frame.args[1]
	local sep = ","
	local t={}
	--split properties
	for str in string.gmatch(str, "([^"..sep.."]+)") do
			table.insert(t, str)
	end
	--get the string done
	for index, prop in ipairs(t) do
		if prop:sub(1,1) == "%s" then
			prop = prop:sub(2,-1)
		end
		expanded = frame:expandTemplate{ title="Property-MBTL", args={prop,"IGNOREPROPERTYERROR"}}
		t[index] = expanded
	end
	
	ret = table.concat(t,", ")
	
	return ret
end

--color code and format game inputs.
--supports entire combos as arguments.
function p.gameInput(frame)
	
	local str = frame.args[1]
	local inputs = {}
	--split the sequence into separate moves
	for link, command, separator, leftbracket, button, rightbracket, s1, suffix, s2 in string.gmatch(str, "([^%w]*)([t]?[k]?[Jj]?%.?[%d]*)([^%w%[%{ ]*)([%[%{]*)([%a%+]*)([%}%]]*)(%(?)(%w*)(%)?)") do
		-- link: Everything in between moves. All whitespace and move linking syntax ends up here.
		-- command: Directional numbers with an optional J for air moves.
		-- separator: Any symbols between the command and button.
		-- leftbracket: [ or { around the button
		-- button: The button. Can be a single letter or a full word.
		-- rightbracket: ] or } around the button
		-- suffix: Extra alphanumeric characters directly after the move. s1 and s2 match surrounding parenthesis.
		
		table.insert(inputs, link)
		
		mainb = nil
		buttons = {}
		--get elements separated by +
    	for b in string.gmatch(button, "([^+]+)") do
        	table.insert(buttons,b)
    	end
   	
   		--check for escaped buttons
   		if (buttons ~= "IAD") or (buttons ~= "MD") or (buttons ~= "RB") then
    		--for each element split the single letters (for followups)
    		for i,p in ipairs(buttons) do
				singlebuttons = {}
    			for b2 in string.gmatch(p, "([%a])") do
    				table.insert(singlebuttons, colorFormat(b2, b2))
					if mainb==nil then mainb = b2 end
    			end
				buttons[i] = table.concat(singlebuttons)
    		end
    		--combine everything into one string
    		button = table.concat(buttons, "+")
    	end
    	
		if separator ~= '' then
			-- If the command and button input are separate, do not color the command input.
			table.insert(inputs, command .. separator)
			table.insert(inputs, colorFormat(mainb, leftbracket))
			table.insert(inputs, button)
			table.insert(inputs, colorFormat(mainb, rightbracket))
		else
			table.insert(inputs, colorFormat(mainb, command .. leftbracket))
			table.insert(inputs, button)
			table.insert(inputs, colorFormat(mainb, rightbracket))
		end
		
		if suffix ~= '' then
			-- If there is a suffix, color it the same as the button.
			table.insert(inputs, colorFormat(mainb, s1 .. suffix .. s2))
		else
			table.insert(inputs, s1 .. suffix .. s2)
		end
	end
	local ret = "<span>" .. table.concat(inputs, "") .. "</span>"
	return ret
end

return p