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


-- [[25.10.2024]] для розрахунку днів
-- ================================================
local TARGET_TIMESTAMP = os.time({year=2024, month=10, day=25, hour=0, min=0, sec=0})
-- КЕШУВАННЯ
-- ================================================


--- Функція для виведення помилки на сторінку
local page_cache = {}
local function error_output(context, error_details)
    -- !!! ЦЕ ВІДОБРАЖАЄТЬСЯ НА СТОРІНЦІ !!!
    return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Page: %s]</span>", context, error_details)
end


--- Видаляє вікі-посилання [[link|text]] і [[link]]
local function get_page_content(page_name)
local function clean_wikilinks(text)
     if page_cache[page_name] then
     if not text then return nil end
         return page_cache[page_name]
    -- [[link|text]] -> text
    text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
    -- [[link]] -> link
    text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
    return mw.text.trim(text)
end
 
 
----------------------------------------------------------------------
-- ГОЛОВНИЙ ПОШУК (Оптимізовано для горизонтальної розмітки `||`)
-- Індексація: 1=№, 2=Ім'я, 3=Значення, 4=Додатково (Провідник)
----------------------------------------------------------------------
 
local function fetch_from_table(page_title, player_name, column_index)
    if not player_name or player_name == "" then
        -- Це не повинно спрацювати, якщо шаблон передає PAGENAME
         return error_output("No Player Name", page_title)
     end
     end
      
      
     local title = mw.title.new(page_title)
     local title = mw.title.new(page_name)
     if not title or not title.exists then  
     if not title or not title.exists then
         return error_output("Page Missing", page_title)
        page_cache[page_name] = nil
         return nil
     end
     end
 
   
     local content = title:getContent()
     local content = title:getContent()
     if not content then return error_output("Content Read Fail", page_title) end
     page_cache[page_name] = content
 
     return content
    -- Регулярний вираз для пошуку таблиці ({| ... |})
end
    local table_content = mw.ustring.match(content, "{|.-([|].-.-[|]})")
     if not table_content then
        return error_output("Table Missing", page_title)
    end


    local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
-- ================================================
    local player_pattern = "%[%[" .. escaped .. "%]%]"
-- АВТОМАТИЧНЕ МЕНЮ СЕКЦІЙ
-- ================================================


    -- Патерн для горизонтальної розмітки:
function p.section_menu(frame)
    -- 1. Знаходимо початок рядка: "|%s*<№>"
     local page_name = frame.args.page or mw.title.getCurrentTitle().text
     -- 2. Знаходимо гравця: "||%s*[[Гравець]]"
     local content = get_page_content(page_name)
    -- 3. Захоплюємо наступні комірки: "|| (.+?) || (.+)" (Залежить від кількості колонок)
    --
    -- Для 4 колонок (Гравці): № || Name || Col3 || Col4
     local full_row_pattern_4col = string.format(
        "^[|]%s*.-%s*||%s*%s*||%s*(.-)%s*||%s*(.-)%s*$", -- Цей патерн занадто складний для багаторядкового пошуку
        player_pattern
    )
      
      
     -- Простіший патерн: шукаємо рядок, що починається з `|-`, містить `[[Гравець]]`, і захоплюємо його.
     if not content then
    -- Ми шукаємо весь рядок: | № || [[Гравець]] || Col3 || Col4
         return ""
   
    local row_match
   
    -- Ітерація по всіх рядках, що починаються з `|-`
    for row in mw.ustring.gmatch(table_content, "\n%s*|-[^%s]*\n(.-)") do
        -- Перевіряємо, чи містить рядок ім'я гравця у форматі [[Гравець]]
        if mw.ustring.find(row, player_pattern, 1, true) then
            row_match = row
            break
         end
     end
     end
    if not row_match then
        return nil -- Гравець не знайдений, повертаємо nil, щоб обробник вивів значення за замовчуванням
    end
    -- Розбиваємо знайдений рядок на комірки за роздільником `||`
    local cells = {}
    local start_pos = 1
   
    -- Перша комірка починається з `|`, наступні з `||`
      
      
    -- 1. Витягуємо першу комірку (№), яка починається з `|`
     local sections = {}
     local first_cell_match = mw.ustring.match(row_match, "^%s*[|]%s*(.-)%s*||")
    if first_cell_match then
        table.insert(cells, first_cell_match)
        start_pos = mw.ustring.find(row_match, first_cell_match, 1, true) + mw.ustring.len(first_cell_match)
        -- Переходимо до пошуку наступних комірок
    else
        -- Якщо гравець знайдений, але рядок не вдалося розбити, це помилка структури
        return error_output("Row Split Error", page_title .. " for " .. player_name)
    end
 
    -- 2. Витягуємо решту комірок (розділені ||)
    local cell_count = 1
      
      
    -- Використовуємо gmatch для пошуку всіх елементів між `||` або до кінця рядка
     for line in mw.ustring.gmatch(content, "[^\n]+") do
     for cell in mw.ustring.gmatch(row_match, "||%s*(.-)") do
         -- ТІЛЬКИ заголовки рівня 2: == Назва ==
         -- Ми хочемо витягнути лише ті комірки, які знаходяться до наступного `||` або кінця
         if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
         local trimmed_cell = mw.text.trim(mw.ustring.match(cell, "^(.-)%s*(||.*|-$)"), " |") -- Тут має бути логіка, що захоплює вміст між ||
             local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
       
        -- Простіше рішення: розбиваємо рядок за '||'
        local parts = {}
        for part in mw.ustring.gmatch(row_match, "(.-)||") do
             table.insert(parts, part)
        end
        -- Додаємо останню частину, якщо вона не має `||` в кінці
        if #parts > 0 and mw.ustring.sub(row_match, #row_match - 1) ~= '||' then
          table.insert(parts, mw.ustring.match(row_match, "||%s*(.*)$"))
        end
       
        -- Оскільки це складно і нестабільно, повертаємося до прямого пошуку за структурою
       
        -- Ми знаємо, що `player_pattern` знаходиться у другій колонці.
        -- Використовуємо патерн, що захоплює Col3 та Col4 (для таблиці "Гравці")
        local final_match_pattern
        local col_count = 0
       
        if page_title == "Гравці" then col_count = 4 end
        if page_title == "Фундація" then col_count = 3 end
        if page_title == "Призовий_фонд" then col_count = 3 end
        if page_title == "Фіналіст" then col_count = 3 end -- Тут 3, бо нам не потрібні всі колонки I, II, III...
       
        if col_count >= 4 then
            -- Патерн для Гравці (4 колонки): | № || [[Player]] || DATE (Col3) || REC (Col4)
            final_match_pattern = "[|]%s*.-%s*||%s*" .. player_pattern .. "%s*||%s*(.-)%s*||%s*(.-)%s*$"
            local col3, col4 = mw.ustring.match(row_match, final_match_pattern)
            cells = {nil, nil, col3, col4} -- № та [[Player]] ігноруємо
              
              
        elseif col_count >= 3 then
            if section_name and section_name ~= "" then
            -- Патерн для Фундація/Призовий_фонд: | № || [[Player]] || AMOUNT (Col3)
                -- Виконуємо шаблони щоб отримати реальний текст
            final_match_pattern = "[|]%s*.-%s*||%s*" .. player_pattern .. "%s*||%s*(.-)%s*$"
                local processed = frame:preprocess(section_name)
            local col3 = mw.ustring.match(row_match, final_match_pattern)
                processed = mw.text.trim(processed)
            cells = {nil, nil, col3}
               
        end
                -- Anchor
       
                local anchor = mw.ustring.gsub(processed, " ", "_")
        -- Якщо ми не знайшли за структурою:
               
        if #cells == 0 or not cells[column_index] then
                if processed ~= "" then
             return nil
                    table.insert(sections, {
                        display = processed,
                        anchor = anchor
                    })
                end
             end
         end
         end
     end
     end
      
      
     local result = cells[column_index]
     if #sections == 0 then
   
         return ""
    if not result then
         return nil
     end
     end
    return clean_wikilinks(result)
end
----------------------------------------------------------------------
-- ФУНКЦІЇ ДЛЯ {{MCC Player New}}
----------------------------------------------------------------------
-- [1-2. Провідник/Дата приєднання]: Колонка 3 (Дата), Колонка 4 (Провідник)
function p.recruiter(frame)
    local name = frame.args.player
    local raw = fetch_from_table("Гравці", name, 4)
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
      
      
     if raw == "Відсутній" or raw == "-" then return "Не вказано" end
     -- Будуємо HTML
    return raw or "Не вказано"
     local html_parts = {}
end
 
function p.date_added(frame)
    local name = frame.args.player
    local raw = fetch_from_table("Гравці", name, 3)
   
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
 
    return raw and date_with_days(raw) or "Невідомо"
end
 
-- [3. Призовий фонд]: Колонка 3
function p.prize_pool(frame)
    local name = frame.args.player
    local raw = fetch_from_table("Призовий_фонд", name, 3)
 
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
 
    if raw then
        raw = mw.ustring.gsub(raw, "[^%d%s]", "")
        raw = mw.text.trim(raw)
    end
    return (raw or "0") .. " ₴"
end
 
-- [4. Фундація]: Колонка 3
function p.foundation(frame)
     local name = frame.args.player
    local raw = fetch_from_table("Фундація", name, 3)
 
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
      
      
     if raw then
     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;">')
        raw = mw.ustring.gsub(raw, "[^%d%s]", "")
        raw = mw.text.trim(raw)
    end
    return (raw or "0") .. " ₴"
end
 
-- [5. Фіналіст]: Колонка 3 (загальна кількість фіналів)
function p.final(frame)
    local name = frame.args.player
      
      
     -- Припускаємо, що таблиця Фіналіст має 3 колонки: № || [[Player]] || TOTAL
     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>')
    local raw = fetch_from_table("Фіналіст", name, 3)
      
      
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
     for i, section in ipairs(sections) do
   
        local border = ""
    local count = raw and mw.ustring.match(raw, "(%d+)")
        if i < #sections then
   
            border = "border-bottom:1px solid rgba(255,255,255,0.05);"
    if count then
        end
         return count .. "/9"
       
    else
         table.insert(html_parts, string.format(
         return "0/9"
            '<div class="l-box-item" style="padding:12px 20px; %s">[[#%s|%s]]</div>',
            border, section.anchor, section.display
         ))
     end
     end
end
-- [6. Результат сезону]
function p.season_result(frame)
    local season = frame.args.season
    local player = frame.args.player
    if not player or player == "" then return error_output("No Player Name", "Season") end
      
      
     local season_titles = {
     table.insert(html_parts, '</div>')
        "Перший_сезон", "Другий_сезон", "Третій_сезон",
        "Четвертий_сезон", "П'ятий_сезон", "Шостий_сезон",
        "Сьомий_сезон", "Восьмий_сезон", "Дев'ятий_сезон"
    }
      
      
     local season_title = season_titles[tonumber(season)]
     return table.concat(html_parts, '\n')
 
    if not season_title then return error_output("Invalid Season Index", season) end
   
    local title = mw.title.new(season_title)
    if not title or not title.exists then return error_output("Season Page Missing", season_title) end
   
    local content = title:getContent()
    if not content then return error_output("Season Content Read Fail", season_title) end
 
    -- Шукаємо секцію "Рейтинг" та таблицю
    -- Припускаємо, що "Рейтинг" - це розділ, після якого йде таблиця
    local rating_section = mw.ustring.match(content, "==%s*Рейтинг%s*==.-{|%s*class%s*=%s*\"wikitable sortable\"(.-)|}")
    if not rating_section then
        return error_output("Rating Section Missing", season_title)
    end
 
    -- Патерн для пошуку позиції (перша колонка) гравця (друга колонка) у форматі `| № || [[Гравець]]`
    local escaped_player = mw.ustring.gsub(player, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
   
    -- Шукаємо: | <№> || [[Player]]
    local pattern = "[|]%s*(%d+)%s*||%s*%[%[" .. escaped_player .. "%]%]"
   
    local rank = mw.ustring.match(rating_section, pattern)
   
    if not rank then
        return "--"
    end
 
    return rank
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