Moduł:StatystykiHistoryczne
Wygląd
Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:StatystykiHistoryczne/opis
local p = {}
-- =========================================================
-- MODUŁ: STATYSTYKISTORYCZNE
--
-- Łączne statystyki zawodników drużyny koszykarskiej
-- ze wszystkich wprowadzonych sezonów.
--
-- Tabele:
-- 1. Mecze
-- 2. Punkty
-- 3. Minuty
-- 4. Asysty
-- 5. Zbiórki
-- 6. Bloki
--
-- Użycie:
--
-- {{#invoke:StatystykiHistoryczne|main
-- |druzyna=Enea Basket Poznań
-- }}
--
-- Moduł jest uniwersalny dla drużyn koszykarskich.
-- =========================================================
function p.main(frame)
-- =====================================================
-- DRUŻYNA
-- =====================================================
local druzyna =
mw.text.trim(
frame.args.druzyna or ""
)
if druzyna == "" then
return
"''Nie podano drużyny.''"
end
-- =====================================================
-- BEZPIECZNE PRZYGOTOWANIE NAZWY DRUŻYNY
-- =====================================================
local druzynaSQL =
mw.ustring.gsub(
druzyna,
"'",
"''"
)
-- =====================================================
-- POLA POBIERANE Z CARGO
-- =====================================================
local fields = table.concat({
"UdzialZawodnika.zawodnik=zawodnik",
"UdzialZawodnika.druzyna=druzyna",
"UdzialZawodnika.mecz=mecz",
"UdzialZawodnika.minuty=minuty",
"UdzialZawodnika.minuty_format=minuty_format",
"UdzialZawodnika.gol=gol",
"UdzialZawodnika.asysta=asysta",
"UdzialZawodnika.zbiorka=zbiorka",
"UdzialZawodnika.blok=blok"
}, ",")
-- =====================================================
-- ZAPYTANIE CARGO
-- =====================================================
local args = {
where =
"UdzialZawodnika.druzyna='"
.. druzynaSQL
.. "'",
limit = 5000
}
local rows =
mw.ext.cargo.query(
"UdzialZawodnika",
fields,
args
)
-- =====================================================
-- BRAK DANYCH
-- =====================================================
if not rows or #rows == 0 then
return
"''Brak statystyk dla drużyny "
.. druzyna
.. " w bazie.''"
end
-- =====================================================
-- STRUKTURA ZAWODNIKA
-- =====================================================
local zawodnicy = {}
-- =====================================================
-- NOWY REKORD ZAWODNIKA
-- =====================================================
local function nowyZawodnik()
return {
mecze = 0,
punkty = 0,
minuty = 0,
asysty = 0,
zbiorki = 0,
bloki = 0
}
end
-- =====================================================
-- BEZPIECZNE POBIERANIE LICZBY
-- =====================================================
local function liczba(wartosc)
return
tonumber(wartosc or 0)
or 0
end
-- =====================================================
-- ZLICZANIE STATYSTYK
-- =====================================================
for _, row in ipairs(rows) do
local zawodnik =
mw.text.trim(
row.zawodnik or ""
)
if zawodnik ~= "" then
if not zawodnicy[zawodnik] then
zawodnicy[zawodnik] =
nowyZawodnik()
end
local stat =
zawodnicy[zawodnik]
-- ---------------------------------------------
-- MECZE
-- ---------------------------------------------
stat.mecze =
stat.mecze + 1
-- ---------------------------------------------
-- PUNKTY
-- ---------------------------------------------
stat.punkty =
stat.punkty
+ liczba(row.gol)
-- ---------------------------------------------
-- MINUTY
-- ---------------------------------------------
stat.minuty =
stat.minuty
+ liczba(row.minuty)
-- ---------------------------------------------
-- ASYSTY
-- ---------------------------------------------
stat.asysty =
stat.asysty
+ liczba(row.asysta)
-- ---------------------------------------------
-- ZBIÓRKI
-- ---------------------------------------------
stat.zbiorki =
stat.zbiorki
+ liczba(row.zbiorka)
-- ---------------------------------------------
-- BLOKI
-- ---------------------------------------------
stat.bloki =
stat.bloki
+ liczba(row.blok)
end
end
-- =====================================================
-- LISTA ZAWODNIKÓW
-- =====================================================
local lista = {}
for zawodnik, stat in pairs(zawodnicy) do
table.insert(
lista,
{
zawodnik = zawodnik,
stat = stat
}
)
end
-- =====================================================
-- FORMATOWANIE MINUT
-- =====================================================
local function formatujMinuty(minuty)
minuty =
tonumber(minuty)
or 0
local godziny =
math.floor(
minuty / 60
)
local min =
minuty % 60
return string.format(
"%d:%02d",
godziny,
min
)
end
-- =====================================================
-- LINK DO STRONY ZAWODNIKA
-- =====================================================
local function linkZawodnika(nazwa)
return
"[["
.. nazwa
.. "|"
.. nazwa
.. "]]"
end
-- =====================================================
-- GENEROWANIE TABELI
-- =====================================================
local function generujTabele(
tytul,
pole,
formatWartosci
)
-- -------------------------------------------------
-- SORTOWANIE
-- -------------------------------------------------
table.sort(
lista,
function(a, b)
local wartoscA =
a.stat[pole]
or 0
local wartoscB =
b.stat[pole]
or 0
if wartoscA ~= wartoscB then
return
wartoscA > wartoscB
end
-- Przy remisie alfabetycznie
return
a.zawodnik
< b.zawodnik
end
)
-- -------------------------------------------------
-- TABELA
-- -------------------------------------------------
local wynik = {}
table.insert(
wynik,
'{| class="wikitable sortable" style="width:100%; text-align:center; margin:0;"'
)
table.insert(
wynik,
"! Miejsce"
)
table.insert(
wynik,
"! Zawodnik"
)
table.insert(
wynik,
"! "
.. tytul
)
-- -------------------------------------------------
-- WIERSZE
-- -------------------------------------------------
for i, rekord in ipairs(lista) do
local wartosc =
rekord.stat[pole]
or 0
if formatWartosci then
wartosc =
formatWartosci(
wartosc
)
end
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"| "
.. i
)
table.insert(
wynik,
"| "
.. linkZawodnika(
rekord.zawodnik
)
)
table.insert(
wynik,
"| '''"
.. wartosc
.. "'''"
)
end
table.insert(
wynik,
"|}"
)
return
table.concat(
wynik,
"\n"
)
end
-- =====================================================
-- WYNIK
-- =====================================================
local wynik = {}
table.insert(
wynik,
"=== Statystyki historyczne – "
.. druzyna
.. " ==="
)
table.insert(
wynik,
""
)
-- =====================================================
-- TABELA GŁÓWNA
--
-- 3 KOLUMNY × 2 RZĘDY
--
-- MECZE | PUNKTY | MINUTY
-- ASYSTY | ZBIÓRKI | BLOKI
-- =====================================================
table.insert(
wynik,
'{| style="width:100%; border-collapse:separate; border-spacing:10px; background:transparent;"'
)
-- =====================================================
-- PIERWSZY RZĄD
-- =====================================================
table.insert(
wynik,
"|- valign=\"top\""
)
-- =====================================================
-- MECZE
-- =====================================================
table.insert(
wynik,
"| style=\"width:33.33%; vertical-align:top;\" |\n"
.. "'''MECZE'''\n\n"
.. generujTabele(
"Mecze",
"mecze",
nil
)
)
-- =====================================================
-- PUNKTY
-- =====================================================
table.insert(
wynik,
"| style=\"width:33.33%; vertical-align:top;\" |\n"
.. "'''PUNKTY'''\n\n"
.. generujTabele(
"Punkty",
"punkty",
nil
)
)
-- =====================================================
-- MINUTY
-- =====================================================
table.insert(
wynik,
"| style=\"width:33.33%; vertical-align:top;\" |\n"
.. "'''MINUTY'''\n\n"
.. generujTabele(
"Minuty",
"minuty",
formatujMinuty
)
)
-- =====================================================
-- DRUGI RZĄD
-- =====================================================
table.insert(
wynik,
"|- valign=\"top\""
)
-- =====================================================
-- ASYSTY
-- =====================================================
table.insert(
wynik,
"| style=\"width:33.33%; vertical-align:top;\" |\n"
.. "'''ASYSTY'''\n\n"
.. generujTabele(
"Asysty",
"asysty",
nil
)
)
-- =====================================================
-- ZBIÓRKI
-- =====================================================
table.insert(
wynik,
"| style=\"width:33.33%; vertical-align:top;\" |\n"
.. "'''ZBIÓRKI'''\n\n"
.. generujTabele(
"Zbiórki",
"zbiorki",
nil
)
)
-- =====================================================
-- BLOKI
-- =====================================================
table.insert(
wynik,
"| style=\"width:33.33%; vertical-align:top;\" |\n"
.. "'''BLOKI'''\n\n"
.. generujTabele(
"Bloki",
"bloki",
nil
)
)
-- =====================================================
-- KONIEC TABELI GŁÓWNEJ
-- =====================================================
table.insert(
wynik,
"|}"
)
-- =====================================================
-- ZWRÓCENIE WYNIKU
-- =====================================================
return
table.concat(
wynik,
"\n"
)
end
return p