Przejdź do zawartości

Moduł:H2H

Z Encyklopedia Poznanskiego Sportu

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

local p = {}

function p.main(frame)

    local team1 = frame.args.team1 or ""
    local team2 = frame.args.team2 or ""

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

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

    local function esc(value)
        value = tostring(value or "")
        return mw.ustring.gsub(value, "'", "''")
    end

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

    local function num(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 wynik(row)

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

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

        local w = row.wynik or ""

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

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

        return nil, nil
    end

    local function strona(row)

        if row.druzyna1 == team1 then
            return "dom"
        end

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

        return nil
    end

    local function nazwaMeczu(row)

        local d1 = row.druzyna1 or ""
        local d2 = row.druzyna2 or ""

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

        if row.strona_meczu and row.strona_meczu ~= "" then
            return "[[" ..
                row.strona_meczu ..
                "|" .. nazwa .. "]]"
        end

        return nazwa
    end

    local function linkOsoby(osoba)

        if not osoba or osoba == "" then
            return "—"
        end

        return "[[" .. osoba .. "]]"
    end


    -- =========================================================
    -- MECZE H2H
    --
    -- KLUCZOWE:
    -- używamy nazwa_statystyki1 / nazwa_statystyki2,
    -- a nie aktualnej nazwy sponsorskiej drużyny.
    -- =========================================================

    local fieldsMecze = table.concat({

        "Mecze.id=mecz",
        "Mecze.data=data",
        "Mecze.sezon=sezon",
        "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"

    }, ",")

    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(
        "Mecze",
        fieldsMecze,
        {
            where = whereH2H,
            orderBy = "Mecze.data ASC",
            limit = 5000
        }
    )


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


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

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

    for _, row in ipairs(mecze) do

        local g1, g2 = wynik(row)

        if g1 and g2 then

            bilans.mecze =
                bilans.mecze + 1

            if row.ns1 == team1 then

                bilans.punkty1 =
                    bilans.punkty1 + g1

                bilans.punkty2 =
                    bilans.punkty2 + g2

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

            else

                bilans.punkty1 =
                    bilans.punkty1 + g2

                bilans.punkty2 =
                    bilans.punkty2 + g1

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

            end

        end

    end


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

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

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

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

        local lista = tabela[miejsce]

        if #lista == 0 then
            table.insert(lista, {
                row = row,
                roznica = roznica
            })
            return
        end

        local obecna =
            lista[1].roznica

        if roznica > obecna then

            tabela[miejsce] = {
                {
                    row = row,
                    roznica = roznica
                }
            }

        elseif roznica == obecna then

            table.insert(lista, {
                row = row,
                roznica = roznica
            })

        end
    end


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

    local serie = {

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

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

        wyjazd = {
            W = 0,
            P = 0
        }

    }

    local aktualne = {

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

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

        wyjazd = {
            W = 0,
            P = 0
        }

    }


    -- =========================================================
    -- ANALIZA MECZÓW
    -- =========================================================

    for _, row in ipairs(mecze) do

        local g1, g2 = wynik(row)

        if g1 and g2 then

            local mojaStrona

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

            local mojePunkty
            local rywalPunkty

            if mojaStrona == "dom" then
                mojePunkty = g1
                rywalPunkty = g2
            else
                mojePunkty = g2
                rywalPunkty = g1
            end

            local roznica =
                mojePunkty - rywalPunkty


            -- ZWYCIĘSTWO

            if roznica > 0 then

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

            end


            -- PORAŻKA

            if roznica < 0 then

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

            end


            -- SERIA OGÓŁEM

            if roznica > 0 then

                aktualne.wszystkie.W =
                    aktualne.wszystkie.W + 1

                aktualne.wszystkie.P = 0

                if aktualne.wszystkie.W >
                    serie.wszystkie.W then

                    serie.wszystkie.W =
                        aktualne.wszystkie.W

                end

            elseif roznica < 0 then

                aktualne.wszystkie.P =
                    aktualne.wszystkie.P + 1

                aktualne.wszystkie.W = 0

                if aktualne.wszystkie.P >
                    serie.wszystkie.P then

                    serie.wszystkie.P =
                        aktualne.wszystkie.P

                end

            end


            -- SERIA DOMOWA

            if mojaStrona == "dom" then

                if roznica > 0 then

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

                    aktualne.dom.P = 0

                    if aktualne.dom.W >
                        serie.dom.W then

                        serie.dom.W =
                            aktualne.dom.W

                    end

                elseif roznica < 0 then

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

                    aktualne.dom.W = 0

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

                        serie.dom.P =