Модуль:FetchData2
Документацію для цього модуля можна створити у Модуль:FetchData2/документація
local p = {}
----------------------------------------------------------------------
-- Функція: шукає у таблиці рядок, де друга колонка = player_name
-- Повертає дані з column_index того ж рядка
----------------------------------------------------------------------
local function fetch_data_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
local table_content = mw.ustring.match(content, "{|.-|}")
if not table_content then return nil end
-- Обробляємо можливі варіанти написання імені в таблиці
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local player_pattern =
"%[%[" .. escaped .. "%]%]" ..
"|%[%[" .. escaped .. "|.-%]%]" ..
"|" .. escaped
local target_row = nil
for line in mw.ustring.gmatch(table_content, "[^\n]+") do
if mw.ustring.match(line, player_pattern) then
target_row = line
break
end
end
if not target_row then return nil end
-- очищення "|-"
target_row = mw.ustring.gsub(target_row, "^%s*|%-", "")
-- розбиття на комірки
local cells = {}
for cell in mw.ustring.gmatch(target_row, "%s*|%s*([^|]+)") do
table.insert(cells, mw.text.trim(cell))
end
local result = cells[column_index]
if not result then return nil end
-- прибираємо [[link]]
result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2")
result = mw.text.trim(result)
result = result ~= "" and result or nil
return result
end
----------------------------------------------------------------------
-- Рахує різницю в днях між датою у таблиці та 25.10.2024
----------------------------------------------------------------------
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
----------------------------------------------------------------------
-- ДАНІ З ТАБЛИЦІ "Гравці"
----------------------------------------------------------------------
function p.recruiter(frame)
local name = mw.title.getCurrentTitle().text
return fetch_data_from_table("Гравці", name, 4) or "Невідомо"
end
function p.date_added(frame)
local name = mw.title.getCurrentTitle().text
local raw_date = fetch_data_from_table("Гравці", name, 3)
return raw_date and getDaysDifference(raw_date) or "Невідомо"
end
----------------------------------------------------------------------
-- ДАНІ З ТАБЛИЦІ "Призовий_фонд"
----------------------------------------------------------------------
function p.prize_pool(frame)
local name = mw.title.getCurrentTitle().text
return fetch_data_from_table("Призовий_фонд", name, 3) or "0"
end
----------------------------------------------------------------------
-- ДАНІ З ТАБЛИЦІ "Фундація"
----------------------------------------------------------------------
function p.foundation(frame)
local name = mw.title.getCurrentTitle().text
return fetch_data_from_table("Фундація", name, 2) or "0"
end
----------------------------------------------------------------------
-- ДАНІ З ТАБЛИЦІ "Фіналіст"
----------------------------------------------------------------------
function p.final(frame)
local name = mw.title.getCurrentTitle().text
return fetch_data_from_table("Фіналіст", name, 2) or "Ні"
end
return p