Przejdź do zawartości

Moduł:HistoriaHal

Z Encyklopedia Poznanskiego Sportu

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

local p = {}

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

local function trim(v)
    if v == nil then
        return ""
    end

    return mw.text.trim(tostring(v))
end


local function esc(v)
    return mw.ustring.gsub(
        trim(v),
        "'",
        "''"
    )
end


local function num(v)
    if v == nil or trim(v) == "" then
        return 0
    end

    local text = mw.ustring.gsub(
        trim(v),
        ",",
        "."
    )

    return tonumber(text) or 0
end


local function pct(wins, games)

    if games == 0 then
        return "0,0%"
    end

    local value =
        wins / games * 100

    local text =
        string.format(
            "%.1f",
            value
        )

    text =
        mw.ustring.gsub(
            text,
            "%.",
            ","
        )

    return text .. "%"
end


-- =========================================================
-- SPRAWDZENIE PAUZY
-- =========================================================

local function isPause(row)

    local d1 =
        mw.ustring.lower(
            trim(row.druzyna1)
        )

    local d2 =
        mw.ustring.lower(
            trim(row.druzyna2)
        )

    local n1 =
        mw.ustring.lower(
            trim(row.nazwa_statystyki1)
        )

    local n2 =
        mw.ustring.lower(
            trim(row.nazwa_statystyki2)
        )


    if d1 == "pauza"
        or d2 == "pauza"
        or n1 == "pauza"
        or n2 == "pauza" then

        return true
    end


    return false
end


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

local function opponentLink(row, opponent)

    local title =
        trim(row.strona_meczu)


    if title == "" then
        return opponent
    end


    return (
        "[["
        .. title
        .. "|"
        .. opponent
        .. "]]"
    )
end


-- =========================================================
-- DATA / LINK DO MECZU
-- =========================================================

local function matchDateLink(row)

    local title =
        trim(row.strona_meczu)

    local date =
        trim(row.data)


    if date == "" then
        return ""
    end


    if title == "" then
        return date
    end


    return (
        "[["
        .. title
        .. "|"
        .. date
        .. "]]"
    )
end


-- =========================================================
-- REKORD NAJWIĘKSZEJ WYGRANEJ / PORAŻKI
-- =========================================================

local function recordText(record)

    if record == nil then
        return "—"
    end


    local opponent =
        opponentLink(
            record.row,
            record.opponent
        )


    return (
        matchDateLink(record.row)
        .. " – "
        .. opponent
        .. " "
        .. tostring(record.forScore)
        .. ":"
        .. tostring(record.againstScore)
        .. " (+"
        .. tostring(record.diff)
        .. ")"
    )
end


-- =========================================================
-- PORÓWNANIE REKORDÓW
-- =========================================================

local function betterRecord(
    oldRecord,
    newRecord
)

    if oldRecord == nil then
        return true
    end


    if newRecord.diff >
        oldRecord.diff then

        return true
    end


    if newRecord.diff <
        oldRecord.diff then

        return false
    end


    return trim(newRecord.row.data)
        < trim(oldRecord.row.data)
end


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

function p.main(frame)

    local team =
        trim(frame.args.druzyna)


    -- -----------------------------------------------------
    -- NAZWA DRUŻYNY
    -- -----------------------------------------------------

    if team == "" then

        local title =
            mw.title.getCurrentTitle()


        if title ~= nil then

            team =
                trim(
                    title.prefixedText
                )

        end
    end


    if team == "" then

        return
            "''Nie podano drużyny.''"

    end


    -- =====================================================
    -- POLA CARGO
    -- =====================================================

    local fields = table.concat(
        {

            "Mecze.id=mecz",

            "Mecze.data=data",

            "Mecze.sezon=sezon",

            "Mecze.druzyna1=druzyna1",

            "Mecze.druzyna2=druzyna2",

            "Mecze.nazwa_statystyki1=nazwa_statystyki1",

            "Mecze.nazwa_statystyki2=nazwa_statystyki2",

            "Mecze.gole1=gole1",

            "Mecze.gole2=gole2",

            "Mecze.wynik=wynik",

            "Mecze.hala=hala",

            "Mecze.strona_meczu=strona_meczu"

        },
        ","
    )


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

    local teamSQL =
        esc(team)


    local rows =
        mw.ext.cargo.query(
            "Mecze",
            fields,
            {

                where =
                    "Mecze.druzyna1='"
                    .. teamSQL
                    .. "'",

                orderBy =
                    "Mecze.data ASC",

                limit = 10000

            }
        )


    if not rows
        or #rows == 0 then

        return (
            "''Brak wprowadzonych spotkań domowych dla drużyny: "
            .. team
            .. ".''"
        )

    end


    -- =====================================================
    -- GRUPY HAL
    -- =====================================================

    local halls = {}


    -- =====================================================
    -- GRUPY SEZONÓW
    -- =====================================================

    local seasons = {}


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

    for _, row in ipairs(rows) do

        -- -------------------------------------------------
        -- PAUZY NIE SĄ BRANE POD UWAGĘ
        -- -------------------------------------------------

        if not isPause(row) then


            -- =============================================
            -- SEZON
            -- =============================================

            local season =
                trim(row.sezon)


            if season == "" then

                season =
                    "Brak podanego sezonu"

            end


            local seasonItem =
                seasons[season]


            if not seasonItem then

                seasonItem = {

                    nazwa = season,

                    mecze = 0,

                    wygrane = 0,

                    porazki = 0,

                    remisy = 0,

                    forScore = 0,

                    againstScore = 0

                }


                seasons[season] =
                    seasonItem

            end


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

            local gf =
                num(row.gole1)


            local ga =
                num(row.gole2)


            -- =============================================
            -- STATYSTYKI SEZONU
            -- =============================================

            seasonItem.mecze =
                seasonItem.mecze + 1


            seasonItem.forScore =
                seasonItem.forScore + gf


            seasonItem.againstScore =
                seasonItem.againstScore + ga


            if gf > ga then

                seasonItem.wygrane =
                    seasonItem.wygrane + 1


            elseif gf < ga then

                seasonItem.porazki =
                    seasonItem.porazki + 1


            else

                seasonItem.remisy =
                    seasonItem.remisy + 1

            end


            -- =============================================
            -- HALA
            -- =============================================

            local hall =
                trim(row.hala)


            if hall == "" then

                hall =
                    "Brak podanej hali"

            end


            local opponent =
                trim(
                    row.nazwa_statystyki2
                )


            if opponent == "" then

                opponent =
                    trim(row.druzyna2)

            end


            local item =
                halls[hall]


            if not item then

                item = {

                    nazwa = hall,

                    mecze = 0,

                    wygrane = 0,

                    porazki = 0,

                    remisy = 0,

                    forScore = 0,

                    againstScore = 0,

                    spotkania = {},

                    winRecord = nil,

                    lossRecord = nil

                }


                halls[hall] =
                    item

            end


            -- =============================================
            -- NUMER MECZU NA HALI
            -- =============================================

            item.mecze =
                item.mecze + 1


            local nrMeczu =
                item.mecze


            -- =============================================
            -- STATYSTYKI HALI
            -- =============================================

            item.forScore =
                item.forScore + gf


            item.againstScore =
                item.againstScore + ga


            if gf > ga then

                item.wygrane =
                    item.wygrane + 1


            elseif gf < ga then

                item.porazki =
                    item.porazki + 1


            else

                item.remisy =
                    item.remisy + 1

            end


            -- =============================================
            -- NAJWIĘKSZA WYGRANA
            -- =============================================

            if gf > ga then

                local current = {

                    row = row,

                    opponent = opponent,

                    forScore = gf,

                    againstScore = ga,

                    diff = gf - ga

                }


                if betterRecord(
                    item.winRecord,
                    current
                ) then

                    item.winRecord =
                        current

                end

            end


            -- =============================================
            -- NAJWIĘKSZA PORAŻKA
            -- =============================================

            if gf < ga then

                local current = {

                    row = row,

                    opponent = opponent,

                    forScore = gf,

                    againstScore = ga,

                    diff = ga - gf

                }


                if betterRecord(
                    item.lossRecord,
                    current
                ) then

                    item.lossRecord =
                        current

                end

            end


            -- =============================================
            -- LISTA SPOTKAŃ
            -- =============================================

            table.insert(
                item.spotkania,
                {

                    row = row,

                    nr = nrMeczu,

                    opponent = opponent,

                    gf = gf,

                    ga = ga

                }
            )

        end

    end


    -- =====================================================
    -- LISTA HAL
    -- =====================================================

    local hallList = {}


    for _, item in pairs(halls) do

        table.insert(
            hallList,
            item
        )

    end


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

            if a.mecze ~= b.mecze then

                return a.mecze >
                    b.mecze

            end


            return mw.ustring.lower(
                a.nazwa
            )
            <
            mw.ustring.lower(
                b.nazwa
            )

        end
    )


    -- =====================================================
    -- LISTA SEZONÓW
    -- =====================================================

    local seasonList = {}


    for _, item in pairs(seasons) do

        table.insert(
            seasonList,
            item
        )

    end


    -- =====================================================
    -- SORTOWANIE SEZONÓW
    --
    -- Najnowszy sezon pierwszy.
    -- =====================================================

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

            return a.nazwa >
                b.nazwa

        end
    )


    -- =====================================================
    -- WYJŚCIE
    -- =====================================================

    local out = {}


    table.insert(
        out,
        "== Historia meczów domowych =="
    )


    table.insert(
        out,
        ""
    )


    table.insert(
        out,
        "Historia domowych spotkań drużyny "
        .. team
        .. "."
    )


    table.insert(
        out,
        ""
    )


    -- =====================================================
    -- STATYSTYKI WEDŁUG SEZONÓW
    -- =====================================================

    table.insert(
        out,
        "=== Statystyki według sezonów ==="
    )


    table.insert(
        out,
        ""
    )


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


    table.insert(
        out,
        "! Sezon !! Mecze !! Wygrane !! Porażki !! % wygranych !! Kosze rzucone !! Kosze stracone !! Różnica"
    )


    for _, item in ipairs(
        seasonList
    ) do

        local difference =
            item.forScore
            - item.againstScore


        table.insert(
            out,
            "|-"
        )


        table.insert(
            out,
            "| "
            .. item.nazwa
            .. " || "
            .. tostring(item.mecze)
            .. " || "
            .. tostring(item.wygrane)
            .. " || "
            .. tostring(item.porazki)
            .. " || "
            .. pct(
                item.wygrane,
                item.mecze
            )
            .. " || "
            .. tostring(item.forScore)
            .. " || "
            .. tostring(item.againstScore)
            .. " || "
            .. tostring(difference)
        )

    end


    table.insert(
        out,
        "|}"
    )


    table.insert(
        out,
        ""
    )


    -- =====================================================
    -- PODSUMOWANIE HAL
    -- =====================================================

    table.insert(
        out,
        "=== Statystyki według hali ==="
    )


    table.insert(
        out,
        ""
    )


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


    table.insert(
        out,
        "! Hala !! Mecze !! Wygrane !! Porażki !! % wygranych !! Kosze rzucone !! Kosze stracone !! Różnica !! Największa wygrana !! Największa porażka"
    )


    for _, item in ipairs(
        hallList
    ) do

        local difference =
            item.forScore
            - item.againstScore


        table.insert(
            out,
            "|-"
        )


        table.insert(
            out,
            '| style="text-align:left;" | '
            .. item.nazwa
        )


        table.insert(
            out,
            "| "
            .. tostring(item.mecze)
            .. " || "
            .. tostring(item.wygrane)
            .. " || "
            .. tostring(item.porazki)
            .. " || "
            .. pct(
                item.wygrane,
                item.mecze
            )
            .. " || "
            .. tostring(item.forScore)
            .. " || "
            .. tostring(item.againstScore)
            .. " || "
            .. tostring(difference)
            .. " || "
            .. recordText(
                item.winRecord
            )
            .. " || "
            .. recordText(
                item.lossRecord
            )

        )

    end


    table.insert(
        out,
        "|}"
    )


    table.insert(
        out,
        ""
    )


    -- =====================================================
    -- WSZYSTKIE SPOTKANIA
    -- =====================================================

    table.insert(
        out,
        "=== Wszystkie spotkania domowe ==="
    )


    table.insert(
        out,
        ""
    )


    for _, item in ipairs(
        hallList
    ) do


        table.insert(
            out,
            "==== "
            .. item.nazwa
            .. " ===="
        )


        table.insert(
            out,
            ""
        )


        table.insert(
            out,
            "Liczba rozegranych spotkań: '''"
            .. tostring(item.mecze)
            .. "'''."
        )


        table.insert(
            out,
            ""
        )


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


        table.insert(
            out,
            "! Nr !! Data !! Przeciwnik !! Wynik !! Jubileusz"
        )


        for _, match in ipairs(
            item.spotkania
        ) do

            local row =
                match.row


            local nr =
                match.nr


            local opponent =
                opponentLink(
                    row,
                    match.opponent
                )


            local jubileusz = ""


            if nr % 25 == 0 then

                jubileusz =
                    "'''🏆 "
                    .. tostring(nr)
                    .. ". MECZ NA TEJ HALI'''"

            end


            table.insert(
                out,
                "|-"
            )


            if jubileusz ~= "" then

                table.insert(
                    out,
                    "| '''"
                    .. tostring(nr)
                    .. "''' || '''"
                    .. trim(row.data)
                    .. "''' || '''"
                    .. opponent
                    .. "''' || '''"
                    .. tostring(match.gf)
                    .. ":"
                    .. tostring(match.ga)
                    .. "''' || "
                    .. jubileusz
                )


            else

                table.insert(
                    out,
                    "| "
                    .. tostring(nr)
                    .. " || "
                    .. trim(row.data)
                    .. " || "
                    .. opponent
                    .. " || "
                    .. tostring(match.gf)
                    .. ":"
                    .. tostring(match.ga)
                    .. " || "
                )

            end

        end


        table.insert(
            out,
            "|}"
        )


        table.insert(
            out,
            ""
        )

    end


    -- =====================================================
    -- ZWROT
    -- =====================================================

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

end


return p