Jump to content

Module:DigimonTab: Difference between revisions

From Untitled Monster Game
imported>Ziebster
No edit summary
imported>Ziebster
No edit summary
 
(58 intermediate revisions by the same user not shown)
Line 3: Line 3:
function p.show(frame)
function p.show(frame)
     local versionTag = frame.args[1] or (frame:getParent() and frame:getParent().args[1]) or "Error: No version tag"
     local versionTag = frame.args[1] or (frame:getParent() and frame:getParent().args[1]) or "Error: No version tag"
     local label = frame.args[2] or (frame:getParent() and frame:getParent().args[2]) or versionTag  -- Fallback to versionTag if no label provided
     local label = frame.args[2] or (frame:getParent() and frame:getParent().args[2]) or versionTag  -- Fallback to versionTag if no label
     local data = mw.loadData("Module:MasterList")
     local data = mw.loadData("Module:MasterList")
     local summonable, unsummonable = {}, {}
     local summonable, unsummonable = {}, {}
Line 9: Line 9:
     local has_refs = false
     local has_refs = false


-- Extracting the Battle Type from the individual page
    local title = mw.title.getCurrentTitle()
    local content = title:getContent()
    local battleTypeX = nil
    for match in content:gmatch('{{Category|(.-)}}') do
        local parts = mw.text.split(match, '|')
        if #parts >= 1 then
            local cat_name = mw.text.trim(parts[1])
            local match_x = cat_name:match('^Battle Type (.*)$')
            if match_x then
                battleTypeX = match_x
                break
            end
        end
    end
-- Fills in the array for the digimon based on their "Y","N","?", or "*"
     for _, digimon in ipairs(data) do
     for _, digimon in ipairs(data) do
         for i, v in ipairs(digimon.versions or {}) do
         for i, v in ipairs(digimon.ver or {}) do
             if v == versionTag then
             if v == versionTag then
                 local summonable_field = digimon.summonable
                 local summonable_field = digimon.sum
                 local status
                 local status
                 if type(summonable_field) == 'table' then
                 if type(summonable_field) == 'table' then
                     status = summonable_field[i] or "Yes"
                     status = nil
                    for j = i, 1, -1 do
                        if summonable_field[j] then
                            status = summonable_field[j]
                            break
                        end
                    end
                    if not status then
                        error("No summonable status defined for Digimon: " .. digimon.name)
                    end
                 else
                 else
                     status = summonable_field or "Yes"
                     status = summonable_field or "Y"
                 end
                 end
                 local entry = {
                 local entry = {
                    image = digimon.image and digimon.image ~= "" and "[[File:" .. digimon.image .. "|frameless|50x50px]]" or "",
                     name = "'''" .. digimon.name .. "'''"
                     name = "'''" .. digimon.name .. "'''"
                 }
                 }
                 if status == "''Yes''" then  
                local image_str = ""
                     entry.name = entry.name .. " <ref group=\"" .. label .. "\" name=\"multi-device-battle\">You need another device with a similar Battle Type in order to battle with this Digimon. This Digimon is ''ONLY'' Summonable with this device.</ref>"
                 if digimon.image and digimon.image ~= "" then
                     image_str = "[[File:" .. digimon.image .. "Icon.png|frameless]]"
                else
                    local processed_name = digimon.name
                    processed_name = processed_name:gsub("[^%a%d]", "")
                    image_str = "[[File:" .. processed_name .. "Icon.png|frameless]]"
                end
                entry.image = image_str
                if status == "*" then
                    entry.name = entry.name .. " [[#" .. label .. "|*]]"
                     has_refs = true
                     has_refs = true
                 end
                 end
                 if status == "Yes" or status == "''Yes''" then has_yes = true end
                 if status == "Y" or status == "*" then has_yes = true end
                 table.insert(status == "No" and unsummonable or summonable, entry)
                 table.insert(status == "N" and unsummonable or summonable, entry)
                 break
                 break
             end
             end
Line 34: Line 68:
     end
     end


    table.sort(summonable, function(a,b) return mw.text.unstripNoWiki(a.name) < mw.text.unstripNoWiki(b.name) end)
-- Builds the filled-in array into a flexbox grid
    table.sort(unsummonable, function(a,b) return mw.text.unstripNoWiki(a.name) < mw.text.unstripNoWiki(b.name) end)
 
     local function buildGrid(entries)
     local function buildGrid(entries)
         local rows = ""
         local items = ""
        local max_entries_per_row = 3  -- Limit to 2 entries per row (4 columns) to avoid scroll bars; increase to 3 if needed
         for _, entry in ipairs(entries) do
         for i = 1, #entries, max_entries_per_row do
             items = items .. '<div style="display: flex; align-items: center; gap: 5px; padding: 0px; width: 200px; flex: 0 0 200px;">' ..
             local cells = {}
                    entry.image .. '' .. entry.name .. '</div>\n'
            for j = 0, max_entries_per_row - 1 do
                local idx = i + j
                if idx > #entries then break end
                table.insert(cells, 'style="border:none;" | ' .. entries[idx].image)
                table.insert(cells, 'style="border:none;" | ' .. entries[idx].name)
            end
            if #cells > 0 then
                rows = rows .. "|-\n| " .. table.concat(cells, " || ") .. "\n"
            end
         end
         end
         return rows
         return items
     end
     end


-- Writes out the titles for the sections, then injects the flexbox grid respectively
     local summonable_title = (#summonable > 0 and has_yes) and "Summonable Digimon" or "Unimplemented Digimon"
     local summonable_title = (#summonable > 0 and has_yes) and "Summonable Digimon" or "Unimplemented Digimon"
     local out = (#summonable > 0 and "='''" .. summonable_title .. "'''=\n{| class=\"fandom-table\" style=\"border: none; border-collapse: collapse;\"\n" .. buildGrid(summonable) .. "|}\n" or "") ..
     local out = (#summonable > 0 and "=<span id=" .. label .. "S></span>'''" .. summonable_title .. "'''=\n<div style=\"display: flex; flex-wrap: wrap; gap: 8px; align-items: stretch;\">\n" .. buildGrid(summonable) .. "</div>\n" or "") ..
                 (#unsummonable > 0 and "='''Unsummonable Digimon'''=\n{| class=\"fandom-table\" style=\"border: none; border-collapse: collapse;\"\n" .. buildGrid(unsummonable) .. "|}\n" or (#summonable == 0 and "''No Digimon in this version.''\n" or ""))
                 (#unsummonable > 0 and "='''Unsummonable Digimon'''=\n<div style=\"display: flex; flex-wrap: wrap; gap: 8px; align-items: stretch;\">\n" .. buildGrid(unsummonable) .. "</div>\n" or (#summonable == 0 and "''No Digimon in this version.''\n" or ""))


-- Adds the ref line if necessary
     if has_refs then
     if has_refs then
         out = out .. "\n<references group=\"" .. label .. "\" />\n"
        local battleTypeText = battleTypeX and '[[:Category:Battle Type ' .. battleTypeX .. '|same Battle Type]]' or 'same Battle Type'
         out = out .. "\n:<span id=" .. label .. ">[[#" .. label .. "S|↑]]</span> <sub>You need another device with the " .. battleTypeText .. " in order to battle with this Digimon.  This Digimon '''''can be summoned''''' but '''''cannot battle''''' with this device.</sub>\n"
     end
     end


     return frame:preprocess(out)
     return out
end
end


return p
return p

Latest revision as of 08:12, 15 January 2026

Example Usage:

<tabber>
|-|Example=<div class="mobileonly"><h3>Example</h3></div>
{{#invoke:DigimonTab|show|Exam|Ex}}
</tabber>
  • Example = Version tabber label
  • Exam = Master List device tag
  • Ex = Footnote tag (if applicable)

local p = {}

function p.show(frame)
    local versionTag = frame.args[1] or (frame:getParent() and frame:getParent().args[1]) or "Error: No version tag"
    local label = frame.args[2] or (frame:getParent() and frame:getParent().args[2]) or versionTag  -- Fallback to versionTag if no label
    local data = mw.loadData("Module:MasterList")
    local summonable, unsummonable = {}, {}
    local has_yes = false
    local has_refs = false

-- Extracting the Battle Type from the individual page
    local title = mw.title.getCurrentTitle()
    local content = title:getContent()
    local battleTypeX = nil
    for match in content:gmatch('{{Category|(.-)}}') do
        local parts = mw.text.split(match, '|')
        if #parts >= 1 then
            local cat_name = mw.text.trim(parts[1])
            local match_x = cat_name:match('^Battle Type (.*)$')
            if match_x then
                battleTypeX = match_x
                break
            end
        end
    end

-- Fills in the array for the digimon based on their "Y","N","?", or "*"
    for _, digimon in ipairs(data) do
        for i, v in ipairs(digimon.ver or {}) do
            if v == versionTag then
                local summonable_field = digimon.sum
                local status
                if type(summonable_field) == 'table' then
                    status = nil
                    for j = i, 1, -1 do
                        if summonable_field[j] then
                            status = summonable_field[j]
                            break
                        end
                    end
                    if not status then
                        error("No summonable status defined for Digimon: " .. digimon.name)
                    end
                else
                    status = summonable_field or "Y"
                end
                local entry = {
                    name = "'''" .. digimon.name .. "'''"
                }
                local image_str = ""
                if digimon.image and digimon.image ~= "" then
                    image_str = "[[File:" .. digimon.image .. "Icon.png|frameless]]"
                else
                    local processed_name = digimon.name
                    processed_name = processed_name:gsub("[^%a%d]", "")
                    image_str = "[[File:" .. processed_name .. "Icon.png|frameless]]"
                end
                entry.image = image_str
                if status == "*" then 
                    entry.name = entry.name .. " [[#" .. label .. "|*]]"
                    has_refs = true
                end
                if status == "Y" or status == "*" then has_yes = true end
                table.insert(status == "N" and unsummonable or summonable, entry)
                break
            end
        end
    end

-- Builds the filled-in array into a flexbox grid
    local function buildGrid(entries)
        local items = ""
        for _, entry in ipairs(entries) do
            items = items .. '<div style="display: flex; align-items: center; gap: 5px; padding: 0px; width: 200px; flex: 0 0 200px;">' ..
                    entry.image .. '' .. entry.name .. '</div>\n'
        end
        return items
    end

-- Writes out the titles for the sections, then injects the flexbox grid respectively
    local summonable_title = (#summonable > 0 and has_yes) and "Summonable Digimon" or "Unimplemented Digimon"
    local out = (#summonable > 0 and "=<span id=" .. label .. "S></span>'''" .. summonable_title .. "'''=\n<div style=\"display: flex; flex-wrap: wrap; gap: 8px; align-items: stretch;\">\n" .. buildGrid(summonable) .. "</div>\n" or "") ..
                (#unsummonable > 0 and "='''Unsummonable Digimon'''=\n<div style=\"display: flex; flex-wrap: wrap; gap: 8px; align-items: stretch;\">\n" .. buildGrid(unsummonable) .. "</div>\n" or (#summonable == 0 and "''No Digimon in this version.''\n" or ""))

-- Adds the ref line if necessary
    if has_refs then
        local battleTypeText = battleTypeX and '[[:Category:Battle Type ' .. battleTypeX .. '|same Battle Type]]' or 'same Battle Type'
        out = out .. "\n:<span id=" .. label .. ">[[#" .. label .. "S|↑]]</span> <sub>You need another device with the " .. battleTypeText .. " in order to battle with this Digimon.  This Digimon '''''can be summoned''''' but '''''cannot battle''''' with this device.</sub>\n"
    end

    return out
end

return p