Модуль:StatEngine/RatingBuilder

Материал из ЧТМ
Версия от 14:08, 1 мая 2026; Подбырин (обсуждение | вклад) (Восстановлена версия 67707 участника Подбырин (Restorer))
Перейти к навигации Перейти к поиску
Документация Документация

Сейчас проводятся усиленные тесты на странице RatingBuilder. Пока модуль категорически не готов к использованию, поскольку требует очень серьёзной отладки, но потенциал у него колоссальный.

Скрипты для отладки:

  • StatEngine/Tester — для сравнения со старой БД Data/Rating.
    • Вызов: {{#invoke:StatEngine/Tester|test_year|ГОД}}
  • StatEngine/RatingBuilder/Debug — для тупого вывода всех результатов за указанный год.
    • Вызов: {{#invoke:StatEngine/RatingBuilder/Debug|run|ГОД}}

Пожалуйста, добавляйте категории на страницу документации.

-- =========================================================================
-- Модуль:StatEngine/RatingBuilder
-- =========================================================================

local Builder = {}
local TeamsDB = require('Модуль:Data/Teams')

-- =========================================================
-- ЖЕСТКАЯ ЛЕСТНИЦА РАНГОВ
-- =========================================================
local ranks = {
    ["оп"] = 1,
    ["о"] = 2, ["о1"] = 3, ["о2"] = 4, ["о3"] = 5,
    ["о1-3г-no"] = 10, ["о-8г-no"] = 11, ["о-7г-no"] = 12, ["о-6г-no"] = 13,
    ["о-5г-no"] = 14, ["о-4г-no"] = 15, ["о-3г-no"] = 16,
    ["о1-6г"] = 20, ["о1-5г"] = 21, ["о1-4г"] = 22, ["о1-3г"] = 23, ["о1-2г"] = 24, ["о1-1г"] = 25,
    ["о2-6г"] = 30, ["о2-5г"] = 31, ["о2-4г"] = 32, ["о2-3г"] = 33, ["о2-2г"] = 34, ["о2-1г"] = 35,
    ["о3-6г"] = 40, ["о3-5г"] = 41, ["о3-4г"] = 42, ["о3-3г"] = 43, ["о3-2г"] = 44, ["о3-1г"] = 45,
    ["о-8г"] = 50, ["о-7г"] = 51, ["о-6г"] = 52, ["о-5г"] = 53, ["о-4г"] = 54, ["о-3г"] = 55, ["о-2г"] = 56, ["о-1г"] = 57,
    ["ос"] = 60, ["ос-5г"] = 61, ["ос-4г"] = 62, ["ос-3г"] = 63, ["ос-2г"] = 64, ["ос-1г"] = 65,
    
    -- === ЛИГА НАЦИЙ ===
    ["4гD"]=101, ["3гD-no"]=102, ["3гD"]=103, ["2гD"]=104, ["1гD"]=105, ["чфD"]=106, ["пD"]=107, ["фD"]=108, ["побD"]=109,
    ["4гC"]=111, ["3гC-no"]=112, ["3гC"]=113, ["2гC"]=114, ["1гC"]=115, ["чфC"]=116, ["пC"]=117, ["фC"]=118, ["побC"]=119,
    ["4гB"]=121, ["3гB-no"]=122, ["3гB"]=123, ["2гB"]=124, ["1гB"]=125, ["чфB"]=126, ["пB"]=127, ["фB"]=128, ["побB"]=129,
    ["4гA"]=131, ["3гA-no"]=132, ["3гA"]=133, ["2гA"]=134, ["1гA"]=135, ["чфA"]=136, ["пA"]=137, ["фA"]=138,
    
    -- === ФИНАЛЬНЫЕ ТУРНИРЫ ===
    ["8"] = 270, ["7"] = 271, ["6"] = 272, ["5"] = 273,
    ["6г"] = 280, ["5г"] = 281, ["4г"] = 282, ["3г"] = 283, ["2г"] = 284, ["1г"] = 285,
    ["в"] = 290, ["чф"] = 291, ["д"] = 292, ["б"] = 293, ["ф"] = 294, ["ч"] = 295
}

-- =========================================================
-- РУЧНЫЕ ПЕРЕОПРЕДЕЛЕНИЯ (КОСТЫЛИ)
-- Формат: [год] = { ["Турнир"] = { ["Команда"] = "Ключ" } }
-- =========================================================
-- иногда логика настолько сложна, что ГОРАЗДО проще написать руками,
-- особенно Кубки Океании с дичайшими системами с ума сходят.
local manual_overrides = {
    [2009] = {
        ["КОк"] = {
            ["САМ"] = "ос-2г", ["МАР"] = "ос-2г", ["НОВ"] = "ос-2г", ["ПАЛ"] = "о-3г", ["ТОН"] = "о-4г",
            ["МИК"] = "о-4г", ["ТУВ"] = "о-3г", ["НАУ"] = "о-3г", ["ОКУ"] = "о-3г",
        }
    },
    [2013] = {
        ["КОк"] = {
            ["НОВ"] = "о1", ["НИУ"] = "о1", ["ФРА"] = "о1", ["МИК"] = "о1", ["ТОН"] = "о1", ["НОР"] = "о1", 
            ["ТУВ"] = "о1", ["НАУ"] = "ос", ["ПАЛ"] = "ос", ["ПАП"] = "ос", ["ОКУ"] = "ос",
        },
        ["КАм"] = {
            ["ПУЭ"] = "ос",
        }
    },
    [2014] = {
        ["ЧТМ"] = {
            ["ПАЛ"] = "оп", ["ПАП"] = "оп",
        }
    },
    [2017] = {
        ["КОк"] = {
            ["ПАП"] = "о3", ["МИД"] = "о3", ["НОВ"] = "о2", ["ФРА"] = "о1", ["КРБ"] = "ос",
            ["МИК"] = "о2", ["ПАЛ"] = "ос", ["ТОН"] = "о1", ["ОКУ"] = "о3", ["УОЛ"] = "о2",
            ["МАР"] = "о3", ["ТОК"] = "о1", ["НОР"] = "о2", ["ТУВ"] = "о1",
        }
    },
    [2020] = {
        ["КОк"] = {
            ["ПАП"] = "о2-3г", ["МИД"] = "о1", ["НИУ"] = "о1", ["НАУ"] = "о1",
            ["ФРА"] = "о2-3г", ["МИК"] = "о2-3г", ["ТОН"] = "о1", ["УОЛ"] = "о1",
            ["ТОК"] = "о1", ["НОР"] = "о1", ["ТУВ"] = "о1",
        }
    },
    [2024] = {
        ["КОк"] = {
            ["МИД"] = "о1", ["НИУ"] = "о1", ["ХЁР"] = "о1", ["ТОН"] = "о1",
            ["ОКУ"] = "о1", ["КЕР"] = "о1", ["УОЛ"] = "о1", ["ТОК"] = "о1",
            ["ТАС"] = "о1", ["НОР"] = "о1", ["ТУВ"] = "о1",
        }
    },
    [2033] = {
        ["ККо"] = {
            ["ДОМ"] = "5", ["КИР"] = "6", ["АСА"] = "7", ["МОЗ"] = "8",
        }
    },
    [2037] = {
        ["ККо"] = {
            ["ОСТ"] = "5", ["НИК"] = "6", ["ГВА"] = "7", ["САН"] = "8",
        }
    },
    [2039] = {
        ["ЛНа"] = {
            ["ОРО"] = "3гC-no", ["ЧАТ"] = "4гC", ["АВС"] = "3гC", ["ЗАП"] = "3гC-no",
        }
    },
}

-- =========================================================
-- УТИЛИТЫ
-- =========================================================
local function get_match_result(m, rounds)
    local t1, t2 = m[1], m[2]
    if rounds == 1 then
        local g1, g2 = m[3] or 0, m[4] or 0
        if g1 > g2 then return t1, t2 end
        if g2 > g1 then return t2, t1 end
        local p1, p2 = m[6] or 0, m[7] or 0
        if p1 > p2 then return t1, t2 end
        if p2 > p1 then return t2, t1 end
    else
        -- 1. Сравниваем общую сумму голов
        local g1_agg = (m[3] or 0) + (m[5] or 0)
        local g2_agg = (m[4] or 0) + (m[6] or 0)
        if g1_agg > g2_agg then return t1, t2 end
        if g2_agg > g1_agg then return t2, t1 end
        
        -- 2. Пенальти (Индексы 8 и 9)
        local p1, p2 = m[8] or 0, m[9] or 0
        if p1 > p2 then return t1, t2 end
        if p2 > p1 then return t2, t1 end
        
        -- 3. ПРАВИЛО ГОСТЕВЫХ ГОЛОВ (Индексы 5 и 4)
        local t1_away = m[5] or 0
        local t2_away = m[4] or 0
        if t1_away > t2_away then return t1, t2 end
        if t2_away > t1_away then return t2, t1 end
    end
    return nil, nil
end

local function get_group_stats(team, stages)
    local pts, gf, ga = 0, 0, 0
    for stage_name, s_data in pairs(stages) do
        if s_data.type == "group" and string.match(stage_name, "Group") then
            for _, m in ipairs(s_data.matches) do
                if m[1] == team then
                    local g1, g2 = m[3] or 0, m[4] or 0
                    gf = gf + g1; ga = ga + g2
                    if g1 > g2 then pts = pts + 3 elseif g1 == g2 then pts = pts + 1 end
                elseif m[2] == team then
                    local g1, g2 = m[4] or 0, m[3] or 0
                    gf = gf + g1; ga = ga + g2
                    if g1 > g2 then pts = pts + 3 elseif g1 == g2 then pts = pts + 1 end
                end
            end
        end
    end
    return pts, gf - ga, gf
end

local function assign_places_by_stats(team_results, source_key, target_keys, stages)
    local teams = {}
    for t, res in pairs(team_results) do
        if res == source_key then
            local pts, gd, gf = get_group_stats(t, stages)
            table.insert(teams, {name = t, pts = pts, gd = gd, gf = gf})
        end
    end
    if #teams == 0 then return end
    table.sort(teams, function(a, b)
        if a.pts ~= b.pts then return a.pts > b.pts end
        if a.gd ~= b.gd then return a.gd > b.gd end
        if a.gf ~= b.gf then return a.gf > b.gf end
        return a.name < b.name
    end)
    local current_idx = 1
    for i, t_info in ipairs(teams) do
        if i > 1 then
            local prev = teams[i-1]
            if t_info.pts == prev.pts and t_info.gd == prev.gd and t_info.gf == prev.gf then
            else current_idx = current_idx + 1 end
        end
        local key = target_keys[current_idx] or target_keys[#target_keys]
        team_results[t_info.name] = key
    end
end

local function update_result(tab, team, new_key)
    if not new_key then return end
    local cur_rank = tab[team] and ranks[tab[team]] or 0
    local new_rank = ranks[new_key] or 0
    if new_rank > cur_rank then tab[team] = new_key end
end

-- =========================================================
-- СПЕЦ-ОБРАБОТЧИК ЛИГИ НАЦИЙ (ЛНа)
-- =========================================================
local function process_lna(stages)
    local team_results = {}
    
    -- 1. ГРУППЫ (1R)
    for stage_name, s_data in pairs(stages) do
        local r, div, reg = stage_name:match("^(%d)R_([A-D])_([^_]+)")
        if r == "1" and s_data.type == "group" then
            local num_teams = #s_data.standings
            for place, row in ipairs(s_data.standings) do
                local team = row[1]
                local key = place .. "г" .. div
                
                -- Халявщики в низших дивизионах (D везде, C в Океании)
                if num_teams == 3 and place == 3 and (div == "D" or (div == "C" and reg == "Oceania")) then
                    local pts = get_group_stats(team, {[stage_name] = s_data})
                    local req_pts = (s_data.number_of_rounds == 1) and 2 or 4
                    if pts < req_pts then key = key .. "-no" end
                end
                update_result(team_results, team, key)
            end
        end
    end
    
    -- 2. ПЛЕЙ-ОФФ ДИВИЗИОНОВ (2R)
    for stage_name, s_data in pairs(stages) do
        local r, div = stage_name:match("^(%d)R_([A-D])_")
        if r == "2" and s_data.type == "knockout" then
            for _, match in ipairs(s_data.matches) do
                local winner, loser = get_match_result(match, s_data.number_of_rounds)
                if winner and loser then
                    if stage_name:match("Quarterfinal") then update_result(team_results, loser, "чф" .. div)
                    elseif stage_name:match("Semifinal") then update_result(team_results, loser, "п" .. div)
                    elseif stage_name:match("Final") then
                        update_result(team_results, loser, "ф" .. div)
                        if div ~= "A" then update_result(team_results, winner, "поб" .. div) end
                    end
                end
            end
        end
    end
    
    -- 3. ГЛОБАЛЬНЫЙ ПЛЕЙ-ОФФ ЗА МЕДАЛИ (3R)
    for stage_name, s_data in pairs(stages) do
        local r = stage_name:match("^(%d)R_")
        if r == "3" and s_data.type == "knockout" then
            for _, match in ipairs(s_data.matches) do
                local winner, loser = get_match_result(match, s_data.number_of_rounds)
                if winner and loser then
                    if stage_name:match("3rdPlace") then update_result(team_results, winner, "б"); update_result(team_results, loser, "д")
                    elseif stage_name:match("Final") then update_result(team_results, winner, "ч"); update_result(team_results, loser, "ф")
                    elseif stage_name:match("Semifinal") then update_result(team_results, loser, "б") -- Если нет 3-го места
                    end
                end
            end
        end
    end
    
    return team_results
end

-- =========================================================
-- ОБРАБОТЧИК СТАНДАРТНОГО ТУРНИРА
-- =========================================================
local function process_tournament(full_name, stages, global_playoffs)
    local team_results = {}
    local is_qual = string.match(full_name, "Qual") ~= nil
    
    local max_r_by_region = {}
    for stage_name in pairs(stages) do
        local r, reg = stage_name:match("^(%d+)R_([^_]+)")
        if not r then r = stage_name:match("^(%d+)R_"); reg = "Global" end
        if r then
            r = tonumber(r)
            if reg == "GroupA" or reg:match("Group") or reg == "Playoffs" then reg = "Global" end
            max_r_by_region[reg] = math.max(max_r_by_region[reg] or 0, r)
        end
    end

    local function get_qual_key(stage_name, place)
        local r, reg = stage_name:match("^(%d+)R_([^_]+)")
        if not r then r = stage_name:match("^(%d+)R_"); reg = "Global" end
        if reg == "GroupA" or (reg and reg:match("Group")) then reg = "Global" end
        if not r then return string.match(stage_name, "Playoff") and "ос" or "о" end
        r = tonumber(r)
        if r == 0 then return "оп" end
        
        -- ФИКС 2034/2038: Если в глобале раундов больше, чем в регионе, значит отбор многоступенчатый для всех
        local max_r = max_r_by_region[reg] or 1
        if max_r_by_region["Global"] and max_r_by_region["Global"] > max_r then
            max_r = max_r_by_region["Global"]
        end
        
        local prefix = (max_r <= 1) and "о-" or ("о" .. r .. "-")
        if place then return prefix .. place .. "г" end
        if string.match(stage_name, "Playoff") then return "ос" end
        return "о" .. r
    end

    for stage_name, s_data in pairs(stages) do
        if s_data.type == "group" and string.match(stage_name, "Group") then
            for place, row in ipairs(s_data.standings) do
                local team = row[1]
                local key = ""
                if not is_qual then key = place .. "г"
                else
                    key = get_qual_key(stage_name, place)
                    if key and key ~= "оп" and #s_data.standings == 3 and place == 3 then
                        local pts = get_group_stats(team, {[stage_name] = s_data})
                        local req_pts = (s_data.number_of_rounds == 1) and 2 or 4
                        if pts < req_pts then key = key .. "-no" end
                    end
                end
                update_result(team_results, team, key)
            end
        end
    end
    
    local has_3rd_place_match = stages["FR_3rdPlace"] ~= nil
    for stage_name, s_data in pairs(stages) do
        if s_data.type == "knockout" then
            local is_playoff = string.match(stage_name, "Playoff") ~= nil
            for _, match in ipairs(s_data.matches) do
                local winner, loser = get_match_result(match, s_data.number_of_rounds)
                if winner and loser then
                    if is_qual then
                        if is_playoff then
                            global_playoffs[winner] = true
                            global_playoffs[loser] = true
                            if not team_results[loser] then update_result(team_results, loser, "ос") end
                        else
                            local key = get_qual_key(stage_name, nil)
                            if key then update_result(team_results, loser, key) end
                        end
                    else
                        if stage_name == "FR_Last 16" or stage_name == "FR_Last16" then update_result(team_results, loser, "в")
                        elseif stage_name == "FR_Quarterfinal" then update_result(team_results, loser, "чф")
                        elseif stage_name == "FR_3rdPlace" then update_result(team_results, winner, "б"); update_result(team_results, loser, "д")
                        elseif stage_name == "FR_Final" then update_result(team_results, winner, "ч"); update_result(team_results, loser, "ф")
                        elseif stage_name == "FR_Semifinal" and not has_3rd_place_match then update_result(team_results, loser, "б")
                        end
                    end
                end
            end
        end
    end
    
    for t in pairs(global_playoffs) do
        if team_results[t] then
            if team_results[t] == "о" or team_results[t] == "оп" or string.match(team_results[t], "^о%d$") then
                update_result(team_results, t, "ос")
            else
                local new_key = string.gsub(team_results[t], "^о%d?%-", "ос-")
                update_result(team_results, t, new_key)
            end
        else
            update_result(team_results, t, "ос")
        end
    end
    
    return team_results
end

-- =========================================================
-- ГЛАВНАЯ ФУНКЦИЯ
-- =========================================================
function Builder.build_year(year, db_tournaments)
    local output = {} 
    local global_playoff_participants = {} 
    
    for full_tourney_name, stages in pairs(db_tournaments) do
        local base_tourney = string.match(full_tourney_name, "^([^_]+)")
        if not output[base_tourney] then output[base_tourney] = {} end
        if not global_playoff_participants[base_tourney] then global_playoff_participants[base_tourney] = {} end
        
        local team_results = {}
        
        if base_tourney == "ЛНа" then
            team_results = process_lna(stages)
        else
            team_results = process_tournament(full_tourney_name, stages, global_playoff_participants[base_tourney])
        end
        
        -- ХУКИ
        if year == 2009 and base_tourney == "КОк" then assign_places_by_stats(team_results, "2г", {"б", "д"}, stages) end
        if base_tourney == "ККо" and (year == 2009 or year == 2017 or year == 2025 or year == 2029) then
            assign_places_by_stats(team_results, "3г", {"5", "6"}, stages)
            assign_places_by_stats(team_results, "4г", {"7", "8"}, stages)
        end
        if year == 2013 and base_tourney == "ККо" then
            if stages["FR_Playoffs"] then
                for _, m in ipairs(stages["FR_Playoffs"].matches) do
                    local _, loser = get_match_result(m, stages["FR_Playoffs"].number_of_rounds)
                    if loser then team_results[loser] = "5" end
                end
            end
            assign_places_by_stats(team_results, "2г", {"6"}, stages)
            assign_places_by_stats(team_results, "3г", {"7", "8"}, stages)
        end
        if year == 2021 and base_tourney == "ККо" then
            for t, res in pairs(team_results) do
                if res == "3г" then team_results[t] = "б" elseif res == "4г" then team_results[t] = "д"
                elseif res == "5г" then team_results[t] = "5" elseif res == "6г" then team_results[t] = "6" end
            end
        end
        if year == 2015 and base_tourney == "КФе" then
            if stages["FR_Stage2_Losers"] then
                for _, m in ipairs(stages["FR_Stage2_Losers"].matches) do
                    local _, loser = get_match_result(m, stages["FR_Stage2_Losers"].number_of_rounds)
                    if loser then team_results[loser] = "д" end
                end
            end
            if stages["FR_Semifinal"] then
                for _, m in ipairs(stages["FR_Semifinal"].matches) do
                    local _, loser = get_match_result(m, stages["FR_Semifinal"].number_of_rounds)
                    if loser then team_results[loser] = "б" end
                end
            end
        end
        if year == 2013 and base_tourney == "КЮжАм" then
            for t, res in pairs(team_results) do if res == "3г" then team_results[t] = "б" end end
        end
        
        for t, k in pairs(team_results) do update_result(output[base_tourney], t, k) end
    end
    
    -- ИМПОРТ ОТБОРОВ
    if year == 2009 or year == 2013 or year == 2017 then
        local next_year = year + 1
        local success, next_db = pcall(require, 'Модуль:Data/Tournaments/' .. next_year)
        if success and next_db["ЧТМ_"..next_year.."_Qual"] then
            local dummy_playoffs = {}
            local ctm_results = process_tournament("ЧТМ_"..next_year.."_Qual", next_db["ЧТМ_"..next_year.."_Qual"], dummy_playoffs)
            local conf_to_cup = { ["Америка"] = "КАм", ["Африка"] = "КАф", ["Евразия"] = "КЕв" } 
            
            for team, res_key in pairs(ctm_results) do
                local t_info = TeamsDB.getTeam(team)
                if t_info and t_info.conf then
                    local base_tourney = conf_to_cup[t_info.conf]
                    if base_tourney then
                        if not output[base_tourney] then output[base_tourney] = {} end
                        local final_key = res_key
                        if global_playoff_participants[base_tourney] and global_playoff_participants[base_tourney][team] then
                            if res_key == "о" or res_key == "оп" or string.match(res_key, "^о%d$") then final_key = "ос"
                            else final_key = string.gsub(res_key, "^о%d?%-", "ос-") end
                        end
                        update_result(output[base_tourney], team, final_key)
                    end
                end
            end
        end
    end

    if manual_overrides[year] then
        for tourney, teams in pairs(manual_overrides[year]) do
            if not output[tourney] then output[tourney] = {} end
            for team, key in pairs(teams) do output[tourney][team] = key end
        end
    end
    
    return output
end

return Builder