Module:Page/Condition

local cargo = require('Module:CargoUtil') local util_table = require('Module:TableUtil')

local render_gear = require('Module:Render/Gear') local render_item = require('Module:Render/Item') local render_job = require('Module:Render/Job')

local p = {} local h = {} h.LANGS = {"english", "japanese"}

function p.sources(frame) local args = require('Module:Arguments').getArgs(frame, {		parentFirst = true,	}) return table.concat({		'== Skills Relating to this Condition ==',		h.tablesFor('Conditions.type<>1 AND Conditions.type<>5', 'inflict', args),		h.tablesFor('Conditions.type=1', 'cure', args),		h.tablesFor('Conditions.type=5', 'grant immunity to', args),	}, '\n') end

function h.quote(s) if not s then return end return string.format('%q', s) end

function h.serializeInames(tbl) local inames = util_table.extractValueToList(tbl, 'iname') return util_table.concat(inames, ',', h.quote) end

function h.tablesFor(whereType, msg, args) local langs = util_table.concat(h.LANGS, ',', h.quote)

local conditionName = args[2] local server = args.server or 'gl' local lang = args.lang

local conditions = cargo.query{ tables = 'Conditions, Conditions__conds', join = 'Conditions._ID = Conditions__conds._rowID', fields = 'iname, type', where = { whereType, ('server = "%s"'):format(server), '_value = '..args[1], }	}	local cids = h.serializeInames(conditions)

local skill_rows = cids ~= '' and cargo.query{ tables = 'Skill, SkillLoc, Pages', join = 'Skill.iname = SkillLoc.iname, Skill.iname = Pages.iname', fields = 'Pages._pageName=_pageName, Skill.iname=iname, s_cond, t_cond, name, expr, lang', where = { ('Skill.server = "%s"'):format(server), ('lang IN (%s)'):format(langs), ('s_cond IN (%s) OR t_cond IN (%s)'):format(cids, cids), },	} or {} local skills = cids ~= '' and h.makeSource{ linksKey = 'cond', fields = {'s_cond', 't_cond'}, inNodes = conditions, outNodes = skill_rows, } or {} local skill_ids = h.serializeInames(skills) local ability_rows = skill_ids ~= '' and cargo.query{ tables = 'Ability', fields = { '_pageName, iname', 'skl1, skl2, skl3, skl4, skl5, skl6, skl7, skl8, skl9, skl10', },			where = { ('server = "%s"'):format(server), table.concat({					('skl1 IN (%s)'):format(skill_ids),					('skl2 IN (%s)'):format(skill_ids),					('skl3 IN (%s)'):format(skill_ids),					('skl4 IN (%s)'):format(skill_ids),					('skl5 IN (%s)'):format(skill_ids),					('skl6 IN (%s)'):format(skill_ids),					('skl7 IN (%s)'):format(skill_ids),					('skl8 IN (%s)'):format(skill_ids),					('skl9 IN (%s)'):format(skill_ids),					('skl10 IN (%s)'):format(skill_ids),				}, ' OR '), },		} or {} local abilities = skill_ids ~= '' and h.makeSource{ linksKey = 'skill', fields = {'skl1', 'skl2', 'skl3', 'skl4', 'skl5', 'skl6', 'skl7', 'skl8', 'skl9', 'skl10'}, inNodes = skills, outNodes = ability_rows, } or {} local ability_ids = h.serializeInames(abilities)

local sb = {} --	-- Jobs -- --

local job_rows = ability_ids ~= '' and cargo.query{ tables = 'Job, JobLoc, Pages', join = 'Job.iname = JobLoc.iname, Job.iname = Pages.iname', fields = { 'Pages._pageName=_pageName', 'Job.iname=iname', 'Job.fixabl=fixabl', 'COALESCE(Job.ac2d,Job.mdl)=icon', 'JobLoc.name=name', 'JobLoc.lang=lang', },		where = { ('server = "%s"'):format(server), ('lang IN (%s)'):format(langs), ('fixabl IN (%s)'):format(ability_ids), ('Job.iname NOT LIKE "JB_ALC_%"'), -- remove dev jobs. },	} or {} local jobs = ability_ids ~= '' and h.makeSource{ linksKey = 'ability', fields = {'fixabl'}, inNodes = abilities, outNodes = job_rows, } or {}

if #jobs > 0 then -- TODO: Need to support abilities beyond main sb[#sb+1] = '\n=== Skills which '..msg..' '..conditionName..' that are fixed to a job: ===\n{| class="wikitable frozen-header sortable" \n!Job\n!Skill\n!Description' local t = {} for _, job in ipairs(jobs) do			-- TODO: Wire into Module:Render/Job local icon = render_job._icon2{data=job, size='small'} -- sb[#sb+1] = '\n|-\n| '..(#job._links.skill > 1 and 'rowspan='..#job._links.skill..' | ' or '') -- sb[#sb+1] = icon for i, skill in ipairs(job._links.skill) do				local k = job.name..'|'..icon..'|'..skill.name..'|'..skill.expr t[k] = t[k] or { name = job.name, icon = icon, skill_name = skill.name, skill_expr = skill.expr, }				-- sb[#sb+1] = (i > 1 and '\n|-' or '')..'\n| '..skill.name..'\n| '..skill.expr end end local t1 = {} for k, v in pairs(t) do			t1[#t1+1] = k		end table.sort(t1) for i, k in ipairs(t1) do			local v = t[k] sb[#sb+1] = '\n|-\n| ' sb[#sb+1] = v.icon sb[#sb+1] = '\n| '..v.skill_name..'\n| '..v.skill_expr end sb[#sb+1] = '\n|}' end ---	-- Artifacts -- ---	local artifact_rows = ability_ids ~= '' and cargo.query{ tables = { 'Artifact', 'Artifact__abils', 'ArtifactLoc', 'Pages', },		join = { 'Artifact._ID = Artifact__abils._rowID', 'Artifact.iname = ArtifactLoc.iname', 'Artifact.iname = Pages.iname', },		fields = { 'COALESCE(Pages._pageName,ArtifactLoc.name) = _pageName', -- '_pageName', 'Artifact.iname=iname', 'icon', 'rini', '_value=abil', 'ArtifactLoc.name=name', 'ArtifactLoc.lang=lang', },		where = { ('server = "%s"'):format(server), ('lang IN (%s)'):format(langs), ('_value IN (%s)'):format(ability_ids), }	} or {} local artifacts = ability_ids ~= '' and h.makeSource{ linksKey = 'ability', fields = {'abil'}, inNodes = abilities, outNodes = artifact_rows, } or {} local artifact_rows = skill_ids ~= '' and cargo.query{ tables = { 'Artifact', 'ArtifactLoc', 'Pages', },		join = { 'Artifact.iname = ArtifactLoc.iname', 'Artifact.iname = Pages.iname', },		fields = { 'COALESCE(Pages._pageName,ArtifactLoc.name) = _pageName', 'Artifact.iname = iname', 'icon', 'rini', 'attack1', 'attack2', 'attack3', 'attack4', 'attack5', 'equip1', 'equip2','equip3', 'equip4', 'equip5', 'ArtifactLoc.name=name', 'ArtifactLoc.lang=lang', },		where = { ('server = "%s"'):format(server), ('lang IN (%s)'):format(langs), table.concat({				('attack1 IN (%s)'):format(skill_ids),				('attack2 IN (%s)'):format(skill_ids),				('attack3 IN (%s)'):format(skill_ids),				('attack4 IN (%s)'):format(skill_ids),				('attack5 IN (%s)'):format(skill_ids),				('equip1 IN (%s)'):format(skill_ids),				('equip2 IN (%s)'):format(skill_ids),				('equip3 IN (%s)'):format(skill_ids),				('equip4 IN (%s)'):format(skill_ids),				('equip5 IN (%s)'):format(skill_ids),			}, ' OR '), }	} or {} local artifacts2 = skill_ids ~= '' and h.makeSource{ linksKey = 'skill', fields = { 'attack1', 'attack2', 'attack3', 'attack4', 'attack5', 'equip1', 'equip2','equip3', 'equip4', 'equip5', },		inNodes = skills, outNodes = artifact_rows, } or {} for _, artifact in ipairs(artifacts2) do		for _, skill in ipairs(artifact._links.skill) do skill.name = 'Basic Attack' end util_table.push(artifacts, artifact) end if #artifacts > 0 then table.sort(artifacts, function(a, b) return a.iname < b.iname end) sb[#sb+1] = '\n=== Skills which '..msg..' '..conditionName..' that are provided by gear: === \n{| class="wikitable frozen-header sortable" \n!Gear\n!Skill\n!Description' for _, artifact in ipairs(artifacts) do			sb[#sb+1] = '\n|-\n| '..(#artifact._links.skill > 1 and 'rowspan='..#artifact._links.skill..' | ' or '') sb[#sb+1] = render_gear._icon2{data = artifact, size = 'small'} for i, skill in ipairs(artifact._links.skill) do				sb[#sb+1] = (i > 1 and '\n|-' or '')..'\n| '..skill.name..'\n| '..skill.expr end end sb[#sb+1] = '\n|}' end ---	-- Items -- ---	local item_rows = skill_ids ~= '' and cargo.query{ tables = { 'Item', 'ItemLoc', 'Pages', },		join = { 'Item.iname = ItemLoc.iname', 'Item.iname = Pages.iname', },		fields = { 'Pages._pageName=_pageName', 'Item.iname=iname', 'Item.icon=icon', 'Item.rare=rare', 'Item.type=type', 'Item.skill=skill', 'ItemLoc.name=name', 'ItemLoc.expr=expr', 'ItemLoc.lang=lang', },		where = { ('Item.server = "%s"'):format(server), ('ItemLoc.lang in (%s)'):format(langs), ('Item.skill IN (%s)'):format(skill_ids), }	} or {} local items = skill_ids ~= '' and h.makeSource{ linksKey = 'skill', fields = {'skill'}, inNodes = skills, outNodes = item_rows, } or {}

if #items > 0 then sb[#sb+1] = '\n=== Consumables which '..msg..' '..conditionName..' upon use: === \n{| class="wikitable frozen-header sortable" \n!Item\n!Description' for _, item in ipairs(items) do			sb[#sb+1] = '\n|-\n| '..render_item._icon2{data = item, size = 'small'}..'\n| '..(item.expr or '') end sb[#sb+1] = '\n|}' end sb[#sb+1] = '' return table.concat(sb) end

function h.makeSource(args) if args.outNodes[1] and args.outNodes[1].lang then args.outNodes = h.handleLangs(args.outNodes) end

local hash = {} for _, inNode in ipairs(args.inNodes) do		if type(inNode) == 'string' then inNode = {iname=inNode} end local foundNodes = h.findNodes(inNode, args.outNodes, args.fields) for _, outNode in ipairs(foundNodes) do			if hash[outNode.iname] == nil then outNode._links = inNode._links or {} outNode._links[args.linksKey] = {inNode} hash[outNode.iname] = outNode else util_table.push(hash[outNode.iname]._links[args.linksKey], inNode) end end end local entries = {} for _, v in pairs(hash) do		util_table.push(entries, v)	end table.sort(entries, function(a, b) return a.iname < b.iname end) return entries end

function h.handleLangs(rows) local outNodes = {} local t = {} for _, row in ipairs(rows) do		local lang = util_table.remove(row, 'lang') t[row.iname] = t[row.iname] or {} t[row.iname][lang] = row end for _, v in pairs(t) do		local node = {} for i, lang in ipairs(h.LANGS) do			for param, value in pairs(v[lang] or {}) do				if not node[param] then node[param] = value end end end util_table.push(outNodes, node) end return outNodes end

function h.findNodes(inNode, outNodes, fields) local result = {} for _, outNode in ipairs(outNodes) do		local outNodeCopy = util_table.shallowClone(outNode) local found = false for i, field in ipairs(fields) do			if inNode.iname == util_table.remove(outNodeCopy, field) then found = true end end if found then util_table.push(result, outNodeCopy) end end return result end

return p