Przejdź do zawartości

Moduł:Rekordy sezon

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Rekordy sezon/opis

local p = {}

-- =========================================================
-- Moduł:Rekordy_sezon
--
-- Obsługiwane dyscypliny:
--   koszykowka
--   pilka nozna
--
-- Wywołanie:
--
-- {{#invoke:Rekordy_sezon|main
--  |sezon=2026/2027
--  |druzyna=Lech Poznań
--  |dyscyplina=pilka nozna
-- }}
--
-- DANE:
-- Mecze
-- UdzialZawodnika
-- Strzelcy
--
-- WAŻNE:
-- Moduł korzysta wyłącznie z mw.ext.cargo.query().
-- Nie używa SELECT.
-- =========================================================


-- =========================================================
-- FUNKCJE WSPÓLNE
-- =========================================================

local function liczba(value)

    if value == nil or value == "" then
        return 0
    end

    value = tostring(value)
    value = mw.ustring.gsub(value, "%s+", "")

    return tonumber(value) or 0

end


local function esc(value)

    value = tostring(value or "")

    return mw.ustring.gsub(value, "'", "''")

end


-- =========================================================
-- WYNIK MECZU
-- =========================================================

local function wynikMeczu(row)

    local g1 = liczba(row.gole1)
    local g2 = liczba(row.gole2)

    if row.gole1 ~= nil and row.gole2 ~= nil
       and row.gole1 ~= "" and row.gole2 ~= "" then

        return g1, g2

    end


    local wynik = tostring(row.wynik or "")

    local a, b =
        mw.ustring.match(
            wynik,
            "(%d+)%s*:%s*(%d+)"
        )

    if a and b then
        return tonumber(a), tonumber(b)
    end

    return nil, nil

end


-- =========================================================
-- MIEJSCE MECZU
-- =========================================================

local function miejsceMeczu(row, druzyna)

    local d1 = tostring(row.druzyna1 or "")
    local d2 = tostring(row.druzyna2 or "")

    if d1 == druzyna then
        return "dom"
    end

    if d2 == druzyna then
        return "wyjazd"
    end

    return nil

end


-- =========================================================
-- NAZWA MECZU
-- =========================================================

local function nazwaMeczu(row)

    local d1 = tostring(row.druzyna1 or "")
    local d2 = tostring(row.druzyna2 or "")
    local strona = tostring(row.strona_meczu or "")

    local tekst =
        d1 .. " – " .. d2

    if strona ~= "" then

        return "[[" ..
            strona ..
            "|" ..
            tekst ..
            "]]"

    end

    return tekst

end


-- =========================================================
-- DODAWANIE REKORDU
-- =========================================================

local function dodajRekord(tabela, miejsce, wartosc, item)

    if not tabela[miejsce] then
        tabela[miejsce] = {}
    end

    local lista = tabela[miejsce]


    if #lista == 0 then

        table.insert(lista, item)

        return

    end


    local obecna =
        tonumber(lista[1].wartosc) or 0


    if wartosc > obecna then

        tabela[miejsce] = {
            item
        }

    elseif wartosc == obecna then

        table.insert(
            lista,
            item
        )

    end

end


-- =========================================================
-- KOSZYKÓWKA
-- =========================================================

local function rekordyKoszykowka(
    sezon,
    druzyna
)

    -- =====================================================
    -- MECZE
    -- =====================================================

    local pola = table.concat({

        "Mecze.id=mecz",
        "Mecze.data=data",
        "Mecze.sezon=sezon",
        "Mecze.rozgrywki=rozgrywki",
        "Mecze.druzyna1=druzyna1",
        "Mecze.druzyna2=druzyna2",
        "Mecze.gole1=gole1",
        "Mecze.gole2=gole2",
        "Mecze.wynik=wynik",
        "Mecze.strona_meczu=strona_meczu"

    }, ",")


    local mecze =
        mw.ext.cargo.query(
            "Mecze",
            pola,
            {
                where =
                    "(Mecze.druzyna1='" ..
                    esc(druzyna) ..
                    "' OR Mecze.druzyna2='" ..
                    esc(druzyna) ..
                    "')" ..
                    " AND Mecze.sezon='" ..
                    esc(sezon) ..
                    "'",

                orderBy =
                    "Mecze.data ASC",

                limit = 5000
            }
        )


    if not mecze or #mecze == 0 then

        return
            "''Brak meczów dla tej drużyny w podanym sezonie.''"

    end


    local zwyciestwa = {
        dom = {},
        wyjazd = {}
    }

    local porazki = {
        dom = {},
        wyjazd = {}
    }


    -- =====================================================
    -- PRZETWARZANIE MECZÓW
    -- =====================================================

    for _, row in ipairs(mecze) do

        local g1, g2 =
            wynikMeczu(row)

        local miejsce =
            miejsceMeczu(row, druzyna)

        if g1 and g2 and miejsce then

            local zdobyte
            local stracone

            if miejsce == "dom" then

                zdobyte = g1
                stracone = g2

            else

                zdobyte = g2
                stracone = g1

            end


            local roznica =
                zdobyte - stracone


            local item = {

                tekst =
                    nazwaMeczu(row),

                wynik =
                    tostring(g1) ..
                    ":" ..
                    tostring(g2),

                roznica =
                    roznica,

                wartosc =
                    math.abs(roznica)

            }


            if roznica > 0 then

                dodajRekord(
                    zwyciestwa,
                    miejsce,
                    roznica,
                    item
                )

            elseif roznica < 0 then

                dodajRekord(
                    porazki,
                    miejsce,
                    math.abs(roznica),
                    item
                )

            end

        end

    end


    -- =====================================================
    -- STATYSTYKI ZAWODNIKÓW
    -- =====================================================

    local polaZawodnicy = table.concat({

        "UdzialZawodnika.zawodnik=zawodnik",
        "UdzialZawodnika.druzyna=druzyna_zawodnika",
        "UdzialZawodnika.gol=gol",
        "UdzialZawodnika.minuty=minuty",
        "UdzialZawodnika.minuty_format=minuty_format",
        "UdzialZawodnika.asysta=asysta",
        "UdzialZawodnika.zbiorka=zbiorka",
        "UdzialZawodnika.eval=eval",
        "UdzialZawodnika.plusminus=plusminus",

        "Mecze.id=mecz",
        "Mecze.data=data",
        "Mecze.druzyna1=druzyna1",
        "Mecze.druzyna2=druzyna2",
        "Mecze.wynik=wynik",
        "Mecze.strona_meczu=strona_meczu"

    }, ",")


    local zawodnicy =
        mw.ext.cargo.query(
            "UdzialZawodnika,Mecze",
            polaZawodnicy,
            {
                join =
                    "UdzialZawodnika.mecz=Mecze.id",

                where =
                    "UdzialZawodnika.druzyna='" ..
                    esc(druzyna) ..
                    "'" ..
                    " AND Mecze.sezon='" ..
                    esc(sezon) ..
                    "'" ..
                    " AND UdzialZawodnika.minuty > 0",

                orderBy =
                    "Mecze.data ASC",

                limit = 10000
            }
        )


    local rekordy = {

        punkty = {
            dom = {},
            wyjazd = {}
        },

        minuty = {
            dom = {},
            wyjazd = {}
        },

        asysty = {
            dom = {},
            wyjazd = {}
        },

        zbiorki = {
            dom = {},
            wyjazd = {}
        },

        eval = {
            dom = {},
            wyjazd = {}
        },

        plusminus = {
            dom = {},
            wyjazd = {}
        }

    }


    if zawodnicy then

        for _, row in ipairs(zawodnicy) do

            local miejsce =
                miejsceMeczu(row, druzyna)

            if miejsce then

                local item = {

                    zawodnik =
                        row.zawodnik or "",

                    mecz =
                        row.mecz or "",

                    tekst =
                        nazwaMeczu(row),

                    wynik =
                        row.wynik or "",

                    punkty =
                        liczba(row.gol),

                    minuty =
                        liczba(row.minuty),

                    minuty_format =
                        row.minuty_format or "",

                    asysty =
                        liczba(row.asysta),

                    zbiorki =
                        liczba(row.zbiorka),

                    eval =
                        liczba(row.eval),

                    plusminus =
                        liczba(row.plusminus)

                }


                dodajRekord(
                    rekordy.punkty,
                    miejsce,
                    item.punkty,
                    {
                        zawodnik=item.zawodnik,
                        tekst=item.tekst,
                        wynik=item.wynik,
                        punkty=item.punkty,
                        wartosc=item.punkty
                    }
                )


                dodajRekord(
                    rekordy.minuty,
                    miejsce,
                    item.minuty,
                    {
                        zawodnik=item.zawodnik,
                        tekst=item.tekst,
                        wynik=item.wynik,
                        minuty=item.minuty,
                        minuty_format=item.minuty_format,
                        wartosc=item.minuty
                    }
                )


                dodajRekord(
                    rekordy.asysty,
                    miejsce,
                    item.asysty,
                    {
                        zawodnik=item.zawodnik,
                        tekst=item.tekst,
                        wynik=item.wynik,
                        asysty=item.asysty,
                        wartosc=item.asysty
                    }
                )


                dodajRekord(
                    rekordy.zbiorki,
                    miejsce,
                    item.zbiorki,
                    {
                        zawodnik=item.zawodnik,
                        tekst=item.tekst,
                        wynik=item.wynik,
                        zbiorki=item.zbiorki,
                        wartosc=item.zbiorki
                    }
                )


                dodajRekord(
                    rekordy.eval,
                    miejsce,
                    item.eval,
                    {
                        zawodnik=item.zawodnik,
                        tekst=item.tekst,
                        wynik=item.wynik,
                        eval=item.eval,
                        wartosc=item.eval
                    }
                )


                dodajRekord(
                    rekordy.plusminus,
                    miejsce,
                    item.plusminus,
                    {
                        zawodnik=item.zawodnik,
                        tekst=item.tekst,
                        wynik=item.wynik,
                        plusminus=item.plusminus,
                        wartosc=item.plusminus
                    }
                )

            end

        end

    end


    -- =====================================================
    -- SERIE
    -- =====================================================

    local serie = {

        wszystkieW = 0,
        wszystkieP = 0,

        domW = 0,
        domP = 0,

        wyjazdW = 0,
        wyjazdP = 0

    }


    local aktualne = {

        wszystkieW = 0,
        wszystkieP = 0,

        domW = 0,
        domP = 0,

        wyjazdW = 0,
        wyjazdP = 0

    }


    for _, row in ipairs(mecze) do

        local g1, g2 =
            wynikMeczu(row)

        local miejsce =
            miejsceMeczu(row, druzyna)

        if g1 and g2 and miejsce then

            local wynik

            if miejsce == "dom" then

                if g1 > g2 then
                    wynik = "W"
                elseif g1 < g2 then
                    wynik = "P"
                else
                    wynik = "R"
                end

            else

                if g2 > g1 then
                    wynik = "W"
                elseif g2 < g1 then
                    wynik = "P"
                else
                    wynik = "R"
                end

            end


            if wynik == "W" then

                aktualne.wszystkieW =
                    aktualne.wszystkieW + 1

                aktualne.wszystkieP = 0

                if aktualne.wszystkieW >
                    serie.wszystkieW then

                    serie.wszystkieW =
                        aktualne.wszystkieW

                end

            elseif wynik == "P" then

                aktualne.wszystkieP =
                    aktualne.wszystkieP + 1

                aktualne.wszystkieW = 0

                if aktualne.wszystkieP >
                    serie.wszystkieP then

                    serie.wszystkieP =
                        aktualne.wszystkieP

                end

            else

                aktualne.wszystkieW = 0
                aktualne.wszystkieP = 0

            end


            if miejsce == "dom" then

                if wynik == "W" then

                    aktualne.domW =
                        aktualne.domW + 1

                    aktualne.domP = 0

                    if aktualne.domW >
                        serie.domW then

                        serie.domW =
                            aktualne.domW

                    end

                elseif wynik == "P" then

                    aktualne.domP =
                        aktualne.domP + 1

                    aktualne.domW = 0

                    if aktualne.domP >
                        serie.domP then

                        serie.domP =
                            aktualne.domP

                    end

                else

                    aktualne.domW = 0
                    aktualne.domP = 0

                end

            else

                if wynik == "W" then

                    aktualne.wyjazdW =
                        aktualne.wyjazdW + 1

                    aktualne.wyjazdP = 0

                    if aktualne.wyjazdW >
                        serie.wyjazdW then

                        serie.wyjazdW =
                            aktualne.wyjazdW

                    end

                elseif wynik == "P" then

                    aktualne.wyjazdP =
                        aktualne.wyjazdP + 1

                    aktualne.wyjazdW = 0

                    if aktualne.wyjazdP >
                        serie.wyjazdP then

                        serie.wyjazdP =
                            aktualne.wyjazdP

                    end

                else

                    aktualne.wyjazdW = 0
                    aktualne.wyjazdP = 0

                end

            end

        end

    end


    -- =====================================================
    -- FORMATOWANIE
    -- =====================================================

    local function meczeTekst(lista)

        if not lista or #lista == 0 then
            return "—"
        end

        local out = {}

        for _, item in ipairs(lista) do

            table.insert(
                out,
                item.tekst ..
                " (" ..
                item.wynik ..
                ", " ..
                (item.roznica >= 0
                    and "+" .. item.roznica
                    or tostring(item.roznica)) ..
                ")"
            )

        end

        return table.concat(out, "<br>")

    end


    local function zawodnicyTekst(lista, typ)

        if not lista or #lista == 0 then
            return "—"
        end

        local out = {}

        for _, item in ipairs(lista) do

            local wartosc = ""

            if typ == "punkty" then

                wartosc =
                    tostring(item.punkty) ..
                    " pkt"

            elseif typ == "minuty" then

                wartosc =
                    item.minuty_format or ""

                if wartosc == "" then
                    wartosc =
                        tostring(item.minuty)
                end

                wartosc =
                    wartosc .. " min"

            elseif typ == "asysty" then

                wartosc =
                    tostring(item.asysty) ..
                    " as."

            elseif typ == "zbiorki" then

                wartosc =
                    tostring(item.zbiorki) ..
                    " zb."

            elseif typ == "eval" then

                wartosc =
                    tostring(item.eval) ..
                    " EVAL"

            elseif typ == "plusminus" then

                if item.plusminus >= 0 then
                    wartosc =
                        "+" ..
                        tostring(item.plusminus)
                else
                    wartosc =
                        tostring(item.plusminus)
                end

                wartosc =
                    wartosc .. " +/-"

            end


            table.insert(
                out,
                "[[" ..
                item.zawodnik ..
                "]] — " ..
                wartosc ..
                "<br>" ..
                item.tekst ..
                " (" ..
                item.wynik ..
                ")"
            )

        end

        return table.concat(out, "<br>")

    end


    -- =====================================================
    -- WYNIK
    -- =====================================================

    local out = {}

    table.insert(
        out,
        "=== Rekordy sezonu ==="
    )

    table.insert(out, "")


    table.insert(
        out,
        "==== Rekordy meczowe ===="
    )

    table.insert(out, "")

    table.insert(
        out,
        '{| class="wikitable" style="width:100%; text-align:center;"'
    )

    table.insert(
        out,
        "! Rekord !! 🏠 Dom !! ✈️ Wyjazd"
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! 🏆 Najwyższe zwycięstwo"
    )

    table.insert(
        out,
        "| " ..
        meczeTekst(zwyciestwa.dom)
    )

    table.insert(
        out,
        "| " ..
        meczeTekst(zwyciestwa.wyjazd)
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! ❌ Najwyższa porażka"
    )

    table.insert(
        out,
        "| " ..
        meczeTekst(porazki.dom)
    )

    table.insert(
        out,
        "| " ..
        meczeTekst(porazki.wyjazd)
    )

    table.insert(out, "|}")


    table.insert(out, "")


    table.insert(
        out,
        "==== Rekordy indywidualne ===="
    )

    table.insert(out, "")

    table.insert(
        out,
        '{| class="wikitable" style="width:100%; text-align:center;"'
    )

    table.insert(
        out,
        "! Rekord !! 🏠 Dom !! ✈️ Wyjazd"
    )


    local lista = {

        {
            nazwa = "🏀 Najwięcej punktów",
            tabela = rekordy.punkty,
            typ = "punkty"
        },

        {
            nazwa = "⏱️ Najwięcej minut",
            tabela = rekordy.minuty,
            typ = "minuty"
        },

        {
            nazwa = "🎯 Najwięcej asyst",
            tabela = rekordy.asysty,
            typ = "asysty"
        },

        {
            nazwa = "🏀 Najwięcej zbiórek",
            tabela = rekordy.zbiorki,
            typ = "zbiorki"
        },

        {
            nazwa = "⭐ Najwyższy EVAL",
            tabela = rekordy.eval,
            typ = "eval"
        },

        {
            nazwa = "➕ Najwyższy +/-",
            tabela = rekordy.plusminus,
            typ = "plusminus"
        }

    }


    for _, r in ipairs(lista) do

        table.insert(out, "|-")

        table.insert(
            out,
            "! " .. r.nazwa
        )

        table.insert(
            out,
            "| " ..
            zawodnicyTekst(
                r.tabela.dom,
                r.typ
            )
        )

        table.insert(
            out,
            "| " ..
            zawodnicyTekst(
                r.tabela.wyjazd,
                r.typ
            )
        )

    end


    table.insert(out, "|}")


    table.insert(out, "")


    table.insert(
        out,
        "==== Serie ===="
    )

    table.insert(out, "")

    table.insert(
        out,
        '{| class="wikitable" style="width:100%; text-align:center;"'
    )

    table.insert(
        out,
        "! Seria !! Wszystkie mecze !! 🏠 Dom !! ✈️ Wyjazd"
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! 🔥 Najdłuższa seria zwycięstw"
    )

    table.insert(
        out,
        "| " ..
        tostring(serie.wszystkieW)
    )

    table.insert(
        out,
        "| " ..
        tostring(serie.domW)
    )

    table.insert(
        out,
        "| " ..
        tostring(serie.wyjazdW)
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! 📉 Najdłuższa seria porażek"
    )

    table.insert(
        out,
        "| " ..
        tostring(serie.wszystkieP)
    )

    table.insert(
        out,
        "| " ..
        tostring(serie.domP)
    )

    table.insert(
        out,
        "| " ..
        tostring(serie.wyjazdP)
    )

    table.insert(out, "|}")


    return table.concat(out, "\n")

end


-- =========================================================
-- PIŁKA NOŻNA
-- =========================================================

local function rekordyPilkaNozna(
    sezon,
    druzyna
)

    -- =====================================================
    -- MECZE
    -- =====================================================

    local pola = table.concat({

        "Mecze.id=mecz",
        "Mecze.data=data",
        "Mecze.sezon=sezon",
        "Mecze.rozgrywki=rozgrywki",
        "Mecze.druzyna1=druzyna1",
        "Mecze.druzyna2=druzyna2",
        "Mecze.gole1=gole1",
        "Mecze.gole2=gole2",
        "Mecze.wynik=wynik",
        "Mecze.strona_meczu=strona_meczu"

    }, ",")


    local mecze =
        mw.ext.cargo.query(
            "Mecze",
            pola,
            {
                where =
                    "(Mecze.druzyna1='" ..
                    esc(druzyna) ..
                    "' OR Mecze.druzyna2='" ..
                    esc(druzyna) ..
                    "')" ..
                    " AND Mecze.sezon='" ..
                    esc(sezon) ..
                    "'",

                orderBy =
                    "Mecze.data ASC",

                limit = 5000
            }
        )


    if not mecze or #mecze == 0 then

        return
            "''Brak meczów dla tej drużyny w podanym sezonie.''"

    end


    -- =====================================================
    -- REKORDY WYNIKÓW
    -- =====================================================

    local zwyciestwa = {
        dom = {},
        wyjazd = {}
    }

    local porazki = {
        dom = {},
        wyjazd = {}
    }


    -- =====================================================
    -- STATYSTYKI WYNIKÓW
    -- =====================================================

    local bilans = {

        wszystkie = {
            W = 0,
            R = 0,
            P = 0
        },

        dom = {
            W = 0,
            R = 0,
            P = 0
        },

        wyjazd = {
            W = 0,
            R = 0,
            P = 0
        }

    }


    local seriaBezPorazki = {
        wszystkie = 0,
        dom = 0,
        wyjazd = 0
    }


    local aktualnaBezPorazki = {
        wszystkie = 0,
        dom = 0,
        wyjazd = 0
    }


    local serieWygranych = {
        wszystkie = 0,
        dom = 0,
        wyjazd = 0
    }


    local aktualneWygrane = {
        wszystkie = 0,
        dom = 0,
        wyjazd = 0
    }


    local serieBezWygranej = {
        wszystkie = 0,
        dom = 0,
        wyjazd = 0
    }


    local aktualneBezWygranej = {
        wszystkie = 0,
        dom = 0,
        wyjazd = 0
    }


    -- =====================================================
    -- NAJCZĘSTSZE WYNIKI
    -- =====================================================

    local czestoscWynikow = {}


    -- =====================================================
    -- GOLE
    -- =====================================================

    local goleZdobyte = {

        ["0-15"] = 0,
        ["16-30"] = 0,
        ["31-45"] = 0,
        ["45+"] = 0,
        ["46-60"] = 0,
        ["61-75"] = 0,
        ["76-90"] = 0,
        ["90+"] = 0,
        ["91-105"] = 0,
        ["105+"] = 0,
        ["106-120"] = 0,
        ["120+"] = 0

    }


    local goleStracone = {

        ["0-15"] = 0,
        ["16-30"] = 0,
        ["31-45"] = 0,
        ["45+"] = 0,
        ["46-60"] = 0,
        ["61-75"] = 0,
        ["76-90"] = 0,
        ["90+"] = 0,
        ["91-105"] = 0,
        ["105+"] = 0,
        ["106-120"] = 0,
        ["120+"] = 0

    }


    -- =====================================================
    -- NAJLEPSI STRZELCY W JEDNYM MECZU
    -- =====================================================

    local goleZawodnika = {}


    -- =====================================================
    -- PRZETWARZANIE MECZÓW
    -- =====================================================

    for _, row in ipairs(mecze) do

        local g1, g2 =
            wynikMeczu(row)

        local miejsce =
            miejsceMeczu(row, druzyna)

        if g1 and g2 and miejsce then

            local zdobyte
            local stracone

            if miejsce == "dom" then

                zdobyte = g1
                stracone = g2

            else

                zdobyte = g2
                stracone = g1

            end


            local roznica =
                zdobyte - stracone


            local wynik

            if roznica > 0 then

                wynik = "W"

            elseif roznica < 0 then

                wynik = "P"

            else

                wynik = "R"

            end


            -- =============================================
            -- BILANS
            -- =============================================

            bilans.wszystkie[wynik] =
                bilans.wszystkie[wynik] + 1

            bilans[miejsce][wynik] =
                bilans[miejsce][wynik] + 1


            -- =============================================
            -- NAJWYŻSZE ZWYCIĘSTWO / PORAŻKA
            -- =============================================

            local item = {

                tekst =
                    nazwaMeczu(row),

                wynik =
                    tostring(g1) ..
                    ":" ..
                    tostring(g2),

                roznica =
                    roznica,

                rozgrywki =
                    row.rozgrywki or "",

                wartosc =
                    math.abs(roznica)

            }


            if roznica > 0 then

                dodajRekord(
                    zwyciestwa,
                    miejsce,
                    roznica,
                    item
                )

            elseif roznica < 0 then

                dodajRekord(
                    porazki,
                    miejsce,
                    math.abs(roznica),
                    item
                )

            end


            -- =============================================
            -- NAJCZĘSTSZE WYNIKI
            -- =============================================

            local klucz =
                tostring(g1) ..
                ":" ..
                tostring(g2)

            czestoscWynikow[klucz] =
                (czestoscWynikow[klucz] or 0) + 1


            -- =============================================
            -- SERIA BEZ PORAŻKI
            --
            -- W + R
            -- =============================================

            if wynik ~= "P" then

                aktualnaBezPorazki.wszystkie =
                    aktualnaBezPorazki.wszystkie + 1

            else

                aktualnaBezPorazki.wszystkie = 0

            end


            if aktualnaBezPorazki.wszystkie >
                seriaBezPorazki.wszystkie then

                seriaBezPorazki.wszystkie =
                    aktualnaBezPorazki.wszystkie

            end


            if wynik == "W" then

                aktualneWygrane.wszystkie =
                    aktualneWygrane.wszystkie + 1

                aktualneBezWygranej.wszystkie = 0

            elseif wynik == "R" then

                aktualneWygrane.wszystkie = 0

                aktualneBezWygranej.wszystkie =
                    aktualneBezWygranej.wszystkie + 1

            else

                aktualneWygrane.wszystkie = 0

                aktualneBezWygranej.wszystkie =
                    aktualneBezWygranej.wszystkie + 1

            end


            if aktualneWygrane.wszystkie >
                serieWygranych.wszystkie then

                serieWygranych.wszystkie =
                    aktualneWygrane.wszystkie

            end


            if aktualneBezWygranej.wszystkie >
                serieBezWygranej.wszystkie then

                serieBezWygranej.wszystkie =
                    aktualneBezWygranej.wszystkie

            end


            -- =============================================
            -- DOM / WYJAZD
            -- =============================================

            if wynik ~= "P" then

                aktualnaBezPorazki[miejsce] =
                    aktualnaBezPorazki[miejsce] + 1

            else

                aktualnaBezPorazki[miejsce] = 0

            end


            if aktualnaBezPorazki[miejsce] >
                seriaBezPorazki[miejsce] then

                seriaBezPorazki[miejsce] =
                    aktualnaBezPorazki[miejsce]

            end


            if wynik == "W" then

                aktualneWygrane[miejsce] =
                    aktualneWygrane[miejsce] + 1

                aktualneBezWygranej[miejsce] = 0

            else

                aktualneWygrane[miejsce] = 0

                aktualneBezWygranej[miejsce] =
                    aktualneBezWygranej[miejsce] + 1

            end


            if aktualneWygrane[miejsce] >
                serieWygranych[miejsce] then

                serieWygranych[miejsce] =
                    aktualneWygrane[miejsce]

            end


            if aktualneBezWygranej[miejsce] >
                serieBezWygranej[miejsce] then

                serieBezWygranej[miejsce] =
                    aktualneBezWygranej[miejsce]

            end

        end

    end


    -- =====================================================
    -- STRZELCY
    --
    -- Zapytanie do Cargo:
    -- tabela = Strzelcy
    -- =====================================================

    local polaStrzelcy = table.concat({

        "Strzelcy.mecz=mecz",
        "Strzelcy.zawodnik=zawodnik",
        "Strzelcy.druzyna=druzyna",
        "Strzelcy.minuta=minuta"

    }, ",")


    local strzelcy =
        mw.ext.cargo.query(
            "Strzelcy",
            polaStrzelcy,
            {
                limit = 10000
            }
        )


    -- =====================================================
    -- FUNKCJA PRZEDZIAŁU CZASOWEGO
    -- =====================================================

    local function przedzial(minuta)

        local tekst =
            tostring(minuta or "")

        tekst =
            mw.ustring.gsub(
                tekst,
                "%s+",
                ""
            )

        local liczbaMinuty =
            tonumber(
                mw.ustring.match(
                    tekst,
                    "%d+"
                )
            )

        if not liczbaMinuty then
            return nil
        end


        local plus =
            mw.ustring.find(
                tekst,
                "%+"
            ) ~= nil


        if plus then

            if liczbaMinuty == 45 then
                return "45+"
            end

            if liczbaMinuty == 90 then
                return "90+"
            end

            if liczbaMinuty == 105 then
                return "105+"
            end

            if liczbaMinuty == 120 then
                return "120+"
            end

        end


        if liczbaMinuty <= 15 then
            return "0-15"
        end

        if liczbaMinuty <= 30 then
            return "16-30"
        end

        if liczbaMinuty <= 45 then
            return "31-45"
        end

        if liczbaMinuty <= 60 then
            return "46-60"
        end

        if liczbaMinuty <= 75 then
            return "61-75"
        end

        if liczbaMinuty <= 90 then
            return "76-90"
        end

        if liczbaMinuty <= 105 then
            return "91-105"
        end

        if liczbaMinuty <= 120 then
            return "106-120"
        end

        return "120+"

    end


    -- =====================================================
    -- PRZETWARZANIE STRZELCÓW
    -- =====================================================

    if strzelcy then

        for _, gol in ipairs(strzelcy) do

            local znalezionyMecz = nil


            for _, mecz in ipairs(mecze) do

                if tostring(mecz.mecz or "") ==
                    tostring(gol.mecz or "") then

                    znalezionyMecz = mecz
                    break

                end

            end


            if znalezionyMecz then

                local miejsce =
                    miejsceMeczu(
                        znalezionyMecz,
                        druzyna
                    )


                local przedzialCzasu =
                    przedzial(gol.minuta)


                if miejsce and przedzialCzasu then

                    local druzynaGola =
                        tostring(
                            gol.druzyna or ""
                        )


                    if druzynaGola == druzyna then

                        goleZdobyte[
                            przedzialCzasu
                        ] =
                            goleZdobyte[
                                przedzialCzasu
                            ] + 1

                    else

                        goleStracone[
                            przedzialCzasu
                        ] =
                            goleStracone[
                                przedzialCzasu
                            ] + 1

                    end

                end


                -- =========================================
                -- GOLE ZAWODNIKA W JEDNYM MECZU
                -- =========================================

                if tostring(gol.druzyna or "") ==
                    druzyna then

                    local klucz =
                        tostring(gol.mecz or "") ..
                        "|" ..
                        tostring(gol.zawodnik or "")


                    if not goleZawodnika[klucz] then

                        goleZawodnika[klucz] = {

                            mecz =
                                gol.mecz,

                            zawodnik =
                                gol.zawodnik,

                            liczba = 0,

                            miejsce =
                                miejsce,

                            tekst =
                                nazwaMeczu(
                                    znalezionyMecz
                                ),

                            wynik =
                                znalezionyMecz.wynik or ""

                        }

                    end


                    goleZawodnika[klucz].liczba =
                        goleZawodnika[klucz].liczba + 1

                end

            end

        end

    end


    -- =====================================================
    -- NAJLEPSZY STRZELEC W JEDNYM MECZU
    -- =====================================================

    local najwiecejGoli = {
        dom = {},
        wyjazd = {}
    }


    for _, item in pairs(goleZawodnika) do

        if item.miejsce then

            dodajRekord(
                najwiecejGoli,
                item.miejsce,
                item.liczba,
                {
                    zawodnik =
                        item.zawodnik,

                    tekst =
                        item.tekst,

                    wynik =
                        item.wynik,

                    gole =
                        item.liczba,

                    wartosc =
                        item.liczba

                }
            )

        end

    end


    -- =====================================================
    -- NAJCZĘSTSZE WYNIKI
    -- =====================================================

    local najczestszeWyniki = {}

    for wynik, liczbaWyniku in pairs(
        czestoscWynikow
    ) do

        table.insert(
            najczestszeWyniki,
            {
                wynik = wynik,
                liczba = liczbaWyniku
            }
        )

    end


    table.sort(
        najczestszeWyniki,
        function(a, b)

            if a.liczba == b.liczba then
                return a.wynik < b.wynik
            end

            return a.liczba > b.liczba

        end
    )


    -- =====================================================
    -- FORMATOWANIE
    -- =====================================================

    local function meczeTekst(lista)

        if not lista or #lista == 0 then
            return "—"
        end

        local out = {}

        for _, item in ipairs(lista) do

            table.insert(
                out,
                item.tekst ..
                " (" ..
                item.wynik ..
                ")"
            )

        end

        return table.concat(
            out,
            "<br>"
        )

    end


    local function strzelcyTekst(lista)

        if not lista or #lista == 0 then
            return "—"
        end

        local out = {}

        for _, item in ipairs(lista) do

            table.insert(
                out,
                "[[" ..
                item.zawodnik ..
                "]] — " ..
                tostring(item.gole) ..
                " gole<br>" ..
                item.tekst ..
                " (" ..
                item.wynik ..
                ")"
            )

        end

        return table.concat(
            out,
            "<br>"
        )

    end


    -- =====================================================
    -- WYNIK
    -- =====================================================

    local out = {}


    table.insert(
        out,
        "=== Rekordy sezonu ==="
    )

    table.insert(out, "")


    -- =====================================================
    -- REKORDY MECZOWE
    -- =====================================================

    table.insert(
        out,
        "==== Rekordy meczowe ===="
    )

    table.insert(out, "")

    table.insert(
        out,
        '{| class="wikitable" style="width:100%; text-align:center;"'
    )

    table.insert(
        out,
        "! Rekord !! 🏠 Dom !! ✈️ Wyjazd"
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! 🏆 Najwyższe zwycięstwo"
    )

    table.insert(
        out,
        "| " ..
        meczeTekst(
            zwyciestwa.dom
        )
    )

    table.insert(
        out,
        "| " ..
        meczeTekst(
            zwyciestwa.wyjazd
        )
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! ❌ Najwyższa porażka"
    )

    table.insert(
        out,
        "| " ..
        meczeTekst(
            porazki.dom
        )
    )

    table.insert(
        out,
        "| " ..
        meczeTekst(
            porazki.wyjazd
        )
    )

    table.insert(out, "|}")


    -- =====================================================
    -- BILANS
    -- =====================================================

    table.insert(out, "")

    table.insert(
        out,
        "==== Bilans spotkań ===="
    )

    table.insert(out, "")

    table.insert(
        out,
        '{| class="wikitable" style="width:100%; text-align:center;"'
    )

    table.insert(
        out,
        "! !! Wszystkie !! 🏠 Dom !! ✈️ Wyjazd"
    )

    table.insert(out, "|-")

    table.insert(
        out,
        "! 🟢 Zwycięstwa"
    )

    table.insert(
        out,
        "| " ..
        bilans.wszystkie.W
    )

    table.insert(
        out,
        "| " ..
        bilans.dom.W
    )

    table.insert(
        out,
        "| " ..
        bilans.wyjazd.W
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! 🟡 Remisy"
    )

    table.insert(
        out,
        "| " ..
        bilans.wszystkie.R
    )

    table.insert(
        out,
        "| " ..
        bilans.dom.R
    )

    table.insert(
        out,
        "| " ..
        bilans.wyjazd.R
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! 🔴 Porażki"
    )

    table.insert(
        out,
        "| " ..
        bilans.wszystkie.P
    )

    table.insert(
        out,
        "| " ..
        bilans.dom.P
    )

    table.insert(
        out,
        "| " ..
        bilans.wyjazd.P
    )

    table.insert(out, "|}")


    -- =====================================================
    -- SERIE
    -- =====================================================

    table.insert(out, "")

    table.insert(
        out,
        "==== Serie ===="
    )

    table.insert(out, "")

    table.insert(
        out,
        '{| class="wikitable" style="width:100%; text-align:center;"'
    )

    table.insert(
        out,
        "! Seria !! Wszystkie !! 🏠 Dom !! ✈️ Wyjazd"
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! 🔥 Bez porażki"
    )

    table.insert(
        out,
        "| " ..
        seriaBezPorazki.wszystkie
    )

    table.insert(
        out,
        "| " ..
        seriaBezPorazki.dom
    )

    table.insert(
        out,
        "| " ..
        seriaBezPorazki.wyjazd
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! 🏆 Seria zwycięstw"
    )

    table.insert(
        out,
        "| " ..
        serieWygranych.wszystkie
    )

    table.insert(
        out,
        "| " ..
        serieWygranych.dom
    )

    table.insert(
        out,
        "| " ..
        serieWygranych.wyjazd
    )


    table.insert(out, "|-")

    table.insert(
        out,
        "! ⚪ Bez zwycięstwa"
    )

    table.insert(
        out,
        "| " ..
        serieBezWygranej.wszystkie
    )

    table.insert(
        out,
        "| " ..
        serieBezWygranej.dom
    )

    table.insert(
        out,
        "| " ..
        serieBezWygranej.wyjazd
    )


    table.insert(out, "|}")


    -- =====================================================
    -- NAJWIĘCEJ GOLI JEDNEGO ZAWODNIKA
    -- =====================================================

    table.insert(out, "")

    table.insert(
        out,
        "==== Najwięcej goli jednego zawodnika w meczu ===="
    )

    table.insert(out, "")

    table.insert(
        out,
        '{| class="wikitable" style="width:100%; text-align:center;"'
    )

    table.insert(
        out,
        "! !! 🏠 Dom !! ✈️ Wyjazd"
    )

    table.insert(out, "|-")

    table.insert(
        out,
        "! ⚽ Najwięcej goli"
    )

    table.insert(
        out,
        "| " ..
        strzelcyTekst(
            najwiecejGoli.dom
        )
    )

    table.insert(
        out,
        "| " ..
        strzelcyTekst(
            najwiecejGoli.wyjazd
        )
    )

    table.insert(out, "|}")


    -- =====================================================
    -- NAJCZĘSTSZE WYNIKI
    -- =====================================================

    table.insert(out, "")

    table.insert(
        out,
        "==== Najczęstsze wyniki ===="
    )

    table.insert(out, "")

    table.insert(
        out,
        '{| class="wikitable" style="width:400px; text-align:center;"'
    )

    table.insert(
        out,
        "! Wynik !! Liczba"
    )


    for _, item in ipairs(najczestszeWyniki) do

        table.insert(out, "|-")

        table.insert(
            out,
            "| " ..
            item.wynik
        )

        table.insert(
            out,
            "| " ..
            item.liczba
        )

    end


    table.insert(out, "|}")


    -- =====================================================
    -- GOLE W PRZEDZIAŁACH CZASOWYCH
    -- =====================================================

    local przedzialy = {

        "0-15",
        "16-30",
        "31-45",
        "45+",
        "46-60",
        "61-75",
        "76-90",
        "90+",
        "91-105",
        "105+",
        "106-120",
        "120+"

    }


    table.insert(out, "")

    table.insert(
        out,
        "==== Gole w przedziałach czasowych ===="
    )

    table.insert(out, "")

    table.insert(
        out,
        '{| class="wikitable" style="text-align:center;"'
    )

    table.insert(
        out,
        "! Przedział !! ⚽ Zdobyte !! ❌ Stracone"
    )


    for _, przedzialNazwa in ipairs(
        przedzialy
    ) do

        table.insert(out, "|-")

        table.insert(
            out,
            "! " ..
            przedzialNazwa
        )

        table.insert(
            out,
            "| " ..
            goleZdobyte[przedzialNazwa]
        )

        table.insert(
            out,
            "| " ..
            goleStracone[przedzialNazwa]
        )

    end


    table.insert(out, "|}")


    return table.concat(
        out,
        "\n"
    )

end


-- =========================================================
-- GŁÓWNA FUNKCJA
-- =========================================================

function p.main(frame)

    local sezon =
        tostring(
            frame.args.sezon or ""
        )

    local druzyna =
        tostring(
            frame.args.druzyna or ""
        )

    local dyscyplina =
        tostring(
            frame.args.dyscyplina or
            "koszykowka"
        )


    if sezon == "" then

        return
            "'''Błąd:''' nie podano sezonu."

    end


    if druzyna == "" then

        return
            "'''Błąd:''' nie podano drużyny."

    end


    if dyscyplina == "koszykowka" then

        return rekordyKoszykowka(
            sezon,
            druzyna
        )

    elseif dyscyplina == "pilka nozna" then

        return rekordyPilkaNozna(
            sezon,
            druzyna
        )

    else

        return
            "'''Błąd:''' nieobsługiwana dyscyplina: " ..
            dyscyplina

    end

end


return p