Модуль:FetchData6: відмінності між версіями
Admin (обговорення | внесок) (Створена сторінка: local p = {} --- Отримує дані гравця з конкретного сезону local function get_season_data(season_number, player_name) local season_names = { [1] = "Перший сезон", [2] = "Другий сезон", [3] = "Третій сезон", [4] = "Четвертий сезон", [5] = "П'ятий сезон", [6] = "Шостий сезон", [7] = "Сьомий...) |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 1: | Рядок 1: | ||
local p = {} | 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 | |||
--- Отримує дані гравця з конкретного сезону | --- Отримує дані гравця з конкретного сезону | ||
| Рядок 31: | Рядок 39: | ||
-- Знаходимо секцію "Рейтинг" | -- Знаходимо секцію "Рейтинг" | ||
local rating_section = mw.ustring.match(content, "==+%s*Рейтинг%s*==+(.-)==") | local rating_section = mw.ustring.match(content, "==+%s*Рейтинг%s*==+(.-)\n==") | ||
if not rating_section then | if not rating_section then | ||
rating_section = mw.ustring.match(content, "==+%s*Рейтинг%s*==+(.*)") | rating_section = mw.ustring.match(content, "==+%s*Рейтинг%s*==+(.*)") | ||
| Рядок 52: | Рядок 60: | ||
-- Шукаємо рядок з гравцем | -- Шукаємо рядок з гравцем | ||
local rows = {} | local rows = {} | ||
for row in mw.ustring.gmatch(table_content, "| | 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(.-) | -- Додаємо останній рядок | ||
local last_row = mw.ustring.match(table_content, "|%-\n(.-)\n|}") | |||
if last_row then | if last_row then | ||
table.insert(rows, last_row) | table.insert(rows, last_row) | ||
| Рядок 65: | Рядок 74: | ||
if mw.ustring.find(row, "%[%[" .. player_name .. "%]%]") then | if mw.ustring.find(row, "%[%[" .. player_name .. "%]%]") then | ||
local cells = {} | local cells = {} | ||
for | -- Розбиваємо рядок по | та || | ||
for line in mw.ustring.gmatch(row .. "\n", "([^\n]+)\n") do | |||
if | line = mw.text.trim(line) | ||
table.insert(cells, | if mw.ustring.match(line, "^|") then | ||
-- Видаляємо початковий | | |||
line = mw.ustring.gsub(line, "^|%s*", "") | |||
table.insert(cells, line) | |||
end | end | ||
end | end | ||
-- cells[1] = № | -- Структура таблиці: №, Пан/Пані, Σ, І, % | ||
-- cells[1] = № | |||
-- cells[2] = Пан/Пані (з вікі-посиланням) | |||
-- cells[3] = Σ (бали) | |||
-- cells[4] = І (ігор) | |||
-- cells[5] = % (відсоток перемог) | |||
if #cells >= 5 then | if #cells >= 5 then | ||
return { | return { | ||
Версія за 11:09, 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 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(.-)\n|}")
if last_row then
table.insert(rows, last_row)
end
for _, row in ipairs(rows) do
-- Перевіряємо чи є ім'я гравця в цьому рядку
if mw.ustring.find(row, "%[%[" .. player_name .. "%]%]") then
local cells = {}
-- Розбиваємо рядок по | та ||
for line in mw.ustring.gmatch(row .. "\n", "([^\n]+)\n") do
line = mw.text.trim(line)
if mw.ustring.match(line, "^|") then
-- Видаляємо початковий |
line = mw.ustring.gsub(line, "^|%s*", "")
table.insert(cells, line)
end
end
-- Структура таблиці: №, Пан/Пані, Σ, І, %
-- cells[1] = №
-- cells[2] = Пан/Пані (з вікі-посиланням)
-- cells[3] = Σ (бали)
-- cells[4] = І (ігор)
-- cells[5] = % (відсоток перемог)
if #cells >= 5 then
return {
place = mw.text.trim(cells[1]),
points = mw.text.trim(cells[3]),
games = mw.text.trim(cells[4]),
winrate = mw.text.trim(cells[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