Przejdź do zawartości

Moduł:HistoriaWystepow

Z Encyklopedia Poznanskiego Sportu

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

local p = {}

function p.main(frame)

    local zawodnik = frame.args.zawodnik

    if not zawodnik or zawodnik == "" then
        zawodnik = mw.title.getCurrentTitle().text
    end

    -- Bezpieczne przygotowanie nazwy zawodnika do SQL
    local zawodnikSQL = mw.ustring.gsub(zawodnik, "'", "''")

    -- ==========================================
    -- POBRANIE WYSTĘPÓW
    -- ==========================================

    local fields = table.concat({
        "UdzialZawodnika.mecz=mecz",
        "UdzialZawodnika.druzyna=druzyna",
        "UdzialZawodnika.minuty=minuty",
        "UdzialZawodnika.minuty_format=minuty_format",
        "UdzialZawodnika.gol=gol",
        "UdzialZawodnika.eval=eval",
        "UdzialZawodnika.plusminus=plusminus",
        "Mecze.data=data",
        "Mecze.druzyna1=druzyna1",
        "Mecze.druzyna2=druzyna2",
        "Mecze.wynik=wynik",
        "Mecze.strona_meczu=strona_meczu"
    }, ",")

    local args = {
        join = "UdzialZawodnika.mecz=Mecze.id",
        where = "UdzialZawodnika.zawodnik='" .. zawodnikSQL .. "' AND UdzialZawodnika.minuty > 0",
        orderBy = "Mecze.data ASC",
        limit = 5000
    }

    local rows = mw.ext.cargo.query(
        "UdzialZawodnika,Mecze",
        fields,
        args
    )

    if not rows or #rows == 0 then
        return "''Brak wprowadzonych występów w bazie.''"
    end

    -- ==========================================
    -- PODSUMOWANIE
    -- ==========================================

    local wystepy = #rows
    local zwyciestwa = 0
    local porazki = 0
    local remisy = 0

    local punkty = 0
    local sumaMinut = 0
    local sumaEval = 0
    local sumaPlusMinus = 0

    for _, row in ipairs(rows) do

        punkty = punkty + tonumber(row.gol or 0)
        sumaMinut = sumaMinut + tonumber(row.minuty or 0)
        sumaEval = sumaEval + tonumber(row.eval or 0)
        sumaPlusMinus = sumaPlusMinus + tonumber(row.plusminus or 0)

        local d1 = row.druzyna1 or ""
        local d2 = row.druzyna2 or ""
        local druzyna = row.druzyna or ""
        local wynikMeczu = row.wynik or ""

        local g1, g2 = wynikMeczu:match("(%d+)%s*:%s*(%d+)")

        if g1 and g2 then

            g1 = tonumber(g1)
            g2 = tonumber(g2)

            if druzyna == d1 then

                if g1 > g2 then
                    zwyciestwa = zwyciestwa + 1
                elseif g1 < g2 then
                    porazki = porazki + 1
                else
                    remisy = remisy + 1
                end

            elseif druzyna == d2 then

                if g2 > g1 then
                    zwyciestwa = zwyciestwa + 1
                elseif g2 < g1 then
                    porazki = porazki + 1
                else
                    remisy = remisy + 1
                end

            end
        end
    end

    -- ==========================================
    -- FORMAT MINUT
    -- ==========================================

    local godziny = math.floor(sumaMinut / 60)
    local minuty = sumaMinut % 60

    local sumaMinutFormat = string.format(
        "%d:%02d",
        godziny,
        minuty
    )

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

    local wynik = {}

    table.insert(wynik, "== Historia występów ==")
    table.insert(wynik, "")

    -- ==========================================
    -- TABELA PODSUMOWANIA
    -- ==========================================

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

    table.insert(wynik, '! colspan="2" | Występy')
    table.insert(wynik, "! Zwycięstwa")
    table.insert(wynik, "! Porażki")
    table.insert(wynik, "! Remisy")
    table.insert(wynik, "! Punkty")
    table.insert(wynik, "! Minuty")
    table.insert(wynik, "! EVAL")
    table.insert(wynik, "! +/-")

    table.insert(wynik, "|-")

    table.insert(wynik, "| colspan=2 | '''" .. wystepy .. "'''")
    table.insert(wynik, "| '''" .. zwyciestwa .. "'''")
    table.insert(wynik, "| '''" .. porazki .. "'''")
    table.insert(wynik, "| '''" .. remisy .. "'''")
    table.insert(wynik, "| '''" .. punkty .. "'''")
    table.insert(wynik, "| " .. sumaMinutFormat)
    table.insert(wynik, "| " .. sumaEval)
    table.insert(wynik, "| " .. sumaPlusMinus)

    table.insert(wynik, "|}")

    table.insert(wynik, "")

    -- ==========================================
    -- TABELA WYSTĘPÓW
    -- ==========================================

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

    table.insert(wynik, "! Nr")
    table.insert(wynik, "! Data")
    table.insert(wynik, "! Przeciwnik")
    table.insert(wynik, "! Wynik")
    table.insert(wynik, "! Min.")
    table.insert(wynik, "! Pkt")
    table.insert(wynik, "! EVAL")
    table.insert(wynik, "! +/-")
    table.insert(wynik, "! Jubileusz")

    -- ==========================================
    -- POSZCZEGÓLNE MECZE
    -- ==========================================

    for i, row in ipairs(rows) do

        local data = row.data or ""
        local d1 = row.druzyna1 or ""
        local d2 = row.druzyna2 or ""
        local druzyna = row.druzyna or ""
        local wynikMeczu = row.wynik or ""
        local strona = row.strona_meczu or ""

        local minutyFormat = row.minuty_format or ""
        local pkt = row.gol or "0"
        local eval = row.eval or "0"
        local plusminus = row.plusminus or "0"

        -- ======================================
        -- PRZECIWNIK
        -- ======================================

        local przeciwnik = ""

        if druzyna == d1 then
            przeciwnik = d2
        elseif druzyna == d2 then
            przeciwnik = d1
        else
            przeciwnik = d1
        end

        -- ======================================
        -- LINK DO STRONY MECZU
        -- ======================================

        local meczTekst = przeciwnik

        if strona ~= "" then
            meczTekst = "[[" .. strona .. "|" .. przeciwnik .. "]]"
        end

        -- ======================================
        -- JUBILEUSZ
        -- ======================================

        local jubileusz = ""

        if i % 50 == 0 then
            jubileusz = "'''🏆 " .. i .. ". występ'''"
        end

        -- ======================================
        -- WIERSZ
        -- ======================================

        table.insert(wynik, "|-")
        table.insert(wynik, "| " .. i)
        table.insert(wynik, "| " .. data)
        table.insert(wynik, "| " .. meczTekst)
        table.insert(wynik, "| " .. wynikMeczu)
        table.insert(wynik, "| " .. minutyFormat)
        table.insert(wynik, "| " .. pkt)
        table.insert(wynik, "| " .. eval)
        table.insert(wynik, "| " .. plusminus)
        table.insert(wynik, "| " .. jubileusz)

    end

    table.insert(wynik, "|}")

    return table.concat(wynik, "\n")
end

return p