Модуль:FetchData2: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 1: | Рядок 1: | ||
local p = {} | local p = {} | ||
--- Функція для виведення помилки на сторінку | |||
--- Функція для виведення помилки на сторінку | |||
local function error_output(context, error_details) | local function error_output(context, error_details) | ||
local safe_details = error_details or "N/A" | local safe_details = error_details or "N/A" | ||
return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Page: %s]</span>", context or "Unknown Context", safe_details) | return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Page: %s]</span>", context or "Unknown Context", safe_details) | ||
end | end | ||
| Рядок 15: | Рядок 10: | ||
local function clean_wikilinks(text) | local function clean_wikilinks(text) | ||
if not text then return nil end | if not text then return nil end | ||
text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") | text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") | ||
text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1") | text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1") | ||
return mw.text.trim(text) | return mw.text.trim(text) | ||
end | end | ||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
-- ГОЛОВНИЙ ПОШУК ( | -- ГОЛОВНИЙ ПОШУК (ЗАВЖДИ ШУКАЄ "Кексік" - як ви просили) | ||
- | |||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
local function fetch_from_table(page_title, | local function fetch_from_table(page_title, passed_player_name, column_index) | ||
-- !!! ЖОРСТКЕ КОДУВАННЯ: Незалежно від того, яке ім'я передано, шукаємо "Кексік" | |||
local player_name = "Кексік" | |||
if not player_name or player_name == "" then | if not player_name or player_name == "" then | ||
return error_output("No Player Name", page_title) | return error_output("No Player Name (Hardcoded)", page_title) | ||
end | end | ||
| Рядок 56: | Рядок 35: | ||
if not content then return error_output("Content Read Fail", page_title) end | if not content then return error_output("Content Read Fail", page_title) end | ||
local table_content = mw.ustring.match(content, "{|.-([|].-.-[|]})") | local table_content = mw.ustring.match(content, "{|.-([|].-.-[|]})") | ||
if not table_content then | if not table_content then | ||
| Рядок 65: | Рядок 43: | ||
local player_pattern = "%[%[" .. escaped .. "%]%]" | local player_pattern = "%[%[" .. escaped .. "%]%]" | ||
local | -- Використовуємо вкладену функцію, щоб повернути результат, або продовжити зовнішній цикл | ||
local result = nil | |||
for row in mw.ustring.gmatch(table_content, "\n%s*|-[^%s]*\n(.-)") do | for row in mw.ustring.gmatch(table_content, "\n%s*|-[^%s]*\n(.-)") do | ||
-- Перевіряємо, чи містить рядок ім'я гравця у форматі [[Гравець]] | -- Перевіряємо, чи містить рядок ім'я гравця у форматі [[Гравець]] | ||
if mw.ustring.find(row, player_pattern, 1, true) then | if mw.ustring.find(row, player_pattern, 1, true) then | ||
break | local cell_data = {} | ||
local initial_split = mw.text.split(row, '||') | |||
if #initial_split > 0 then | |||
local first_cell_content = mw.ustring.match(initial_split[1], "^%s*[|]%s*(.-)%s*$") | |||
if not first_cell_content then | |||
-- Якщо перша комірка неправильна, продовжуємо цикл (нічого не робимо) | |||
else | |||
table.insert(cell_data, mw.text.trim(first_cell_content)) | |||
for i = 2, #initial_split do | |||
table.insert(cell_data, mw.text.trim(initial_split[i])) | |||
end | |||
if cell_data[2] and mw.ustring.find(cell_data[2], player_pattern) then | |||
if column_index <= #cell_data and cell_data[column_index] then | |||
-- ЗНАЙДЕНО! Повертаємо результат, встановлюючи його в зовнішній змінній | |||
result = clean_wikilinks(cell_data[column_index]) | |||
break -- Вихід з циклу gmatch | |||
end | |||
end | |||
end | |||
end | |||
end | end | ||
end | end | ||
return result | |||
end | end | ||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
| Рядок 122: | Рядок 88: | ||
local name = frame.args.player | local name = frame.args.player | ||
local raw = fetch_from_table("Гравці", name, 4) | local raw = fetch_from_table("Гравці", name, 4) | ||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | ||
if raw == "Відсутній" or raw == "-" then return "Не вказано" end | if raw == "Відсутній" or raw == "-" then return "Не вказано" end | ||
return raw or "Не вказано" | return raw or "Не вказано" | ||
| Рядок 130: | Рядок 98: | ||
local name = frame.args.player | local name = frame.args.player | ||
local raw = fetch_from_table("Гравці", name, 3) | local raw = fetch_from_table("Гравці", name, 3) | ||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | ||
return raw | |||
return raw or "Невідомо" | |||
end | end | ||
| Рядок 137: | Рядок 107: | ||
local name = frame.args.player | local name = frame.args.player | ||
local raw = fetch_from_table("Призовий_фонд", name, 3) | local raw = fetch_from_table("Призовий_фонд", name, 3) | ||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | ||
if raw then | if raw then | ||
raw = mw.ustring.gsub(raw, "[^%d%s]", "") | raw = mw.ustring.gsub(raw, "[^%d%s]", "") | ||
| Рядок 148: | Рядок 120: | ||
local name = frame.args.player | local name = frame.args.player | ||
local raw = fetch_from_table("Фундація", name, 3) | local raw = fetch_from_table("Фундація", name, 3) | ||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | ||
if raw then | if raw then | ||
raw = mw.ustring.gsub(raw, "[^%d%s]", "") | raw = mw.ustring.gsub(raw, "[^%d%s]", "") | ||
| Рядок 159: | Рядок 133: | ||
local name = frame.args.player | local name = frame.args.player | ||
local raw = fetch_from_table("Фіналіст", name, 3) | local raw = fetch_from_table("Фіналіст", name, 3) | ||
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end | ||
local count = raw and mw.ustring.match(raw, "(%d+)") | local count = raw and mw.ustring.match(raw, "(%d+)") | ||
if count then | if count then | ||
return count | return count | ||
else | else | ||
return "0 | return "0" | ||
end | end | ||
end | end | ||
-- Ця функція залишається порожньою, як ви просили | |||
function p.season_result(frame) | function p.season_result(frame) | ||
return nil | |||
end | end | ||
return p | return p | ||
Версія за 00:45, 24 листопада 2025
Документацію для цього модуля можна створити у Модуль:FetchData2/документація
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. Page: %s]</span>", context or "Unknown Context", safe_details)
end
--- Видаляє вікі-посилання [[link|text]] і [[link]]
local function clean_wikilinks(text)
if not text then return nil end
text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
return mw.text.trim(text)
end
----------------------------------------------------------------------
-- ГОЛОВНИЙ ПОШУК (ЗАВЖДИ ШУКАЄ "Кексік" - як ви просили)
----------------------------------------------------------------------
local function fetch_from_table(page_title, passed_player_name, column_index)
-- !!! ЖОРСТКЕ КОДУВАННЯ: Незалежно від того, яке ім'я передано, шукаємо "Кексік"
local player_name = "Кексік"
if not player_name or player_name == "" then
return error_output("No Player Name (Hardcoded)", 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_content = mw.ustring.match(content, "{|.-([|].-.-[|]})")
if not table_content then
return error_output("Table Missing", page_title)
end
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local player_pattern = "%[%[" .. escaped .. "%]%]"
-- Використовуємо вкладену функцію, щоб повернути результат, або продовжити зовнішній цикл
local result = nil
for row in mw.ustring.gmatch(table_content, "\n%s*|-[^%s]*\n(.-)") do
-- Перевіряємо, чи містить рядок ім'я гравця у форматі [[Гравець]]
if mw.ustring.find(row, player_pattern, 1, true) then
local cell_data = {}
local initial_split = mw.text.split(row, '||')
if #initial_split > 0 then
local first_cell_content = mw.ustring.match(initial_split[1], "^%s*[|]%s*(.-)%s*$")
if not first_cell_content then
-- Якщо перша комірка неправильна, продовжуємо цикл (нічого не робимо)
else
table.insert(cell_data, mw.text.trim(first_cell_content))
for i = 2, #initial_split do
table.insert(cell_data, mw.text.trim(initial_split[i]))
end
if cell_data[2] and mw.ustring.find(cell_data[2], player_pattern) then
if column_index <= #cell_data and cell_data[column_index] then
-- ЗНАЙДЕНО! Повертаємо результат, встановлюючи його в зовнішній змінній
result = clean_wikilinks(cell_data[column_index])
break -- Вихід з циклу gmatch
end
end
end
end
end
end
return result
end
----------------------------------------------------------------------
-- ФУНКЦІЇ ДЛЯ {{MCC Player New}} (без змін)
----------------------------------------------------------------------
function p.recruiter(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 raw end
if raw == "Відсутній" or raw == "-" then return "Не вказано" end
return raw or "Не вказано"
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 raw end
return raw or "Невідомо"
end
function p.prize_pool(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 raw end
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)
local name = frame.args.player
local raw = fetch_from_table("Фундація", name, 3)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
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)
local name = frame.args.player
local raw = fetch_from_table("Фіналіст", name, 3)
if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
local count = raw and mw.ustring.match(raw, "(%d+)")
if count then
return count
else
return "0"
end
end
-- Ця функція залишається порожньою, як ви просили
function p.season_result(frame)
return nil
end
return p