Модуль:FetchData2: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 3: | Рядок 3: | ||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
-- ГОЛОВНИЙ ПОШУК | -- ГОЛОВНИЙ ПОШУК | ||
-- Індексація: 1=№, 2=Ім'я, 3=Значення, 4=Додатково | -- Індексація: 1=№, 2=Ім'я, 3=Значення, 4=Додатково | ||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
| Рядок 19: | Рядок 17: | ||
local player_pattern = "%[%[" .. escaped .. "%]%]" | local player_pattern = "%[%[" .. escaped .. "%]%]" | ||
-- 1. Вилучаємо вміст таблиці wikitable | -- 1. Вилучаємо вміст таблиці 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 | ||
-- 2. Знаходимо весь блок даних гравця (4 комірки) | -- 2. Знаходимо весь блок даних гравця (4 комірки: №, Ім'я, Значення, Додатково) | ||
-- | -- Шукаємо | <№> | [[Ім'я]] | <Значення> | <Додатково> | ||
local full_row_pattern = "([|]%s*.-%s*[|]%s*" .. player_pattern .. "%s*[|]%s*.-%s*[|]%s*.-%s*)" | 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) | local full_row_match = mw.ustring.match(table_content, full_row_pattern) | ||
| Рядок 33: | Рядок 31: | ||
local cells = {} | local cells = {} | ||
for cell in mw.ustring.gmatch(full_row_match, "[|](.-)") do | for cell in mw.ustring.gmatch(full_row_match, "[|](.-)") do | ||
local trimmed_cell = mw.text.trim(cell) | local trimmed_cell = mw.text.trim(cell) | ||
| Рядок 40: | Рядок 37: | ||
end | end | ||
end | end | ||
local result = cells[column_index] | local result = cells[column_index] | ||
| Рядок 47: | Рядок 42: | ||
-- 4. Прибираємо [[link|text]] і [[link]] | -- 4. Прибираємо [[link|text]] і [[link]] | ||
result = mw.ustring.gsub(result, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") | result = mw.ustring.gsub(result, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") | ||
result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1") | result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1") | ||
return mw.text.trim(result) | return mw.text.trim(result) | ||
| Рядок 76: | Рядок 71: | ||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
-- ПОЛЯ | -- ПОЛЯ (ОТРИМУЮТЬ ІМ'Я З frame.args.player) | ||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
function p.recruiter(frame) | function p.recruiter(frame) | ||
local name = | -- Беремо ім'я з параметра 'player' | ||
local raw = fetch_from_table("Гравці", name, 4) | local name = frame.args.player | ||
local raw = fetch_from_table("Гравці", name, 4) -- Колонка 4 | |||
if raw == "Відсутній" then return "Не вказано" end | if raw == "Відсутній" then return "Не вказано" end | ||
return raw or "Не вказано" | return raw or "Не вказано" | ||
| Рядок 88: | Рядок 83: | ||
function p.date_added(frame) | function p.date_added(frame) | ||
local name = | -- Беремо ім'я з параметра 'player' | ||
local raw = fetch_from_table("Гравці", name, 3) | local name = frame.args.player | ||
local raw = fetch_from_table("Гравці", name, 3) -- Колонка 3 | |||
return raw and date_with_days(raw) or "Невідомо" | return raw and date_with_days(raw) or "Невідомо" | ||
end | end | ||
function p.prize_pool(frame) | |||
function p. | -- Беремо ім'я з параметра 'player' | ||
local name = | local name = frame.args.player | ||
local raw = fetch_from_table("Призовий_фонд", name, 3) -- Колонка 3 | |||
local raw = fetch_from_table(" | |||
if raw then | if raw then | ||
raw = mw.ustring.gsub(raw, "[^%d%s]", "") | raw = mw.ustring.gsub(raw, "[^%d%s]", "") | ||
| Рядок 106: | Рядок 100: | ||
end | end | ||
function p.foundation(frame) | |||
function p. | -- Беремо ім'я з параметра 'player' | ||
local name = | local name = frame.args.player | ||
local raw = fetch_from_table("Фундація", name, 3) -- Колонка 3 | |||
local raw = fetch_from_table(" | |||
if raw then | if raw then | ||
raw = mw.ustring.gsub(raw, "[^%d%s]", "") | raw = mw.ustring.gsub(raw, "[^%d%s]", "") | ||
| Рядок 118: | Рядок 111: | ||
end | end | ||
function p.final(frame) | function p.final(frame) | ||
local name = | -- Беремо ім'я з параметра 'player' | ||
local name = frame.args.player | |||
local raw = fetch_from_table("Фіналіст", name, 3) | local raw = fetch_from_table("Фіналіст", name, 3) -- Колонка 3 | ||
local count = raw and mw.ustring.match(raw, "(%d+)") | local count = raw and mw.ustring.match(raw, "(%d+)") | ||
Версія за 00:30, 24 листопада 2025
Документацію для цього модуля можна створити у Модуль:FetchData2/документація
local p = {}
----------------------------------------------------------------------
-- ГОЛОВНИЙ ПОШУК
-- Індексація: 1=№, 2=Ім'я, 3=Значення, 4=Додатково
----------------------------------------------------------------------
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 комірки: №, Ім'я, Значення, Додатково)
-- Шукаємо | <№> | [[Ім'я]] | <Значення> | <Додатково>
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
local result = cells[column_index]
if not result then return nil end
-- 4. Прибираємо [[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
-- Цільова дата: 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
----------------------------------------------------------------------
-- ПОЛЯ (ОТРИМУЮТЬ ІМ'Я З frame.args.player)
----------------------------------------------------------------------
function p.recruiter(frame)
-- Беремо ім'я з параметра 'player'
local name = frame.args.player
local raw = fetch_from_table("Гравці", name, 4) -- Колонка 4
if raw == "Відсутній" then return "Не вказано" end
return raw or "Не вказано"
end
function p.date_added(frame)
-- Беремо ім'я з параметра 'player'
local name = frame.args.player
local raw = fetch_from_table("Гравці", name, 3) -- Колонка 3
return raw and date_with_days(raw) or "Невідомо"
end
function p.prize_pool(frame)
-- Беремо ім'я з параметра 'player'
local name = frame.args.player
local raw = fetch_from_table("Призовий_фонд", name, 3) -- Колонка 3
if raw then
raw = mw.ustring.gsub(raw, "[^%d%s]", "")
raw = mw.text.trim(raw)
end
return (raw or "0") .. " ₴"
end
function p.foundation(frame)
-- Беремо ім'я з параметра 'player'
local name = frame.args.player
local raw = fetch_from_table("Фундація", name, 3) -- Колонка 3
if raw then
raw = mw.ustring.gsub(raw, "[^%d%s]", "")
raw = mw.text.trim(raw)
end
return (raw or "0") .. " ₴"
end
function p.final(frame)
-- Беремо ім'я з параметра 'player'
local name = frame.args.player
local raw = fetch_from_table("Фіналіст", name, 3) -- Колонка 3
local count = raw and mw.ustring.match(raw, "(%d+)")
if count then
return count .. "/9"
else
return "0/9"
end
end
return p