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

local p = {}

----------------------------------------------------------------------
-- ГОЛОВНИЙ ПОШУК
-- Шукає у таблиці рядок, де друга колонка = player_name
-- Повертає значення з column_index того ж самого рядка
-- Індексація: 1=№, 2=Ім'я, 3=Значення, 4=Додатково
----------------------------------------------------------------------

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

    local content = title:getContent()
    if not content then return nil end

    -- Escape для імен з дужками/крапками
    local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
    local player_pattern = "%[%[" .. escaped .. "%]%]"

    -- 1. Вилучаємо вміст таблиці wikitable (все між {| і |})
    local table_content = mw.ustring.match(content, "{|.-([|].-.-[|]})")
    if not table_content then return nil end

    -- 2. Знаходимо весь блок даних гравця (4 комірки)
    -- Патерн, що захоплює послідовність 4 комірок, де друга містить ім'я гравця.
    local full_row_pattern = "([|]%s*.-%s*[|]%s*" .. player_pattern .. "%s*[|]%s*.-%s*[|]%s*.-%s*)"
    local full_row_match = mw.ustring.match(table_content, full_row_pattern)
    
    if not full_row_match then return nil end

    -- 3. Розділяємо захоплений блок на комірки
    local cells = {}
    
    -- Розділяємо блок по символу |
    for cell in mw.ustring.gmatch(full_row_match, "[|](.-)") do
        local trimmed_cell = mw.text.trim(cell)
        if trimmed_cell ~= "" then
             table.insert(cells, trimmed_cell)
        end
    end

    -- cells[1] = №, cells[2] = [[Гравець]], cells[3] = Дата/Сума, cells[4] = Провідник/Рахунок
    
    local result = cells[column_index]
    if not result then return nil end

    -- 4. Прибираємо [[link|text]] і [[link]]
    result = mw.ustring.gsub(result, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") -- [[link|text]] -> text
    result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1")          -- [[link]] -> link

    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

    -- Цільова дата: 25.10.2024
    local start = os.time({day = tonumber(d), month = tonumber(m), year = tonumber(y), hour = 0, min = 0, sec = 0})
    local finish = os.time({day = 25, month = 10, year = 2024, hour = 0, min = 0, sec = 0})
    
    local diff = math.floor((finish - start) / 86400)
    if diff < 0 then diff = 0 end 

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


----------------------------------------------------------------------
-- ПОЛЯ
----------------------------------------------------------------------

-- ТАБЛИЦЯ "ГРАВЦІ" (Індекс 3 та 4)
function p.recruiter(frame)
    local name = mw.title.getCurrentTitle().text
    local raw = fetch_from_table("Гравці", name, 4)
    if raw == "Відсутній" then return "Не вказано" end
    return raw 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

-- ТАБЛИЦЯ "ФУНДАЦІЯ" (Індекс 3)
function p.foundation(frame)
    local name = mw.title.getCurrentTitle().text
    -- Колонка 3 для суми
    local raw = fetch_from_table("Фундація", name, 3)
    -- Очищення від символів валют та пробілів (на випадок, якщо там 1000 ₴)
    if raw then 
        raw = mw.ustring.gsub(raw, "[^%d%s]", "")
        raw = mw.text.trim(raw)
    end
    return (raw or "0") .. " ₴"
end

-- ТАБЛИЦЯ "ПРИЗОВИЙ_ФОНД" (Індекс 3)
function p.prize_pool(frame)
    local name = mw.title.getCurrentTitle().text
    -- Колонка 3 для суми
    local raw = fetch_from_table("Призовий_фонд", name, 3)
    if raw then 
        raw = mw.ustring.gsub(raw, "[^%d%s]", "")
        raw = mw.text.trim(raw)
    end
    return (raw or "0") .. " ₴"
end

-- ТАБЛИЦЯ "ФІНАЛІСТ" (Індекс 3)
function p.final(frame)
    local name = mw.title.getCurrentTitle().text
    -- Колонка 3 для рахунку (наприклад, 3)
    local raw = fetch_from_table("Фіналіст", name, 3)
    
    -- Вилучаємо лише число (на випадок, якщо там написано "3/9")
    local count = raw and mw.ustring.match(raw, "(%d+)")
    
    if count then
        return count .. "/9"
    else
        return "0/9"
    end
end

return p