Немає опису редагування
Немає опису редагування
Рядок 4: Рядок 4:
local TARGET_TIMESTAMP = os.time({year=2024, month=10, day=25, hour=0, min=0, sec=0})  
local TARGET_TIMESTAMP = os.time({year=2024, month=10, day=25, hour=0, min=0, sec=0})  


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


Рядок 20: Рядок 22:
end
end


-- Рахує різницю в днях між датою та 25.10.2024
local function date_with_days(dateString)
    if not dateString or mw.ustring.find(dateString, "Error") 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 = tonumber(d), month = tonumber(m), year = tonumber(y), hour = 0, min = 0, sec = 0})
    local finish = TARGET_TIMESTAMP
   
    local diff = math.floor((finish - start) / 86400)
    if diff < 0 then diff = 0 end
    return string.format("%s (%d д)", dateString, diff)
end


----------------------------------------------------------------------
----------------------------------------------------------------------
Рядок 28: Рядок 45:
local function fetch_from_table(page_title, player_name, column_index)
local function fetch_from_table(page_title, player_name, column_index)
     if not player_name or player_name == "" then
     if not player_name or player_name == "" then
        -- Це не повинно спрацювати, якщо шаблон передає PAGENAME
         return error_output("No Player Name", page_title)  
         return error_output("No Player Name", page_title)  
     end
     end
Рядок 40: Рядок 56:
     if not content then return error_output("Content Read Fail", page_title) end
     if not content then return error_output("Content Read Fail", page_title) end


     -- Регулярний вираз для пошуку таблиці ({| ... |})
     -- Регулярний вираз для пошуку вмісту таблиці ({| ... |})
     local table_content = mw.ustring.match(content, "{|.-([|].-.-[|]})")
     local table_content = mw.ustring.match(content, "{|.-([|].-.-[|]})")
     if not table_content then  
     if not table_content then  
Рядок 49: Рядок 65:
     local player_pattern = "%[%[" .. escaped .. "%]%]"
     local player_pattern = "%[%[" .. escaped .. "%]%]"


    -- Патерн для горизонтальної розмітки:
    -- 1. Знаходимо початок рядка: "|%s*<№>"
    -- 2. Знаходимо гравця: "||%s*[[Гравець]]"
    -- 3. Захоплюємо наступні комірки: "|| (.+?) || (.+)" (Залежить від кількості колонок)
    --
    -- Для 4 колонок (Гравці): № || Name || Col3 || Col4
    local full_row_pattern_4col = string.format(
        "^[|]%s*.-%s*||%s*%s*||%s*(.-)%s*||%s*(.-)%s*$", -- Цей патерн занадто складний для багаторядкового пошуку
        player_pattern
    )
   
    -- Простіший патерн: шукаємо рядок, що починається з `|-`, містить `[[Гравець]]`, і захоплюємо його.
    -- Ми шукаємо весь рядок: | № || [[Гравець]] || Col3 || Col4
   
     local row_match
     local row_match
      
      
Рядок 75: Рядок 77:


     if not row_match then
     if not row_match then
         return nil -- Гравець не знайдений, повертаємо nil, щоб обробник вивів значення за замовчуванням
         return nil -- Гравець не знайдений
     end
     end


    -- Розбиваємо знайдений рядок на комірки за роздільником `||`
     local cells = {}
     local cells = {}
     local start_pos = 1
     local final_match_pattern
    local col_count = 0
      
      
     -- Перша комірка починається з `|`, наступні з `||`
     -- Визначаємо очікувану кількість колонок у цільовій таблиці
    if page_title == "Гравці" then col_count = 4 end
    if page_title == "Фундація" or page_title == "Призовий_фонд" or page_title == "Фіналіст" then col_count = 3 end
      
      
     -- 1. Витягуємо першу комірку (№), яка починається з `|`
     if col_count == 4 then
    local first_cell_match = mw.ustring.match(row_match, "^%s*[|]%s*(.-)%s*||")
        -- Патерн для Гравці (4 колонки): | || [[Player]] || DATE (Col3) || REC (Col4)
     if first_cell_match then
        final_match_pattern = "[|]%s*.-%s*||%s*" .. player_pattern .. "%s*||%s*(.-)%s*||%s*(.-)%s*$"
         table.insert(cells, first_cell_match)
        local col3, col4 = mw.ustring.match(row_match, final_match_pattern)
         start_pos = mw.ustring.find(row_match, first_cell_match, 1, true) + mw.ustring.len(first_cell_match)
        cells = {nil, nil, col3, col4} -- Ігноруємо № (1) та [[Player]] (2)
         -- Переходимо до пошуку наступних комірок
       
     elseif col_count == 3 then
         -- Патерн для Фундація/Призовий_фонд/Фіналіст: | № || [[Player]] || VALUE (Col3)
         final_match_pattern = "[|]%s*.-%s*||%s*" .. player_pattern .. "%s*||%s*(.-)%s*$"
        local col3 = mw.ustring.match(row_match, final_match_pattern)
         cells = {nil, nil, col3}
     else
     else
         -- Якщо гравець знайдений, але рядок не вдалося розбити, це помилка структури
         -- Якщо ми не очікуємо цю сторінку, або не можемо визначити структуру
         return error_output("Row Split Error", page_title .. " for " .. player_name)
         return error_output("Unknown Table Structure", page_title)
    end
 
    -- 2. Витягуємо решту комірок (розділені ||)
    local cell_count = 1
   
    -- Використовуємо gmatch для пошуку всіх елементів між `||` або до кінця рядка
    for cell in mw.ustring.gmatch(row_match, "||%s*(.-)") do
        -- Ми хочемо витягнути лише ті комірки, які знаходяться до наступного `||` або кінця
        local trimmed_cell = mw.text.trim(mw.ustring.match(cell, "^(.-)%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
            -- Патерн для Фундація/Призовий_фонд: | № || [[Player]] || AMOUNT (Col3)
            final_match_pattern = "[|]%s*.-%s*||%s*" .. player_pattern .. "%s*||%s*(.-)%s*$"
            local col3 = mw.ustring.match(row_match, final_match_pattern)
            cells = {nil, nil, col3}
        end
       
        -- Якщо ми не знайшли за структурою:
        if #cells == 0 or not cells[column_index] then
            return nil
        end
     end
     end
      
      
Рядок 147: Рядок 107:
      
      
     if not result then
     if not result then
         return nil
        -- Це може означати, що ми очікували 4 колонки, але знайшли лише 3, і запит був на Col4
         return nil  
     end
     end


Рядок 155: Рядок 116:


----------------------------------------------------------------------
----------------------------------------------------------------------
-- ФУНКЦІЇ ДЛЯ {{MCC Player New}}
-- ФУНКЦІЇ ДЛЯ {{MCC Player New}} (без змін)
----------------------------------------------------------------------
----------------------------------------------------------------------


-- [1-2. Провідник/Дата приєднання]: Колонка 3 (Дата), Колонка 4 (Провідник)
function p.recruiter(frame)
function p.recruiter(frame)
     local name = frame.args.player
     local name = frame.args.player
     local raw = fetch_from_table("Гравці", name, 4)  
     local raw = fetch_from_table("Гравці", name, 4)  
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
   
     if raw == "Відсутній" or raw == "-" then return "Не вказано" end
     if raw == "Відсутній" or raw == "-" then return "Не вказано" end
     return raw or "Не вказано"
     return raw or "Не вказано"
Рядок 172: Рядок 130:
     local name = frame.args.player
     local name = frame.args.player
     local raw = fetch_from_table("Гравці", name, 3)  
     local raw = fetch_from_table("Гравці", name, 3)  
   
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
     return raw and date_with_days(raw) or "Невідомо"
     return raw and date_with_days(raw) or "Невідомо"
end
end


-- [3. Призовий фонд]: Колонка 3
function p.prize_pool(frame)
function p.prize_pool(frame)
     local name = frame.args.player
     local name = frame.args.player
     local raw = fetch_from_table("Призовий_фонд", name, 3)  
     local raw = fetch_from_table("Призовий_фонд", name, 3)  
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
     if raw then  
     if raw then  
         raw = mw.ustring.gsub(raw, "[^%d%s]", "")
         raw = mw.ustring.gsub(raw, "[^%d%s]", "")
Рядок 192: Рядок 145:
end
end


-- [4. Фундація]: Колонка 3
function p.foundation(frame)
function p.foundation(frame)
     local name = frame.args.player
     local name = frame.args.player
     local raw = fetch_from_table("Фундація", name, 3)
     local raw = fetch_from_table("Фундація", name, 3)
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
   
     if raw then  
     if raw then  
         raw = mw.ustring.gsub(raw, "[^%d%s]", "")
         raw = mw.ustring.gsub(raw, "[^%d%s]", "")
Рядок 206: Рядок 156:
end
end


-- [5. Фіналіст]: Колонка 3 (загальна кількість фіналів)
function p.final(frame)
function p.final(frame)
     local name = frame.args.player
     local name = frame.args.player
   
    -- Припускаємо, що таблиця Фіналіст має 3 колонки: № || [[Player]] || TOTAL
     local raw = fetch_from_table("Фіналіст", name, 3)
     local raw = fetch_from_table("Фіналіст", name, 3)
   
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
   
     local count = raw and mw.ustring.match(raw, "(%d+)")
     local count = raw and mw.ustring.match(raw, "(%d+)")
   
     if count then
     if count then
         return count .. "/9"
         return count .. "/9"
Рядок 224: Рядок 168:
end
end


-- [6. Результат сезону]
function p.season_result(frame)
function p.season_result(frame)
     local season = frame.args.season
     local season = frame.args.season
Рядок 247: Рядок 190:
     if not content then return error_output("Season Content Read Fail", season_title) end
     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\"(.-)|}")
     local rating_section = mw.ustring.match(content, "==%s*Рейтинг%s*==.-{|%s*class%s*=%s*\"wikitable sortable\"(.-)|}")
     if not rating_section then  
     if not rating_section then  
Рядок 254: Рядок 195:
     end
     end


    -- Патерн для пошуку позиції (перша колонка) гравця (друга колонка) у форматі `| № || [[Гравець]]`
     local escaped_player = mw.ustring.gsub(player, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
     local escaped_player = mw.ustring.gsub(player, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
      
      

Версія за 00:42, 24 листопада 2025

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

local p = {}

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

--- Функція для виведення помилки на сторінку (Виправлено рядок 57)
local function error_output(context, error_details)
    -- Якщо detail (error_details) дорівнює nil, замінюємо його на "N/A"
    local safe_details = error_details or "N/A" 
    
    return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Page: %s]</span>", context or "Unknown Context", safe_details)
end

--- Видаляє вікі-посилання [[link|text]] і [[link]]
local function clean_wikilinks(text)
    if not text then return nil end
    -- [[link|text]] -> text
    text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
    -- [[link]] -> link
    text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
    return mw.text.trim(text)
end

-- Рахує різницю в днях між датою та 25.10.2024
local function date_with_days(dateString)
    if not dateString or mw.ustring.find(dateString, "Error") 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 = tonumber(d), month = tonumber(m), year = tonumber(y), hour = 0, min = 0, sec = 0})
    local finish = TARGET_TIMESTAMP
    
    local diff = math.floor((finish - start) / 86400)
    if diff < 0 then diff = 0 end 

    return string.format("%s (%d д)", dateString, diff)
end

----------------------------------------------------------------------
-- ГОЛОВНИЙ ПОШУК (Оптимізовано для горизонтальної розмітки `||`)
-- Індексація: 1=№, 2=Ім'я, 3=Значення, 4=Додатково (Провідник)
----------------------------------------------------------------------

local function fetch_from_table(page_title, player_name, column_index)
    if not player_name or player_name == "" then
        return error_output("No Player Name", page_title) 
    end
    
    local title = mw.title.new(page_title)
    if not title or not title.exists then 
        return error_output("Page Missing", page_title)
    end

    local content = title:getContent()
    if not content then return error_output("Content Read Fail", page_title) 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 .. "%]%]"

    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

    if not row_match then
        return nil -- Гравець не знайдений
    end

    local cells = {}
    local final_match_pattern
    local col_count = 0
    
    -- Визначаємо очікувану кількість колонок у цільовій таблиці
    if page_title == "Гравці" then col_count = 4 end
    if page_title == "Фундація" or page_title == "Призовий_фонд" or page_title == "Фіналіст" then col_count = 3 end
    
    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} -- Ігноруємо № (1) та [[Player]] (2)
        
    elseif col_count == 3 then
        -- Патерн для Фундація/Призовий_фонд/Фіналіст: | № || [[Player]] || VALUE (Col3)
        final_match_pattern = "[|]%s*.-%s*||%s*" .. player_pattern .. "%s*||%s*(.-)%s*$"
        local col3 = mw.ustring.match(row_match, final_match_pattern)
        cells = {nil, nil, col3}
    else
        -- Якщо ми не очікуємо цю сторінку, або не можемо визначити структуру
        return error_output("Unknown Table Structure", page_title)
    end
    
    local result = cells[column_index]
    
    if not result then
        -- Це може означати, що ми очікували 4 колонки, але знайшли лише 3, і запит був на Col4
        return nil 
    end

    return clean_wikilinks(result)
end


----------------------------------------------------------------------
-- ФУНКЦІЇ ДЛЯ {{MCC Player New}} (без змін)
----------------------------------------------------------------------

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
    return raw or "Не вказано"
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

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

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 
        raw = mw.ustring.gsub(raw, "[^%d%s]", "")
        raw = mw.text.trim(raw)
    end
    return (raw or "0") .. " ₴"
end

function p.final(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
    local count = raw and mw.ustring.match(raw, "(%d+)")
    if count then
        return count .. "/9"
    else
        return "0/9"
    end
end

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 = {
        "Перший_сезон", "Другий_сезон", "Третій_сезон",
        "Четвертий_сезон", "П'ятий_сезон", "Шостий_сезон", 
        "Сьомий_сезон", "Восьмий_сезон", "Дев'ятий_сезон"
    }
    
    local season_title = season_titles[tonumber(season)]

    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

return p