Немає опису редагування
Немає опису редагування
Мітка: Ручний відкіт
 
(Не показані 43 проміжні версії цього користувача)
Рядок 1: Рядок 1:
local p = {}
local p = {}


----------------------------------------------------------------------
-- ================================================
-- ГОЛОВНИЙ ПОШУК
-- КЕШУВАННЯ
-- Шукає у таблиці рядок, де друга колонка = player_name
-- ================================================
-- Повертає значення з column_index того ж самого рядка
----------------------------------------------------------------------


local function fetch_from_table(page_title, player_name, column_index)
local page_cache = {}
    local title = mw.title.new(page_title)
    if not title then return nil end


local function get_page_content(page_name)
    if page_cache[page_name] then
        return page_cache[page_name]
    end
   
    local title = mw.title.new(page_name)
    if not title or not title.exists then
        page_cache[page_name] = nil
        return nil
    end
   
     local content = title:getContent()
     local content = title:getContent()
     if not content then return nil end
     page_cache[page_name] = content
    return content
end


    -- Беремо всю таблицю {| … |}
-- ================================================
    local table_content = mw.ustring.match(content, "{|.-|}")
-- АВТОМАТИЧНЕ МЕНЮ СЕКЦІЙ
    if not table_content then return nil end
-- ================================================


    -- Escape для імен з дужками/крапками
function p.section_menu(frame)
     local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
     local page_name = frame.args.page or mw.title.getCurrentTitle().text
 
    local content = get_page_content(page_name)
     -- Патерн для 2-ї колонки:
      
     -- | [[Ім’я]]
     if not content then
    local player_pattern = "%[%[" .. escaped .. "%]%]"
        return ""
 
    end
     local target_row = nil
   
 
     local sections = {}
     -- Шукаємо рядок, який містить [[Name]] у другій колонці
      
     for line in mw.ustring.gmatch(table_content, "[^\n]+") do
     for line in mw.ustring.gmatch(content, "[^\n]+") do
         -- Рядки таблиці починаються з |
         -- ТІЛЬКИ заголовки рівня 2: == Назва ==
         if mw.ustring.match(line, "^%s*|") and mw.ustring.find(line, player_pattern) then
         if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
            target_row = line
            local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
             break
           
            if section_name and section_name ~= "" then
                -- Виконуємо шаблони щоб отримати реальний текст
                local processed = frame:preprocess(section_name)
                processed = mw.text.trim(processed)
               
                -- Anchor
                local anchor = mw.ustring.gsub(processed, " ", "_")
               
                if processed ~= "" then
                    table.insert(sections, {
                        display = processed,
                        anchor = anchor
                    })
                end
             end
         end
         end
     end
     end
 
   
     if not target_row then return nil end
     if #sections == 0 then
 
        return ""
     ------------------------------------------------------------------
    end
     -- Зчищаємо "|-" і розбиваємо на | cell
   
    ------------------------------------------------------------------
     -- Будуємо HTML
     target_row = mw.ustring.gsub(target_row, "^%s*|%-", "")
    local html_parts = {}
 
   
     local cells = {}
     table.insert(html_parts, '<div class="l-box" style="position:fixed; top:20px; left:20px; z-index:100; border-radius:16px; background-color:#23232c; width:220px; box-sizing:border-box; overflow:hidden;">')
    for cell in mw.ustring.gmatch(target_row, "%s*|%s*([^|]+)") do
   
        table.insert(cells, mw.text.trim(cell))
     table.insert(html_parts, '<div style="padding:15px 20px; font-weight:bold; font-size:15px; color:#fff; border-bottom:1px solid rgba(255,255,255,0.1);">Зміст</div>')
   
     for i, section in ipairs(sections) do
        local border = ""
        if i < #sections then
            border = "border-bottom:1px solid rgba(255,255,255,0.05);"
        end
       
        table.insert(html_parts, string.format(
            '<div class="l-box-item" style="padding:12px 20px; %s">[[#%s|%s]]</div>',
            border, section.anchor, section.display
        ))
     end
     end
 
      
    local result = cells[column_index]
     table.insert(html_parts, '</div>')
    if not result then return nil end
      
 
     return table.concat(html_parts, '\n')
    ------------------------------------------------------------------
    -- Прибираємо [[link|text]] і [[link]]
    ------------------------------------------------------------------
     result = mw.ustring.gsub(result, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
     result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1")
 
    return mw.text.trim(result)
end
 
 
----------------------------------------------------------------------
-- Рахує різницю в днях між датою та 25.10.2024
----------------------------------------------------------------------
 
local function date_with_days(dateString)
    if not dateString then return "" end
 
    local d, m, y = dateString:match("(%d%d)%.(%d%d)%.(%d%d%d%d)")
    if not d then return dateString end
 
    local start = os.time({day = d, month = m, year = y})
    local finish = os.time({day = 25, month = 10, year = 2024})
    local diff = math.floor((finish - start) / 86400)
 
    return string.format("%s (%d днів)", dateString, diff)
end
 
 
----------------------------------------------------------------------
-- ПОЛЯ
-- Всі функції беруть PAGENAME та шукають його в 2-й колонці таблиці
----------------------------------------------------------------------
 
function p.recruiter(frame)
     local name = mw.title.getCurrentTitle().text
     return fetch_from_table("Гравці", name, 4) or "Невідомо"
end
 
function p.date_added(frame)
    local name = mw.title.getCurrentTitle().text
    local raw = fetch_from_table("Гравці", name, 3)
    return raw and date_with_days(raw) or "Невідомо"
end
 
function p.foundation(frame)
    local name = mw.title.getCurrentTitle().text
    return fetch_from_table("Фундація", name, 2) or "0"
end
 
function p.prize_pool(frame)
    local name = mw.title.getCurrentTitle().text
    return fetch_from_table("Призовий_фонд", name, 2) or "0"
end
 
function p.final(frame)
    local name = mw.title.getCurrentTitle().text
    return fetch_from_table("Фіналіст", name, 2) or "Ні"
end
end


return p
return p

Поточна версія на 10:24, 8 грудня 2025

Документацію для цього модуля можна створити у Модуль:FetchData2/документація

local p = {}

-- ================================================
-- КЕШУВАННЯ
-- ================================================

local page_cache = {}

local function get_page_content(page_name)
    if page_cache[page_name] then
        return page_cache[page_name]
    end
    
    local title = mw.title.new(page_name)
    if not title or not title.exists then
        page_cache[page_name] = nil
        return nil
    end
    
    local content = title:getContent()
    page_cache[page_name] = content
    return content
end

-- ================================================
-- АВТОМАТИЧНЕ МЕНЮ СЕКЦІЙ
-- ================================================

function p.section_menu(frame)
    local page_name = frame.args.page or mw.title.getCurrentTitle().text
    local content = get_page_content(page_name)
    
    if not content then
        return ""
    end
    
    local sections = {}
    
    for line in mw.ustring.gmatch(content, "[^\n]+") do
        -- ТІЛЬКИ заголовки рівня 2: == Назва ==
        if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
            local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
            
            if section_name and section_name ~= "" then
                -- Виконуємо шаблони щоб отримати реальний текст
                local processed = frame:preprocess(section_name)
                processed = mw.text.trim(processed)
                
                -- Anchor
                local anchor = mw.ustring.gsub(processed, " ", "_")
                
                if processed ~= "" then
                    table.insert(sections, {
                        display = processed,
                        anchor = anchor
                    })
                end
            end
        end
    end
    
    if #sections == 0 then
        return ""
    end
    
    -- Будуємо HTML
    local html_parts = {}
    
    table.insert(html_parts, '<div class="l-box" style="position:fixed; top:20px; left:20px; z-index:100; border-radius:16px; background-color:#23232c; width:220px; box-sizing:border-box; overflow:hidden;">')
    
    table.insert(html_parts, '<div style="padding:15px 20px; font-weight:bold; font-size:15px; color:#fff; border-bottom:1px solid rgba(255,255,255,0.1);">Зміст</div>')
    
    for i, section in ipairs(sections) do
        local border = ""
        if i < #sections then
            border = "border-bottom:1px solid rgba(255,255,255,0.05);"
        end
        
        table.insert(html_parts, string.format(
            '<div class="l-box-item" style="padding:12px 20px; %s">[[#%s|%s]]</div>',
            border, section.anchor, section.display
        ))
    end
    
    table.insert(html_parts, '</div>')
    
    return table.concat(html_parts, '\n')
end

return p