On Friday, April 19, 2024 at 10:00 PM New York time, all OpenWiki Project sites will be undergoing scheduled maintenance for about 2 hours. Expect read-only access and brief periods of downtime.

Module:ChapChars: Difference between revisions

From Fire Emblem Wiki, your source on Fire Emblem information. By fans, for fans.
m (Removed some redundancies; added some explanatory comments)
mNo edit summary
Line 11: Line 11:
game = game:sub(1, -2)
game = game:sub(1, -2)
end
end
if tonumber(game) ~= nil or game == "wa" then -- if the game is set as a number or "wa"
if tonumber(game) ~= nil then -- if the game is set as a number
num = tonumber(game)
num = tonumber(game)
end
if num > 0 or game == "wa" then -- if num was set, or game is set to "wa"
game = "fe" .. game
game = "fe" .. game
end
end
local trs = game:sub(1, 3) == "trs" -- if the set game starts with "trs"
local trs = game:sub(1, 3) == "trs" -- if the set game starts with "trs"

Revision as of 14:52, 25 September 2021

Contains three functions for use with {{ChapChars}}: newunits manages the new unit tables, and oldunits manages the required, available, and undeployable character sections; additionally, returning is a deprecated function that is no longer used.


local p = {}

function p.returning(frame)
	-- Naming the input
	local in_args = frame:getParent().args
	
	-- Determines whether or not to use "small"
	local size = ""; num = 0
	local game = tostring(in_args["game#"])
	if game:find("%c", -1) ~= nil then -- if game ends with a control character
		game = game:sub(1, -2)
	end
	
	if tonumber(game) ~= nil then -- if the game is set as a number
		num = tonumber(game)
	end
	if num > 0 or game == "wa" then -- if num was set, or game is set to "wa"
		game = "fe" .. game
	end
	
	local trs = game:sub(1, 3) == "trs" -- if the set game starts with "trs"
	
	if num > 5 or trs then -- games 1–5 and Warriors are false; games 6–16 and TRS are true
		size = "Small "
	end
	
	-- Begins construction of output
	local args = {}; images = {}; links = {}
	local hdr = "[[File:" .. size .. "portrait " -- Start of an image link
	local link = " " .. game .. ".png|link=" -- Middle portion of an image link
	if trs then
		link = link .. "Saga:"
	end
	local ftr = "|x80px]]" -- End of link
	local output = hdr -- starts output with header value
	
	-- loops through the input and turns it into a list of images and one of links
	for k, v in pairs(in_args) do
		if v:find("%c", -1) ~= nil then -- if an input ends with a control character
			v = v:sub(1, -2) -- removes last character
		end
		if tostring(k):sub(-7) == "article" then -- checks if last seven characters are "article"
			links[tonumber(k:sub(7,-8))] = v
		elseif tostring(k):sub(1,6) == "return" then -- checks if first six characters are "return"
			images[tonumber(k:sub(7))] = mw.ustring.lower(v)
			if links[tonumber(k:sub(7))] == nil then
				links[tonumber(k:sub(7))] = v
			end
		end
	end
	
	if images[1] == nil then -- true if images[1] is empty, which should only happen if return1 is not set
		return "None" -- ends the program
	end
	
	-- Turns the previous two lists into one list of partial links
	for k in pairs(images) do
		args[k] = images[k] .. link .. links[k] -- creates the middle portion of the link
	end
	
	-- takes list created above and turns it into useable wikitext
	output = output .. table.concat(args, ftr .. hdr)
	
	-- returns completed image set
	return output .. ftr
end

function p.newunits(frame)
	-- Naming input to be more convinient
	local in_args = frame:getParent().args
	
	local units = {} -- variabe that will contain every "newunit#" field
	
	-- Checks input for newunit fields
	for k, v in pairs(in_args) do
		if tostring(k):find("newunit%d+") ~= nil then
			units[tonumber(k:sub(8))] = v
		end
	end
	
	local rowbreak = 0
	
	if #units == 0 then -- if there are no new units
		return "|\nNone"
	elseif #units > 6 then -- if there are more than six new units
		rowbreak = math.min(math.ceil(#units / 2), 6)
	end
	--[[rowbreak determines were new rows are made: breaks after the 4th unit
		for seven or eight units, after the 5th for nine or ten, and after every
		6th for anything above ten; no breaks for fewer than six new units.]]
	
	local output = ""
	
	for k, v in pairs(units) do -- assembles output
		output = output .. "|\n" .. v .. "\n"
		if k % rowbreak == 0 then -- adds new rows
			output = output .. "|-\n"
		end
	end
	
	return output
end

return p