Модуль:FetchData: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування Мітка: Скасовано |
Admin (обговорення | внесок) Немає опису редагування Мітка: Скасовано |
||
| Рядок 1: | Рядок 1: | ||
local p = {} | local p = {} | ||
-- Функція для читання таблиці | |||
local function | local function get_table_from_page(page_name) | ||
local title = mw.title.new(page_name) | |||
if not title or not title.exists then return nil end | |||
local title = mw.title.new( | |||
if not title or not title.exists then | |||
local content = title:getContent() | local content = title:getContent() | ||
if not content then | if not content then return nil 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 nil end | ||
return mw.ustring.sub(content, table_start, table_end + 1) | |||
end | end | ||
-- | -- Парсинг таблиці | ||
local function parse_table_rows(table_content) | |||
function | |||
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) end | ||
return rows | |||
end | end | ||
function | local function parse_row_cells(row) | ||
local | 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 | |||
local | |||
end | end | ||
return cells | |||
return | |||
end | end | ||
function | 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 | end | ||
-- | -- Знайти дані гравця в таблиці | ||
local function find_player_data(table_content, player_name) | |||
function | if not table_content then return nil end | ||
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") | |||
local escaped = mw.ustring.gsub( | |||
local player_pattern = "%[%[" .. escaped .. "%]%]" | local player_pattern = "%[%[" .. escaped .. "%]%]" | ||
local rows = parse_table_rows(table_content) | |||
local rows = | |||
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 | ||
return parse_row_cells(row) | |||
end | end | ||
end | end | ||
return | return nil | ||
end | end | ||
function | -- Отримати всіх гравців зі списку | ||
local | local function get_all_players() | ||
local table_content = get_table_from_page("Гравці") | |||
if not table_content then return nil end | |||
local rows = parse_table_rows(table_content) | |||
local players = {} | |||
if | for _, row in ipairs(rows) do | ||
local cells = parse_row_cells(row) | |||
if #cells >= 2 then | |||
local player_name = clean_wikilinks(cells[2]) | |||
if player_name then | |||
table.insert(players, player_name) | |||
end | |||
end | |||
end | end | ||
return | return players | ||
end | end | ||
function p. | -- Згенерувати код для одного гравця | ||
local | function p.generate_single_player(frame) | ||
local player_name = frame.args.player or frame.args[1] | |||
if | if not player_name or player_name == "" then | ||
return " | return "Помилка: Не вказано ім'я гравця" | ||
end | end | ||
-- Збираємо дані з різних таблиць | |||
local stats = find_player_data(get_table_from_page("Статистика"), player_name) | |||
local player_info = find_player_data(get_table_from_page("Гравці"), player_name) | |||
local foundation = find_player_data(get_table_from_page("Фундація"), player_name) | |||
local prize_pool = find_player_data(get_table_from_page("Призовий_фонд"), player_name) | |||
local finalist = find_player_data(get_table_from_page("Фіналіст"), player_name) | |||
-- | -- FOTY Rating з таблиці "Період" | ||
local | local foty_rating = "—" | ||
local period_content = get_table_from_page("Період") | |||
if period_content then | |||
local content = mw.title.new("Період"):getContent() | |||
local foty_section = mw.ustring.match(content, "==+%s*Фінал Року%s*==+(.-)\n==") | |||
if not foty_section then | |||
foty_section = mw.ustring.match(content, "==+%s*Фінал Року%s*==+(.*)") | |||
end | |||
if foty_section then | |||
local table_start = mw.ustring.find(foty_section, "{|") | |||
local table_end = mw.ustring.find(foty_section, "|}", table_start) | |||
if table_start and table_end then | |||
local table_content = mw.ustring.sub(foty_section, table_start, table_end + 1) | |||
local foty_cells = find_player_data(table_content, player_name) | |||
if foty_cells and #foty_cells >= 3 then | |||
local place = mw.text.trim(foty_cells[1]) | |||
local rating = mw.text.trim(foty_cells[3]) | |||
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 | |||
foty_rating = rating .. " (" .. place .. " місце)" | |||
end | |||
end | |||
end | |||
end | |||
end | end | ||
-- Форматуємо дату | |||
local date_added = "Лише Бог знає" | |||
if player_info and #player_info >= 3 then | |||
local raw_date = player_info[3] | |||
if raw_date and raw_date ~= "" and raw_date ~= "-" then | |||
local day, month, year = mw.ustring.match(raw_date, "(%d+)%.(%d+)%.(%d+)") | |||
if day and month and year then | |||
local end_date = os.time({year=2024, month=12, day=1}) | |||
local start_date = os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day)}) | |||
local days_diff = math.floor((end_date - start_date) / 86400) | |||
date_added = string.format("%s (%d днів)", raw_date, days_diff) | |||
end | |||
end | |||
end | end | ||
if | -- Recruiter | ||
local recruiter = "Не вказано" | |||
if player_info and #player_info >= 2 then | |||
local raw = mw.text.trim(player_info[2]) | |||
if raw ~= "Відсутній" and raw ~= "-" and raw ~= "" then | |||
recruiter = raw | |||
end | end | ||
end | end | ||
-- Генеруємо код | |||
local output = {} | |||
table.insert(output, "{{MCC Player New") | |||
table.insert(output, "| nickname = " .. player_name) | |||
if | if stats and #stats >= 5 then | ||
table.insert(output, "| games = " .. (stats[2] or "0")) | |||
table.insert(output, "| wins = " .. (stats[3] or "0")) | |||
table.insert(output, "| losses = " .. (stats[4] or "0")) | |||
table.insert(output, "| winrate = " .. (stats[5] or "0")) | |||
else | |||
table.insert(output, "| games = 0") | |||
table.insert(output, "| wins = 0") | |||
table.insert(output, "| losses = 0") | |||
table.insert(output, "| winrate = 0") | |||
end | end | ||
table.insert(output, "| foty_rating = " .. foty_rating) | |||
table.insert(output, "| recruiter = " .. recruiter) | |||
table.insert(output, "| date_added = " .. date_added) | |||
if foundation and #foundation >= 2 then | |||
local found = mw.ustring.gsub(foundation[2] or "0", "[^%d]", "") | |||
table.insert(output, "| foundation = " .. found) | |||
else | |||
table.insert(output, "| foundation = 0") | |||
end | end | ||
if prize_pool and #prize_pool >= 2 then | |||
local prize = mw.ustring.gsub(prize_pool[2] or "0", "[^%d]", "") | |||
table.insert(output, "| prize_pool = " .. prize) | |||
else | else | ||
table.insert(output, "| prize_pool = 0") | |||
end | end | ||
if finalist and #finalist >= 2 then | |||
local fin = mw.ustring.gsub(finalist[2] or "0", "[^%d]", "") | |||
table.insert(output, "| finalist = " .. fin) | |||
else | |||
table.insert(output, "| finalist = 0") | |||
end | end | ||
table.insert(output, "}}") | |||
return | return "<pre>" .. table.concat(output, "\n") .. "</pre>" | ||
end | end | ||
function p. | -- Згенерувати код для всіх гравців | ||
function p.generate_all_players(frame) | |||
local players = get_all_players() | local players = get_all_players() | ||
if not players then | if not players then | ||
return "" | return "Помилка: Не вдалося отримати список гравців" | ||
end | end | ||
local output = {} | |||
table.insert(output, "== Згенеровані шаблони для всіх гравців ==\n") | |||
for i, player_name in ipairs(players) do | |||
table.insert(output, string.format("=== %d. %s ===", i, player_name)) | |||
-- Генеруємо код для цього гравця | |||
local player_code = p.generate_single_player({args = {player = player_name}}) | |||
if | table.insert(output, player_code) | ||
table.insert(output, "\n") | |||
-- Обмеження: тільки перші 10 гравців (щоб не перевантажити) | |||
if i >= 10 then | |||
table.insert(output, "\n''... і ще " .. (#players - 10) .. " гравців''") | |||
break | |||
end | end | ||
end | end | ||
return | return table.concat(output, "\n") | ||
end | end | ||
return p | return p | ||
Версія за 10:46, 1 грудня 2025
Документацію для цього модуля можна створити у Модуль:FetchData/документація
local p = {}
-- Функція для читання таблиці
local function get_table_from_page(page_name)
local title = mw.title.new(page_name)
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
return mw.ustring.sub(content, table_start, table_end + 1)
end
-- Парсинг таблиці
local function parse_table_rows(table_content)
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
return rows
end
local function parse_row_cells(row)
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
return cells
end
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 find_player_data(table_content, player_name)
if not table_content then return nil end
local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
local player_pattern = "%[%[" .. escaped .. "%]%]"
local rows = parse_table_rows(table_content)
for _, row in ipairs(rows) do
if mw.ustring.find(row, player_pattern) then
return parse_row_cells(row)
end
end
return nil
end
-- Отримати всіх гравців зі списку
local function get_all_players()
local table_content = get_table_from_page("Гравці")
if not table_content then return nil end
local rows = parse_table_rows(table_content)
local players = {}
for _, row in ipairs(rows) do
local cells = parse_row_cells(row)
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.generate_single_player(frame)
local player_name = frame.args.player or frame.args[1]
if not player_name or player_name == "" then
return "Помилка: Не вказано ім'я гравця"
end
-- Збираємо дані з різних таблиць
local stats = find_player_data(get_table_from_page("Статистика"), player_name)
local player_info = find_player_data(get_table_from_page("Гравці"), player_name)
local foundation = find_player_data(get_table_from_page("Фундація"), player_name)
local prize_pool = find_player_data(get_table_from_page("Призовий_фонд"), player_name)
local finalist = find_player_data(get_table_from_page("Фіналіст"), player_name)
-- FOTY Rating з таблиці "Період"
local foty_rating = "—"
local period_content = get_table_from_page("Період")
if period_content then
local content = mw.title.new("Період"):getContent()
local foty_section = mw.ustring.match(content, "==+%s*Фінал Року%s*==+(.-)\n==")
if not foty_section then
foty_section = mw.ustring.match(content, "==+%s*Фінал Року%s*==+(.*)")
end
if foty_section then
local table_start = mw.ustring.find(foty_section, "{|")
local table_end = mw.ustring.find(foty_section, "|}", table_start)
if table_start and table_end then
local table_content = mw.ustring.sub(foty_section, table_start, table_end + 1)
local foty_cells = find_player_data(table_content, player_name)
if foty_cells and #foty_cells >= 3 then
local place = mw.text.trim(foty_cells[1])
local rating = mw.text.trim(foty_cells[3])
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
foty_rating = rating .. " (" .. place .. " місце)"
end
end
end
end
end
-- Форматуємо дату
local date_added = "Лише Бог знає"
if player_info and #player_info >= 3 then
local raw_date = player_info[3]
if raw_date and raw_date ~= "" and raw_date ~= "-" then
local day, month, year = mw.ustring.match(raw_date, "(%d+)%.(%d+)%.(%d+)")
if day and month and year then
local end_date = os.time({year=2024, month=12, day=1})
local start_date = os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day)})
local days_diff = math.floor((end_date - start_date) / 86400)
date_added = string.format("%s (%d днів)", raw_date, days_diff)
end
end
end
-- Recruiter
local recruiter = "Не вказано"
if player_info and #player_info >= 2 then
local raw = mw.text.trim(player_info[2])
if raw ~= "Відсутній" and raw ~= "-" and raw ~= "" then
recruiter = raw
end
end
-- Генеруємо код
local output = {}
table.insert(output, "{{MCC Player New")
table.insert(output, "| nickname = " .. player_name)
if stats and #stats >= 5 then
table.insert(output, "| games = " .. (stats[2] or "0"))
table.insert(output, "| wins = " .. (stats[3] or "0"))
table.insert(output, "| losses = " .. (stats[4] or "0"))
table.insert(output, "| winrate = " .. (stats[5] or "0"))
else
table.insert(output, "| games = 0")
table.insert(output, "| wins = 0")
table.insert(output, "| losses = 0")
table.insert(output, "| winrate = 0")
end
table.insert(output, "| foty_rating = " .. foty_rating)
table.insert(output, "| recruiter = " .. recruiter)
table.insert(output, "| date_added = " .. date_added)
if foundation and #foundation >= 2 then
local found = mw.ustring.gsub(foundation[2] or "0", "[^%d]", "")
table.insert(output, "| foundation = " .. found)
else
table.insert(output, "| foundation = 0")
end
if prize_pool and #prize_pool >= 2 then
local prize = mw.ustring.gsub(prize_pool[2] or "0", "[^%d]", "")
table.insert(output, "| prize_pool = " .. prize)
else
table.insert(output, "| prize_pool = 0")
end
if finalist and #finalist >= 2 then
local fin = mw.ustring.gsub(finalist[2] or "0", "[^%d]", "")
table.insert(output, "| finalist = " .. fin)
else
table.insert(output, "| finalist = 0")
end
table.insert(output, "}}")
return "<pre>" .. table.concat(output, "\n") .. "</pre>"
end
-- Згенерувати код для всіх гравців
function p.generate_all_players(frame)
local players = get_all_players()
if not players then
return "Помилка: Не вдалося отримати список гравців"
end
local output = {}
table.insert(output, "== Згенеровані шаблони для всіх гравців ==\n")
for i, player_name in ipairs(players) do
table.insert(output, string.format("=== %d. %s ===", i, player_name))
-- Генеруємо код для цього гравця
local player_code = p.generate_single_player({args = {player = player_name}})
table.insert(output, player_code)
table.insert(output, "\n")
-- Обмеження: тільки перші 10 гравців (щоб не перевантажити)
if i >= 10 then
table.insert(output, "\n''... і ще " .. (#players - 10) .. " гравців''")
break
end
end
return table.concat(output, "\n")
end
return p