Module:QuestDataToWiki

local parseReward = require 'Module:RewardJsonParser'.convertToString local datetime = require 'Module:DatetimeUtil' local Hash = require 'Module:HashUtil' local List = require 'Module:ListUtil'

local SORTING_PATTERN = { [1] = {['pattern'] = 'YEAR'}, [2] = {['pattern'] = '^W?_?M'}, -- Monthly [3] = {['pattern'] = '^WEEK_'}, [4] = {['pattern'] = '^DAILY_'}, [10] = {['pattern'] = 'STORY', ['nbQuests'] = 3}, -- Story maps [11] = {['pattern'] = 'STORY', ['nbQuests'] = 5}, -- Paralogue maps [15] = {['pattern'] = 'VOTE'}, -- Voting Gauntlet [16] = {['pattern'] = 'SENKA'}, -- Tempest Trials [17] = {['pattern'] = 'TAPBTL'}, -- Tap Battle [18] = {['pattern'] = 'DAISEIATU'}, -- Grand Conquests [20] = {['pattern'] = 'SHADOW'}, -- Rokkr Sieges [23] = {['pattern'] = 'MJOLNIR'}, -- Mjölnir's Strike [24] = {['pattern'] = 'MAMORE'}, -- Frontline Phalanx [25] = {['pattern'] = 'BOARDGAME'}, -- Pawns of Loki [31] = {['pattern'] = 'HERO'}, -- Grand Hero [32] = {['pattern'] = 'KIZUNA'}, -- Bound Hero --[] = {['pattern'] = 'CHARA'}, -- Three Heroes --[] = {['pattern'] = 'SKY'}, -- Aether Raids --[] = {['pattern'] = 'ARENA'}, -- Coliseum --[] = {['pattern'] = 'BRAVE'}, -- Unit specific (Alfonse, Ljosalfar & Heroes, etc) --[] = {['pattern'] = 'ADVANCE'} -- Movetype 'Strike' --[] = {['pattern'] = 'BIND'} -- Movetype 'Mastery' [50] = {['pattern'] = 'SUBSC'}, -- FeH Pass }

local data = require("Module:FEH lang/USEN/data") local fillData = function (dataFile) local cargo = mw.ext.cargo for _, t in ipairs(mw.text.jsonDecode(dataFile)) do		data[t.key] = t.value end for _, unit in ipairs(cargo.query('Units', '_pageName, TagID', { where = "IFNULL(Properties__full, '') NOT LIKE '%enemy%'", limit = 1000, })) do		data[unit.TagID] = unit._pageName end for _, map in ipairs(cargo.query('Maps', '_pageName, Map', { limit = 5000, })) do		data[map.Map] = map._pageName end data["E0000"] = "Training Tower/Starting Stratum" data["E0001"] = "Training Tower/First Stratum" data["E0002"] = "Training Tower/Second Stratum" data["E0003"] = "Training Tower/Third Stratum" data["E0004"] = "Training Tower/Fourth Stratum" data["E0005"] = "Training Tower/Fifth Stratum" data["E0006"] = "Training Tower/Sixth Stratum" data["E0007"] = "Training Tower/Seventh Stratum" data["E0008"] = "Training Tower/Eighth Stratum" data["E0009"] = "Training Tower/Ninth Stratum" data["E0010"] = "Training Tower/Tenth Stratum" for _, vg in ipairs(cargo.query('VotingGauntlets', "DATE_FORMAT(StartTime, '%Y%m%d')=start,_pageName")) do data["VG " .. vg.start] = vg._pageName end for _, tt in ipairs(cargo.query('TempestTrials', "DATE_FORMAT(StartTime, '%Y%m')=start,_pageName")) do if data["TT " .. tt.start] then data["TT " .. tt.start .. "_2"] = tt._pageName else data["TT " .. tt.start] = tt._pageName end end for _, tb in ipairs(cargo.query('TapBattles', "DATE_FORMAT(StartTime, '%Y%m')=start,_pageName")) do data['TB ' .. tb.start] = tb._pageName end

for _, gc in ipairs(cargo.query('GrandConquests', "DATE_FORMAT(StartTime, '%Y%m')=start, _pageName")) do if data["GC " .. gc.start] then data["GC " .. gc.start .. "_2"] = gc._pageName else data["GC " .. gc.start] = gc._pageName end end for _, rs in ipairs(cargo.query('RokkrSieges', "DATE_FORMAT(StartTime, '%Y%m')=start,_pageName")) do data["shadow_" .. rs.start] = rs._pageName end end

local getSortValue = function(group) for value, cond in pairs(SORTING_PATTERN) do		if (not cond.pattern or string.match(group.id_tag, cond.pattern)) and (not cond.nbQuests or cond.nbQuests ~= group.lists[1].quest_count) then return value end end return 40 end

local parseStage = function(quest, start) if quest.game_mode == 7 then local startTime = datetime.from_iso8601(start) - tonumber(mw.ustring.match(quest.quest_id, "_(%d%d%d)_")) * 2 * 86400 quest.map_group = "VG " .. os.date("%Y%m%d", startTime) elseif quest.game_mode == 8 then quest.map_group = "TT " .. quest.map_group elseif quest.game_mode == 12 then quest.map_group = "TB " .. mw.ustring.match(quest.quest_id, "S(%d+)TAPBTL") elseif quest.game_mode == 14 then quest.map_group = "GC " .. quest.map_group elseif quest.game_mode == 21 then -- Rokkr Sieges end return quest.map_group and (";stage=" .. (data[quest.map_group] or quest.map_group)) or "" end

local parseGroupQuest = function(questdata) groups = {} for _, group in ipairs(questdata) do		groups[#groups+1] = { ['title'] = data["MID_MISSION_" .. group.title] or (""), ['startTime'] = group.avail.start, ['endTime'] = datetime.to_iso8601(datetime.from_iso8601(group.avail.finish) - 1), ['sort'] = tostring(getSortValue(group)), ['quests'] = {} }

for _, difficulty in ipairs(group.lists) do			for _, quest in ipairs(difficulty.quests) do				local id = quest.common_id or quest.quest_id groups[#groups].quests[#groups[#groups].quests + 1] = "{name=" .. mw.ustring.gsub(data["MID_MISSION_" .. id] or "MID_MISSION_" .. id, '\n', ' ') .. ";description=" .. mw.ustring.gsub(data["MID_MISSION_H_" .. id] or "MID_MISSION_H_" .. id, '\n', ' ') .. (quest.times ~= 1 and (";times=" .. quest.times) or '') .. ";reward=" .. parseReward(quest.reward) .. (quest.unit_reqs.hero_id and (";unit=" .. (data[quest.unit_reqs.hero_id] or quest.unit_reqs.hero_id)) or "") .. parseStage(quest, group.avail.start) .. (#group.lists ~= 1 and (";difficulty=" .. mw.ustring.lower(difficulty.difficulty)) or '') .. ";}"			end groups[#groups].quests = '[\n     ' .. List.reduce(groups[#groups].quests, function (s1, s2)				return s1 .. ';\n     ' .. s2			end) .. '\n   ]' end end return groups end

local mergeSameQuest = function(groupQuest) groups = {} for _, group in ipairs(groupQuest) do		local _, a = List.find_if(groups, function (v)			return group.title == v.title and group.quests == v.quests		end) if a then a.startTime = a.startTime .. ";" .. group.startTime a.endTime = a.endTime .. ';' .. group.endTime else groups[#groups + 1] = group end end return groups end

local convert = function (args, frame) fillData(args[2] or '[]') local questdata = mw.text.jsonDecode(args[1] or "[]") local s = "[\n"

groups = parseGroupQuest(questdata) groups = mergeSameQuest(groups) return '[\n ' .. List.reduce(groups, function (s, v)		return (#s ~= 0 and (s .. ';\n ') or '') ..			'{\n    title=' .. v.title .. 			';\n    startTime=' .. v.startTime ..			';\n    endTime=' .. v.endTime ..			';\n    sort=' .. v.sort ..			';\n    quests=' .. v.quests ..			'\n  }'	end, "") .. '\n]' end

return require 'Module:MakeMWModule'.makeMWModule { convert = convert, }