Module:Roles Text Parser

From Another Eden Wiki

Documentation for this module may be created at Module:Roles Text Parser/doc

local p = {}
local TableUtil = require('Module:TableUtil')
local Cargo = mw.ext.cargo

function p.main(frame)
	local args = require('Module:ProcessArgs').merge(true)
	
	local pagename = args[1] or mw.title.getCurrentTitle().text
	local skills = p.doSkillsQuery(pagename)
	local skillRoles = {}
	
	-- Loop through all the skills queried and add them into skillRoles
	for index, skill in ipairs(skills) do
		local skillName = skill['skillName']
		table.insert(skillRoles, {name = skillName, skillRoles = p.getRoles(skill)})
	end
	
	local formatted = p.formatSkillRoles(skillRoles)

	return p.makeList(formatted)
end

--- Use Cargo to query for the variables we need.
-- @param pagename Name of the page we're querying
-- @return result Result of the Query, or nil
function p.doSkillsQuery(pagename)
	-- These are really chunky fields, so break them out of the query for readability.
	local cargoTables = [[HeroSkills=s,Characters=c1,
Characters__Skills,Characters=c2,
Characters__Passives,Characters=c3,
Characters__Stellars,Characters=c4]]
	local cargoFields = [[s.ActionType=actionType,
s.Description=description,
s._pageName=skillName,
s.Element=element,
s.SkillType=skillType,
Characters__Skills._position=position]]
	local cargoJoin = [[s._pageName=c1._pageName,
s._pageName=Characters__Skills._value,
Characters__Skills._rowID=c2._ID,
s._pageName=Characters__Passives._value,
Characters__Passives._rowID=c3._ID,
s._pageName=Characters__Stellars._value,
Characters__Stellars._rowID=c4._ID]]

	local result = mw.ext.cargo.query(
		cargoTables,
		cargoFields,
		{
			join = cargoJoin,
			where = "'" .. pagename .. "' IN (c1._pageName,c2._pageName,c3._pageName,c4._pageName)",
			orderBy = "position ASC",
			limit = 30
		}
	)

	if #result == 0 then
		return nil
	else
		return result
	end
end

function p.getRoles(skill, format)
	local actionTypeRoles = p.getActionTypeSkillRoles(skill['actionType'])
	local basicAttackRoles = p.getBasicAttackSkillRoles(skill['description'])
	--local sidekickSkillRoles = p.getSidekickSkillRoles(format, skill['description'], skill['skillName']])
	local stanceSkillRoles = p.getStanceSkillRoles(skill['description'], skill['skillName'])

	return TableUtil.mergeArrays(
		actionTypeRoles,
		basicAttackRoles,
		sidekickSkillRoles,
		stanceSkillRoles
	)
end

--- Add roles based on action type
-- @param actionType
-- @return actionTypeSkillRoles
function p.getActionTypeSkillRoles(actionType)
	local actionTypeSkillRoles = {}
	
	if actionType == nil then
		return {}
	end
	
	if actionType == "Prayer" or actionType == "Song" or actionType == "Aura" then
		table.insert(actionTypeSkillRoles, actionType)
	elseif actionType == "Valor Chant" then
		table.insert(actionTypeSkillRoles, "VC")
	elseif actionType == "Another Sense" then
		table.insert(actionTypeSkillRoles, "AS")
	elseif actionType == "EX Special Move" then
		table.insert(actionTypeSkillRoles, "EX")
	elseif actionType == "Battle Start" then
		table.insert(actionTypeSkillRoles, "SoB")
	elseif string.find(actionType, "End of turn") ~= nil then
		table.insert(actionTypeSkillRoles, "EoT")
	elseif string.find(actionType, "Start of turn") ~= nil then
		table.insert(actionTypeSkillRoles, "SoT")
	elseif actionType == "Preemptive" then
		table.insert(actionTypeSkillRoles, "Pre")
	elseif actionType == "Delayed" then
		table.insert(actionTypeSkillRoles, "Del")
	elseif actionType == "Counter" then
		if string.find(actionType, "magically.attacked", 1, false) ~= nil then
			table.insert(actionTypeSkillRoles, "Cntr_M")
		elseif string.find(actionType, "physically.attacked", 1, false) ~= nil then
			table.insert(actionTypeSkillRoles, "Cntr_P")
		end
	end
	
	return actionTypeSkillRoles
end

--- Get basic attack skill role if applicable
-- @param description
function p.getBasicAttackSkillRoles(description)
	if string.find(string.lower(description), 'replaces the basic attack', 1, false) then
		return {"Basic"}
	end
	return {}
end

--- Get sidekick role based on format (either action type or pagename)
-- @param format
-- @param actionType
-- @param pagename
function p.getSidekickSkillRoles(format, actionType, pagename)
	local sidekickSkillRoles = {}
	if string.lower(format) == 'skill' then
		if actionType == "Auto" or actionType == "Charged" or actionType=="Aura" then
			table.insert(sidekickSkillRoles, "Bud")
		end
	elseif pagename ~= nil or pagename ~= '' then
		table.insert(sidekickSkillRoles, "Bud")
	end
	return sidekickSkillRoles
end

--- Get stance skill roles based on description and sometimes pagename
-- @param description
-- @param pagename
-- @return stanceSkillRoles
function p.getStanceSkillRoles(description, pagename)
	local stanceSkillRoles = {}

	if string.find(description, "Deploy.Raging Fire Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Fire")
	elseif string.find(description, "Deploy.Sacred Water Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Water")
	elseif string.find(description, "Deploy.Wind King Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Wind")
	elseif string.find(description, "Deploy.Torn Earth Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Earth")
	elseif string.find(description, "Deploy.Biting Shade Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Shade")
	elseif string.find(description, "Deploy.Crystalline Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Crystal")
	elseif string.find(description, "Deploy.Thundering Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Thunder")
	elseif string.find(description, "Deploy.Dazzling Slash Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Slash")
	elseif string.find(description, "Deploy.Magic Fates Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Magic")
	elseif string.find(description, "Deploy.Piercing Flash Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Pierce")
	elseif string.find(description, "Deploy.Roaring Bash Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Blunt")
	elseif string.find(description, "Deploy.Defense Wall", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Defense")
	elseif string.find(description, "Deploy.Flash Strike Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Flash")
	elseif string.find(description, "Deploy.Sea of Stars Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Stars")
	elseif string.find(description, "Deploy.Moonlight Stance", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_Moon")
	elseif string.find(description, "Deploy.Double Strike Stance: Magic Slash", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_MaSl")
	elseif string.find(description, "Deploy.Stance: Water Crystal", 1, false) ~= nil then
		table.insert(stanceSkillRoles, "Zone_WaCr")
	end
	
	if string.find(description, "Awaken.Another Zone", 1, false) ~= nil
	and string.find(description, "once per battle", 1, false) ~= nil
	and pagename ~= "Another Zone" then
		table.insert(stanceSkillRoles, "ZoneAwaken")
	end

	if string.find(description, "Awaken.Another Zone", 1, false) ~= nil
	and string.find(description, "once per battle", 1, false) == nil
	and pagename ~= "Another Zone" then
		table.insert(stanceSkillRoles, "ZoneAwakenMulti")
	end

	return stanceSkillRoles
end

--- Turn the list of roles into a delimited string
-- @param skillRoles
-- @return skillRoleStrings
function p.formatSkillRoles(skillRoles)
	local skillRoleStrings = {}
	for _,skill in ipairs(skillRoles) do
		local skillRoleString = "<b>[" .. skill['name'] .. "]</b>"
		for _,skillRoleTag in pairs(skill['skillRoles']) do
			skillRoleString = skillRoleString .. "_" .. skillRoleTag
		end
		table.insert(skillRoleStrings, skillRoleString)
	end
	return skillRoleStrings
end

--- Add the page formatting to output the list of skill roles
-- @param skillRoleStrings
-- @return skillRoleList
function p.makeList(skillRoleStrings)
	local skillRoleList = mw.html.create('ul')

	for _,skillRoleString in ipairs(skillRoleStrings) do
		skillRoleList:tag('li'):tag('small'):tag('code'):wikitext(skillRoleString)
	end

	return skillRoleList
end

return p