Модуль:FetchData2: відмінності між версіями

нема опису редагування
Немає опису редагування
Немає опису редагування
Рядок 1: Рядок 1:
local p = {}
--------------------------------------------------------------------
-- UNIVERSAL TABLE PARSER
--------------------------------------------------------------------
local function fetch_data_from_table(page_title, player_name, column_index)
local function fetch_data_from_table(page_title, player_name, column_index)
     local title = mw.title.new(page_title)
     local title = mw.title.new(page_title)
Рядок 6: Рядок 11:
     if not content then return nil end
     if not content then return nil end


     -- Витягуємо будь-яку wikitable
     -- Беремо будь-яку wikitable
     local table_content = mw.ustring.match(content, "{|.-|}")
     local table_content = mw.ustring.match(content, "{|.-|}")
     if not table_content then return nil end
     if not table_content then return nil end


     -- Робимо імʼя безпечним у RegEx
     -- Escape special chars
     local escaped_player = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
     local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")


     -- Шукаємо у форматах:
     -- Патерн для пошуку імені
    -- [[Player]], [[Player|текст]], Player
     local name_pattern =
     local player_pattern =
         "%[%[" .. escaped .. "%]%]" ..
         "%[%[" .. escaped_player .. "%]%]" ..
         "|%[%[" .. escaped .. "|.-%]%]" ..
         "|%[%[" .. escaped_player .. "|.-%]%]" ..
         "|" .. escaped
         "|" .. escaped_player


     -- Знайдемо весь рядок, де є гравець
     -- Знаходимо рядок
     local row = nil
     local row = nil
     for line in mw.ustring.gmatch(table_content, "[^\n]+") do
     for line in mw.ustring.gmatch(table_content, "[^\n]+") do
         if mw.ustring.match(line, player_pattern) then
         if mw.ustring.match(line, name_pattern) then
             row = line
             row = line
             break
             break
         end
         end
     end
     end
     if not row then return nil end
     if not row then return nil end


     -- Забираємо все після "|-" якщо є
     -- Чистимо від |-
     row = mw.ustring.gsub(row, "^%s*|%-", "")
     row = mw.ustring.gsub(row, "^%s*|%-", "")


     -- Розбиваємо рядок на комірки
     -- Розбиваємо комірки
     local cells = {}
     local cells = {}
     for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do
     for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do
Рядок 39: Рядок 44:
     end
     end


    -- Вибираємо потрібну колонку
     local result = cells[column_index]
     local result = cells[column_index]
     if not result then return nil end
     if not result then return nil end


     -- Прибираємо вікі-посилання
     -- Забираємо вікі-лінки
     result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2")
     result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2")
    result = mw.text.trim(result)
     if result == "" then
     if result == "" then
         result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1")
         result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1")
     end
     end


     return result
     return mw.text.trim(result)
end
 
--------------------------------------------------------------------
-- PUBLIC FUNCTIONS
--------------------------------------------------------------------
 
-- 1) Дата приєднання
function p.join_date(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Гравці", name, 3) or "Невідомо"
end
 
-- 2) Провідник
function p.leader(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Гравці", name, 4) or "Відсутній"
end
 
-- 3) Фундація
function p.fund(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Фундація", name, 2) or "0"
end
 
-- 4) Призовий фонд
function p.prize(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Призовий_фонд", name, 2) or "0"
end
 
-- 5) Фіналіст
function p.finalist(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Фіналіст", name, 2) or "Невідомо"
end
 
-- 6) Титул
function p.title(frame)
    local name = frame.args[1]
    return fetch_data_from_table("Гравці", name, 5) or "Відсутній"
end
end
--------------------------------------------------------------------
-- EXPORT MODULE
--------------------------------------------------------------------
return p