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


function p.season_result(frame)
-- ================================================
    local season = frame.args.season
-- КЕШУВАННЯ
    local player = frame.args.player
-- ================================================


    -- Updated list of seasons with new entries
local page_cache = {}
    local season_titles = {
        "Перший_сезон", "Другий_сезон", "Третій_сезон",
        "Четвертий_сезон", "П'ятий_сезон", "Шостий_сезон",
        "Сьомий_сезон", "Восьмий_сезон", "Дев'ятий_сезон"
    }


    mw.log("Season index: ", season)
local function get_page_content(page_name)
     mw.log("Player name: ", player)
     if page_cache[page_name] then
 
        return page_cache[page_name]
     local season_title = season_titles[tonumber(season)]
    end
    mw.log("Season page title: ", season_title)
   
 
     local title = mw.title.new(page_name)
     if not season_title then
     if not title or not title.exists then
         return "??"
        page_cache[page_name] = nil
         return nil
     end
     end
   
    local content = title:getContent()
    page_cache[page_name] = content
    return content
end


    local title = mw.title.new(season_title)
-- ================================================
    local content = title and title:getContent()
-- АВТОМАТИЧНЕ МЕНЮ СЕКЦІЙ
-- ================================================


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
     if not content then
        mw.log("Error: Content not found for the season page.")
         return ""
         return "??"
     end
     end
 
   
     mw.log("Page content loaded, length: ", string.len(content))
    local sections = {}
    mw.log("Page content preview: ", mw.ustring.sub(content, 1, 500))
      
 
    for line in mw.ustring.gmatch(content, "[^\n]+") do
    -- Attempt to extract the "Рейтинг" section
        -- ТІЛЬКИ заголовки рівня 2: == Назва ==
    local rating_section = mw.ustring.match(content, "==%s*Рейтинг%s*==.-{|%s*class%s*=%s*\"wikitable sortable\"(.-)|}")
        if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
    if not rating_section then
            local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
        mw.log("Error: Rating section not found within the page content.")
           
        mw.log("Page content: ", mw.ustring.sub(content, 1, 1000)) -- Log more content for better debugging
            if section_name and section_name ~= "" then
         return "Rating section not found"
                -- Виконуємо шаблони щоб отримати реальний текст
                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
     end
 
   
     mw.log("Rating section extracted, length: ", string.len(rating_section))
    -- Будуємо HTML
     mw.log("Rating section preview: ", mw.ustring.sub(rating_section, 1, 500))
    local html_parts = {}
 
   
     -- Adjusted to fetch the player's position (column 1 "№") instead of points
     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;">')
    local pattern = "|%s*(%d+)%s*|%s*%[%[" .. mw.ustring.gsub(player, "([%(%)%.%-%+%[%]])", "%%%1") .. "%]%]"
      
    local direct_pattern = "|%s*(%d+)%s*|%s*" .. mw.ustring.gsub(player, "([%(%)%.%-%+%[%]])", "%%%1") .. "%s*"
    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 rank = mw.ustring.match(rating_section, pattern) or mw.ustring.match(rating_section, direct_pattern)
   
 
     for i, section in ipairs(sections) do
    if not rank then
        local border = ""
        mw.log("Error: Player not found in season rankings or regex failed.")
        if i < #sections then
         mw.log("Pattern used: ", pattern)
            border = "border-bottom:1px solid rgba(255,255,255,0.05);"
        mw.log("Direct pattern used: ", direct_pattern)
        end
        return "--"
       
        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
 
   
     mw.log("Player rank found: ", rank)
     table.insert(html_parts, '</div>')
     return rank
   
     return table.concat(html_parts, '\n')
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