Module:MapDataToWikitext

local List = require 'Module:ListUtil' local Hash = require 'Module:HashUtil' local CargoUtil = require 'Module:CargoUtil'

local PLAYABLE_ENEMIES = { 'ヘルビンディ', 'レーギャルン', 'レーヴァテイン', 'ロキ', 'スルト', 'スラシル', 'リーヴ', 'ヘル', 'プルメリア', 'スカビオサ', 'フレイヤ', 'ダグ', 'ノート', } local USE_ALLY_STATS = Hash.from_ipairs(PLAYABLE_ENEMIES, function (v) return ('PID_%s味方'):format(v), true end) local USE_ENEMY_STATS = Hash.from_ipairs(PLAYABLE_ENEMIES, function (v) return ('EID_%s'):format(v), true end)

local unitsQuery = nil local skillsQuery = nil local accessoriesQuery = nil

local function cacheInfo cacheInfo = function end

unitsQuery = Hash.from_ipairs(CargoUtil.full_query( 'Units,WeaponTypes', TagID,IFNULL(CONCAT(Units.Name,': ',Title),Units.Name)=Name,IF(Properties__full LIKE '%generic%','1','')=isGeneric,		 MoveType=Moves,IF(Classes__full LIKE '%Ranged%','Ranged','Close')=Weapons, { join = 'Units.WeaponType=WeaponTypes.WikiName', where = 'TagID IS NOT NULL', groupBy = 'TagID', }), function (v) return v.TagID, v end) for _, v in pairs(unitsQuery) do		v.Name = v.Name ~= '' and v.Name or nil end

skillsQuery = Hash.from_ipairs(CargoUtil.full_query('Skills', "TagID,IF(Scategory='weapon',_pageName,Name)=Name,IFNULL(RefinePath,'')=Refine", { where = 'TagID IS NOT NULL', groupBy = 'TagID', }), function (v) return v.TagID, v end) for _, v in pairs(skillsQuery) do		v.Name = v.Name ~= '' and v.Name or nil v.Refine = v.Refine ~= '' and v.Refine or nil end

accessoriesQuery = Hash.from_ipairs(CargoUtil.full_query('Accessories', "TagID,_pageName=Page", { where = 'TagID IS NOT NULL', groupBy = 'TagID', }), function (v) return v.TagID, v end) end

local function makeObjectArg (v) local vt = type(v) if vt == 'nil' then return '' elseif vt == 'table' then if v['=hash'] then return '{' .. table.concat(List.map(v, function (v) return ("%s=%s"):format(makeObjectArg(v[1]), makeObjectArg(v[2])) end), ';') .. '}'		else return '[' .. table.concat(List.map(v, function (v) return ("%s"):format(makeObjectArg(v)) end), ';') .. ']'		end else v = tostring(v) if mw.ustring.find(v, '=;%[%]%{%}') then error('String contains control characters: ' .. v, 2) end return mw.text.trim(v, '%s') end end

local compact_hash = function (t) t['=hash'] = true return List.keep_if(t, function (v) return v[2] ~= nil end) end

local comment = function (s) return (""):format(tostring(s)) end

local unit_lookup = function (pid) return unitsQuery[pid] and unitsQuery[pid].Name or comment(pid) end

local skill_lookup = function (sid) if not sid then return '-' end local skill = skillsQuery[sid] return skill and skill.Name or comment(sid) end

local refine_lookup = function (sid) if not sid then return nil end local skill = skillsQuery[sid] return skill and skill.Refine end

local accessory_lookup = function (daid) if not daid then return nil end local acc = accessoriesQuery[daid] return acc and acc.Page or comment(daid) end

local convert1 = function (jsontext) cacheInfo local mapdata = mw.text.jsonDecode(jsontext) local hasRandom = mw.ustring.sub(mapdata.field.id, 1, 1) == 'W' -- Tempest Trials

local ret = {'['}

for slot, unit in ipairs(mapdata.units) do		local props = List.compact { not unit.is_enemy and 'is_ally' or nil, unit.is_enemy and USE_ALLY_STATS[unit.id_tag] and 'use_ally_stats' or nil, not unit.is_enemy and USE_ENEMY_STATS[unit.id_tag] and 'use_enemy_stats' or nil, }

local unitArgs = nil if hasRandom and unitsQuery[unit.id_tag] and unitsQuery[unit.id_tag].isGeneric ~= '' then unitArgs = compact_hash { {'pos', string.char(unit.pos.x + 97) .. tostring(unit.pos.y + 1)}, {'rarity', unit.rarity}, {'slot', slot}, {'level', unit.true_lv}, {'displaylevel', unit.true_lv ~= unit.lv and unit.lv or nil}, {'ai', unit.is_enemy and compact_hash { {'turn', unit.start_turn ~= -1 and unit.start_turn or nil}, {'group', unit.movement_group ~= -1 and unit.movement_group or nil}, {'delay', unit.movement_delay ~= -1 and unit.delay or nil}, {'break_walls', unit.break_terrain and '1' or nil}, {'tether', unit.tether and '1' or nil}, } or nil}, {'random', compact_hash { {'moves', unitsQuery[unit.id_tag].Moves}, {'weapons', unitsQuery[unit.id_tag].Weapons}, {'staff', unitsQuery[unit.id_tag].Weapons == 'Ranged' and 'false' or nil}, }},			}		else unitArgs = compact_hash { {'unit', unit_lookup(unit.id_tag)}, {'pos', string.char(unit.pos.x + 97) .. tostring(unit.pos.y + 1)}, {'rarity', unit.rarity}, {'slot', slot}, {'level', unit.true_lv}, {'displaylevel', unit.true_lv ~= unit.lv and unit.lv or nil}, {'stats', { unit.stats.hp, unit.stats.atk, unit.stats.spd, unit.stats.def, unit.stats.res, }},				{'weapon', skill_lookup(unit.skills[1])}, {'refine', refine_lookup(unit.skills[1])}, {'assist', skill_lookup(unit.skills[2])}, {'special', skill_lookup(unit.skills[3])}, {'a', skill_lookup(unit.skills[4])}, {'b', skill_lookup(unit.skills[5])}, {'c', skill_lookup(unit.skills[6])}, {'seal', skill_lookup(unit.skills[7])}, {'accessory', accessory_lookup(unit.accessory)}, {'cooldown', unit.cooldown_count ~= -1 and unit.cooldown_count or nil}, {'ai', unit.is_enemy and compact_hash { {'turn', unit.start_turn ~= -1 and unit.start_turn or nil}, {'group', unit.movement_group ~= -1 and unit.movement_group or nil}, {'delay', unit.movement_delay ~= -1 and unit.delay or nil}, {'break_walls', unit.break_terrain and '1' or nil}, {'tether', unit.tether and '1' or nil}, } or nil}, {'properties', #props > 0 and table.concat(props, ',') or nil}, }		end

if unit.spawn_count ~= -1 then unitArgs[#unitArgs + 1] = {'spawn', compact_hash { {'turn', unit.spawn_turns ~= -1 and unit.spawn_turns + 1 or nil}, {'count', unit.spawn_count ~= 1 and unit.spawn_count or nil}, {'target', unit.spawn_check and unit_lookup(unit.spawn_check)}, {'remain', unit.spawn_target_remain ~= -1 and unit.spawn_target_remain or nil}, {'kills', unit.spawn_target_kills ~= -1 and unit.spawn_target_kills or nil}, }}		end

ret[#ret + 1] = mw.ustring.gsub(makeObjectArg(unitArgs), '}}', '};}') .. ';'	end

ret[#ret + 1] = ']' return table.concat(ret, '\n') end

local convert = function (args, frame) return convert1(args[1]) end

local p = require 'Module:MakeMWModule'.makeMWModule {convert = convert} p.convert1 = convert1 return p