Magia Record English Wiki
No edit summary
No edit summary
Line 67: Line 67:
 
function p.render(frame, character_set, item_m, item_a)
 
function p.render(frame, character_set, item_m, item_a)
 
output = item_m .. item_a
 
output = item_m .. item_a
for i=1, #character_set do
+
for girl, _ in pairs(arr) do
output = output .. "|" .. character_set[i]
+
output = output .. ";" .. girl
 
end
 
end
 
return output
 
return output

Revision as of 14:11, 30 June 2020

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 = {}
    for girl in (list_str .. ";"):gmatch("%s*([^;]*)%s*;") do
      set[girl] = true
    end
    local item_main = frame.args['item_jp']
    local item_alt = frame.args['item_en']
    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 capture_nr
        end
    end
end
 
function counter(frame, girl, item)
    local items, start, index, capture, nr
    local awaken_amount = 0
    local magia_amount = 0
    local item_len = item: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, item_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, item_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, item_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)
    output = item_m .. item_a
    for girl, _ in pairs(arr) do
        output = output .. ";" .. girl
    end
    return output
end
    
function p.render2(frame, character_set, item_m, item_a)
    
    local amounts = {}
    local item_m = item_m:gsub("&#39;", "'"):lower() -- %p replaces ',' as , for some reason can't be gsubbed?
    local item_a = item_a:gsub("&#39;", "'"):lower() -- %p replaces ',' as , for some reason can't be gsubbed?
    for girl in (list_str .. ";"):gmatch("%s*([^;]*)%s*;") do
        result, awaken_amount, magia_amount = pcall(counter, frame, girl, item)
        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