Magia Record English Wiki
Advertisement

Documentation for this module may be created at Module:TFF/doc

local p = {}
 
-- Returns a string as Girl_A_name,Girl_A_awaken_amount_of_item,Girl_A_magia_amount_of_item;Girl_B_name,Girl_B_awaken_amount_of_item,Girl_B_magia_amount_of_item; (...) 
-- The girls are listed in alphabetical order
-- Girls without the item in neither awaken nor magia slots are not included
 
 
function p.main(frame)
    local list_str = frame:expandTemplate{ title = frame.args['CharacterListSource1'] } .. ";" .. frame:expandTemplate{ title = frame.args['CharacterListSource2'] }
    local set = {} -- takes from both char lists so have to remove duplicates, done using a set
    for girl in (list_str .. ";"):gmatch("%s*([^;]*)%s*;") do
      set[girl] = true
    end
    local item_main = frame.args['item_jp']:gsub("'", "'"):lower() -- %p replaces ',' as , for some reason can't be gsubbed?
    local item_alt = frame.args['item_en']:gsub("'", "'"):lower() -- %p replaces ',' as , for some reason can't be gsubbed?
    return p.render(frame, set, item_main, item_alt)
end
 
function check(diff, capture, item, item_len)
    if diff >= item_len+4 and diff <= item_len+5 then -- check that the nr is either 1 or 2 digits long, and rest is same length as wanted item (light weak check, but tight)
        local capture_name, capture_nr = capture:match("^(.*)¤(.*)$") -- real check but heavier
        if capture_name:lower() == item then
            return tonumber(capture_nr)
        end
    end
end
 
function counter(frame, girl, item_m, item_a)
    local items, start, index, capture, nr
    local awaken_amount = 0
    local magia_amount = 0
    local item_m_len = item_m:len()
    local item_a_len = item_a:len()
    for rank=1,5 do
        -- Get items at the current rank and slot, add it to amount if is wanted item
        items = frame:expandTemplate{ title = 'Template:'.. girl .. ' Items', args = {'Get', 'Magia Item ' .. rank .. "/1", 'Magia quantity ' .. rank .. "/1", 'Item' .. rank .. 1, 'Quantity' .. rank .. 1, 'Magia Item ' .. rank .. "/2", 'Magia quantity ' .. rank .. "/2", 'Item' .. rank .. 2, 'Quantity' .. rank .. 2, 'Magia Item ' .. rank .. "/3", 'Magia quantity ' .. rank .. "/3", 'Item' .. rank .. 3, 'Quantity' .. rank .. 3, 'Magia Item ' .. rank .. "/4", 'Magia quantity ' .. rank .. "/4", 'Item' .. rank .. 4, 'Quantity' .. rank .. 4, 'Magia Item ' .. rank .. "/5", 'Magia quantity ' .. rank .. "/5", 'Item' .. rank .. 5, 'Quantity' .. rank .. 5, 'Magia Item ' .. rank .. "/6", 'Magia quantity ' .. rank .. "/6", 'Item' .. rank .. 6, 'Quantity' .. rank .. 6}}
 
        start, index, capture = string.find(items, "^([^¤]*¤[^¤]*)¤")
        nr = check(index-start, capture, item_m, item_m_len)
        if nr ~= nil then
            magia_amount = magia_amount + nr
        end
        nr = check(index-start, capture, item_a, item_a_len)
        if nr ~= nil then
            magia_amount = magia_amount + nr
        end
        magia_switch = false
 
        while true do
            start, index, capture = string.find(items, "([^¤]*¤[^¤]*)¤", index+1)
            if start == nil then break end
            nr = check(index-start, capture, item_m, item_m_len)
            if nr ~= nil then
                if magia_switch then
                    magia_amount = magia_amount + nr
                else
                    awaken_amount = awaken_amount + nr
                end
            end
            nr = check(index-start, capture, item_a, item_a_len)
            if nr ~= nil then
                if magia_switch then
                    magia_amount = magia_amount + nr
                else
                    awaken_amount = awaken_amount + nr
                end
            end
            magia_switch = not magia_switch -- use to keep track if magia or awaken item by calling them alternatingly 
        end
 
        start, index, capture = string.find(items, "([^¤]*¤[^¤]*)$")
        nr = check(index-start+2, capture, item_m, item_m_len)
        if nr ~= nil then
            awaken_amount = awaken_amount + nr
        end
        nr = check(index-start+2, capture, item_a, item_a_len)
        if nr ~= nil then
            awaken_amount = awaken_amount + nr
        end
    end
    return awaken_amount, magia_amount
end

function p.render(frame, character_set, item_m, item_a)
    local amounts = {}
    for girl, _ in pairs(character_set) do
        result, awaken_amount, magia_amount = pcall(counter, frame, girl, item_m, item_a)
        if result then  -- If pcall returns not error
            if awaken_amount > 0 or magia_amount > 0 then
                table.insert(amounts, girl .. "," .. awaken_amount .. "," .. magia_amount .. "," .. awaken_amount + magia_amount)
            end
        else
            table.insert(amounts, girl .. ",Unknown,Could not,get items")
        end
    end
    table.sort(amounts)
 
    local output = amounts[1]
    for id= 2, #amounts do
        output = output .. ";" .. amounts[id]
    end
    return output
end
 
return p
Advertisement