Przejdź do zawartości

Moduł:HistoriaSpotkanDruzyny

Z Encyklopedia Poznanskiego Sportu

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

local p = {}

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 qsum(v)

    local text = trim(v)

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

    if not a then
        return 0
    end

    return tonumber(a) + tonumber(b)
end


local function location(row, team)

    if trim(row.druzyna1) == team then
        return "dom"
    end

    if trim(row.druzyna2) == team then
        return "wyjazd"
    end

    return nil
end


-- =========================================================
-- PRZECIWNIK
--
-- Nazwa przeciwnika jest pobierana z:
-- nazwa_statystyki1 / nazwa_statystyki2
--
-- drużyna1 / drużyna2 służą tylko do ustalenia
-- czy nasza drużyna grała u siebie czy na wyjeździe.
-- =========================================================

local function opponent(row, team)

    local loc = location(
        row,
        team
    )

    if loc == "dom" then
        return trim(row.nazwa_statystyki2)
    end

    if loc == "wyjazd" then
        return trim(row.nazwa_statystyki1)
    end

    return ""
end


local function overtime(
    row,
    g1,
    g2
)

    local q1 = trim(row.kwarta1)
    local q2 = trim(row.kwarta2)
    local q3 = trim(row.kwarta3)
    local q4 = trim(row.kwarta4)

    if q1 == ""
        or q2 == ""
        or q3 == ""
        or q4 == "" then

        return 0
    end

    local regular_total =
        qsum(q1)
        + qsum(q2)
        + qsum(q3)
        + qsum(q4)

    local final_total =
        g1 + g2

    if regular_total < final_total then
        return 1
    end

    return 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


local function matchLink(row)

    local title =
        trim(row.strona_meczu)

    local date =
        trim(row.data)

    if title == "" then
        return date
    end

    if date == "" then
        return "[[" .. title .. "]]"
    end

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


local function recordText(record)

    if record == nil then
        return "—"
    end

    local place

    if record.location == "dom" then
        place = "dom"
    else
        place = "wyjazd"
    end

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


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


function p.main(frame)

    local team =
        trim(frame.args.druzyna)

    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


    local fields = table.concat(
        {
            "Mecze.id=mecz",
            "Mecze.data=data",
            "Mecze.sezon=sezon",

            "Mecze.druzyna1=druzyna1",
            "Mecze.druzyna2=druzyna2",

            -- KLUCZOWE POLA DLA PRZECIWNIKA
            "Mecze.nazwa_statystyki1=nazwa_statystyki1",
            "Mecze.nazwa_statystyki2=nazwa_statystyki2",

            "Mecze.gole1=gole1",
            "Mecze.gole2=gole2",
            "Mecze.wynik=wynik",
            "Mecze.strona_meczu=strona_meczu",
            "Mecze.kwarta1=kwarta1",
            "Mecze.kwarta2=kwarta2",
            "Mecze.kwarta3=kwarta3",
            "Mecze.kwarta4=kwarta4"
        },
        ","
    )


    local teamSQL =
        esc(team)


    local rows =
        mw.ext.cargo.query(
            "Mecze",
            fields,
            {
                where =
                    "(Mecze.druzyna1='"
                    .. teamSQL
                    .. "' OR Mecze.druzyna2='"
                    .. teamSQL
                    .. "')",

                orderBy =
                    "Mecze.data ASC",

                limit = 10000
            }
        )


    if not rows
        or #rows == 0 then

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


    local groups = {}


    local records = {

        win = {
            dom = nil,
            wyjazd = nil
        },

        loss = {
            dom = nil,
            wyjazd = nil
        }
    }


    for _, row in ipairs(rows) do

        -- =================================================
        -- POMIJANIE PAUZY
        --
        -- Pauza nie jest meczem i nie może wpływać
        -- na żadne statystyki.
        -- =================================================

        local wynikText =
            mw.ustring.lower(
                trim(row.wynik)
            )

        local isPauza =
            mw.ustring.find(
                wynikText,
                "pauza",
                1,
                true
            ) ~= nil


        if not isPauza then

            local g1 =
                num(row.gole1)

            local g2 =
                num(row.gole2)


            local loc =
                location(
                    row,
                    team
                )


            -- Przeciwnik z nazwa_statystyki1/2
            local opp =
                opponent(
                    row,
                    team
                )


            if loc and opp ~= "" then

                local item =
                    groups[opp]


                if not item then

                    item = {

                        nazwa = opp,

                        mecze = 0,

                        wygrane = 0,

                        porazki = 0,

                        dogrywki = 0,

                        forScore = 0,

                        againstScore = 0
                    }


                    groups[opp] =
                        item
                end


                local gf
                local ga


                if loc == "dom" then

                    gf = g1
                    ga = g2

                else

                    gf = g2
                    ga = g1

                end


                item.mecze =
                    item.mecze + 1


                item.forScore =
                    item.forScore + gf


                item.againstScore =
                    item.againstScore + ga


                item.dogrywki =
                    item.dogrywki
                    + overtime(
                        row,
                        g1,
                        g2
                    )


                local kind = nil


                if gf > ga then

                    item.wygrane =
                        item.wygrane + 1

                    kind = "win"


                elseif gf < ga then

                    item.porazki =
                        item.porazki + 1

                    kind = "loss"

                end


                if kind ~= nil then

                    local old =
                        records[kind][loc]


                    local current = {

                        row = row,

                        opponent = opp,

                        location = loc,

                        forScore = gf,

                        againstScore = ga,

                        diff = math.abs(
                            gf - ga
                        )
                    }


                    if betterRecord(
                        old,
                        current
                    ) then

                        records[kind][loc] =
                            current

                    end
                end
            end
        end
    end


    local list = {}


    for _, item in pairs(groups) do

        table.insert(
            list,
            item
        )
    end


    table.sort(
        list,
        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
    )


    local out = {}


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


    table.insert(
        out,
        ""
    )


    table.insert(
        out,
        "Historia spotkań drużyny "
        .. team
        .. " ze wszystkimi przeciwnikami wprowadzonymi do bazy."
    )


    table.insert(
        out,
        ""
    )


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


    table.insert(
        out,
        "! Przeciwnik !! Mecze ogółem !! Wygrane !! Porażki !! Dogrywki !! % wygranych !! Kosze rzucone !! Kosze stracone"
    )


    for _, item in ipairs(list) do

        table.insert(
            out,
            "|-"
        )


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


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


    table.insert(
        out,
        "|}"
    )


    table.insert(
        out,
        ""
    )


    table.insert(
        out,
        "== Największa wygrana i największa porażka =="
    )


    table.insert(
        out,
        ""
    )


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


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


    table.insert(
        out,
        "|-"
    )


    table.insert(
        out,
        "! 🏆 Największa wygrana"
    )


    table.insert(
        out,
        "| "
        .. recordText(
            records.win.dom
        )
        .. " || "
        .. recordText(
            records.win.wyjazd
        )
    )


    table.insert(
        out,
        "|-"
    )


    table.insert(
        out,
        "! 📉 Największa porażka"
    )


    table.insert(
        out,
        "| "
        .. recordText(
            records.loss.dom
        )
        .. " || "
        .. recordText(
            records.loss.wyjazd
        )
    )


    table.insert(
        out,
        "|}"
    )


    table.insert(
        out,
        ""
    )


    table.insert(
        out,
        "''Dogrywka = końcowy wynik jest większy niż suma czterech zapisanych kwart. Liczba dogrywek nie jest zgadywana.''"
    )


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

end


return p