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

local p = {}

----------------------------------------------------------------------
-- ГОЛОВНИЙ ПОШУК
-- Індексація: 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 комірки: №, Ім'я, Значення, Додатково)
    -- Шукаємо | <№> | [[Ім'я]] | <Значення> | <Додатково>
    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
    
    local result = cells[column_index]
    if not result then return nil end

    -- 4. Прибираємо [[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

    -- Цільова дата: 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


----------------------------------------------------------------------
-- ПОЛЯ (ОТРИМУЮТЬ ІМ'Я З frame.args.player)
----------------------------------------------------------------------

function p.recruiter(frame)
    -- Беремо ім'я з параметра 'player'
    local name = frame.args.player
    local raw = fetch_from_table("Гравці", name, 4) -- Колонка 4
    if raw == "Відсутній" then return "Не вказано" end
    return raw or "Не вказано"
end

function p.date_added(frame)
    -- Беремо ім'я з параметра 'player'
    local name = frame.args.player
    local raw = fetch_from_table("Гравці", name, 3) -- Колонка 3
    return raw and date_with_days(raw) or "Невідомо"
end

function p.prize_pool(frame)
    -- Беремо ім'я з параметра 'player'
    local name = frame.args.player
    local raw = fetch_from_table("Призовий_фонд", name, 3) -- Колонка 3
    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)
    -- Беремо ім'я з параметра 'player'
    local name = frame.args.player
    local raw = fetch_from_table("Фундація", name, 3) -- Колонка 3
    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)
    -- Беремо ім'я з параметра 'player'
    local name = frame.args.player
    local raw = fetch_from_table("Фіналіст", name, 3) -- Колонка 3
    
    local count = raw and mw.ustring.match(raw, "(%d+)")
    
    if count then
        return count .. "/9"
    else
        return "0/9"
    end
end

return p