Модуль:FetchData2
Документацію для цього модуля можна створити у Модуль:FetchData2/документація
local p = {}
----------------------------------------------------------------------
-- ГОЛОВНИЙ ПОШУК
-- Шукає у таблиці рядок, де друга колонка = player_name
-- Повертає значення з column_index того ж самого рядка
----------------------------------------------------------------------
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
return p