Модуль:FetchData2: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 1: | Рядок 1: | ||
local p = {} | local p = {} | ||
-- | ---------------------------------------------------------------------- | ||
-- ГОЛОВНИЙ ПОШУК | |||
-- Шукає у таблиці рядок, де друга колонка = player_name | |||
-- Повертає значення з column_index того ж самого рядка | |||
---------------------------------------------------------------------- | |||
local function fetch_from_table(page_title, player_name, column_index) | |||
local function | |||
local title = mw.title.new(page_title) | local title = mw.title.new(page_title) | ||
if not title then return nil end | |||
if not content then | local content = title:getContent() | ||
if not content then return nil end | |||
end | |||
-- Беремо всю таблицю {| … |} | |||
local table_content = mw.ustring.match(content, "{|.-|}") | |||
if not table_content then return nil end | |||
-- | -- Escape для імен з дужками/крапками | ||
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") | |||
local | |||
-- Патерн для 2-ї колонки: | |||
local | -- | [[Ім’я]] | ||
local player_pattern = "%[%[" .. escaped .. "%]%]" | |||
local target_row = nil | |||
local | |||
-- Шукаємо рядок, який містить [[Name]] у другій колонці | |||
for line in mw.ustring.gmatch(table_content, "[^\n]+") do | |||
-- Рядки таблиці починаються з | | |||
if mw.ustring.match(line, "^%s*|") and mw.ustring.find(line, player_pattern) then | |||
target_row = line | |||
break | |||
end | end | ||
end | end | ||
if not target_row then return nil end | |||
-- | ------------------------------------------------------------------ | ||
-- | -- Зчищаємо "|-" і розбиваємо на | cell | ||
------------------------------------------------------------------ | |||
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 | end | ||
if result then | local result = cells[column_index] | ||
if not result then return nil end | |||
------------------------------------------------------------------ | |||
-- Прибираємо [[link|text]] і [[link]] | |||
------------------------------------------------------------------ | |||
result = mw.ustring.gsub(result, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") | |||
result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1") | |||
return mw.text.trim(result) | |||
end | end | ||
---------------------------------------------------------------------- | |||
-- Рахує різницю в днях між датою та 25.10.2024 | |||
---------------------------------------------------------------------- | |||
if not | 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)") | |||
local | if not d then return dateString end | ||
if not | |||
local | local start = os.time({day = d, month = m, year = y}) | ||
local finish = os.time({day = 25, month = 10, year = 2024}) | |||
local | local diff = math.floor((finish - start) / 86400) | ||
return string.format("%s (%d днів)", dateString, diff) | |||
return string.format("%s (%d | |||
end | end | ||
---------------------------------------------------------------------- | |||
-- ПОЛЯ | |||
-- Всі функції беруть PAGENAME та шукають його в 2-й колонці таблиці | |||
---------------------------------------------------------------------- | |||
function p.recruiter(frame) | function p.recruiter(frame) | ||
local | local name = mw.title.getCurrentTitle().text | ||
return fetch_from_table("Гравці", name, 4) or "Невідомо" | |||
end | end | ||
function p. | function p.date_added(frame) | ||
local | local name = mw.title.getCurrentTitle().text | ||
local raw = fetch_from_table("Гравці", name, 3) | |||
local | return raw and date_with_days(raw) or "Невідомо" | ||
end | end | ||
function p.foundation(frame) | function p.foundation(frame) | ||
local | local name = mw.title.getCurrentTitle().text | ||
return fetch_from_table("Фундація", name, 2) or "0" | |||
end | |||
function p.prize_pool(frame) | |||
local name = mw.title.getCurrentTitle().text | |||
return fetch_from_table("Призовий_фонд", name, 2) or "0" | |||
return | |||
end | end | ||
function p.final(frame) | function p.final(frame) | ||
local | local name = mw.title.getCurrentTitle().text | ||
return fetch_from_table("Фіналіст", name, 2) or "Ні" | |||
end | end | ||
return p | return p | ||
Версія за 00:23, 24 листопада 2025
Документацію для цього модуля можна створити у Модуль: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
-- Беремо всю таблицю {| … |}
local table_content = mw.ustring.match(content, "{|.-|}")
if not table_content then return nil end
-- Escape для імен з дужками/крапками
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
-- Патерн для 2-ї колонки:
-- | [[Ім’я]]
local player_pattern = "%[%[" .. escaped .. "%]%]"
local target_row = nil
-- Шукаємо рядок, який містить [[Name]] у другій колонці
for line in mw.ustring.gmatch(table_content, "[^\n]+") do
-- Рядки таблиці починаються з |
if mw.ustring.match(line, "^%s*|") and mw.ustring.find(line, player_pattern) then
target_row = line
break
end
end
if not target_row then return nil end
------------------------------------------------------------------
-- Зчищаємо "|-" і розбиваємо на | cell
------------------------------------------------------------------
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|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
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
----------------------------------------------------------------------
-- ПОЛЯ
-- Всі функції беруть PAGENAME та шукають його в 2-й колонці таблиці
----------------------------------------------------------------------
function p.recruiter(frame)
local name = mw.title.getCurrentTitle().text
return fetch_from_table("Гравці", name, 4) 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
function p.foundation(frame)
local name = mw.title.getCurrentTitle().text
return fetch_from_table("Фундація", name, 2) or "0"
end
function p.prize_pool(frame)
local name = mw.title.getCurrentTitle().text
return fetch_from_table("Призовий_фонд", name, 2) or "0"
end
function p.final(frame)
local name = mw.title.getCurrentTitle().text
return fetch_from_table("Фіналіст", name, 2) or "Ні"
end
return p