|
|
| Рядок 1: |
Рядок 1: |
| local p = {}
| |
|
| |
| -- Цільова дата для розрахунку днів з моменту приєднання (25 жовтня 2024 року)
| |
| local TARGET_DATE_STR = "25.10.2024"
| |
| local TARGET_TIMESTAMP = os.time({year=2024, month=10, day=25, hour=0, min=0, sec=0})
| |
|
| |
| --- Функція для вилучення даних з вказаної сторінки та колонки
| |
| -- @param page_title Назва сторінки (наприклад, "Гравці")
| |
| -- @param player_name Ім'я гравця для пошуку
| |
| -- @param column_index Індекс колонки (1 = №, 2 = Пан/Пані, 3 = Дата/Сума/Рахунок, 4 = Провідник)
| |
| -- @return Очищене значення або nil
| |
| local function fetch_data_from_table(page_title, player_name, column_index) | | local function fetch_data_from_table(page_title, player_name, column_index) |
| local title = mw.title.new(page_title) | | local title = mw.title.new(page_title) |
| local content = title and title:getContent() | | if not title then return nil end |
|
| |
|
| if not content then | | local content = title:getContent() |
| return nil
| | if not content then return nil end |
| end
| |
|
| |
|
| -- Вилучаємо вміст таблиці між {| і |}, використовуючи нежадібний пошук | | -- Витягуємо будь-яку wikitable |
| local table_content = mw.ustring.match(content, "{|%s*class%s*=%s*\"wikitable\".-([|]-.-[|]})") | | local table_content = mw.ustring.match(content, "{|.-|}") |
| if not table_content then | | if not table_content then return nil end |
| return nil
| |
| end
| |
|
| |
|
| local escaped_player = mw.ustring.gsub(player_name, "([%(%)%.%-%+%[%]])", "%%%1") | | -- Робимо імʼя безпечним у RegEx |
| local player_link_pattern = "%[?%[" .. escaped_player .. "%]%]?"
| | local escaped_player = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") |
|
| |
|
| -- СТІЙКИЙ ПОШУК РЯДКА: Шукаємо ім'я гравця у вікі-посиланні, | | -- Шукаємо у форматах: |
| -- і захоплюємо весь блок, що починається з `|-` або `|` перед гравцем, | | -- [[Player]], [[Player|текст]], Player |
| -- і закінчується на кінці рядка (`\n|-`). | | local player_pattern = |
|
| | "%[%[" .. escaped_player .. "%]%]" .. |
| -- Шаблон знаходить: | будь-який вміст, який закінчується на гравцеві.
| | "|%[%[" .. escaped_player .. "|.-%]%]" .. |
| -- Ми використовуємо `(.-)` (будь-які символи) і не обмежуємося символами переносу рядка,
| | "|" .. escaped_player |
| -- щоб знайти всю секцію, що містить гравця.
| |
|
| |
| local row_match_start = mw.ustring.find(table_content, player_link_pattern)
| |
| if not row_match_start then
| |
| return nil
| |
| end
| |
|
| |
|
| -- Намагаємося знайти початок рядка (наприклад, |-) перед гравцем | | -- Знайдемо весь рядок, де є гравець |
| local row_start_idx = mw.ustring.find(table_content, "[|]%s*[%-]", 1, row_match_start) | | local row = nil |
| if not row_start_idx then
| | for line in mw.ustring.gmatch(table_content, "[^\n]+") do |
| -- Якщо не знайшли |-, шукаємо початок таблиці
| | if mw.ustring.match(line, player_pattern) then |
| row_start_idx = 1
| | row = line |
| end
| | break |
|
| |
| -- Знаходимо кінець рядка (наступний |-) після гравця
| |
| local row_end_idx = mw.ustring.find(table_content, "[|]%s*[%-]", row_match_start + 1)
| |
| if not row_end_idx then
| |
| row_end_idx = #table_content + 1 -- До кінця вмісту таблиці
| |
| end
| |
| | |
| local full_row_content = mw.ustring.sub(table_content, row_start_idx, row_end_idx - 1)
| |
|
| |
| local columns = {}
| |
|
| |
| -- Розділяємо, використовуючи | як роздільник комірок.
| |
| -- Це працює незалежно від того, чи | у тому ж рядку, чи з нового рядка.
| |
| -- Додаємо всі непусті комірки.
| |
| for cell in mw.ustring.gmatch(full_row_content, "[|](.-)") do | |
| local trimmed_cell = mw.text.trim(cell)
| |
| -- Перевіряємо, чи це не заголовок (бо заголовки теж містять |)
| |
| if trimmed_cell ~= "" and not mw.ustring.match(trimmed_cell, "^!%s*") then | |
| table.insert(columns, trimmed_cell)
| |
| end | | end |
| end | | end |
|
| | if not row then return nil end |
| -- Колонка 1 = №, Колонка 2 = Пан/Пані, Колонка 3 = Дата/Сума, Колонка 4 = Провідник
| |
| local result = columns[column_index]
| |
| | |
| if result then | |
| -- Очищуємо від вікі-посилань (якщо вони залишилися)
| |
| result = mw.text.trim(mw.ustring.gsub(result, "%[?%[?(.+)%]%]?", "%1"))
| |
| return result
| |
| else
| |
| return nil
| |
| end
| |
| end | |
|
| |
|
| --- Функції, що викликаються шаблоном MCC Player New --- | | -- Забираємо все після "|-" якщо є |
| | row = mw.ustring.gsub(row, "^%s*|%-", "") |
|
| |
|
| function p.date_added(frame)
| | -- Розбиваємо рядок на комірки |
| local player = frame.args.player | | local cells = {} |
| local date_str = fetch_data_from_table("Гравці", player, 3)
| | for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do |
| | | table.insert(cells, mw.text.trim(cell)) |
| if not date_str or date_str == "" then
| |
| return "''Невідомо''" | |
| end | | end |
|
| |
|
| local day, month, year = mw.ustring.match(date_str, "(%d%d)%.(%d%d)%.(%d%d%d%d)") | | -- Вибираємо потрібну колонку |
|
| | local result = cells[column_index] |
| if not day then | | if not result then return nil end |
| return date_str .. " (Помилка формату)"
| |
| end
| |
|
| |
|
| local join_timestamp = os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day), hour=0, min=0, sec=0}) | | -- Прибираємо вікі-посилання |
| | | result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2") |
| local diff_seconds = TARGET_TIMESTAMP - join_timestamp
| | result = mw.text.trim(result) |
|
| | if result == "" then |
| if diff_seconds < 0 then | | result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1") |
| return date_str | |
| end | | end |
|
| |
|
| local diff_days = math.floor(diff_seconds / 86400)
| | return result |
|
| |
| return string.format("%s (%d д)", date_str, diff_days) | |
| end | | end |
|
| |
| function p.recruiter(frame)
| |
| local player = frame.args.player
| |
| local recruiter_name = fetch_data_from_table("Гравці", player, 4)
| |
|
| |
| if not recruiter_name or recruiter_name == "" or recruiter_name == "Не вказано" or recruiter_name == "-" or recruiter_name == "Відсутній" then
| |
| return "Не вказано"
| |
| end
| |
|
| |
| if mw.ustring.match(recruiter_name, "%[?%[?") then
| |
| return recruiter_name
| |
| else
| |
| return string.format("[[%s|%s]]", recruiter_name, recruiter_name)
| |
| end
| |
| end
| |
|
| |
| function p.prize_pool(frame)
| |
| local player = frame.args.player
| |
| local amount = fetch_data_from_table("Призовий_фонд", player, 3)
| |
|
| |
| if not amount or amount == "" then
| |
| return "0 ₴"
| |
| end
| |
|
| |
| amount = mw.ustring.gsub(amount, "[^%d%s]", "")
| |
| amount = mw.text.trim(amount)
| |
|
| |
| return amount .. " ₴"
| |
| end
| |
|
| |
| function p.foundation(frame)
| |
| local player = frame.args.player
| |
| local amount = fetch_data_from_table("Фундація", player, 3)
| |
|
| |
| if not amount or amount == "" then
| |
| return "0 ₴"
| |
| end
| |
|
| |
| amount = mw.ustring.gsub(amount, "[^%d%s]", "")
| |
| amount = mw.text.trim(amount)
| |
|
| |
| return amount .. " ₴"
| |
| end
| |
|
| |
| function p.final(frame)
| |
| local player = frame.args.player
| |
| local count = fetch_data_from_table("Фіналіст", player, 3)
| |
|
| |
| if not count or count == "" then
| |
| return "0/9"
| |
| end
| |
|
| |
| count = mw.ustring.match(count, "(%d+)")
| |
|
| |
| if not count then
| |
| return "0/9"
| |
| end
| |
|
| |
| return count .. "/9"
| |
| end
| |
|
| |
| return p
| |