Module:SkillJSONToWikitext

local p = {} local lang_USEN = mw.loadData("Module:FEH lang/USEN/data") local bit32 = require "bit32" local cargo = mw.ext.cargo local ListUtil = require 'Module:ListUtil' local HashUtil = require 'Module:HashUtil' local escapePattern = (require "Module:String")._escapePattern local frame = mw.getCurrentFrame local ScriptUtil = require "Module:ScriptUtil"

-- TODO: refactor using ScriptUtil template objects to reduce code duplication -- TODO add refines -- TODO add otherlanguages detection for when pagename != english -- TODO add otherlanguages

local moveTypes = {} local weaponTypes = {}

for _,v in ipairs(cargo.query("MoveTypes","WikiName",{ orderBy = "Sort", limit = 5000 })) do	moveTypes[#moveTypes+1] = v["WikiName"] end for _,v in ipairs(cargo.query("WeaponTypes","WikiName",{ orderBy = "Sort", limit = 5000 })) do	weaponTypes[#weaponTypes+1] = v["WikiName"] end

local weaponGroupsToType = {}

local function xListWikitext(listType,params) return table.concat(mw.text.split((frame:expandTemplate{ title = listType.."List", args = params }),"%s*,%s*"),",") end

local commonMoveGroupingsToWikitext = { [xListWikitext("Move",{"All"})] = "" } local commonWeaponGroupingsToWikitext = { [xListWikitext("Weapon",{"All"})] = "", [xListWikitext("Weapon",{"Close"})] = "", [xListWikitext("Weapon",{"Physical"})] = "", [xListWikitext("Weapon",{"Melee"})] = "", [xListWikitext("Weapon",{["exclude"]="Staff"})] = "" }

for _,v in ipairs(cargo.query("WeaponTypes","GROUP_CONCAT(DISTINCT WikiName ORDER BY Sort SEPARATOR ',')=list,Type", {groupBy= "Type" ,limit = 5000})) do	weaponGroupsToType[v.list] = v.Type commonWeaponGroupingsToWikitext[v.list] = "" end

local function generateBitSet(num,maxbits) -- Returns a bitset ordered from least significant to most significant local temp = 1 local maxnum = 2^maxbits local bitset = {} while temp < maxnum do		bitset[#bitset+1] = bit32.band(num,temp) ~= 0 temp = temp * 2 end return bitset end

local function getWeaponsFromBitmask(bitmask) local canUseWeapons = {} for k,canUse in ipairs(generateBitSet(bitmask,#weaponTypes)) do		if canUse then canUseWeapons[#canUseWeapons+1] = weaponTypes[k] end end return canUseWeapons end

local function getMovesFromBitmask(bitmask) local canUseMove = {} for k,canUse in ipairs(generateBitSet(bitmask,#moveTypes)) do		if canUse then canUseMove[#canUseMove+1] = moveTypes[k] end end return canUseMove end

local function getWeaponListWikitext(bitmask) local weapons = table.concat(getWeaponsFromBitmask(bitmask),",") return commonWeaponGroupingsToWikitext[weapons] or weapons end

local function getMoveListWikitext(bitmask) local moves = table.concat(getMovesFromBitmask(bitmask),",") return commonMoveGroupingsToWikitext[moves] or moves end

local function processDescription(description) local s = description or "" s = (mw.ustring.gsub( s, "\n\n", "

")) -- TODO replace any arbitrary sequence with >1 \n characters with the equivalent amount of instead of just 2 only	s = (mw.ustring.gsub( s, "-\n", "-"))	s = (mw.ustring.gsub( s , "】\n", "】 ")) -- Any line break after 】 is likely to be an explicit line break for a keyword	s = (mw.ustring.gsub( s, "\n", " "))	if mw.ustring.find( s, "^$a%D" ) then		s = (mw.ustring.gsub( s , "^$a", ""))	end	return s end

local function getFieldFromSID(sid, field) if sid then local query = cargo.query("Skills",""..field.."=f",{ where = "TagID='"..sid.."'" })[1] if query then return query["f"] end end return nil end

local function getPageNameFromSID(sid) return getFieldFromSID(sid, "_pageName") end

local sidsToSkillNames = {}

local function getRequiredListString(prerequisites, useName) -- Templates are inconsistent in what is provided to the required parameter, Weapon Infobox asks for a page name while Passive asks for name local pages = {} if prerequisites then -- Query for page name of SIDs in the array. If no results are found, put cargo query substs for page names from SIDs instead local field if useName then field = "Name" else field = "_pageName" end for _,sid in HashUtil.sorted_pairs(prerequisites) do			if sid then local pg				pg = sidsToSkillNames[sid] if not pg then pg = getFieldFromSID(sid, field) end if not pg then pg = "{}}{subst:|undefined#cargo_query:tables=Skills|fields=CONCAT("..field..")|where=TagID='"..sid.."'|limit=1|default=|more results text=|undefined" end pages[#pages+1] = pg			end end end if #pages == 0 then return "-" else return table.concat(pages,",") end end

local function generateWeaponWikitext(skillData, refineData) local pageTitle = lang_USEN[skillData.name_id] or skillData.id_tag or "Project:Sandbox" local wikitext = "" wikitext = wikitext.."\n==Notes==\n" if hasRefines then wikitext = wikitext.."==Upgrades==\n\n===Notes===\n*\n" end wikitext = wikitext.. [=[==List of owners==

List of owners
]=]	if passiveTemplate:getArg("exclusive") == "1" then wikitext = wikitext .. [=[==Availability==

]=]	end wikitext = wikitext .. [=[==Trivia==