Module:RewardJsonParser

local cargo = mw.ext.cargo local enum = require 'Module:RewardJsonParser/data'

local data = {}

local cacheData = function 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 _, accessory in ipairs(cargo.query('Accessories', '_pageName, TagID', { limit = 1000, })) do		data[accessory.TagID] = accessory._pageName end for _, skill in ipairs(cargo.query('Skills', 'Name, TagID', { where = "(Scategory = 'sacredseal' OR Scategory LIKE 'passive%') AND NOT Exclusive", limit = 5000, })) do		data[skill.TagID] = skill.Name end return data end

local convert = function (jsonReward) data = cacheData cacheData = function return data end

if type(jsonReward) ~= 'table' then jsonReward = mw.text.jsonDecode(jsonReward) end

local warnings = {} local result = {} for _, reward in ipairs(jsonReward) do		local kind = enum.ITEM_KIND[tonumber(reward.kind)] or ""

if kind == "Hero" then if not data[reward.id_tag] then warnings[reward.id_tag] = {["kind"]=kind, ["tag"]=reward.id_tag} end result[#result + 1] = { ["hero"] = data[reward.id_tag] or (''), ["rarity"] = reward.rarity }		elseif kind == "FB Conversation" then if not data[reward.id_tag] then warnings[reward.id_tag] = {["kind"]=kind, ["tag"]=reward.id_tag} end result[#result + 1] = { ["hero"] = data[reward.id_tag] or (''), ["fbrank"] = enum.FB_RANK[reward.support_rank] }		elseif kind == "Accessory" then if not data[reward.id_tag] then warnings[reward.id_tag] = {["kind"]=kind, ["tag"]=reward.id_tag} end result[#result + 1] = { ["accessory"] = data[reward.id_tag] or ('') }		elseif kind == "Sacred Seal" then if not data[reward.id_tag] then warnings[reward.id_tag] = {["kind"]=kind, ["tag"]=reward.id_tag} end result[#result + 1] = { ["seal"] = data[reward.id_tag] or ('') }		else if kind == "Shard" then kind = enum.COLOR[reward.shard_color] .. (reward.great and ' Crystal' or ' Shard') elseif kind == "Badge" then kind = (reward.great and 'Great ' or '') .. enum.COLOR[reward.badge_color+1] .. ' Badge' elseif kind == 'AA Item' then kind = enum.AA_ITEM[reward.aa_kind] elseif kind == 'Blessing' then kind = enum.BLESSING_ELEMENT[reward.element] .. ' Blessing' elseif kind == 'Aether Stone' then if reward.id_tag ~= 'STONE' then warnings[kind .. reward.id_tag] = {["kind"] = kind, ["tag"] = reward.id_tag} kind = '' end elseif kind == 'Throne' then kind = enum.THRONE[reward.throne_type] elseif kind == 'Summoning Ticket' then warnings[kind .. reward.id_tag] = {["kind"] = kind, ["tag"] = reward.id_tag} kind = '' elseif kind == 'Dragonflower' then kind = 'Dragonflower (' .. mw.ustring.sub(enum.MOVE_TYPE[reward.move_type], 1, 1) .. ')' elseif kind == "Music" then warnings[kind .. reward.id_tag] = {["kind"] = kind, ["tag"] = reward.id_tag} kind = '' elseif kind == 'Divine Code' then if #reward.id_tag == 6 then kind = 'Divine Code: Ephemera ' .. tostring(tonumber(string.sub(reward.id_tag, -2))) else kind = 'Divine Code: Part ' .. tostring(tonumber(reward.id_tag) - 2019) end elseif #kind == 0 then warnings[tostring(reward.kind)] = { ["kind"] = (reward._type or '') .. ' (' .. tostring(reward.kind) .. ')', ["tag"] = "Unknow kind of item" }				kind = '' end result[#result + 1] = { ["kind"] = kind, ["count"] = reward.count }		end end warnings = require 'Module:ListUtil'.reduce(require 'Module:HashUtil'.values(warnings), function (s1, v) 		return (s1 == "" and '' or (s1 .. '\n')) .. 'Unknow item of kind "' .. v.kind .. '": ' .. v.tag	end, "") if #warnings ~= 0 then mw.addWarning(warnings) end

result = require 'Module:Reward'.normalize(result) return result end

local convertToString = function (jsonReward) rewards = convert(jsonReward) s = require 'Module:ListUtil'.reduce(rewards, function (s1, r)		if #s1 ~= 0 then s1 = s1 .. ";" end		if r.rarity then			return s1 .. '{hero=' .. r.hero .. ';rarity=' .. r.rarity .. '}'		elseif r.fbrank then			return s1 .. '{hero=' .. r.hero .. ';fbrank=' .. r.fbrank .. '}'		elseif r.accessory then			return s1 .. '{accessory=' .. r.accessory .. '}'		elseif r.seal then			return s1 .. '{seal=' .. r.seal .. '}'		else			return s1 .. '{kind=' .. r.kind .. (r.count ~= 1 and (';count=' .. r.count) or '') .. '}'		end	end, "") if #rewards ~= 1 then return '[' .. s .. ']'	else return s	end end

local p = require 'Module:MakeMWModule'.makeMWModule { convertWikitext = function (args, frame) return convertToString(args[1]) end } p.convert = convert p.convertToString = convertToString return p