Module:MementoList

local model = require("Module:Data").model local memoize = require('Module:Memoize').memoize

local p = {}

local render_memento_icon = require('Module:Render/Memento')._icon local render_unit_icon = require('Module:Render/Unit')._icon local render_item_icon = require('Module:Render/Item')._icon local render_gear_icon = require('Module:Render/Gear')._icon local mementoExtra = mw.loadData("Module:Data/Extra/Memento") local linkUtils = require("Module:LinkUtils") local enums = require('Module:Data/Enums') local originConvert = memoize(enums.originFromKey) local maxItems = 500

local baseMementoFields = "_pageName,iname,rare,first_get_unit,icon,trust_artifact_counts,trust_artifact_names,trust_item_counts,trust_item_names,birth_id,concept_card_groups,leader_skill"

local skillPage = function(iname) return 'Data:Game/MasterParam/Skill/' .. string.gsub(iname, '_', ' ') end

local getSkillExpr = function(skill) if skill ==  then return  end return (model.getLoc(skillPage(skill), 'expr') or '') end

function p.mementoListItem(memData, mode) if mode == nil then mode = 'default' end

local tempOutput = {} table.insert(tempOutput, '|-') table.insert(tempOutput, '|'..render_memento_icon({memData.iname, data=memData, name='none'})) table.insert(tempOutput, '|['..linkUtils.acdbLink(memData.iname)..' '..model.getLoc(memData._pageName, 'name')..']') if mode == 'default' then table.insert(tempOutput, '|'..(memData.rare+1)) local trustOutput = {} if memData.trust_item_names then for i, tempItemName in ipairs(memData.trust_item_names) do               if memData.trust_item_counts[i] then table.insert(trustOutput, render_item_icon({tempItemName, size='small'})..' x '..memData.trust_item_counts[i]) end end end

if memData.trust_artifact_names then for i, tempGearName in ipairs(memData.trust_artifact_names) do               if memData.trust_artifact_counts[i] then table.insert(trustOutput, render_gear_icon({tempGearName, size='small'})..' x '..memData.trust_artifact_counts[i]) end end end table.insert(tempOutput, '|'..table.concat(trustOutput, ' ')) end table.insert(tempOutput, '|'..(originConvert(memData.birth_id) or 'Other'))

table.insert(tempOutput, '|'..((memData.leader_skill and getSkillExpr(memData.leader_skill)) or '')) if mode == 'default' then local mementoGroup = '' if (memData.concept_card_groups and memData.concept_card_groups[1]) then mementoGroup = enums.mementoGroupFromKey(memData.concept_card_groups[1]) or '' if mementoGroup ~= '' then mementoGroup = ..mementoGroup.. end end

table.insert(tempOutput, '|'..mementoGroup) end table.insert(tempOutput, '|'..(mementoExtra[memData.iname]['source'] or 'Unknown')) if mode == 'default' then if memData.first_get_unit and memData.first_get_unit ~= '' then table.insert(tempOutput, '|'..render_unit_icon({memData.first_get_unit, size='small'})) else table.insert(tempOutput, '| ') end end return { name = model.getLoc(memData._pageName, 'name'), output = table.concat(tempOutput, "\n") } end

function p.mementoListOutput(memList, mode) if mode == nil then mode = 'default' end

local sb = {}

table.insert(sb, '{| class="wikitable sortable frozen-header"') table.insert(sb, ' |-') if mode == 'leader' then table.insert(sb, ' ! Image !! Name !! Origin !! Leader Skill !! Source') else table.insert(sb, ' ! Image !! Name !! Rarity !! VCR !! Origin !! Leader Skill !! Memento Group !! Source !! Gives Unit') end local rows = {} for iname, mem in pairs(memList) do       table.insert(rows, p.mementoListItem(mem, mode)) end table.sort(rows, function(a, b) return a.name < b.name end)

for i, row in ipairs(rows) do       table.insert(sb, row.output) end table.insert(sb, "|}") return table.concat(sb, "\n") end

function p.releasedMementoList local memKeys = {} for iname, memData in pairs(mementoExtra) do       if memData['glDate'] then memKeys[#memKeys+1] = iname end end

local sf = string.format local quote = function(s) return sf('%q', s)   end local quotedInames = table.concat(memKeys, ','):gsub('[^,]+', quote) local memList = model.query("ConceptCard", baseMementoFields, {where = 'server = "gl" and iname IN ('..quotedInames..')', limit = maxItems}) or {} return p.mementoListOutput(memList) end

function p._mementoListByIname(mementoKeys, mode) local sf = string.format local quote = function(s) return sf('%q', s)   end local memList = model.query("ConceptCard", baseMementoFields, {where = 'server = "gl" and iname IN ('..table.concat(mementoKeys, ','):gsub('[^,]+', quote)..')', limit = maxItems}) or {} return p.mementoListOutput(memList, mode) end

function p.mementoListByIname(frame) local args = require('Module:Arguments').getArgs(frame, {       parentFirst = true,    }) if args[1] and args[2] then local mode = args[1] local mementoKeys = mw.text.split(args[2] or '', ',') return p._mementoListByIname(mementoKeys, mode) else return 'Invalid request' end end

return p