Модуль:FetchData6: відмінності між версіями
Admin (обговорення | внесок) Немає опису редагування |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 58: | Рядок 58: | ||
local table_content = mw.ustring.sub(rating_section, table_start, table_end + 1) | local table_content = mw.ustring.sub(rating_section, table_start, table_end + 1) | ||
-- | -- Розбиваємо таблицю на рядки (між |- ) | ||
local | local current_row = {} | ||
for | local all_rows = {} | ||
for line in mw.ustring.gmatch(table_content .. "\n", "([^\n]*)\n") do | |||
line = mw.text.trim(line) | |||
if line == "|-" then | |||
if #current_row > 0 then | |||
table.insert(all_rows, current_row) | |||
end | |||
current_row = {} | |||
elseif mw.ustring.match(line, "^|[^-}]") then | |||
-- Це комірка (починається з | але не |- або |}) | |||
local cell_content = mw.ustring.gsub(line, "^|%s*", "") | |||
table.insert(current_row, cell_content) | |||
end | |||
end | end | ||
-- Додаємо останній рядок | -- Додаємо останній рядок якщо є | ||
if #current_row > 0 then | |||
if | table.insert(all_rows, current_row) | ||
table.insert( | |||
end | end | ||
for _, row in ipairs( | -- Шукаємо рядок з гравцем | ||
-- | for _, row in ipairs(all_rows) do | ||
-- row[1] = №, row[2] = Пан/Пані, row[3] = Σ, row[4] = І, row[5] = % | |||
if #row >= 5 then | |||
local player_cell = clean_wikilinks(row[2]) | |||
if | if player_cell == player_name then | ||
return { | return { | ||
place = mw.text.trim( | place = mw.text.trim(row[1]), | ||
points = mw.text.trim( | points = mw.text.trim(row[3]), | ||
games = mw.text.trim( | games = mw.text.trim(row[4]), | ||
winrate = mw.text.trim( | winrate = mw.text.trim(row[5]) | ||
} | } | ||
end | end | ||
Версія за 11:12, 30 листопада 2025
Документацію для цього модуля можна створити у Модуль:FetchData6/документація
local p = {}
--- Видаляє вікі-посилання [[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 get_season_data(season_number, player_name)
local season_names = {
[1] = "Перший сезон",
[2] = "Другий сезон",
[3] = "Третій сезон",
[4] = "Четвертий сезон",
[5] = "П'ятий сезон",
[6] = "Шостий сезон",
[7] = "Сьомий сезон",
[8] = "Восьмий сезон",
[9] = "Дев'ятий сезон"
}
local season_name = season_names[season_number]
if not season_name then
return nil
end
local title = mw.title.new(season_name)
if not title or not title.exists then
return nil
end
local content = title:getContent()
if not content then
return nil
end
-- Знаходимо секцію "Рейтинг"
local rating_section = mw.ustring.match(content, "==+%s*Рейтинг%s*==+(.-)\n==")
if not rating_section then
rating_section = mw.ustring.match(content, "==+%s*Рейтинг%s*==+(.*)")
end
if not rating_section then
return nil
end
-- Знаходимо таблицю
local table_start = mw.ustring.find(rating_section, "{|")
local table_end = mw.ustring.find(rating_section, "|}", table_start)
if not table_start or not table_end then
return nil
end
local table_content = mw.ustring.sub(rating_section, table_start, table_end + 1)
-- Розбиваємо таблицю на рядки (між |- )
local current_row = {}
local all_rows = {}
for line in mw.ustring.gmatch(table_content .. "\n", "([^\n]*)\n") do
line = mw.text.trim(line)
if line == "|-" then
if #current_row > 0 then
table.insert(all_rows, current_row)
end
current_row = {}
elseif mw.ustring.match(line, "^|[^-}]") then
-- Це комірка (починається з | але не |- або |})
local cell_content = mw.ustring.gsub(line, "^|%s*", "")
table.insert(current_row, cell_content)
end
end
-- Додаємо останній рядок якщо є
if #current_row > 0 then
table.insert(all_rows, current_row)
end
-- Шукаємо рядок з гравцем
for _, row in ipairs(all_rows) do
-- row[1] = №, row[2] = Пан/Пані, row[3] = Σ, row[4] = І, row[5] = %
if #row >= 5 then
local player_cell = clean_wikilinks(row[2])
if player_cell == player_name then
return {
place = mw.text.trim(row[1]),
points = mw.text.trim(row[3]),
games = mw.text.trim(row[4]),
winrate = mw.text.trim(row[5])
}
end
end
end
return nil
end
--- Головна функція для виведення таблиці досягнень
function p.season_achievements(frame)
local player_name = frame.args.player
if not player_name or player_name == "" then
return "<span style='color:red;'>Ім'я гравця не вказано</span>"
end
-- Створюємо HTML таблицю
local htmlTable = mw.html.create('table')
:addClass('wikitable sortable')
:css('width', '100%')
:css('font-size', '14.5px')
-- Додаємо заголовки
local headerRow = htmlTable:tag('tr')
headerRow:tag('th'):wikitext('Сезон')
headerRow:tag('th'):wikitext('Місце')
headerRow:tag('th'):wikitext('Бали')
headerRow:tag('th'):wikitext('% перемог')
headerRow:tag('th'):wikitext('Ігор')
local has_data = false
-- Проходимо по всіх 9 сезонах
for season = 1, 9 do
local data = get_season_data(season, player_name)
if data then
has_data = true
local row = htmlTable:tag('tr')
-- Сезон
local season_links = {
[1] = "[[Перший сезон#Рейтинг|01 сезон]]",
[2] = "[[Другий сезон#Рейтинг|02 сезон]]",
[3] = "[[Третій сезон#Рейтинг|03 сезон]]",
[4] = "[[Четвертий сезон#Рейтинг|04 сезон]]",
[5] = "[[П'ятий сезон#Рейтинг|05 сезон]]",
[6] = "[[Шостий сезон#Рейтинг|06 сезон]]",
[7] = "[[Сьомий сезон#Рейтинг|07 сезон]]",
[8] = "[[Восьмий сезон#Рейтинг|08 сезон]]",
[9] = "[[Дев'ятий сезон#Рейтинг|09 сезон]]"
}
row:tag('td')
:css('text-align', 'center')
:css('padding', '8px')
:wikitext(season_links[season])
-- Місце
row:tag('td')
:css('text-align', 'center')
:css('padding', '8px')
:wikitext(data.place)
-- Бали
row:tag('td')
:css('text-align', 'center')
:css('padding', '8px')
:wikitext(data.points)
-- % перемог
row:tag('td')
:css('text-align', 'center')
:css('padding', '8px')
:wikitext(data.winrate)
-- Ігор
row:tag('td')
:css('text-align', 'center')
:css('padding', '8px')
:wikitext(data.games)
end
end
if not has_data then
return "''Гравець не брав участі в жодному сезоні''"
end
return tostring(htmlTable)
end
return p