Przejdź do zawartości

Moduł:H2H: Różnice pomiędzy wersjami

Z Encyklopedia Poznanskiego Sportu
Nie podano opisu zmian
Nie podano opisu zmian
Linia 1: Linia 1:
local p = {}
local p = {}
-- =========================================================
-- Module:H2H
--
-- Historia bezpośrednich spotkań dwóch drużyn.
--
-- Drużyny są rozpoznawane po:
--  Mecze.nazwa_statystyki1
--  Mecze.nazwa_statystyki2
--
-- Dzięki temu zmiana sponsora / nazwy drużyny nie rozbija H2H.
--
-- Funkcje:
--  1. bilans spotkań
--  2. suma punktów
--  3. wszystkie mecze
--  4. najlepsi strzelcy
--  5. trenerzy
--  6. rekordy meczowe
--  7. rekordy indywidualne
--  8. serie zwycięstw / porażek
--  9. kwartowe porównanie wyników
--
-- =========================================================


function p.main(frame)
function p.main(frame)


     local team1 = frame.args.team1 or ""
    -- =====================================================
     local team2 = frame.args.team2 or ""
    -- PARAMETRY
    -- =====================================================
 
     local team1 =
        mw.text.trim(frame.args.team1 or "")
 
     local team2 =
        mw.text.trim(frame.args.team2 or "")
 


     if team1 == "" or team2 == "" then
     if team1 == "" or team2 == "" then
         return "'''Błąd:''' należy podać team1 i team2."
 
         return "'''Błąd:''' należy podać obie drużyny."
 
     end
     end


    -- =========================================================
    -- FUNKCJE POMOCNICZE
    -- =========================================================


     local function esc(value)
    -- =====================================================
        value = tostring(value or "")
    -- BEZPIECZEŃSTWO SQL
         return mw.ustring.gsub(value, "'", "''")
    -- =====================================================
 
     local function sql(value)
 
         return mw.ustring.gsub(
            value,
            "'",
            "''"
        )
 
     end
     end


    local t1 = esc(team1)
    local t2 = esc(team2)


     local function num(value)
    local t1 = sql(team1)
    local t2 = sql(team2)
 
 
    -- =====================================================
    -- WARUNEK H2H DLA TABELI Mecze
    --
    -- WAŻNE:
    -- tutaj używamy nazwa_statystyki.
    -- Dzięki temu zmiana nazwy sponsorskiej nie rozbija H2H.
    -- =====================================================
 
    local whereMecze =
        "(" ..
        "M.nazwa_statystyki1='" .. t1 ..
        "' AND M.nazwa_statystyki2='" .. t2 ..
        "')" ..
        " OR " ..
        "(" ..
        "M.nazwa_statystyki1='" .. t2 ..
        "' AND M.nazwa_statystyki2='" .. t1 ..
        "')"
 
 
    -- =====================================================
    -- FUNKCJE POMOCNICZE
    -- =====================================================
 
     local function number(value)
 
         if value == nil or value == "" then
         if value == nil or value == "" then
             return 0
             return 0
Linia 28: Linia 96:


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


         return tonumber(value) or 0
         return tonumber(value) or 0
     end
     end


    local function wynik(row)


        local g1 = num(row.gole1)
    local function parseScore(value)
        local g2 = num(row.gole2)


         if row.gole1 ~= nil and row.gole2 ~= nil then
         if not value then
             return g1, g2
             return nil
         end
         end


        local w = row.wynik or ""
         local a, b =
 
            tostring(value):match(
         local a, b = w:match("(%d+)%s*:%s*(%d+)")
                "(%d+)%s*:%s*(%d+)"
            )


         if a and b then
         if a and b then
Linia 51: Linia 125:


         return nil, nil
         return nil, nil
     end
     end


    local function strona(row)


         if row.druzyna1 == team1 then
    local function matchScore(row)
 
        local g1 =
            tonumber(row.gole1 or "")
 
        local g2 =
            tonumber(row.gole2 or "")
 
         if g1 and g2 then
            return g1, g2
        end
 
        return parseScore(row.wynik)
 
    end
 
 
    local function location(row)
 
        local d1 =
            row.nazwa_statystyki1 or ""
 
        local d2 =
            row.nazwa_statystyki2 or ""
 
 
        if d1 == team1 then
             return "dom"
             return "dom"
         end
         end


         if row.druzyna2 == team1 then
         if d2 == team1 then
             return "wyjazd"
             return "wyjazd"
         end
         end


         return nil
         return nil
     end
     end


    local function nazwaMeczu(row)


        local d1 = row.druzyna1 or ""
    local function teamScore(row)
         local d2 = row.druzyna2 or ""
 
         local g1, g2 =
            matchScore(row)
 
        if not g1 or not g2 then
            return nil, nil
        end


        local nazwa =
            d1 .. " – " .. d2


         if row.strona_meczu and row.strona_meczu ~= "" then
         if location(row) == "dom" then
             return "[[" ..
             return g1, g2
                row.strona_meczu ..
                "|" .. nazwa .. "]]"
         end
         end


         return nazwa
         return g2, g1
 
    end
 
 
    local function result(row)
 
        local zdobyte, stracone =
            teamScore(row)
 
        if not zdobyte or not stracone then
            return nil
        end
 
        if zdobyte > stracone then
            return "W"
        end
 
        if zdobyte < stracone then
            return "P"
        end
 
        return "R"
 
    end
 
 
    local function matchName(row)
 
        local d1 =
            row.druzyna1 or ""
 
        local d2 =
            row.druzyna2 or ""
 
        local page =
            row.strona_meczu or ""
 
 
        local text =
            d1 ..
            " – " ..
            d2
 
 
        if page ~= "" then
 
            return
                "[[" ..
                page ..
                "|" ..
                text ..
                "]]"
 
        end
 
        return text
 
     end
     end


    local function linkOsoby(osoba)


         if not osoba or osoba == "" then
    local function formatPlusMinus(value)
             return "—"
 
        value =
            number(value)
 
         if value > 0 then
             return "+" .. tostring(value)
         end
         end


         return "[[" .. osoba .. "]]"
         return tostring(value)
 
     end
     end




     -- =========================================================
     -- =====================================================
     -- MECZE H2H
     -- POBRANIE MECZÓW
    --
     -- =====================================================
    -- KLUCZOWE:
 
    -- używamy nazwa_statystyki1 / nazwa_statystyki2,
    local fieldsMecze =
    -- a nie aktualnej nazwy sponsorskiej drużyny.
        table.concat({
     -- =========================================================
 
            "M.id=mecz",
            "M.data=data",
            "M.sezon=sezon",
 
            "M.druzyna1=druzyna1",
            "M.druzyna2=druzyna2",
 
            "M.nazwa_statystyki1=ns1",
            "M.nazwa_statystyki2=ns2",
 
            "M.gole1=gole1",
            "M.gole2=gole2",
 
            "M.wynik=wynik",
 
            "M.kwart1=kwarta1",
            "M.kwart2=kwarta2",
            "M.kwart3=kwarta3",
            "M.kwart4=kwarta4",


    local fieldsMecze = table.concat({
            "M.dogrywka1=dogrywka1",
            "M.dogrywka2=dogrywka2",


        "Mecze.id=mecz",
            "M.hala=hala",
        "Mecze.data=data",
            "M.widzowie=widzowie",
        "Mecze.sezon=sezon",
            "M.sedziowie=sedziowie",
        "Mecze.druzyna1=druzyna1",
        "Mecze.druzyna2=druzyna2",
        "Mecze.nazwa_statystyki1=ns1",
        "Mecze.nazwa_statystyki2=ns2",
        "Mecze.gole1=gole1",
        "Mecze.gole2=gole2",
        "Mecze.wynik=wynik",
        "Mecze.kolejka=kolejka",
        "Mecze.hala=hala",
        "Mecze.widzowie=widzowie",
        "Mecze.sedziowie=sedziowie",
        "Mecze.strona_meczu=strona_meczu",
        "Mecze.kwarta1=kwarta1",
        "Mecze.kwarta2=kwarta2",
        "Mecze.kwarta3=kwarta3",
        "Mecze.kwarta4=kwarta4",
        "Mecze.dogrywka1=dogrywka1",
        "Mecze.dogrywka2=dogrywka2",
        "Mecze.dogrywka3=dogrywka3"


    }, ",")
            "M.strona_meczu=strona_meczu"


    local whereH2H =
         }, ",")
         "(" ..
        "(Mecze.nazwa_statystyki1='" .. t1 ..
        "' AND Mecze.nazwa_statystyki2='" .. t2 .. "')" ..
        " OR " ..
        "(Mecze.nazwa_statystyki1='" .. t2 ..
        "' AND Mecze.nazwa_statystyki2='" .. t1 .. "')" ..
        ")"


local whereH2H_M =
    "(" ..
    "(M.nazwa_statystyki1='" .. t1 ..
    "' AND M.nazwa_statystyki2='" .. t2 .. "')" ..
    " OR " ..
    "(M.nazwa_statystyki1='" .. t2 ..
    "' AND M.nazwa_statystyki2='" .. t1 .. "')" ..
    ")"


     local mecze = mw.ext.cargo.query(
     local mecze =
        "Mecze",
        mw.ext.cargo.query(
        fieldsMecze,
            "Mecze=M",
        {
            fieldsMecze,
            where = whereH2H,
            {
            orderBy = "Mecze.data ASC",
                where = whereMecze,
            limit = 5000
                orderBy = "M.data ASC",
        }
                limit = 20000
    )
            }
        )




     if not mecze or #mecze == 0 then
     if not mecze or #mecze == 0 then
         return "'''Brak danych H2H dla podanych drużyn.'''"
 
         return
            "'''Brak wyników.'''"
 
     end
     end




     -- =========================================================
     -- =====================================================
     -- BILANS
     -- BILANS
     -- =========================================================
     -- =====================================================
 
    local liczbaMeczow = 0
 
    local wygrane1 = 0
    local wygrane2 = 0
 
    local remisy = 0
 
    local punkty1 = 0
    local punkty2 = 0


    local bilans = {
        mecze = 0,
        wygrane1 = 0,
        wygrane2 = 0,
        punkty1 = 0,
        punkty2 = 0
    }


     for _, row in ipairs(mecze) do
     for _, row in ipairs(mecze) do


         local g1, g2 = wynik(row)
         local g1, g2 =
            matchScore(row)


         if g1 and g2 then
         if g1 and g2 then


             bilans.mecze =
             liczbaMeczow =
                 bilans.mecze + 1
                 liczbaMeczow + 1


            if row.ns1 == team1 then


                 bilans.punkty1 =
            local d1 =
                     bilans.punkty1 + g1
                row.nazwa_statystyki1 or ""
 
            local d2 =
                 row.nazwa_statystyki2 or ""
 
 
            if d1 == team1 then
 
                punkty1 =
                     punkty1 + g1
 
                punkty2 =
                    punkty2 + g2


                bilans.punkty2 =
                    bilans.punkty2 + g2


                 if g1 > g2 then
                 if g1 > g2 then
                     bilans.wygrane1 =
                     wygrane1 =
                         bilans.wygrane1 + 1
                         wygrane1 + 1
 
                 elseif g2 > g1 then
                 elseif g2 > g1 then
                     bilans.wygrane2 =
                     wygrane2 =
                         bilans.wygrane2 + 1
                         wygrane2 + 1
 
                else
                    remisy =
                        remisy + 1
                 end
                 end


            else


                 bilans.punkty1 =
            elseif d1 == team2 then
                     bilans.punkty1 + g2
 
                 punkty1 =
                     punkty1 + g2
 
                punkty2 =
                    punkty2 + g1


                bilans.punkty2 =
                    bilans.punkty2 + g1


                 if g2 > g1 then
                 if g2 > g1 then
                     bilans.wygrane1 =
                     wygrane1 =
                         bilans.wygrane1 + 1
                         wygrane1 + 1
 
                 elseif g1 > g2 then
                 elseif g1 > g2 then
                     bilans.wygrane2 =
                     wygrane2 =
                         bilans.wygrane2 + 1
                         wygrane2 + 1
 
                else
                    remisy =
                        remisy + 1
                 end
                 end


Linia 222: Linia 401:




     -- =========================================================
     -- =====================================================
     -- REKORDY MECZOWE
     -- WYNIK PODSUMOWANIA
     -- =========================================================
     -- =====================================================
 
    local output = {}
 


     local rekordyMeczowe = {
     table.insert(
         zwyciestwo = {
        output,
            dom = {},
         "== Bilans bezpośrednich spotkań =="
            wyjazd = {}
    )
        },


         porazka = {
    table.insert(
            dom = {},
         output,
            wyjazd = {}
         ""
         }
     )
     }


    local function dodajRekordMeczowy(tabela, miejsce, roznica, row)


         local lista = tabela[miejsce]
    table.insert(
         output,
        '{| class="wikitable" style="width:100%; text-align:center;"'
    )


        if #lista == 0 then
    table.insert(
            table.insert(lista, {
        output,
                row = row,
        "! Drużyna !! Mecze !! Wygrane !! Remisy !! Porażki !! Punkty"
                roznica = roznica
    )
            })
            return
        end


        local obecna =
            lista[1].roznica


         if roznica > obecna then
    table.insert(
         output,
        "|-"
    )


            tabela[miejsce] = {
    table.insert(
                {
        output,
                    row = row,
        "| '''" ..
                    roznica = roznica
        team1 ..
                }
        "''' || " ..
            }
        liczbaMeczow ..
        " || " ..
        wygrane1 ..
        " || " ..
        remisy ..
        " || " ..
        wygrane2 ..
        " || " ..
        punkty1
    )


        elseif roznica == obecna then


            table.insert(lista, {
    table.insert(
                row = row,
        output,
                roznica = roznica
        "|-"
            })
    )


         end
    table.insert(
     end
        output,
        "| '''" ..
        team2 ..
        "''' || " ..
        liczbaMeczow ..
        " || " ..
        wygrane2 ..
        " || " ..
        remisy ..
        " || " ..
        wygrane1 ..
        " || " ..
         punkty2
     )




     -- =========================================================
     table.insert(
    -- SERIE
        output,
     -- =========================================================
        "|}"
     )


    local serie = {


        wszystkie = {
    -- =====================================================
            W = 0,
    -- HISTORIA SPOTKAŃ
            P = 0
    -- =====================================================
        },


         dom = {
    table.insert(
            W = 0,
         output,
            P = 0
        ""
        },
    )


         wyjazd = {
    table.insert(
            W = 0,
         output,
            P = 0
        "== Historia spotkań =="
        }
    )


     }
     table.insert(
        output,
        ""
    )


    local aktualne = {


         wszystkie = {
    table.insert(
            W = 0,
         output,
            P = 0
        '{| class="wikitable sortable" style="width:100%;"'
        },
    )


         dom = {
    table.insert(
            W = 0,
         output,
            P = 0
        "! Lp. !! Data !! Sezon !! Gospodarz !! Wynik !! Gość !! Hala"
        },
    )


        wyjazd = {
            W = 0,
            P = 0
        }


     }
     local lp = 0




     -- =========================================================
     for i = #mecze, 1, -1 do
    -- ANALIZA MECZÓW
    -- =========================================================


    for _, row in ipairs(mecze) do
        local row =
            mecze[i]


         local g1, g2 = wynik(row)
         local g1, g2 =
            matchScore(row)


         if g1 and g2 then
         if g1 and g2 then


             local mojaStrona
             lp =
                lp + 1


            if row.ns1 == team1 then
                mojaStrona = "dom"
            else
                mojaStrona = "wyjazd"
            end


             local mojePunkty
             table.insert(
             local rywalPunkty
                output,
                "|-"
             )


             if mojaStrona == "dom" then
             table.insert(
                 mojePunkty = g1
                output,
                 rywalPunkty = g2
                "| " ..
            else
                lp ..
                 mojePunkty = g2
                " || " ..
                 rywalPunkty = g1
                (row.data or "") ..
             end
                " || " ..
                (row.sezon or "") ..
                " || " ..
                (row.druzyna1 or "") ..
                " || '''" ..
                 g1 ..
                ":" ..
                 g2 ..
                "''' || " ..
                (row.druzyna2 or "") ..
                 " || " ..
                 (row.hala or "")
             )


            local roznica =
        end
                mojePunkty - rywalPunkty


    end


            -- ZWYCIĘSTWO


            if roznica > 0 then
    table.insert(
        output,
        "|}"
    )


                dodajRekordMeczowy(
                    rekordyMeczowe.zwyciestwo,
                    mojaStrona,
                    roznica,
                    row
                )


            end
    -- =====================================================
    -- NAJLEPSI STRZELCY
    -- =====================================================


    local fieldsStrzelcy =
        table.concat({


             -- PORAŻKA
             "U.zawodnik=zawodnik",
            "U.druzyna=druzyna",
            "SUM(U.gol)=punkty"


            if roznica < 0 then
        }, ",")


                dodajRekordMeczowy(
                    rekordyMeczowe.porazka,
                    mojaStrona,
                    math.abs(roznica),
                    row
                )


             end
    local strzelcy1 =
        mw.ext.cargo.query(
             "UdzialZawodnika=U,Mecze=M",
            fieldsStrzelcy,
            {
                join =
                    "U.mecz=M.id",


                where =
                    "(" ..
                    whereMecze ..
                    ")" ..
                    " AND " ..
                    "(" ..
                    "(M.nazwa_statystyki1='" ..
                    t1 ..
                    "' AND U.druzyna=M.druzyna1)" ..
                    " OR " ..
                    "(M.nazwa_statystyki2='" ..
                    t1 ..
                    "' AND U.druzyna=M.druzyna2)" ..
                    ")",


            -- SERIA OGÓŁEM
                groupBy =
                    "U.zawodnik,U.druzyna",


            if roznica > 0 then
                orderBy =
                    "punkty DESC",


                 aktualne.wszystkie.W =
                 limit = 1000
                    aktualne.wszystkie.W + 1
            }
        )


                aktualne.wszystkie.P = 0


                if aktualne.wszystkie.W >
    local strzelcy2 =
                     serie.wszystkie.W then
        mw.ext.cargo.query(
            "UdzialZawodnika=U,Mecze=M",
            fieldsStrzelcy,
            {
                join =
                     "U.mecz=M.id",


                     serie.wszystkie.W =
                where =
                        aktualne.wszystkie.W
                    "(" ..
                    whereMecze ..
                    ")" ..
                    " AND " ..
                    "(" ..
                    "(M.nazwa_statystyki1='" ..
                     t2 ..
                    "' AND U.druzyna=M.druzyna1)" ..
                    " OR " ..
                    "(M.nazwa_statystyki2='" ..
                    t2 ..
                    "' AND U.druzyna=M.druzyna2)" ..
                    ")",


                 end
                 groupBy =
                    "U.zawodnik,U.druzyna",


            elseif roznica < 0 then
                orderBy =
                    "punkty DESC",


                 aktualne.wszystkie.P =
                 limit = 1000
                    aktualne.wszystkie.P + 1
            }
        )


                aktualne.wszystkie.W = 0


                if aktualne.wszystkie.P >
    table.insert(
                    serie.wszystkie.P then
        output,
        ""
    )


                    serie.wszystkie.P =
    table.insert(
                        aktualne.wszystkie.P
        output,
        "== Najlepsi strzelcy rywalizacji =="
    )


                end
    table.insert(
        output,
        ""
    )


            end


    table.insert(
        output,
        '{| class="wikitable" style="width:100%;"'
    )


            -- SERIA DOMOWA
    table.insert(
        output,
        "! " ..
        team1 ..
        " !! Punkty !! " ..
        team2 ..
        " !! Punkty"
    )


            if mojaStrona == "dom" then


                if roznica > 0 then
    local maxStrzelcy =
        math.max(
            strzelcy1 and #strzelcy1 or 0,
            strzelcy2 and #strzelcy2 or 0
        )


                    aktualne.dom.W =
                        aktualne.dom.W + 1


                    aktualne.dom.P = 0
    for i = 1, maxStrzelcy do


                    if aktualne.dom.W >
        local s1 =
                        serie.dom.W then
            strzelcy1 and strzelcy1[i]


                        serie.dom.W =
        local s2 =
                            aktualne.dom.W
            strzelcy2 and strzelcy2[i]


                    end


                elseif roznica < 0 then
        table.insert(
            output,
            "|-"
        )


                    aktualne.dom.P =
                        aktualne.dom.P + 1


                     aktualne.dom.W = 0
        table.insert(
            output,
            "| " ..
            (
                s1
                and
                "[[" ..
                s1.zawodnik ..
                "]]"
                or
                ""
            ) ..
            " || " ..
            (
                s1
                and
                tostring(
                     number(s1.punkty)
                )
                or
                ""
            ) ..
            " || " ..
            (
                s2
                and
                "[[" ..
                s2.zawodnik ..
                "]]"
                or
                ""
            ) ..
            " || " ..
            (
                s2
                and
                tostring(
                    number(s2.punkty)
                )
                or
                ""
            )
        )


                    if aktualne.dom.P >
    end
                        serie.dom.P then


                        serie.dom.P =
                            aktualne.dom.P


                    end
    table.insert(
        output,
        "|}"
    )


                end


            end
    -- =====================================================
    -- TRENERZY
    -- =====================================================


    local fieldsTrenerzy =
        table.concat({


             -- SERIA WYJAZDOWA
             "S.osoba=osoba",
            "S.druzyna=druzyna",
            "S.rola=rola"


            if mojaStrona == "wyjazd" then
        }, ",")


                if roznica > 0 then


                    aktualne.wyjazd.W =
    local trenerzy =
                        aktualne.wyjazd.W + 1
        mw.ext.cargo.query(
            "SztabMeczu=S,Mecze=M",
            fieldsTrenerzy,
            {
                join =
                    "S.mecz=M.id",


                     aktualne.wyjazd.P = 0
                where =
                    "(" ..
                     whereMecze ..
                    ")" ..
                    " AND " ..
                    "S.rola IN ('Trener','II trener')",


                     if aktualne.wyjazd.W >
                groupBy =
                        serie.wyjazd.W then
                     "S.osoba,S.druzyna,S.rola",


                        serie.wyjazd.W =
                orderBy =
                            aktualne.wyjazd.W
                    "S.osoba ASC",


                    end
                limit = 5000
            }
        )


                elseif roznica < 0 then


                    aktualne.wyjazd.P =
    table.insert(
                        aktualne.wyjazd.P + 1
        output,
        ""
    )


                    aktualne.wyjazd.W = 0
    table.insert(
        output,
        "== Trenerzy w historii rywalizacji =="
    )


                    if aktualne.wyjazd.P >
    table.insert(
                        serie.wyjazd.P then
        output,
        ""
    )


                        serie.wyjazd.P =
                            aktualne.wyjazd.P


                    end
    table.insert(
        output,
        '{| class="wikitable sortable" style="width:100%;"'
    )


                end
    table.insert(
        output,
        "! Trener !! Drużyna !! Rola"
    )


            end


        end
    if trenerzy then


    end
        for _, row in ipairs(trenerzy) do


            table.insert(
                output,
                "|-"
            )


    -- =========================================================
            table.insert(
    -- REKORDY INDYWIDUALNE
                output,
    -- =========================================================
                "| [[" ..
                (row.osoba or "") ..
                "]] || " ..
                (row.druzyna or "") ..
                " || " ..
                (row.rola or "")
            )


    local rekordy = {
        end


        punkty = {dom = {}, wyjazd = {}},
    end
        minuty = {dom = {}, wyjazd = {}},
        asysty = {dom = {}, wyjazd = {}},
        zbiorki = {dom = {}, wyjazd = {}},
        eval = {dom = {}, wyjazd = {}},
        plusminus = {dom = {}, wyjazd = {}}


    }


    table.insert(
        output,
        "|}"
    )


    local fieldsU = table.concat({


        "U.zawodnik=zawodnik",
    -- =====================================================
        "U.druzyna=druzyna_zawodnika",
    -- REKORDY MECZOWE
        "U.gol=gol",
    -- =====================================================
        "U.minuty=minuty",
        "U.minuty_format=minuty_format",
        "U.asysta=asysta",
        "U.zbiorka=zbiorka",
        "U.eval=eval",
        "U.plusminus=plusminus",


        "M.id=mecz",
    local rekordMecz =
         "M.data=data",
         {
        "M.druzyna1=druzyna1",
            zwyciestwo =
        "M.druzyna2=druzyna2",
            {
        "M.nazwa_statystyki1=ns1",
                dom = {},
        "M.nazwa_statystyki2=ns2",
                wyjazd = {}
        "M.wynik=wynik",
            },
        "M.strona_meczu=strona_meczu"


    }, ",")
            porazka =
            {
                dom = {},
                wyjazd = {}
            }
        }




     local zawodnicy = mw.ext.cargo.query(
     local function addMatchRecord(
         "UdzialZawodnika=U,Mecze=M",
         list,
         fieldsU,
        item,
         {
         value,
            join = "U.mecz=M.id",
         field
    )


            where =
        if #list == 0 then
                whereH2H ..
                " AND U.minuty > 0",


             orderBy = "M.data ASC",
             item[field] =
                value


             limit = 20000
             table.insert(
        }
                list,
    )
                item
            )


            return


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


        local lista = tabela[miejsce]


         if #lista == 0 then
         local current =
            list[1][field]


            table.insert(lista, {
                item = item,
                wartosc = wartosc
            })


            return
         if value > current then
         end


        local obecna =
             item[field] =
             lista[1].wartosc
                value


        if wartosc > obecna then
            list[1] =
                item


            tabela[miejsce] = {
        elseif value == current then
                {
                    item = item,
                    wartosc = wartosc
                }
            }


        elseif wartosc == obecna then
            item[field] =
                value


             table.insert(lista, {
             table.insert(
                 item = item,
                 list,
                 wartosc = wartosc
                 item
             })
             )


         end
         end
Linia 589: Linia 918:




     if zawodnicy then
     for _, row in ipairs(mecze) do
 
        local zdobyte, stracone =
            teamScore(row)


         for _, row in ipairs(zawodnicy) do
         local miejsce =
            location(row)


            local miejsce


            -- Strona drużyny jest ustalana po
        if zdobyte and stracone and miejsce then
            -- nazwa_statystyki, a nie po sponsorze.


             if row.ns1 == team1 then
             local roznica =
                 miejsce = "dom"
                 zdobyte - stracone
            elseif row.ns2 == team1 then
                miejsce = "wyjazd"
            end


            if miejsce then


                local item = {
            local item =
            {
                tekst =
                    matchName(row),


                     zawodnik = row.zawodnik or "",
                wynik =
                     mecz = row.mecz or "",
                     tostring(
                     data = row.data or "",
                        row.gole1 or zdobyte
                     wynik = row.wynik or "",
                     ) ..
                    ":" ..
                     tostring(
                        row.gole2 or stracone
                     ),


                    tekst =
                roznica =
                        (row.druzyna1 or "") ..
                    roznica
                        " – " ..
            }
                        (row.druzyna2 or ""),


                    punkty = num(row.gol),
                    minuty = num(row.minuty),
                    minuty_format =
                        row.minuty_format or "",
                    asysty = num(row.asysta),
                    zbiorki = num(row.zbiorka),
                    eval = num(row.eval),
                    plusminus = num(row.plusminus)


                }
            if roznica > 0 then


                 dodajRekord(
                 addMatchRecord(
                     rekordy.punkty,
                     rekordMecz.zwyciestwo[miejsce],
                     miejsce,
                     item,
                     item.punkty,
                     roznica,
                     item
                     "wartosc"
                 )
                 )


                 dodajRekord(
            elseif roznica < 0 then
                     rekordy.minuty,
 
                     miejsce,
                 addMatchRecord(
                     item.minuty,
                     rekordMecz.porazka[miejsce],
                     item
                     item,
                     math.abs(roznica),
                     "wartosc"
                 )
                 )


                dodajRekord(
            end
                    rekordy.asysty,
 
                    miejsce,
        end
                    item.asysty,
 
                    item
    end
                )
 
 
    local function formatMatchRecord(list)
 
        if not list or #list == 0 then
            return "—"
        end
 


                dodajRekord(
        local out = {}
                    rekordy.zbiorki,
                    miejsce,
                    item.zbiorki,
                    item
                )


                dodajRekord(
                    rekordy.eval,
                    miejsce,
                    item.eval,
                    item
                )


                dodajRekord(
        for _, item in ipairs(list) do
                    rekordy.plusminus,
                    miejsce,
                    item.plusminus,
                    item
                )


             end
             table.insert(
                out,
                item.tekst ..
                " (" ..
                item.wynik ..
                ", +" ..
                tostring(
                    item.wartosc
                ) ..
                ")"
            )


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


     end
     end




     -- =========================================================
     table.insert(
     -- TRENERZY
        output,
     -- =========================================================
        ""
    )
 
    table.insert(
        output,
        "== Rekordy meczowe =="
    )
 
    table.insert(
        output,
        ""
    )
 
 
    table.insert(
        output,
        '{| class="wikitable" style="width:100%;"'
    )
 
     table.insert(
        output,
        "! Rekord !! 🏠 Dom !! ✈️ Wyjazd"
     )


    local fieldsSztab = table.concat({


        "S.osoba=osoba",
    table.insert(
        "S.druzyna=druzyna",
         output,
        "S.rola=rola",
         "|-"
         "S.mecz=mecz",
    )
         "M.data=data",
        "M.nazwa_statystyki1=ns1",
        "M.nazwa_statystyki2=ns2",
        "M.gole1=gole1",
        "M.gole2=gole2"


     }, ",")
     table.insert(
        output,
        "! 🏆 Najwyższe zwycięstwo || " ..
        formatMatchRecord(
            rekordMecz.zwyciestwo.dom
        ) ..
        " || " ..
        formatMatchRecord(
            rekordMecz.zwyciestwo.wyjazd
        )
    )




     local sztab = mw.ext.cargo.query(
     table.insert(
         "SztabMeczu=S,Mecze=M",
         output,
         fieldsSztab,
         "|-"
        {
    )
            join = "S.mecz=M.id",


             where =
    table.insert(
                whereH2H ..
        output,
                " AND S.rola IN ('Trener','II trener')",
        "! ❌ Najwyższa porażka || " ..
        formatMatchRecord(
             rekordMecz.porazka.dom
        ) ..
        " || " ..
        formatMatchRecord(
            rekordMecz.porazka.wyjazd
        )
    )


            orderBy = "M.data ASC",


            limit = 20000
    table.insert(
         }
         output,
        "|}"
     )
     )




     local trenerzy = {}
     -- =====================================================
    -- REKORDY INDYWIDUALNE
    -- =====================================================


     if sztab then
     local fieldsZaw =
        table.concat({


        for _, row in ipairs(sztab) do
            "U.zawodnik=zawodnik",
            "U.druzyna=druzyna_zawodnika",


             local klucz =
             "U.gol=gol",
                (row.osoba or "") ..
            "U.minuty=minuty",
                "|" ..
            "U.minuty_format=minuty_format",
                (row.druzyna or "") ..
                "|" ..
                (row.rola or "")


             if not trenerzy[klucz] then
             "U.asysta=asysta",
            "U.zbiorka=zbiorka",


                trenerzy[klucz] = {
            "U.eval=eval",
                    osoba = row.osoba or "",
            "U.plusminus=plusminus",
                    druzyna = row.druzyna or "",
                    rola = row.rola or "",
                    mecze = 0,
                    wygrane = 0,
                    przegrane = 0
                }


             end
             "M.id=mecz",
            "M.data=data",


             local t = trenerzy[klucz]
             "M.druzyna1=druzyna1",
            "M.druzyna2=druzyna2",


             t.mecze =
             "M.gole1=gole1",
                t.mecze + 1
            "M.gole2=gole2",


             local g1, g2 =
             "M.wynik=wynik",
                wynik(row)


             if g1 and g2 then
             "M.nazwa_statystyki1=ns1",
            "M.nazwa_statystyki2=ns2",


                local dlaDruzyny
            "M.strona_meczu=strona_meczu"


                if row.druzyna == row.ns1 then
        }, ",")
                    dlaDruzyny = g1 - g2
                elseif row.druzyna == row.ns2 then
                    dlaDruzyny = g2 - g1
                end


                if dlaDruzyny then


                    if dlaDruzyny > 0 then
    local zawodnicy =
                        t.wygrane =
        mw.ext.cargo.query(
                            t.wygrane + 1
            "UdzialZawodnika=U,Mecze=M",
                     elseif dlaDruzyny < 0 then
            fieldsZaw,
                        t.przegrane =
            {
                            t.przegrane + 1
                join =
                    end
                     "U.mecz=M.id",


                 end
                 where =
                    "(" ..
                    whereMecze ..
                    ")" ..
                    " AND U.minuty > 0",


            end
                orderBy =
                    "M.data ASC",


         end
                limit = 20000
            }
         )


    end


    local rekordy =
    {
        punkty =
        {
            dom = {},
            wyjazd = {}
        },


    -- =========================================================
        minuty =
    -- NAJLEPSI STRZELCY
        {
    --
            dom = {},
    -- SUMA PUNKTÓW ZOSTAJE.
            wyjazd = {}
    -- =========================================================
        },


    local strzelcy = {
        asysty =
        team1 = {},
        {
        team2 = {}
            dom = {},
    }
            wyjazd = {}
        },


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


    if zawodnicy then
        eval =
        {
            dom = {},
            wyjazd = {}
        },


         for _, row in ipairs(zawodnicy) do
         plusminus =
        {
            dom = {},
            wyjazd = {}
        }
    }


            local stronaDruzyny


            if row.ns1 == team1 then
    local function addPlayerRecord(
        tabela,
        miejsce,
        wartosc,
        item
    )


                if row.druzyna_zawodnika == row.druzyna1 then
        local list =
                    stronaDruzyny = "team1"
            tabela[miejsce]
                end


            elseif row.ns2 == team1 then


                if row.druzyna_zawodnika == row.druzyna2 then
        if #list == 0 then
                    stronaDruzyny = "team1"
                end


             elseif row.ns1 == team2 then
             item.wartosc =
                wartosc


                if row.druzyna_zawodnika == row.druzyna1 then
            table.insert(
                    stronaDruzyny = "team2"
                list,
                 end
                 item
            )


             elseif row.ns2 == team2 then
             return


                if row.druzyna_zawodnika == row.druzyna2 then
        end
                    stronaDruzyny = "team2"
                end


            end


        local current =
            list[1].wartosc


            if stronaDruzyny then


                local zawodnik =
        if wartosc > current then
                    row.zawodnik or ""


                 if not strzelcy[stronaDruzyny][zawodnik] then
            item.wartosc =
                 wartosc


                    strzelcy[stronaDruzyny][zawodnik] = 0
            tabela[miejsce] =
            {
                item
            }


                end
        elseif wartosc == current then


                strzelcy[stronaDruzyny][zawodnik] =
            item.wartosc =
                    strzelcy[stronaDruzyny][zawodnik] +
                wartosc
                    num(row.gol)


             end
             table.insert(
                list,
                item
            )


         end
         end
Linia 844: Linia 1237:




     local function posortowaniStrzelcy(tabela)
     if zawodnicy then


         local lista = {}
         for _, row in ipairs(zawodnicy) do


        for zawodnik, punkty in pairs(tabela) do
            local miejsce =
                location(row)


            table.insert(lista, {
                zawodnik = zawodnik,
                punkty = punkty
            })


        end
            if miejsce then
 
                local item =
                {
                    zawodnik =
                        row.zawodnik or "",
 
                    mecz =
                        row.mecz or "",
 
                    tekst =
                        matchName(row),
 
                    wynik =
                        row.wynik or "",
 
                    punkty =
                        number(row.gol),
 
                    minuty =
                        number(row.minuty),
 
                    minuty_format =
                        row.minuty_format or "",
 
                    asysty =
                        number(row.asysta),
 
                    zbiorki =
                        number(row.zbiorka),
 
                    eval =
                        number(row.eval),


        table.sort(lista, function(a,b)
                    plusminus =
                        number(row.plusminus)
                }


            if a.punkty == b.punkty then
                return a.zawodnik < b.zawodnik
            end


            return a.punkty > b.punkty
                addPlayerRecord(
                    rekordy.punkty,
                    miejsce,
                    item.punkty,
                    item
                )


        end)


        return lista
                addPlayerRecord(
                    rekordy.minuty,
                    miejsce,
                    item.minuty,
                    item
                )


    end


                addPlayerRecord(
                    rekordy.asysty,
                    miejsce,
                    item.asysty,
                    item
                )


    -- =========================================================
    -- FORMATOWANIE REKORDÓW
    -- =========================================================


    local function meczeRekordowe(lista)
                addPlayerRecord(
                    rekordy.zbiorki,
                    miejsce,
                    item.zbiorki,
                    item
                )


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


        local wynik = {}
                addPlayerRecord(
                    rekordy.eval,
                    miejsce,
                    item.eval,
                    item
                )


        for _, r in ipairs(lista) do


            local row = r.row
                addPlayerRecord(
                    rekordy.plusminus,
                    miejsce,
                    item.plusminus,
                    item
                )


             table.insert(
             end
                wynik,
                nazwaMeczu(row) ..
                " (" ..
                tostring(row.gole1 or "") ..
                ":" ..
                tostring(row.gole2 or "") ..
                ", +" ..
                tostring(r.roznica) ..
                ")"
            )


         end
         end
        return table.concat(
            wynik,
            "<br>"
        )


     end
     end




     local function rekordyZawodnikow(lista, typ)
     local function formatPlayerRecord(
        list,
        typ
    )


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


        local wynik = {}


         for _, r in ipairs(lista) do
        local out = {}
 
 
         for _, item in ipairs(list) do
 
            local value = ""


            local x = r.item
            local wartosc


             if typ == "punkty" then
             if typ == "punkty" then
                 wartosc = x.punkty .. " pkt"
 
                 value =
                    tostring(item.punkty) ..
                    " pkt"


             elseif typ == "minuty" then
             elseif typ == "minuty" then


                 wartosc =
                 value =
                     x.minuty_format ~= "" and
                     item.minuty_format
                     x.minuty_format or
 
                    tostring(x.minuty)
                if value == "" then
 
                     value =
                        tostring(item.minuty)
 
                end


                 wartosc = wartosc .. " min"
                 value =
                    value ..
                    " min"


             elseif typ == "asysty" then
             elseif typ == "asysty" then
                 wartosc = x.asysty .. " as."
 
                 value =
                    tostring(item.asysty) ..
                    " as."


             elseif typ == "zbiorki" then
             elseif typ == "zbiorki" then
                 wartosc = x.zbiorki .. " zb."
 
                 value =
                    tostring(item.zbiorki) ..
                    " zb."


             elseif typ == "eval" then
             elseif typ == "eval" then
                 wartosc = x.eval .. " EVAL"
 
                 value =
                    tostring(item.eval) ..
                    " EVAL"


             elseif typ == "plusminus" then
             elseif typ == "plusminus" then


                 if x.plusminus >= 0 then
                 value =
                     wartosc =
                     formatPlusMinus(
                         "+" .. x.plusminus
                         item.plusminus
                else
                     ) ..
                     wartosc =
                    " +/-"
                        tostring(x.plusminus)
                end


                wartosc =
            end
                    wartosc .. " +/-"


            end


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


         end
         end


         return table.concat(
         return table.concat(
             wynik,
             out,
             "<br>"
             "<br>"
         )
         )
Linia 980: Linia 1430:
     end
     end


    -- =========================================================
    -- WYNIK
    -- =========================================================
    local out = {}


     table.insert(
     table.insert(
         out,
         output,
         "== Bilans bezpośrednich spotkań =="
         ""
     )
     )


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


     table.insert(
     table.insert(
         out,
         output,
         "! Drużyna !! Mecze !! Wygrane !! Porażki !! Punkty zdobyte"
         ""
     )
     )


    table.insert(out, "|-")


     table.insert(
     table.insert(
         out,
         output,
         "| '''" .. team1 .. "'''"
         '{| class="wikitable" style="width:100%;"'
     )
     )


     table.insert(
     table.insert(
         out,
         output,
         "| " .. bilans.mecze
         "! Rekord !! 🏠 Dom !! ✈️ Wyjazd"
     )
     )


    table.insert(
        out,
        "| " .. bilans.wygrane1
    )


     table.insert(
     local rekordyLista =
         out,
    {
        "| " .. bilans.wygrane2
         {
    )
            nazwa =
                "🏀 Najwięcej punktów",


    table.insert(
            tabela =
        out,
                rekordy.punkty,
        "| " .. bilans.punkty1
    )


    table.insert(out, "|-")
            typ =
                "punkty"
        },


    table.insert(
        {
        out,
            nazwa =
        "| '''" .. team2 .. "'''"
                "⏱️ Najwięcej minut",
    )


    table.insert(
            tabela =
        out,
                rekordy.minuty,
        "| " .. bilans.mecze
    )


    table.insert(
            typ =
        out,
                "minuty"
        "| " .. bilans.wygrane2
        },
    )


    table.insert(
        {
        out,
            nazwa =
        "| " .. bilans.wygrane1
                "🎯 Najwięcej asyst",
    )


    table.insert(
            tabela =
        out,
                rekordy.asysty,
        "| " .. bilans.punkty2
    )


    table.insert(out, "|}")
            typ =
                "asysty"
        },


    table.insert(out, "")
        {
            nazwa =
                "🏀 Najwięcej zbiórek",


            tabela =
                rekordy.zbiorki,


    -- =========================================================
            typ =
    -- WSZYSTKIE MECZE H2H
                "zbiorki"
    -- =========================================================
        },


    table.insert(
        {
        out,
            nazwa =
        "== Wszystkie mecze =="
                "⭐ Najwyższy EVAL",
    )


    table.insert(
            tabela =
        out,
                rekordy.eval,
        '{| class="wikitable sortable" style="width:100%;"'
    )


    table.insert(
            typ =
        out,
                "eval"
        "! Nr !! Data !! Sezon !! Gospodarz !! Wynik !! Gość !! Hala"
        },
    )


    for i = #mecze, 1, -1 do
        {
            nazwa =
                "➕ Najwyższy +/-",


        local row = mecze[i]
            tabela =
                rekordy.plusminus,


        table.insert(out, "|-")
            typ =
                "plusminus"
        }
    }


        table.insert(
            out,
            "| " .. (#mecze - i + 1)
        )


        table.insert(
    for _, rekord in ipairs(rekordyLista) do
            out,
            "| " .. (row.data or "")
        )


         table.insert(
         table.insert(
             out,
             output,
             "| " .. (row.sezon or "")
             "|-"
         )
         )


         table.insert(
         table.insert(
             out,
             output,
             "| " .. (row.druzyna1 or "")
             "! " ..
        )
            rekord.nazwa ..
 
             " || " ..
        table.insert(
             formatPlayerRecord(
            out,
                 rekord.tabela.dom,
             "| '''" ..
                 rekord.typ
             (row.wynik or
            ) ..
                 ((row.gole1 or "") ..
             " || " ..
                 ":" ..
             formatPlayerRecord(
                (row.gole2 or ""))) ..
                rekord.tabela.wyjazd,
             "'''"
                rekord.typ
        )
             )
 
        table.insert(
             out,
            "| " .. (row.druzyna2 or "")
        )
 
        table.insert(
            out,
             "| " .. (row.hala or "")
         )
         )


     end
     end


    table.insert(out, "|}")
    -- =========================================================
    -- TRENERZY
    -- =========================================================
    table.insert(out, "")


     table.insert(
     table.insert(
         out,
         output,
         "== Trenerzy w bezpośrednich spotkaniach =="
         "|}"
     )
     )


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


     table.insert(
     -- =====================================================
        out,
    -- SERIE
        "! Trener !! Drużyna !! Rola !! Mecze !! Wygrane !! Porażki !! % wygranych"
     -- =====================================================
     )


     local listaTrenerow = {}
     local serie =
    {
        wszystkie =
        {
            W = 0,
            P = 0,
            maxW = 0,
            maxP = 0
        },


    for _, t in pairs(trenerzy) do
        dom =
         table.insert(listaTrenerow, t)
        {
    end
            W = 0,
            P = 0,
            maxW = 0,
            maxP = 0
         },


    table.sort(listaTrenerow, function(a,b)
        wyjazd =
         return a.mecze > b.mecze
        {
     end)
            W = 0,
            P = 0,
            maxW = 0,
            maxP = 0
         }
     }


    for _, t in ipairs(listaTrenerow) do


         local procent = 0
    local function updateSerie(
         obj,
        wynik
    )


         if t.mecze > 0 then
         if wynik == "W" then
            procent =
                math.floor(
                    (t.wygrane / t.mecze) *
                    1000 + 0.5
                ) / 10
        end


        table.insert(out, "|-")
            obj.W =
                obj.W + 1


        table.insert(
             obj.P =
            out,
                0
             "| " .. linkOsoby(t.osoba)
        )


        table.insert(
             if obj.W > obj.maxW then
             out,
            "| " .. t.druzyna
        )


        table.insert(
                obj.maxW =
            out,
                    obj.W
            "| " .. t.rola
        )


        table.insert(
             end
             out,
            "| " .. t.mecze
        )


        table.insert(
            out,
            "| " .. t.wygrane
        )


         table.insert(
         elseif wynik == "P" then
            out,
            "| " .. t.przegrane
        )


        table.insert(
            obj.P =
            out,
                obj.P + 1
            "| " .. procent .. "%"
        )


    end
            obj.W =
                0


    table.insert(out, "|}")
            if obj.P > obj.maxP then


                obj.maxP =
                    obj.P


    -- =========================================================
            end
    -- NAJLEPSI STRZELCY
    -- =========================================================


    table.insert(out, "")
        else


    table.insert(
            obj.W =
        out,
                0
        "== Najlepsi strzelcy rywalizacji =="
    )


    table.insert(
            obj.P =
        out,
                0
        '{| class="wikitable" style="width:100%;"'
    )


    table.insert(
         end
         out,
        "! " .. team1 .. " !! Punkty !! " ..
        team2 .. " !! Punkty"
    )


     local s1 =
     end
        posortowaniStrzelcy(
            strzelcy.team1
        )


    local s2 =
        posortowaniStrzelcy(
            strzelcy.team2
        )


     local max =
     for _, row in ipairs(mecze) do
        math.max(#s1, #s2)


    for i = 1, max do
        local wynik =
            result(row)


         table.insert(out, "|-")
         local miejsce =
            location(row)


        if s1[i] then


            table.insert(
        if wynik then
                out,
                "| " ..
                linkOsoby(s1[i].zawodnik)
            )


             table.insert(
             updateSerie(
                 out,
                 serie.wszystkie,
                 "| " ..
                 wynik
                s1[i].punkty
             )
             )


        else


             table.insert(out, "|")
             if miejsce == "dom" then
            table.insert(out, "|")


        end
                updateSerie(
                    serie.dom,
                    wynik
                )


        if s2[i] then
            elseif miejsce == "wyjazd" then


            table.insert(
                updateSerie(
                out,
                    serie.wyjazd,
                "| " ..
                    wynik
                 linkOsoby(s2[i].zawodnik)
                 )
            )


             table.insert(
             end
                out,
                "| " ..
                s2[i].punkty
            )
 
        else
 
            table.insert(out, "|")
            table.insert(out, "|")


         end
         end
Linia 1299: Linia 1678:
     end
     end


    table.insert(out, "|}")
    -- =========================================================
    -- REKORDY MECZOWE
    -- =========================================================
    table.insert(out, "")


     table.insert(
     table.insert(
         out,
         output,
         "== Rekordy meczowe – " .. team1 .. " =="
         ""
     )
     )


     table.insert(
     table.insert(
         out,
         output,
         '{| class="wikitable" style="width:100%; text-align:center;"'
         "== Serie zwycięstw i porażek =="
     )
     )


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


    table.insert(out, "|-")


     table.insert(
     table.insert(
         out,
         output,
         "! 🏆 Najwyższe zwycięstwo"
         '{| class="wikitable" style="width:100%; text-align:center;"'
     )
     )


     table.insert(
     table.insert(
         out,
         output,
         "| " ..
         "! Seria !! Wszystkie mecze !! 🏠 Dom !! ✈️ Wyjazd"
        meczeRekordowe(
            rekordyMeczowe.zwyciestwo.dom
        )
     )
     )


     table.insert(
     table.insert(
         out,
         output,
         "| " ..
         "|-"
        meczeRekordowe(
            rekordyMeczowe.zwyciestwo.wyjazd
        )
     )
     )
    table.insert(out, "|-")


     table.insert(
     table.insert(
         out,
         output,
         "! ❌ Najwyższa porażka"
         "! 🔥 Najdłuższa seria zwycięstw || " ..
        serie.wszystkie.maxW ..
        " || " ..
        serie.dom.maxW ..
        " || " ..
        serie.wyjazd.maxW
     )
     )


    table.insert(
        out,
        "| " ..
        meczeRekordowe(
            rekordyMeczowe.porazka.dom
        )
    )


     table.insert(
     table.insert(
         out,
         output,
         "| " ..
         "|-"
        meczeRekordowe(
            rekordyMeczowe.porazka.wyjazd
        )
     )
     )
    table.insert(out, "|}")
    -- =========================================================
    -- REKORDY INDYWIDUALNE
    -- =========================================================
    table.insert(out, "")


     table.insert(
     table.insert(
         out,
         output,
         "== Rekordy indywidualne – " .. team1 .. " =="
         "! 📉 Najdłuższa seria porażek || " ..
        serie.wszystkie.maxP ..
        " || " ..
        serie.dom.maxP ..
        " || " ..
        serie.wyjazd.maxP
     )
     )


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


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




     local rekordyLista = {
     -- =====================================================
    -- KWARTY A KOŃCOWY WYNIK
    --
    -- Nie korzystamy z dogrywka3.
    -- Dzięki temu mecz z trzema dogrywkami nie powoduje
    -- błędu "No field named dogrywka3".
    --
    -- Analizujemy wyłącznie kwarty 1-4.
    -- =====================================================


    local kwartowe =
    {
        [1] =
         {
         {
             "🏀 Najwięcej punktów",
             wygrana = 0,
             rekordy.punkty,
             porazka = 0,
             "punkty"
             remis = 0,
            wszystkie = 0
         },
         },


        [2] =
         {
         {
             "⏱️ Najwięcej minut",
             wygrana = 0,
             rekordy.minuty,
             porazka = 0,
             "minuty"
             remis = 0,
            wszystkie = 0
         },
         },


        [3] =
         {
         {
             "🎯 Najwięcej asyst",
             wygrana = 0,
             rekordy.asysty,
             porazka = 0,
             "asysty"
             remis = 0,
            wszystkie = 0
         },
         },


        [4] =
         {
         {
             "🏀 Najwięcej zbiórek",
             wygrana = 0,
             rekordy.zbiorki,
            porazka = 0,
            "zbiorki"
            remis = 0,
        },
            wszystkie = 0
        }
    }
 
 
    local function quarterScore(value)
 
        if not value or value == "" then
            return nil, nil
        end
 
        return parseScore(value)
 
    end
 
 
    for _, row in ipairs(mecze) do
 
        local final =
            result(row)
 
 
        if final == "W" or final == "P" then
 
             for q = 1, 4 do
 
                local value =
                    row["kwarta" .. q]
 
 
                local a, b =
                    quarterScore(value)
 
 
                if a and b then
 
                    local teamPoints
                    local opponentPoints
 
 
                    if location(row) == "dom" then
 
                        teamPoints = a
                        opponentPoints = b
 
                    elseif location(row) == "wyjazd" then
 
                        teamPoints = b
                        opponentPoints = a
 
                    end
 
 
                    if teamPoints and opponentPoints then
 
                        kwartowe[q].wszystkie =
                            kwartowe[q].wszystkie + 1
 
 
                        if final == "W" then
 
                            if teamPoints > opponentPoints then
 
                                kwartowe[q].wygrana =
                                    kwartowe[q].wygrana + 1
 
                            elseif teamPoints < opponentPoints then
 
                                kwartowe[q].porazka =
                                    kwartowe[q].porazka + 1
 
                            else
 
                                kwartowe[q].remis =
                                    kwartowe[q].remis + 1
 
                            end
 
 
                        elseif final == "P" then


        {
                            if teamPoints < opponentPoints then
            "⭐ Najwyższy EVAL",
            rekordy.eval,
            "eval"
        },


        {
                                kwartowe[q].wygrana =
            "➕ Najwyższy +/-",
                                    kwartowe[q].wygrana + 1
            rekordy.plusminus,
            "plusminus"
        }


    }
                            elseif teamPoints > opponentPoints then


                                kwartowe[q].porazka =
                                    kwartowe[q].porazka + 1


    for _, r in ipairs(rekordyLista) do
                            else


        table.insert(out, "|-")
                                kwartowe[q].remis =
                                    kwartowe[q].remis + 1


        table.insert(
                            end
            out,
            "! " .. r[1]
        )


        table.insert(
                        end
            out,
            "| " ..
            rekordyZawodnikow(
                r[2].dom,
                r[3]
            )
        )


        table.insert(
                    end
            out,
            "| " ..
            rekordyZawodnikow(
                r[2].wyjazd,
                r[3]
            )
        )


    end
                end


    table.insert(out, "|}")
            end


        end


     -- =========================================================
     end
    -- SERIE
    -- =========================================================


    table.insert(out, "")


     table.insert(
     table.insert(
         out,
         output,
         "== Serie – " .. team1 .. " =="
         ""
     )
     )


     table.insert(
     table.insert(
         out,
         output,
         '{| class="wikitable" style="width:100%; text-align:center;"'
         "== Wynik kwarty a końcowy wynik meczu =="
     )
     )


     table.insert(
     table.insert(
         out,
         output,
         "! Seria !! Ogółem !! 🏠 Dom !! ✈️ Wyjazd"
         ""
     )
     )


    table.insert(out, "|-")


     table.insert(
     table.insert(
         out,
         output,
         "! 🔥 Najdłuższa seria zwycięstw"
         '{| class="wikitable" style="width:100%; text-align:center;"'
     )
     )


     table.insert(
     table.insert(
         out,
         output,
         "| " .. serie.wszystkie.W
         "! Kwarta !! Mecz zakończony wygraną !! Mecz zakończony porażką !! Remis w kwarcie !! Wszystkie analizowane mecze"
     )
     )


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


     table.insert(
     for q = 1, 4 do
        out,
 
        "| " .. serie.wyjazd.W
        local data =
    )
            kwartowe[q]
 
 
        table.insert(
            output,
            "|-"
        )


    table.insert(out, "|-")
        table.insert(
            output,
            "! " ..
            q ..
            ". kwarta || " ..
            data.wygrana ..
            " || " ..
            data.porazka ..
            " || " ..
            data.remis ..
            " || " ..
            data.wszystkie
        )


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


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


     table.insert(
     table.insert(
         out,
         output,
         "| " .. serie.dom.P
         "|}"
     )
     )


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


     table.insert(out, "|}")
     -- =====================================================
    -- KONIEC
    -- =====================================================


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


end
end


return p
return p

Wersja z 09:46, 18 sie 2026

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

local p = {}

-- =========================================================
-- Module:H2H
--
-- Historia bezpośrednich spotkań dwóch drużyn.
--
-- Drużyny są rozpoznawane po:
--   Mecze.nazwa_statystyki1
--   Mecze.nazwa_statystyki2
--
-- Dzięki temu zmiana sponsora / nazwy drużyny nie rozbija H2H.
--
-- Funkcje:
--   1. bilans spotkań
--   2. suma punktów
--   3. wszystkie mecze
--   4. najlepsi strzelcy
--   5. trenerzy
--   6. rekordy meczowe
--   7. rekordy indywidualne
--   8. serie zwycięstw / porażek
--   9. kwartowe porównanie wyników
--
-- =========================================================


function p.main(frame)

    -- =====================================================
    -- PARAMETRY
    -- =====================================================

    local team1 =
        mw.text.trim(frame.args.team1 or "")

    local team2 =
        mw.text.trim(frame.args.team2 or "")


    if team1 == "" or team2 == "" then

        return "'''Błąd:''' należy podać obie drużyny."

    end


    -- =====================================================
    -- BEZPIECZEŃSTWO SQL
    -- =====================================================

    local function sql(value)

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

    end


    local t1 = sql(team1)
    local t2 = sql(team2)


    -- =====================================================
    -- WARUNEK H2H DLA TABELI Mecze
    --
    -- WAŻNE:
    -- tutaj używamy nazwa_statystyki.
    -- Dzięki temu zmiana nazwy sponsorskiej nie rozbija H2H.
    -- =====================================================

    local whereMecze =
        "(" ..
        "M.nazwa_statystyki1='" .. t1 ..
        "' AND M.nazwa_statystyki2='" .. t2 ..
        "')" ..
        " OR " ..
        "(" ..
        "M.nazwa_statystyki1='" .. t2 ..
        "' AND M.nazwa_statystyki2='" .. t1 ..
        "')"


    -- =====================================================
    -- FUNKCJE POMOCNICZE
    -- =====================================================

    local function number(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 parseScore(value)

        if not value then
            return nil
        end

        local a, b =
            tostring(value):match(
                "(%d+)%s*:%s*(%d+)"
            )

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

        return nil, nil

    end


    local function matchScore(row)

        local g1 =
            tonumber(row.gole1 or "")

        local g2 =
            tonumber(row.gole2 or "")

        if g1 and g2 then
            return g1, g2
        end

        return parseScore(row.wynik)

    end


    local function location(row)

        local d1 =
            row.nazwa_statystyki1 or ""

        local d2 =
            row.nazwa_statystyki2 or ""


        if d1 == team1 then
            return "dom"
        end

        if d2 == team1 then
            return "wyjazd"
        end

        return nil

    end


    local function teamScore(row)

        local g1, g2 =
            matchScore(row)

        if not g1 or not g2 then
            return nil, nil
        end


        if location(row) == "dom" then
            return g1, g2
        end

        return g2, g1

    end


    local function result(row)

        local zdobyte, stracone =
            teamScore(row)

        if not zdobyte or not stracone then
            return nil
        end

        if zdobyte > stracone then
            return "W"
        end

        if zdobyte < stracone then
            return "P"
        end

        return "R"

    end


    local function matchName(row)

        local d1 =
            row.druzyna1 or ""

        local d2 =
            row.druzyna2 or ""

        local page =
            row.strona_meczu or ""


        local text =
            d1 ..
            " – " ..
            d2


        if page ~= "" then

            return
                "[[" ..
                page ..
                "|" ..
                text ..
                "]]"

        end

        return text

    end


    local function formatPlusMinus(value)

        value =
            number(value)

        if value > 0 then
            return "+" .. tostring(value)
        end

        return tostring(value)

    end


    -- =====================================================
    -- POBRANIE MECZÓW
    -- =====================================================

    local fieldsMecze =
        table.concat({

            "M.id=mecz",
            "M.data=data",
            "M.sezon=sezon",

            "M.druzyna1=druzyna1",
            "M.druzyna2=druzyna2",

            "M.nazwa_statystyki1=ns1",
            "M.nazwa_statystyki2=ns2",

            "M.gole1=gole1",
            "M.gole2=gole2",

            "M.wynik=wynik",

            "M.kwart1=kwarta1",
            "M.kwart2=kwarta2",
            "M.kwart3=kwarta3",
            "M.kwart4=kwarta4",

            "M.dogrywka1=dogrywka1",
            "M.dogrywka2=dogrywka2",

            "M.hala=hala",
            "M.widzowie=widzowie",
            "M.sedziowie=sedziowie",

            "M.strona_meczu=strona_meczu"

        }, ",")


    local mecze =
        mw.ext.cargo.query(
            "Mecze=M",
            fieldsMecze,
            {
                where = whereMecze,
                orderBy = "M.data ASC",
                limit = 20000
            }
        )


    if not mecze or #mecze == 0 then

        return
            "'''Brak wyników.'''"

    end


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

    local liczbaMeczow = 0

    local wygrane1 = 0
    local wygrane2 = 0

    local remisy = 0

    local punkty1 = 0
    local punkty2 = 0


    for _, row in ipairs(mecze) do

        local g1, g2 =
            matchScore(row)

        if g1 and g2 then

            liczbaMeczow =
                liczbaMeczow + 1


            local d1 =
                row.nazwa_statystyki1 or ""

            local d2 =
                row.nazwa_statystyki2 or ""


            if d1 == team1 then

                punkty1 =
                    punkty1 + g1

                punkty2 =
                    punkty2 + g2


                if g1 > g2 then
                    wygrane1 =
                        wygrane1 + 1

                elseif g2 > g1 then
                    wygrane2 =
                        wygrane2 + 1

                else
                    remisy =
                        remisy + 1
                end


            elseif d1 == team2 then

                punkty1 =
                    punkty1 + g2

                punkty2 =
                    punkty2 + g1


                if g2 > g1 then
                    wygrane1 =
                        wygrane1 + 1

                elseif g1 > g2 then
                    wygrane2 =
                        wygrane2 + 1

                else
                    remisy =
                        remisy + 1
                end

            end

        end

    end


    -- =====================================================
    -- WYNIK PODSUMOWANIA
    -- =====================================================

    local output = {}


    table.insert(
        output,
        "== Bilans bezpośrednich spotkań =="
    )

    table.insert(
        output,
        ""
    )


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

    table.insert(
        output,
        "! Drużyna !! Mecze !! Wygrane !! Remisy !! Porażki !! Punkty"
    )


    table.insert(
        output,
        "|-"
    )

    table.insert(
        output,
        "| '''" ..
        team1 ..
        "''' || " ..
        liczbaMeczow ..
        " || " ..
        wygrane1 ..
        " || " ..
        remisy ..
        " || " ..
        wygrane2 ..
        " || " ..
        punkty1
    )


    table.insert(
        output,
        "|-"
    )

    table.insert(
        output,
        "| '''" ..
        team2 ..
        "''' || " ..
        liczbaMeczow ..
        " || " ..
        wygrane2 ..
        " || " ..
        remisy ..
        " || " ..
        wygrane1 ..
        " || " ..
        punkty2
    )


    table.insert(
        output,
        "|}"
    )


    -- =====================================================
    -- HISTORIA SPOTKAŃ
    -- =====================================================

    table.insert(
        output,
        ""
    )

    table.insert(
        output,
        "== Historia spotkań =="
    )

    table.insert(
        output,
        ""
    )


    table.insert(
        output,
        '{| class="wikitable sortable" style="width:100%;"'
    )

    table.insert(
        output,
        "! Lp. !! Data !! Sezon !! Gospodarz !! Wynik !! Gość !! Hala"
    )


    local lp = 0


    for i = #mecze, 1, -1 do

        local row =
            mecze[i]

        local g1, g2 =
            matchScore(row)

        if g1 and g2 then

            lp =
                lp + 1


            table.insert(
                output,
                "|-"
            )

            table.insert(
                output,
                "| " ..
                lp ..
                " || " ..
                (row.data or "") ..
                " || " ..
                (row.sezon or "") ..
                " || " ..
                (row.druzyna1 or "") ..
                " || '''" ..
                g1 ..
                ":" ..
                g2 ..
                "''' || " ..
                (row.druzyna2 or "") ..
                " || " ..
                (row.hala or "")
            )

        end

    end


    table.insert(
        output,
        "|}"
    )


    -- =====================================================
    -- NAJLEPSI STRZELCY
    -- =====================================================

    local fieldsStrzelcy =
        table.concat({

            "U.zawodnik=zawodnik",
            "U.druzyna=druzyna",
            "SUM(U.gol)=punkty"

        }, ",")


    local strzelcy1 =
        mw.ext.cargo.query(
            "UdzialZawodnika=U,Mecze=M",
            fieldsStrzelcy,
            {
                join =
                    "U.mecz=M.id",

                where =
                    "(" ..
                    whereMecze ..
                    ")" ..
                    " AND " ..
                    "(" ..
                    "(M.nazwa_statystyki1='" ..
                    t1 ..
                    "' AND U.druzyna=M.druzyna1)" ..
                    " OR " ..
                    "(M.nazwa_statystyki2='" ..
                    t1 ..
                    "' AND U.druzyna=M.druzyna2)" ..
                    ")",

                groupBy =
                    "U.zawodnik,U.druzyna",

                orderBy =
                    "punkty DESC",

                limit = 1000
            }
        )


    local strzelcy2 =
        mw.ext.cargo.query(
            "UdzialZawodnika=U,Mecze=M",
            fieldsStrzelcy,
            {
                join =
                    "U.mecz=M.id",

                where =
                    "(" ..
                    whereMecze ..
                    ")" ..
                    " AND " ..
                    "(" ..
                    "(M.nazwa_statystyki1='" ..
                    t2 ..
                    "' AND U.druzyna=M.druzyna1)" ..
                    " OR " ..
                    "(M.nazwa_statystyki2='" ..
                    t2 ..
                    "' AND U.druzyna=M.druzyna2)" ..
                    ")",

                groupBy =
                    "U.zawodnik,U.druzyna",

                orderBy =
                    "punkty DESC",

                limit = 1000
            }
        )


    table.insert(
        output,
        ""
    )

    table.insert(
        output,
        "== Najlepsi strzelcy rywalizacji =="
    )

    table.insert(
        output,
        ""
    )


    table.insert(
        output,
        '{| class="wikitable" style="width:100%;"'
    )

    table.insert(
        output,
        "! " ..
        team1 ..
        " !! Punkty !! " ..
        team2 ..
        " !! Punkty"
    )


    local maxStrzelcy =
        math.max(
            strzelcy1 and #strzelcy1 or 0,
            strzelcy2 and #strzelcy2 or 0
        )


    for i = 1, maxStrzelcy do

        local s1 =
            strzelcy1 and strzelcy1[i]

        local s2 =
            strzelcy2 and strzelcy2[i]


        table.insert(
            output,
            "|-"
        )


        table.insert(
            output,
            "| " ..
            (
                s1
                and
                "[[" ..
                s1.zawodnik ..
                "]]"
                or
                ""
            ) ..
            " || " ..
            (
                s1
                and
                tostring(
                    number(s1.punkty)
                )
                or
                ""
            ) ..
            " || " ..
            (
                s2
                and
                "[[" ..
                s2.zawodnik ..
                "]]"
                or
                ""
            ) ..
            " || " ..
            (
                s2
                and
                tostring(
                    number(s2.punkty)
                )
                or
                ""
            )
        )

    end


    table.insert(
        output,
        "|}"
    )


    -- =====================================================
    -- TRENERZY
    -- =====================================================

    local fieldsTrenerzy =
        table.concat({

            "S.osoba=osoba",
            "S.druzyna=druzyna",
            "S.rola=rola"

        }, ",")


    local trenerzy =
        mw.ext.cargo.query(
            "SztabMeczu=S,Mecze=M",
            fieldsTrenerzy,
            {
                join =
                    "S.mecz=M.id",

                where =
                    "(" ..
                    whereMecze ..
                    ")" ..
                    " AND " ..
                    "S.rola IN ('Trener','II trener')",

                groupBy =
                    "S.osoba,S.druzyna,S.rola",

                orderBy =
                    "S.osoba ASC",

                limit = 5000
            }
        )


    table.insert(
        output,
        ""
    )

    table.insert(
        output,
        "== Trenerzy w historii rywalizacji =="
    )

    table.insert(
        output,
        ""
    )


    table.insert(
        output,
        '{| class="wikitable sortable" style="width:100%;"'
    )

    table.insert(
        output,
        "! Trener !! Drużyna !! Rola"
    )


    if trenerzy then

        for _, row in ipairs(trenerzy) do

            table.insert(
                output,
                "|-"
            )

            table.insert(
                output,
                "| [[" ..
                (row.osoba or "") ..
                "]] || " ..
                (row.druzyna or "") ..
                " || " ..
                (row.rola or "")
            )

        end

    end


    table.insert(
        output,
        "|}"
    )


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

    local rekordMecz =
        {
            zwyciestwo =
            {
                dom = {},
                wyjazd = {}
            },

            porazka =
            {
                dom = {},
                wyjazd = {}
            }
        }


    local function addMatchRecord(
        list,
        item,
        value,
        field
    )

        if #list == 0 then

            item[field] =
                value

            table.insert(
                list,
                item
            )

            return

        end


        local current =
            list[1][field]


        if value > current then

            item[field] =
                value

            list[1] =
                item

        elseif value == current then

            item[field] =
                value

            table.insert(
                list,
                item
            )

        end

    end


    for _, row in ipairs(mecze) do

        local zdobyte, stracone =
            teamScore(row)

        local miejsce =
            location(row)


        if zdobyte and stracone and miejsce then

            local roznica =
                zdobyte - stracone


            local item =
            {
                tekst =
                    matchName(row),

                wynik =
                    tostring(
                        row.gole1 or zdobyte
                    ) ..
                    ":" ..
                    tostring(
                        row.gole2 or stracone
                    ),

                roznica =
                    roznica
            }


            if roznica > 0 then

                addMatchRecord(
                    rekordMecz.zwyciestwo[miejsce],
                    item,
                    roznica,
                    "wartosc"
                )

            elseif roznica < 0 then

                addMatchRecord(
                    rekordMecz.porazka[miejsce],
                    item,
                    math.abs(roznica),
                    "wartosc"
                )

            end

        end

    end


    local function formatMatchRecord(list)

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


        local out = {}


        for _, item in ipairs(list) do

            table.insert(
                out,
                item.tekst ..
                " (" ..
                item.wynik ..
                ", +" ..
                tostring(
                    item.wartosc
                ) ..
                ")"
            )

        end


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

    end


    table.insert(
        output,
        ""
    )

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

    table.insert(
        output,
        ""
    )


    table.insert(
        output,
        '{| class="wikitable" style="width:100%;"'
    )

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


    table.insert(
        output,
        "|-"
    )

    table.insert(
        output,
        "! 🏆 Najwyższe zwycięstwo || " ..
        formatMatchRecord(
            rekordMecz.zwyciestwo.dom
        ) ..
        " || " ..
        formatMatchRecord(
            rekordMecz.zwyciestwo.wyjazd
        )
    )


    table.insert(
        output,
        "|-"
    )

    table.insert(
        output,
        "! ❌ Najwyższa porażka || " ..
        formatMatchRecord(
            rekordMecz.porazka.dom
        ) ..
        " || " ..
        formatMatchRecord(
            rekordMecz.porazka.wyjazd
        )
    )


    table.insert(
        output,
        "|}"
    )


    -- =====================================================
    -- REKORDY INDYWIDUALNE
    -- =====================================================

    local fieldsZaw =
        table.concat({

            "U.zawodnik=zawodnik",
            "U.druzyna=druzyna_zawodnika",

            "U.gol=gol",
            "U.minuty=minuty",
            "U.minuty_format=minuty_format",

            "U.asysta=asysta",
            "U.zbiorka=zbiorka",

            "U.eval=eval",
            "U.plusminus=plusminus",

            "M.id=mecz",
            "M.data=data",

            "M.druzyna1=druzyna1",
            "M.druzyna2=druzyna2",

            "M.gole1=gole1",
            "M.gole2=gole2",

            "M.wynik=wynik",

            "M.nazwa_statystyki1=ns1",
            "M.nazwa_statystyki2=ns2",

            "M.strona_meczu=strona_meczu"

        }, ",")


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

                where =
                    "(" ..
                    whereMecze ..
                    ")" ..
                    " AND U.minuty > 0",

                orderBy =
                    "M.data ASC",

                limit = 20000
            }
        )


    local rekordy =
    {
        punkty =
        {
            dom = {},
            wyjazd = {}
        },

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

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

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

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

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


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

        local list =
            tabela[miejsce]


        if #list == 0 then

            item.wartosc =
                wartosc

            table.insert(
                list,
                item
            )

            return

        end


        local current =
            list[1].wartosc


        if wartosc > current then

            item.wartosc =
                wartosc

            tabela[miejsce] =
            {
                item
            }

        elseif wartosc == current then

            item.wartosc =
                wartosc

            table.insert(
                list,
                item
            )

        end

    end


    if zawodnicy then

        for _, row in ipairs(zawodnicy) do

            local miejsce =
                location(row)


            if miejsce then

                local item =
                {
                    zawodnik =
                        row.zawodnik or "",

                    mecz =
                        row.mecz or "",

                    tekst =
                        matchName(row),

                    wynik =
                        row.wynik or "",

                    punkty =
                        number(row.gol),

                    minuty =
                        number(row.minuty),

                    minuty_format =
                        row.minuty_format or "",

                    asysty =
                        number(row.asysta),

                    zbiorki =
                        number(row.zbiorka),

                    eval =
                        number(row.eval),

                    plusminus =
                        number(row.plusminus)
                }


                addPlayerRecord(
                    rekordy.punkty,
                    miejsce,
                    item.punkty,
                    item
                )


                addPlayerRecord(
                    rekordy.minuty,
                    miejsce,
                    item.minuty,
                    item
                )


                addPlayerRecord(
                    rekordy.asysty,
                    miejsce,
                    item.asysty,
                    item
                )


                addPlayerRecord(
                    rekordy.zbiorki,
                    miejsce,
                    item.zbiorki,
                    item
                )


                addPlayerRecord(
                    rekordy.eval,
                    miejsce,
                    item.eval,
                    item
                )


                addPlayerRecord(
                    rekordy.plusminus,
                    miejsce,
                    item.plusminus,
                    item
                )

            end

        end

    end


    local function formatPlayerRecord(
        list,
        typ
    )

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


        local out = {}


        for _, item in ipairs(list) do

            local value = ""


            if typ == "punkty" then

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

            elseif typ == "minuty" then

                value =
                    item.minuty_format

                if value == "" then

                    value =
                        tostring(item.minuty)

                end

                value =
                    value ..
                    " min"

            elseif typ == "asysty" then

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

            elseif typ == "zbiorki" then

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

            elseif typ == "eval" then

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

            elseif typ == "plusminus" then

                value =
                    formatPlusMinus(
                        item.plusminus
                    ) ..
                    " +/-"

            end


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

        end


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

    end


    table.insert(
        output,
        ""
    )

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

    table.insert(
        output,
        ""
    )


    table.insert(
        output,
        '{| class="wikitable" style="width:100%;"'
    )

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


    local rekordyLista =
    {
        {
            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 _, rekord in ipairs(rekordyLista) do

        table.insert(
            output,
            "|-"
        )

        table.insert(
            output,
            "! " ..
            rekord.nazwa ..
            " || " ..
            formatPlayerRecord(
                rekord.tabela.dom,
                rekord.typ
            ) ..
            " || " ..
            formatPlayerRecord(
                rekord.tabela.wyjazd,
                rekord.typ
            )
        )

    end


    table.insert(
        output,
        "|}"
    )


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

    local serie =
    {
        wszystkie =
        {
            W = 0,
            P = 0,
            maxW = 0,
            maxP = 0
        },

        dom =
        {
            W = 0,
            P = 0,
            maxW = 0,
            maxP = 0
        },

        wyjazd =
        {
            W = 0,
            P = 0,
            maxW = 0,
            maxP = 0
        }
    }


    local function updateSerie(
        obj,
        wynik
    )

        if wynik == "W" then

            obj.W =
                obj.W + 1

            obj.P =
                0

            if obj.W > obj.maxW then

                obj.maxW =
                    obj.W

            end


        elseif wynik == "P" then

            obj.P =
                obj.P + 1

            obj.W =
                0

            if obj.P > obj.maxP then

                obj.maxP =
                    obj.P

            end

        else

            obj.W =
                0

            obj.P =
                0

        end

    end


    for _, row in ipairs(mecze) do

        local wynik =
            result(row)

        local miejsce =
            location(row)


        if wynik then

            updateSerie(
                serie.wszystkie,
                wynik
            )


            if miejsce == "dom" then

                updateSerie(
                    serie.dom,
                    wynik
                )

            elseif miejsce == "wyjazd" then

                updateSerie(
                    serie.wyjazd,
                    wynik
                )

            end

        end

    end


    table.insert(
        output,
        ""
    )

    table.insert(
        output,
        "== Serie zwycięstw i porażek =="
    )

    table.insert(
        output,
        ""
    )


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

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


    table.insert(
        output,
        "|-"
    )

    table.insert(
        output,
        "! 🔥 Najdłuższa seria zwycięstw || " ..
        serie.wszystkie.maxW ..
        " || " ..
        serie.dom.maxW ..
        " || " ..
        serie.wyjazd.maxW
    )


    table.insert(
        output,
        "|-"
    )

    table.insert(
        output,
        "! 📉 Najdłuższa seria porażek || " ..
        serie.wszystkie.maxP ..
        " || " ..
        serie.dom.maxP ..
        " || " ..
        serie.wyjazd.maxP
    )


    table.insert(
        output,
        "|}"
    )


    -- =====================================================
    -- KWARTY A KOŃCOWY WYNIK
    --
    -- Nie korzystamy z dogrywka3.
    -- Dzięki temu mecz z trzema dogrywkami nie powoduje
    -- błędu "No field named dogrywka3".
    --
    -- Analizujemy wyłącznie kwarty 1-4.
    -- =====================================================

    local kwartowe =
    {
        [1] =
        {
            wygrana = 0,
            porazka = 0,
            remis = 0,
            wszystkie = 0
        },

        [2] =
        {
            wygrana = 0,
            porazka = 0,
            remis = 0,
            wszystkie = 0
        },

        [3] =
        {
            wygrana = 0,
            porazka = 0,
            remis = 0,
            wszystkie = 0
        },

        [4] =
        {
            wygrana = 0,
            porazka = 0,
            remis = 0,
            wszystkie = 0
        }
    }


    local function quarterScore(value)

        if not value or value == "" then
            return nil, nil
        end

        return parseScore(value)

    end


    for _, row in ipairs(mecze) do

        local final =
            result(row)


        if final == "W" or final == "P" then

            for q = 1, 4 do

                local value =
                    row["kwarta" .. q]


                local a, b =
                    quarterScore(value)


                if a and b then

                    local teamPoints
                    local opponentPoints


                    if location(row) == "dom" then

                        teamPoints = a
                        opponentPoints = b

                    elseif location(row) == "wyjazd" then

                        teamPoints = b
                        opponentPoints = a

                    end


                    if teamPoints and opponentPoints then

                        kwartowe[q].wszystkie =
                            kwartowe[q].wszystkie + 1


                        if final == "W" then

                            if teamPoints > opponentPoints then

                                kwartowe[q].wygrana =
                                    kwartowe[q].wygrana + 1

                            elseif teamPoints < opponentPoints then

                                kwartowe[q].porazka =
                                    kwartowe[q].porazka + 1

                            else

                                kwartowe[q].remis =
                                    kwartowe[q].remis + 1

                            end


                        elseif final == "P" then

                            if teamPoints < opponentPoints then

                                kwartowe[q].wygrana =
                                    kwartowe[q].wygrana + 1

                            elseif teamPoints > opponentPoints then

                                kwartowe[q].porazka =
                                    kwartowe[q].porazka + 1

                            else

                                kwartowe[q].remis =
                                    kwartowe[q].remis + 1

                            end

                        end

                    end

                end

            end

        end

    end


    table.insert(
        output,
        ""
    )

    table.insert(
        output,
        "== Wynik kwarty a końcowy wynik meczu =="
    )

    table.insert(
        output,
        ""
    )


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

    table.insert(
        output,
        "! Kwarta !! Mecz zakończony wygraną !! Mecz zakończony porażką !! Remis w kwarcie !! Wszystkie analizowane mecze"
    )


    for q = 1, 4 do

        local data =
            kwartowe[q]


        table.insert(
            output,
            "|-"
        )

        table.insert(
            output,
            "! " ..
            q ..
            ". kwarta || " ..
            data.wygrana ..
            " || " ..
            data.porazka ..
            " || " ..
            data.remis ..
            " || " ..
            data.wszystkie
        )

    end


    table.insert(
        output,
        "|}"
    )


    -- =====================================================
    -- KONIEC
    -- =====================================================

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

end


return p