Модуль:FetchData: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування |
Admin (обговорення | внесок) Немає опису редагування Мітка: Скасовано |
||
| Рядок 1: | Рядок 1: | ||
local p = {} | local p = {} | ||
-- | --- Функція для виведення помилки на сторінку | ||
local function error_output(context, error_details) | |||
local safe_details = error_details or "N/A" | |||
return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Details: %s]</span>", | |||
context or "Unknown Context", safe_details) | |||
end | |||
local | --- Видаляє вікі-посилання [[link|text]] і [[link]] | ||
local function clean_wikilinks(text) | |||
if not text then return nil end | |||
-- [[link|text]] -> text | |||
text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") | |||
-- [[link]] -> link | |||
text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1") | |||
return mw.text.trim(text) | |||
end | |||
local function | ---------------------------------------------------------------------- | ||
if | -- ГОЛОВНИЙ ПАРСЕР ДЛЯ ГОРИЗОНТАЛЬНИХ ТАБЛИЦЬ | ||
return | ---------------------------------------------------------------------- | ||
local function fetch_from_table(page_title, player_name, column_index) | |||
if not player_name or player_name == "" then | |||
return error_output("No Player Name", page_title) | |||
end | end | ||
local title = mw.title.new( | local title = mw.title.new(page_title) | ||
if not title or not title.exists then | if not title or not title.exists then | ||
return error_output("Page Missing", page_title) | |||
return | |||
end | end | ||
local content = title:getContent() | local content = title:getContent() | ||
if not content then | if not content then | ||
return error_output("Content Read Fail", page_title) | |||
return | |||
end | end | ||
-- Знаходимо таблицю | |||
local table_start = mw.ustring.find(content, "{|") | local table_start = mw.ustring.find(content, "{|") | ||
local table_end = mw.ustring.find(content, "|}", table_start) | local table_end = mw.ustring.find(content, "|}", table_start) | ||
if not table_start or not table_end then | if not table_start or not table_end then | ||
return error_output("Table Missing", page_title) | |||
return | |||
end | end | ||
local table_content = mw.ustring.sub(content, table_start, table_end + 1) | local table_content = mw.ustring.sub(content, table_start, table_end + 1) | ||
-- Екрануємо спеціальні символи в імені гравця | |||
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") | |||
local player_pattern = "%[%[" .. escaped .. "%]%]" | |||
-- | -- Шукаємо рядки, які починаються з |- | ||
local | |||
local | |||
local rows = {} | local rows = {} | ||
for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do | for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do | ||
table.insert(rows, row) | table.insert(rows, row) | ||
end | end | ||
-- Останній рядок (до |}) | |||
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}") | local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}") | ||
if last_row then | if last_row then | ||
table.insert(rows, last_row) | table.insert(rows, last_row) | ||
end | end | ||
-- Обробляємо кожен рядок | |||
for _, row in ipairs(rows) do | for _, row in ipairs(rows) do | ||
if mw.ustring.find(row, player_pattern) then | if mw.ustring.find(row, player_pattern) then | ||
local cells = | -- Розбиваємо рядок на комірки | ||
if | local cells = {} | ||
return cells[column_index] | |||
-- Видаляємо початковий | | |||
row = mw.ustring.gsub(row, "^%s*|%s*", "") | |||
-- Розбиваємо по || | |||
for cell in mw.ustring.gmatch(row, "([^|]+)") do | |||
-- Пропускаємо порожні комірки від подвійних || | |||
local trimmed = mw.text.trim(cell) | |||
if trimmed ~= "" then | |||
table.insert(cells, trimmed) | |||
end | |||
end | |||
-- Повертаємо потрібну колонку | |||
if column_index <= #cells then | |||
return clean_wikilinks(cells[column_index]) | |||
else | |||
return error_output("Column Out of Range", | |||
string.format("Requested col %d, found %d cells", column_index, #cells)) | |||
end | end | ||
end | end | ||
end | end | ||
return | return error_output("Player Not Found", player_name) | ||
end | end | ||
-- | ---------------------------------------------------------------------- | ||
-- | -- ФУНКЦІЇ, ЩО ВИКЛИКАЮТЬСЯ З ШАБЛОНУ | ||
-- | ---------------------------------------------------------------------- | ||
function p.recruiter(frame) | |||
function p. | local name = frame.args.player | ||
local | -- Don't clean wikilinks - keep them for clickable links | ||
local | local title = mw.title.new("Гравці") | ||
if not title or not title.exists then | |||
return error_output("Page Missing", "Гравці") | |||
end | |||
local | local content = title:getContent() | ||
if not content then | |||
" | return error_output("Content Read Fail", "Гравці") | ||
end | |||
local | local table_start = mw.ustring.find(content, "{|") | ||
local table_end = mw.ustring.find(content, "|}", table_start) | |||
if not table_start or not table_end then | |||
if not | return error_output("Table Missing", "Гравці") | ||
end | |||
local | local table_content = mw.ustring.sub(content, table_start, table_end + 1) | ||
local escaped = mw.ustring.gsub(name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") | |||
local player_pattern = "%[%[" .. escaped .. "%]%]" | |||
local | local rows = {} | ||
for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do | |||
table.insert(rows, row) | |||
- | |||
end | end | ||
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}") | |||
if last_row then | |||
table.insert(rows, last_row) | |||
end | end | ||
for _, row in ipairs(rows) do | |||
if mw.ustring.find(row, player_pattern) then | |||
local cells = {} | |||
row = mw.ustring.gsub(row, "^%s*|%s*", "") | |||
for cell in mw.ustring.gmatch(row, "([^|]+)") do | |||
local trimmed = mw.text.trim(cell) | |||
if trimmed ~= "" then | |||
table.insert(cells, trimmed) | |||
end | |||
end | |||
if 4 <= #cells then | |||
local raw = mw.text.trim(cells[4]) | |||
if raw == "Відсутній" or raw == "-" then | |||
return "Не вказано" | |||
end | |||
-- Return with wikilinks intact | |||
return raw | |||
end | |||
end | |||
end | end | ||
return | return "Не вказано" | ||
end | end | ||
function p.date_added(frame) | function p.date_added(frame) | ||
local | local name = frame.args.player | ||
local raw = fetch_from_table("Гравці", name, 3) | |||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then | |||
return "Лише Бог знає" | |||
end | |||
if not raw or raw == "" or raw == "-" or raw == "Відсутній" then | if not raw or raw == "" or raw == "-" or raw == "Відсутній" then | ||
| Рядок 228: | Рядок 169: | ||
end | end | ||
-- Парсимо дату у форматі DD.MM.YYYY | |||
local day, month, year = mw.ustring.match(raw, "(%d+)%.(%d+)%.(%d+)") | local day, month, year = mw.ustring.match(raw, "(%d+)%.(%d+)%.(%d+)") | ||
if day and month and year then | if day and month and year then | ||
local end_date = os.time({year=2024, month= | -- Кінцева дата: 26.10.2024 | ||
local end_date = os.time({year=2024, month=10, day=26}) | |||
local start_date = os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day)}) | local start_date = os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day)}) | ||
local days_diff = math.floor((end_date - start_date) / 86400) | local days_diff = math.floor((end_date - start_date) / 86400) | ||
| Рядок 239: | Рядок 183: | ||
return "Лише Бог знає" | return "Лише Бог знає" | ||
end | end | ||
function p.prize_pool(frame) | function p.prize_pool(frame) | ||
local | local name = frame.args.player | ||
local raw = fetch_from_table("Призовий_фонд", name, 2) | |||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then | |||
return "0 ₴" | |||
end | |||
if | if raw then | ||
raw = mw.ustring.gsub(raw, "[^%d%s]", "") | |||
raw = mw.text.trim(raw) | |||
end | |||
return (raw or "0") .. " ₴" | return (raw or "0") .. " ₴" | ||
end | end | ||
-- Додай цю функцію до FetchData2 (модуль:FetchData2) | |||
function p.foty_rating(frame) | function p.foty_rating(frame) | ||
local | local name = frame.args.player | ||
if not name or name == "" then | |||
return "0" | |||
end | |||
local | local title = mw.title.new("Період") | ||
if not | if not title or not title.exists then | ||
return "0" | |||
end | end | ||
if not | local content = title:getContent() | ||
if not content then | |||
return "0" | |||
end | |||
local | -- Шукаємо секцію "Фінал Року" | ||
local section_start = mw.ustring.find(content, "== Фінал Року ==") | |||
if not section_start then | |||
return "0" | |||
end | |||
if not table_start | -- Знаходимо таблицю після секції | ||
local table_start = mw.ustring.find(content, "{|", section_start) | |||
if not table_start then | |||
return "0" | |||
end | |||
local | local table_end = mw.ustring.find(content, "|}", table_start) | ||
if not table_end then | |||
return "0" | |||
end | |||
local table_content = mw.ustring.sub(content, table_start, table_end + 1) | |||
local | -- Екрануємо спеціальні символи в імені гравця | ||
local | local escaped = mw.ustring.gsub(name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") | ||
local player_pattern = "%[%[" .. escaped .. "%]%]" | |||
-- Збираємо всі рядки | |||
local rows = {} | |||
for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do | |||
table.insert(rows, row) | |||
end | end | ||
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}") | |||
if last_row then | |||
table.insert(rows, last_row) | |||
local | |||
if | |||
end | end | ||
if | -- Обробляємо кожен рядок | ||
for _, row in ipairs(rows) do | |||
if mw.ustring.find(row, player_pattern) then | |||
local cells = {} | |||
row = mw.ustring.gsub(row, "^%s*|%s*", "") | |||
for cell in mw.ustring.gmatch(row, "([^|]+)") do | |||
local trimmed = mw.text.trim(cell) | |||
if trimmed ~= "" then | |||
table.insert(cells, trimmed) | |||
end | |||
end | |||
-- Колонка 1 - це місце (№) | |||
-- Колонка 3 - це рейтинг (Σ) | |||
if #cells >= 3 then | |||
local place = mw.text.trim(cells[1]) | |||
local rating = cells[3] | |||
-- Видаляємо HTML теги з рейтингу | |||
rating = mw.ustring.gsub(rating, "<span[^>]*>", "") | |||
rating = mw.ustring.gsub(rating, "</span>", "") | |||
-- Видаляємо всі нецифрові символи окрім мінуса | |||
rating = mw.ustring.gsub(rating, "[^%d%-]", "") | |||
rating = mw.text.trim(rating) | |||
if rating and rating ~= "" then | |||
return string.format("%s (%s місце)", rating, place) | |||
end | |||
end | |||
return | return "0" | ||
end | end | ||
end | end | ||
return | return "0" | ||
end | end | ||
function p.foundation(frame) | |||
local | local name = frame.args.player | ||
local raw = fetch_from_table("Фундація", name, 3) | |||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then | |||
return "0 ₴" | |||
end | |||
if | if raw then | ||
raw = mw.ustring.gsub(raw, "[^%d%s]", "") | |||
raw = mw.text.trim(raw) | |||
end | end | ||
return (raw or "0") .. " ₴" | |||
end | end | ||
function p. | function p.finalist(frame) | ||
local name = frame.args.player | local name = frame.args.player | ||
local raw = fetch_from_table("Фіналіст", name, 3) | |||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then | |||
return "0/9" | |||
end | |||
if not raw or raw == "" or raw == "-" then | |||
if not | return "0/9" | ||
end | end | ||
-- Видаляємо всі нецифрові символи | |||
local number_only = mw.ustring.gsub(raw, "[^%d]", "") | |||
-- Перетворюємо в число | |||
local count = tonumber(number_only) | |||
if count then | |||
return string.format("%d/9", count) | |||
end | end | ||
return "0/9" | |||
return | |||
end | end | ||
function p.games_count(frame) | |||
local name = frame.args.player | |||
local raw = fetch_from_table("Статистика", name, 3) | |||
local | |||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then | |||
return "0" | |||
end | |||
local | return raw or "0" | ||
end | |||
function p.wins_count(frame) | |||
local name = frame.args.player | |||
local raw = fetch_from_table("Статистика", name, 4) | |||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then | |||
if | return "0" | ||
end | end | ||
return | return raw or "0" | ||
end | end | ||
function p.losses_count(frame) | |||
local name = frame.args.player | |||
local | local raw = fetch_from_table("Статистика", name, 5) | ||
local | |||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then | |||
return "0" | |||
end | end | ||
return | |||
return raw or "0" | |||
end | end | ||
function p. | function p.win_rate(frame) | ||
local | local name = frame.args.player | ||
local raw = fetch_from_table("Статистика", name, 6) | |||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then | |||
return "0%" | |||
end | |||
if raw then | |||
-- Якщо немає знаку %, додаємо його | |||
if not mw.ustring.find(raw, "%%") then | |||
return raw .. "%" | |||
end | end | ||
return raw | |||
end | end | ||
return "0%" | |||
end | end | ||
function p.win_rate_colored(frame) | |||
local name = frame.args.player | |||
local raw = fetch_from_table("Статистика", name, 6) | |||
local | if type(raw) == "string" and mw.ustring.find(raw, "Error") then | ||
return "0%" | |||
return | |||
end | end | ||
if not raw or raw == "" then | |||
return "0%" | |||
end | end | ||
-- Видаляємо % та конвертуємо в число | |||
return | local percent_str = mw.ustring.gsub(raw, "%%", "") | ||
local percent_num = tonumber(percent_str) | |||
if not percent_num then | |||
return raw | |||
end | end | ||
-- Визначаємо колір | |||
local color = "" | |||
if percent_num < 50 then | |||
color = "indianred" | |||
elseif percent_num > 50 then | |||
color = "gold" | |||
else | |||
-- Рівно 50% - білий | |||
color = "white" | |||
end | end | ||
if | -- Додаємо % якщо його немає | ||
local display_text = raw | |||
if not mw.ustring.find(raw, "%%") then | |||
display_text = raw .. "%" | |||
end | end | ||
return " | return string.format("<span style='color:%s;'>%s</span>", color, display_text) | ||
end | end | ||
local function | --- Отримує всіх гравців з таблиці "Гравці" в порядку дати приєднання | ||
if not | local function get_all_players() | ||
local title = mw.title.new("Гравці") | |||
if not title or not title.exists then | |||
return nil | |||
end | |||
local content = title:getContent() | |||
if not content then | |||
return nil | |||
end | |||
local table_start = mw.ustring.find(content, "{|") | |||
local table_end = mw.ustring.find(content, "|}", table_start) | |||
if | if not table_start or not table_end then | ||
return | return nil | ||
end | end | ||
local table_content = mw.ustring.sub(content, table_start, table_end + 1) | |||
local players = {} | |||
local rows = {} | |||
for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do | |||
table.insert(rows, row) | |||
end | end | ||
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}") | |||
if last_row then | |||
table.insert(rows, last_row) | |||
local | end | ||
if | |||
for _, row in ipairs(rows) do | for _, row in ipairs(rows) do | ||
local cells = | local cells = {} | ||
row = mw.ustring.gsub(row, "^%s*|%s*", "") | |||
for cell in mw.ustring.gmatch(row, "([^|]+)") do | |||
for | local trimmed = mw.text.trim(cell) | ||
if trimmed ~= "" then | |||
table.insert(cells, trimmed) | |||
end | end | ||
end | end | ||
if | -- Колонка 2 - ім'я гравця | ||
local | if #cells >= 2 then | ||
local player_name = clean_wikilinks(cells[2]) | |||
if player_name then | |||
table.insert(players, player_name) | |||
end | |||
end | end | ||
end | end | ||
return | return players | ||
end | end | ||
function p. | function p.prev_player(frame) | ||
local | local name = frame.args.player | ||
local players = get_all_players() | |||
if not players then | |||
if not | return "" | ||
return | |||
end | end | ||
for i, player in ipairs(players) do | |||
if player == name then | |||
if i > 1 then | |||
return players[i - 1] | |||
else | |||
return "" -- Перший гравець, немає попереднього | |||
end | |||
for | |||
if | |||
end | end | ||
end | end | ||
return | return "" | ||
end | end | ||
function p. | function p.next_player(frame) | ||
local | local name = frame.args.player | ||
local players = get_all_players() | |||
if not players then | |||
return "" | |||
if not | |||
end | end | ||
for | for i, player in ipairs(players) do | ||
if player == name then | |||
if i < #players then | |||
if | return players[i + 1] | ||
if # | else | ||
return "" -- Останній гравець, немає наступного | |||
end | end | ||
end | end | ||
end | end | ||
return "" | |||
end | |||
function p.prev_player_link(frame) | |||
local name = frame.args.player | |||
local prev = p.prev_player(frame) | |||
if prev == "" or not prev then | |||
return "" | |||
end | end | ||
return | return string.format( | ||
'<a href="/index.php/%s" style="width:48px; height:48px; display:flex; align-items:center; justify-content:center; text-decoration:none; border:2px solid #333; border-radius:8px;"><span style="color:gold; font-size:24px; font-weight:bold;">←</span></a>', | |||
mw.uri.encode(prev, "WIKI") | |||
) | |||
end | end | ||
function p. | function p.next_player_link(frame) | ||
local | local name = frame.args.player | ||
local next = p.next_player(frame) | |||
if next == "" or not next then | |||
return "" | |||
end | end | ||
return | return string.format( | ||
'<a href="/index.php/%s" style="width:48px; height:48px; display:flex; align-items:center; justify-content:center; text-decoration:none; border:2px solid #333; border-radius:8px;"><span style="color:gold; font-size:24px; font-weight:bold;">→</span></a>', | |||
mw.uri.encode(next, "WIKI") | |||
) | |||
end | end | ||
return p | return p | ||
Версія за 10:39, 1 грудня 2025
Документацію для цього модуля можна створити у Модуль:FetchData/документація
local p = {}
--- Функція для виведення помилки на сторінку
local function error_output(context, error_details)
local safe_details = error_details or "N/A"
return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Details: %s]</span>",
context or "Unknown Context", safe_details)
end
--- Видаляє вікі-посилання [[link|text]] і [[link]]
local function clean_wikilinks(text)
if not text then return nil end
-- [[link|text]] -> text
text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
-- [[link]] -> link
text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
return mw.text.trim(text)
end
----------------------------------------------------------------------
-- ГОЛОВНИЙ ПАРСЕР ДЛЯ ГОРИЗОНТАЛЬНИХ ТАБЛИЦЬ
----------------------------------------------------------------------
local function fetch_from_table(page_title, player_name, column_index)
if not player_name or player_name == "" then
return error_output("No Player Name", page_title)
end
local title = mw.title.new(page_title)
if not title or not title.exists then
return error_output("Page Missing", page_title)
end
local content = title:getContent()
if not content then
return error_output("Content Read Fail", page_title)
end
-- Знаходимо таблицю
local table_start = mw.ustring.find(content, "{|")
local table_end = mw.ustring.find(content, "|}", table_start)
if not table_start or not table_end then
return error_output("Table Missing", page_title)
end
local table_content = mw.ustring.sub(content, table_start, table_end + 1)
-- Екрануємо спеціальні символи в імені гравця
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local player_pattern = "%[%[" .. escaped .. "%]%]"
-- Шукаємо рядки, які починаються з |-
local rows = {}
for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do
table.insert(rows, row)
end
-- Останній рядок (до |})
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}")
if last_row then
table.insert(rows, last_row)
end
-- Обробляємо кожен рядок
for _, row in ipairs(rows) do
if mw.ustring.find(row, player_pattern) then
-- Розбиваємо рядок на комірки
local cells = {}
-- Видаляємо початковий |
row = mw.ustring.gsub(row, "^%s*|%s*", "")
-- Розбиваємо по ||
for cell in mw.ustring.gmatch(row, "([^|]+)") do
-- Пропускаємо порожні комірки від подвійних ||
local trimmed = mw.text.trim(cell)
if trimmed ~= "" then
table.insert(cells, trimmed)
end
end
-- Повертаємо потрібну колонку
if column_index <= #cells then
return clean_wikilinks(cells[column_index])
else
return error_output("Column Out of Range",
string.format("Requested col %d, found %d cells", column_index, #cells))
end
end
end
return error_output("Player Not Found", player_name)
end
----------------------------------------------------------------------
-- ФУНКЦІЇ, ЩО ВИКЛИКАЮТЬСЯ З ШАБЛОНУ
----------------------------------------------------------------------
function p.recruiter(frame)
local name = frame.args.player
-- Don't clean wikilinks - keep them for clickable links
local title = mw.title.new("Гравці")
if not title or not title.exists then
return error_output("Page Missing", "Гравці")
end
local content = title:getContent()
if not content then
return error_output("Content Read Fail", "Гравці")
end
local table_start = mw.ustring.find(content, "{|")
local table_end = mw.ustring.find(content, "|}", table_start)
if not table_start or not table_end then
return error_output("Table Missing", "Гравці")
end
local table_content = mw.ustring.sub(content, table_start, table_end + 1)
local escaped = mw.ustring.gsub(name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local player_pattern = "%[%[" .. escaped .. "%]%]"
local rows = {}
for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do
table.insert(rows, row)
end
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}")
if last_row then
table.insert(rows, last_row)
end
for _, row in ipairs(rows) do
if mw.ustring.find(row, player_pattern) then
local cells = {}
row = mw.ustring.gsub(row, "^%s*|%s*", "")
for cell in mw.ustring.gmatch(row, "([^|]+)") do
local trimmed = mw.text.trim(cell)
if trimmed ~= "" then
table.insert(cells, trimmed)
end
end
if 4 <= #cells then
local raw = mw.text.trim(cells[4])
if raw == "Відсутній" or raw == "-" then
return "Не вказано"
end
-- Return with wikilinks intact
return raw
end
end
end
return "Не вказано"
end
function p.date_added(frame)
local name = frame.args.player
local raw = fetch_from_table("Гравці", name, 3)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return "Лише Бог знає"
end
if not raw or raw == "" or raw == "-" or raw == "Відсутній" then
return "Лише Бог знає"
end
-- Парсимо дату у форматі DD.MM.YYYY
local day, month, year = mw.ustring.match(raw, "(%d+)%.(%d+)%.(%d+)")
if day and month and year then
-- Кінцева дата: 26.10.2024
local end_date = os.time({year=2024, month=10, day=26})
local start_date = os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day)})
local days_diff = math.floor((end_date - start_date) / 86400)
return string.format("%s (%d днів)", raw, days_diff)
end
return "Лише Бог знає"
end
function p.prize_pool(frame)
local name = frame.args.player
local raw = fetch_from_table("Призовий_фонд", name, 2)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return "0 ₴"
end
if raw then
raw = mw.ustring.gsub(raw, "[^%d%s]", "")
raw = mw.text.trim(raw)
end
return (raw or "0") .. " ₴"
end
-- Додай цю функцію до FetchData2 (модуль:FetchData2)
function p.foty_rating(frame)
local name = frame.args.player
if not name or name == "" then
return "0"
end
local title = mw.title.new("Період")
if not title or not title.exists then
return "0"
end
local content = title:getContent()
if not content then
return "0"
end
-- Шукаємо секцію "Фінал Року"
local section_start = mw.ustring.find(content, "== Фінал Року ==")
if not section_start then
return "0"
end
-- Знаходимо таблицю після секції
local table_start = mw.ustring.find(content, "{|", section_start)
if not table_start then
return "0"
end
local table_end = mw.ustring.find(content, "|}", table_start)
if not table_end then
return "0"
end
local table_content = mw.ustring.sub(content, table_start, table_end + 1)
-- Екрануємо спеціальні символи в імені гравця
local escaped = mw.ustring.gsub(name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local player_pattern = "%[%[" .. escaped .. "%]%]"
-- Збираємо всі рядки
local rows = {}
for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do
table.insert(rows, row)
end
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}")
if last_row then
table.insert(rows, last_row)
end
-- Обробляємо кожен рядок
for _, row in ipairs(rows) do
if mw.ustring.find(row, player_pattern) then
local cells = {}
row = mw.ustring.gsub(row, "^%s*|%s*", "")
for cell in mw.ustring.gmatch(row, "([^|]+)") do
local trimmed = mw.text.trim(cell)
if trimmed ~= "" then
table.insert(cells, trimmed)
end
end
-- Колонка 1 - це місце (№)
-- Колонка 3 - це рейтинг (Σ)
if #cells >= 3 then
local place = mw.text.trim(cells[1])
local rating = cells[3]
-- Видаляємо HTML теги з рейтингу
rating = mw.ustring.gsub(rating, "<span[^>]*>", "")
rating = mw.ustring.gsub(rating, "</span>", "")
-- Видаляємо всі нецифрові символи окрім мінуса
rating = mw.ustring.gsub(rating, "[^%d%-]", "")
rating = mw.text.trim(rating)
if rating and rating ~= "" then
return string.format("%s (%s місце)", rating, place)
end
end
return "0"
end
end
return "0"
end
function p.foundation(frame)
local name = frame.args.player
local raw = fetch_from_table("Фундація", name, 3)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return "0 ₴"
end
if raw then
raw = mw.ustring.gsub(raw, "[^%d%s]", "")
raw = mw.text.trim(raw)
end
return (raw or "0") .. " ₴"
end
function p.finalist(frame)
local name = frame.args.player
local raw = fetch_from_table("Фіналіст", name, 3)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return "0/9"
end
if not raw or raw == "" or raw == "-" then
return "0/9"
end
-- Видаляємо всі нецифрові символи
local number_only = mw.ustring.gsub(raw, "[^%d]", "")
-- Перетворюємо в число
local count = tonumber(number_only)
if count then
return string.format("%d/9", count)
end
return "0/9"
end
function p.games_count(frame)
local name = frame.args.player
local raw = fetch_from_table("Статистика", name, 3)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return "0"
end
return raw or "0"
end
function p.wins_count(frame)
local name = frame.args.player
local raw = fetch_from_table("Статистика", name, 4)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return "0"
end
return raw or "0"
end
function p.losses_count(frame)
local name = frame.args.player
local raw = fetch_from_table("Статистика", name, 5)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return "0"
end
return raw or "0"
end
function p.win_rate(frame)
local name = frame.args.player
local raw = fetch_from_table("Статистика", name, 6)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return "0%"
end
if raw then
-- Якщо немає знаку %, додаємо його
if not mw.ustring.find(raw, "%%") then
return raw .. "%"
end
return raw
end
return "0%"
end
function p.win_rate_colored(frame)
local name = frame.args.player
local raw = fetch_from_table("Статистика", name, 6)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then
return "0%"
end
if not raw or raw == "" then
return "0%"
end
-- Видаляємо % та конвертуємо в число
local percent_str = mw.ustring.gsub(raw, "%%", "")
local percent_num = tonumber(percent_str)
if not percent_num then
return raw
end
-- Визначаємо колір
local color = ""
if percent_num < 50 then
color = "indianred"
elseif percent_num > 50 then
color = "gold"
else
-- Рівно 50% - білий
color = "white"
end
-- Додаємо % якщо його немає
local display_text = raw
if not mw.ustring.find(raw, "%%") then
display_text = raw .. "%"
end
return string.format("<span style='color:%s;'>%s</span>", color, display_text)
end
--- Отримує всіх гравців з таблиці "Гравці" в порядку дати приєднання
local function get_all_players()
local title = mw.title.new("Гравці")
if not title or not title.exists then
return nil
end
local content = title:getContent()
if not content then
return nil
end
local table_start = mw.ustring.find(content, "{|")
local table_end = mw.ustring.find(content, "|}", table_start)
if not table_start or not table_end then
return nil
end
local table_content = mw.ustring.sub(content, table_start, table_end + 1)
local players = {}
local rows = {}
for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do
table.insert(rows, row)
end
local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}")
if last_row then
table.insert(rows, last_row)
end
for _, row in ipairs(rows) do
local cells = {}
row = mw.ustring.gsub(row, "^%s*|%s*", "")
for cell in mw.ustring.gmatch(row, "([^|]+)") do
local trimmed = mw.text.trim(cell)
if trimmed ~= "" then
table.insert(cells, trimmed)
end
end
-- Колонка 2 - ім'я гравця
if #cells >= 2 then
local player_name = clean_wikilinks(cells[2])
if player_name then
table.insert(players, player_name)
end
end
end
return players
end
function p.prev_player(frame)
local name = frame.args.player
local players = get_all_players()
if not players then
return ""
end
for i, player in ipairs(players) do
if player == name then
if i > 1 then
return players[i - 1]
else
return "" -- Перший гравець, немає попереднього
end
end
end
return ""
end
function p.next_player(frame)
local name = frame.args.player
local players = get_all_players()
if not players then
return ""
end
for i, player in ipairs(players) do
if player == name then
if i < #players then
return players[i + 1]
else
return "" -- Останній гравець, немає наступного
end
end
end
return ""
end
function p.prev_player_link(frame)
local name = frame.args.player
local prev = p.prev_player(frame)
if prev == "" or not prev then
return ""
end
return string.format(
'<a href="/index.php/%s" style="width:48px; height:48px; display:flex; align-items:center; justify-content:center; text-decoration:none; border:2px solid #333; border-radius:8px;"><span style="color:gold; font-size:24px; font-weight:bold;">←</span></a>',
mw.uri.encode(prev, "WIKI")
)
end
function p.next_player_link(frame)
local name = frame.args.player
local next = p.next_player(frame)
if next == "" or not next then
return ""
end
return string.format(
'<a href="/index.php/%s" style="width:48px; height:48px; display:flex; align-items:center; justify-content:center; text-decoration:none; border:2px solid #333; border-radius:8px;"><span style="color:gold; font-size:24px; font-weight:bold;">→</span></a>',
mw.uri.encode(next, "WIKI")
)
end
return p