Немає опису редагування
Мітка: Скасовано
Немає опису редагування
Мітка: Скасовано
Рядок 1: Рядок 1:
local p = {}
local p = {}


--- Функція для виведення помилки на сторінку
-- Функція для читання таблиці
local function error_output(context, error_details)
local function get_table_from_page(page_name)
    local safe_details = error_details or "N/A"
     local title = mw.title.new(page_name)
    return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Details: %s]</span>",
     if not title or not title.exists then return nil end
        context or "Unknown Context", safe_details)
end
 
--- Видаляє вікі-посилання [[link|text]] і [[link]]
local function clean_wikilinks(text)
    if not text then return nil end
    -- [[link|text]] -> text
    text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
    -- [[link]] -> link
    text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
    return mw.text.trim(text)
end
 
----------------------------------------------------------------------
-- ГОЛОВНИЙ ПАРСЕР ДЛЯ ГОРИЗОНТАЛЬНИХ ТАБЛИЦЬ
----------------------------------------------------------------------
local function fetch_from_table(page_title, player_name, column_index)
   
    if not player_name or player_name == "" then
        return error_output("No Player Name", 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()
     local content = title:getContent()
     if not content then  
     if not content then return nil end
        return error_output("Content Read Fail", page_title)
    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 error_output("Table Missing", page_title)
    end
   
    local table_content = mw.ustring.sub(content, table_start, table_end + 1)
   
    -- Екрануємо спеціальні символи в імені гравця
    local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
    local player_pattern = "%[%[" .. escaped .. "%]%]"
   
    -- Шукаємо рядки, які починаються з |-
    local rows = {}
    for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do
        table.insert(rows, row)
    end
      
      
     -- Останній рядок (до |})
     return mw.ustring.sub(content, table_start, table_end + 1)
    local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}")
    if last_row then
        table.insert(rows, last_row)
    end
   
    -- Обробляємо кожен рядок
    for _, row in ipairs(rows) do
        if mw.ustring.find(row, player_pattern) then
            -- Розбиваємо рядок на комірки
            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
           
            -- Повертаємо потрібну колонку
            if column_index <= #cells then
                return clean_wikilinks(cells[column_index])
            else
                return error_output("Column Out of Range",
                    string.format("Requested col %d, found %d cells", column_index, #cells))
            end
        end
    end
   
    return error_output("Player Not Found", player_name)
end
end


----------------------------------------------------------------------
-- Парсинг таблиці
-- ФУНКЦІЇ, ЩО ВИКЛИКАЮТЬСЯ З ШАБЛОНУ
local function parse_table_rows(table_content)
----------------------------------------------------------------------
function p.recruiter(frame)
    local name = frame.args.player
    -- Don't clean wikilinks - keep them for clickable links
    local title = mw.title.new("Гравці")
    if not title or not title.exists then
        return error_output("Page Missing", "Гравці")
    end
   
    local content = title:getContent()
    if not content then
        return error_output("Content Read Fail", "Гравці")
    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 error_output("Table Missing", "Гравці")
    end
   
    local table_content = mw.ustring.sub(content, table_start, table_end + 1)
    local escaped = mw.ustring.gsub(name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
    local player_pattern = "%[%[" .. escaped .. "%]%]"
   
     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
        table.insert(rows, last_row)
     return rows
    end
      
    for _, row in ipairs(rows) do
        if mw.ustring.find(row, player_pattern) then
            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
           
            if 4 <= #cells then
                local raw = mw.text.trim(cells[4])
                if raw == "Відсутній" or raw == "-" then
                    return "Не вказано"
                end
                -- Return with wikilinks intact
                return raw
            end
        end
    end
   
    return "Не вказано"
end
end


function p.date_added(frame)
local function parse_row_cells(row)
     local name = frame.args.player
     local cells = {}
     local raw = fetch_from_table("Гравці", name, 3)
     row = mw.ustring.gsub(row, "^%s*|%s*", "")
   
     for cell in mw.ustring.gmatch(row, "([^|]+)") do
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then
         local trimmed = mw.text.trim(cell)
        return "Лише Бог знає"
         if trimmed ~= "" then table.insert(cells, trimmed) end
     end
   
    if not raw or raw == "" or raw == "-" or raw == "Відсутній" then
        return "Лише Бог знає"
    end
   
    -- Парсимо дату у форматі DD.MM.YYYY
    local day, month, year = mw.ustring.match(raw, "(%d+)%.(%d+)%.(%d+)")
   
    if day and month and year then
        -- Кінцева дата: 26.10.2024
         local end_date = os.time({year=2024, month=10, day=26})
        local start_date = os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day)})
          
        local days_diff = math.floor((end_date - start_date) / 86400)
       
        return string.format("%s (%d днів)", raw, days_diff)
     end
     end
   
     return cells
     return "Лише Бог знає"
end
end


function p.prize_pool(frame)
local function clean_wikilinks(text)
     local name = frame.args.player
     if not text then return nil end
    local raw = fetch_from_table("Призовий_фонд", name, 2)  
    text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
   
     text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then
    return mw.text.trim(text)
        return "0 ₴"
     end
   
    if raw then
        raw = mw.ustring.gsub(raw, "[^%d%s]", "")
        raw = mw.text.trim(raw)
    end
   
    return (raw or "0") .. " ₴"
end
end


-- Додай цю функцію до FetchData2 (модуль:FetchData2)
-- Знайти дані гравця в таблиці
 
local function find_player_data(table_content, player_name)
function p.foty_rating(frame)
     if not table_content then return nil end
     local name = frame.args.player
      
      
    if not name or name == "" then
     local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
        return "0"
    end
   
    local title = mw.title.new("Період")
    if not title or not title.exists then
        return "0"
    end
   
    local content = title:getContent()
    if not content then
        return "0"
    end
   
    -- Шукаємо секцію "Фінал Року"
    local section_start = mw.ustring.find(content, "== Фінал Року ==")
    if not section_start then
        return "0"
    end
   
    -- Знаходимо таблицю після секції
    local table_start = mw.ustring.find(content, "{|", section_start)
    if not table_start then
        return "0"
    end
   
    local table_end = mw.ustring.find(content, "|}", table_start)
    if not table_end then
        return "0"
    end
   
    local table_content = mw.ustring.sub(content, table_start, table_end + 1)
   
    -- Екрануємо спеціальні символи в імені гравця
     local escaped = mw.ustring.gsub(name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
     local player_pattern = "%[%[" .. escaped .. "%]%]"
     local player_pattern = "%[%[" .. escaped .. "%]%]"
      
      
    -- Збираємо всі рядки
     local rows = 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
      
      
    -- Обробляємо кожен рядок
     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
             local cells = {}
             return parse_row_cells(row)
            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
           
            -- Колонка 1 - це місце (№)
            -- Колонка 3 - це рейтинг (Σ)
            if #cells >= 3 then
                local place = mw.text.trim(cells[1])
                local rating = cells[3]
               
                -- Видаляємо HTML теги з рейтингу
                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
                    return string.format("%s (%s місце)", rating, place)
                end
            end
           
            return "0"
         end
         end
     end
     end
      
      
     return "0"
     return nil
end
end


function p.foundation(frame)
-- Отримати всіх гравців зі списку
     local name = frame.args.player
local function get_all_players()
    local raw = fetch_from_table("Фундація", name, 3)
     local table_content = get_table_from_page("Гравці")
    if not table_content then return nil end
      
      
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then
     local rows = parse_table_rows(table_content)
        return "0 ₴"
     local players = {}
     end
      
      
     if raw then  
     for _, row in ipairs(rows) do
        raw = mw.ustring.gsub(raw, "[^%d%s]", "")
        local cells = parse_row_cells(row)
        raw = mw.text.trim(raw)
        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 (raw or "0") .. " ₴"
     return players
end
end


function p.finalist(frame)
-- Згенерувати код для одного гравця
     local name = frame.args.player
function p.generate_single_player(frame)
    local raw = fetch_from_table("Фіналіст", name, 3)
     local player_name = frame.args.player or frame.args[1]
      
      
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then  
     if not player_name or player_name == "" then
         return "0/9"
         return "Помилка: Не вказано ім'я гравця"
     end
     end
      
      
     if not raw or raw == "" or raw == "-" then
     -- Збираємо дані з різних таблиць
        return "0/9"
    local stats = find_player_data(get_table_from_page("Статистика"), player_name)
     end
    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 number_only = mw.ustring.gsub(raw, "[^%d]", "")
    local foty_rating = "—"
   
     local period_content = get_table_from_page("Період")
    -- Перетворюємо в число
    if period_content then
    local count = tonumber(number_only)
        local content = mw.title.new("Період"):getContent()
   
        local foty_section = mw.ustring.match(content, "==+%s*Фінал Року%s*==+(.-)\n==")
    if count then
        if not foty_section then
        return string.format("%d/9", count)
            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
      
      
     return "0/9"
     -- Форматуємо дату
end
    local date_added = "Лише Бог знає"
 
     if player_info and #player_info >= 3 then
function p.games_count(frame)
        local raw_date = player_info[3]
     local name = frame.args.player
        if raw_date and raw_date ~= "" and raw_date ~= "-" then
    local raw = fetch_from_table("Статистика", name, 3)
            local day, month, year = mw.ustring.match(raw_date, "(%d+)%.(%d+)%.(%d+)")
   
            if day and month and year then
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then
                local end_date = os.time({year=2024, month=12, day=1})
        return "0"
                local start_date = os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day)})
    end
                local days_diff = math.floor((end_date - start_date) / 86400)
   
                date_added = string.format("%s (%d днів)", raw_date, days_diff)
    return raw or "0"
            end
end
         end
 
function p.wins_count(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 "0"
    end
   
    return raw or "0"
end
 
function p.losses_count(frame)
    local name = frame.args.player
    local raw = fetch_from_table("Статистика", name, 5)
   
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then
        return "0"
    end
   
    return raw or "0"
end
 
function p.win_rate(frame)
    local name = frame.args.player
    local raw = fetch_from_table("Статистика", name, 6)
   
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then
         return "0%"
     end
     end
      
      
     if raw then
    -- Recruiter
         -- Якщо немає знаку %, додаємо його
    local recruiter = "Не вказано"
        if not mw.ustring.find(raw, "%%") then
     if player_info and #player_info >= 2 then
             return raw .. "%"
         local raw = mw.text.trim(player_info[2])
        if raw ~= "Відсутній" and raw ~= "-" and raw ~= "" then
             recruiter = raw
         end
         end
        return raw
     end
     end
      
      
     return "0%"
     -- Генеруємо код
end
    local output = {}
 
    table.insert(output, "{{MCC Player New")
function p.win_rate_colored(frame)
     table.insert(output, "| nickname = " .. player_name)
     local name = frame.args.player
    local raw = fetch_from_table("Статистика", name, 6)
      
      
     if type(raw) == "string" and mw.ustring.find(raw, "Error") then
     if stats and #stats >= 5 then
         return "0%"
        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
      
      
     if not raw or raw == "" then
     table.insert(output, "| foty_rating = " .. foty_rating)
        return "0%"
    table.insert(output, "| recruiter = " .. recruiter)
     end
     table.insert(output, "| date_added = " .. date_added)
      
      
     -- Видаляємо % та конвертуємо в число
     if foundation and #foundation >= 2 then
    local percent_str = mw.ustring.gsub(raw, "%%", "")
        local found = mw.ustring.gsub(foundation[2] or "0", "[^%d]", "")
    local percent_num = tonumber(percent_str)
        table.insert(output, "| foundation = " .. found)
      
     else
    if not percent_num then
         table.insert(output, "| foundation = 0")
         return raw
     end
     end
      
      
     -- Визначаємо колір
     if prize_pool and #prize_pool >= 2 then
    local color = ""
        local prize = mw.ustring.gsub(prize_pool[2] or "0", "[^%d]", "")
    if percent_num < 50 then
         table.insert(output, "| prize_pool = " .. prize)
        color = "indianred"
    elseif percent_num > 50 then
         color = "gold"
     else
     else
         -- Рівно 50% - білий
         table.insert(output, "| prize_pool = 0")
        color = "white"
     end
     end
      
      
     -- Додаємо % якщо його немає
     if finalist and #finalist >= 2 then
    local display_text = raw
        local fin = mw.ustring.gsub(finalist[2] or "0", "[^%d]", "")
    if not mw.ustring.find(raw, "%%") then
         table.insert(output, "| finalist = " .. fin)
         display_text = raw .. "%"
    else
        table.insert(output, "| finalist = 0")
     end
     end
      
      
     return string.format("<span style='color:%s;'>%s</span>", color, display_text)
     table.insert(output, "}}")
end
 
--- Отримує всіх гравців з таблиці "Гравці" в порядку дати приєднання
local function get_all_players()
    local title = mw.title.new("Гравці")
    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
   
    local table_content = mw.ustring.sub(content, table_start, table_end + 1)
   
    local players = {}
    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
   
    for _, row in ipairs(rows) do
        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
       
        -- Колонка 2 - ім'я гравця
        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
     return "<pre>" .. table.concat(output, "\n") .. "</pre>"
end
end


function p.prev_player(frame)
-- Згенерувати код для всіх гравців
    local name = frame.args.player
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
   
    for i, player in ipairs(players) do
        if player == name then
            if i > 1 then
                return players[i - 1]
            else
                return "" -- Перший гравець, немає попереднього
            end
        end
     end
     end
      
      
     return ""
     local output = {}
end
    table.insert(output, "== Згенеровані шаблони для всіх гравців ==\n")
 
function p.next_player(frame)
    local name = frame.args.player
    local players = get_all_players()
      
      
     if not players then
     for i, player_name in ipairs(players) do
         return ""
         table.insert(output, string.format("=== %d. %s ===", i, player_name))
    end
       
   
        -- Генеруємо код для цього гравця
    for i, player in ipairs(players) do
        local player_code = p.generate_single_player({args = {player = player_name}})
         if player == name then
        table.insert(output, player_code)
             if i < #players then
        table.insert(output, "\n")
                return players[i + 1]
       
            else
        -- Обмеження: тільки перші 10 гравців (щоб не перевантажити)
                return "" -- Останній гравець, немає наступного
         if i >= 10 then
             end
             table.insert(output, "\n''... і ще " .. (#players - 10) .. " гравців''")
             break
         end
         end
     end
     end
      
      
     return ""
     return table.concat(output, "\n")
end
 
function p.prev_player_link(frame)
    local name = frame.args.player
    local prev = p.prev_player(frame)
   
    if prev == "" or not prev then
        return ""
    end
   
    return string.format(
        '<a href="/index.php/%s" style="width:48px; height:48px; display:flex; align-items:center; justify-content:center; text-decoration:none; border:2px solid #333; border-radius:8px;"><span style="color:gold; font-size:24px; font-weight:bold;">←</span></a>',
        mw.uri.encode(prev, "WIKI")
    )
end
 
function p.next_player_link(frame)
    local name = frame.args.player
    local next = p.next_player(frame)
   
    if next == "" or not next then
        return ""
    end
   
    return string.format(
        '<a href="/index.php/%s" style="width:48px; height:48px; display:flex; align-items:center; justify-content:center; text-decoration:none; border:2px solid #333; border-radius:8px;"><span style="color:gold; font-size:24px; font-weight:bold;">→</span></a>',
        mw.uri.encode(next, "WIKI")
    )
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