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

нема опису редагування
Немає опису редагування
Немає опису редагування
Рядок 1: Рядок 1:
local p = {}
local p = {}


--------------------------------------------------------------------
----------------------------------------------------------------------
-- UNIVERSAL TABLE PARSER
-- Функція: шукає у таблиці рядок, де друга колонка = player_name
--------------------------------------------------------------------
-- Повертає дані з column_index того ж рядка
----------------------------------------------------------------------
 
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)
Рядок 11: Рядок 13:
     if not content then return nil end
     if not content then return nil end


    -- Беремо будь-яку 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


     -- Escape special chars
     -- Обробляємо можливі варіанти написання імені в таблиці
     local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
     local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
 
     local player_pattern =
    -- Патерн для пошуку імені
     local name_pattern =
         "%[%[" .. escaped .. "%]%]" ..
         "%[%[" .. escaped .. "%]%]" ..
         "|%[%[" .. escaped .. "|.-%]%]" ..
         "|%[%[" .. escaped .. "|.-%]%]" ..
         "|" .. escaped
         "|" .. escaped


    -- Знаходимо рядок
     local target_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, name_pattern) then
         if mw.ustring.match(line, player_pattern) then
             row = line
             target_row = line
             break
             break
         end
         end
     end
     end


     if not row then return nil end
     if not target_row then return nil end


     -- Чистимо від |-
     -- очищення "|-"
     row = mw.ustring.gsub(row, "^%s*|%-", "")
     target_row = mw.ustring.gsub(target_row, "^%s*|%-", "")


     -- Розбиваємо комірки
     -- розбиття на комірки
     local cells = {}
     local cells = {}
     for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do
     for cell in mw.ustring.gmatch(target_row, "%s*|%s*([^|]+)") do
         table.insert(cells, mw.text.trim(cell))
         table.insert(cells, mw.text.trim(cell))
     end
     end
Рядок 47: Рядок 46:
     if not result then return nil end
     if not result then return nil end


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


     return mw.text.trim(result)
     return result
end
end


--------------------------------------------------------------------
-- PUBLIC FUNCTIONS
--------------------------------------------------------------------


-- 1) Дата приєднання
----------------------------------------------------------------------
function p.join_date(frame)
-- Рахує різницю в днях між датою у таблиці та 25.10.2024
     local name = frame.args[1]
----------------------------------------------------------------------
     return fetch_data_from_table("Гравці", name, 3) or "Невідомо"
 
local function getDaysDifference(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
 
    local start = os.time({day = d, month = m, year = y})
    local finish = os.time({day = 25, month = 10, year = 2024})
 
     local diff = math.floor((finish - start) / 86400)
     return string.format("%s (%d днів)", dateString, diff)
end
end


-- 2) Провідник
 
function p.leader(frame)
----------------------------------------------------------------------
     local name = frame.args[1]
-- ДАНІ З ТАБЛИЦІ "Гравці"
     return fetch_data_from_table("Гравці", name, 4) or "Відсутній"
----------------------------------------------------------------------
 
function p.recruiter(frame)
     local name = mw.title.getCurrentTitle().text
     return fetch_data_from_table("Гравці", name, 4) or "Невідомо"
end
end


-- 3) Фундація
function p.date_added(frame)
function p.fund(frame)
     local name = mw.title.getCurrentTitle().text
     local name = frame.args[1]
     local raw_date = fetch_data_from_table("Гравці", name, 3)
     return fetch_data_from_table("Фундація", name, 2) or "0"
    return raw_date and getDaysDifference(raw_date) or "Невідомо"
end
end


-- 4) Призовий фонд
 
function p.prize(frame)
----------------------------------------------------------------------
     local name = frame.args[1]
-- ДАНІ З ТАБЛИЦІ "Призовий_фонд"
     return fetch_data_from_table("Призовий_фонд", name, 2) or "0"
----------------------------------------------------------------------
 
function p.prize_pool(frame)
     local name = mw.title.getCurrentTitle().text
     return fetch_data_from_table("Призовий_фонд", name, 3) or "0"
end
end


-- 5) Фіналіст
 
function p.finalist(frame)
----------------------------------------------------------------------
     local name = frame.args[1]
-- ДАНІ З ТАБЛИЦІ "Фундація"
     return fetch_data_from_table("Фіналіст", name, 2) or "Невідомо"
----------------------------------------------------------------------
 
function p.foundation(frame)
     local name = mw.title.getCurrentTitle().text
     return fetch_data_from_table("Фундація", name, 2) or "0"
end
end


-- 6) Титул
 
function p.title(frame)
----------------------------------------------------------------------
     local name = frame.args[1]
-- ДАНІ З ТАБЛИЦІ "Фіналіст"
     return fetch_data_from_table("Гравці", name, 5) or "Відсутній"
----------------------------------------------------------------------
 
function p.final(frame)
     local name = mw.title.getCurrentTitle().text
     return fetch_data_from_table("Фіналіст", name, 2) or "Ні"
end
end


--------------------------------------------------------------------
 
-- EXPORT MODULE
--------------------------------------------------------------------
return p
return p