Moduł:StatystykiHistoryczne
Wygląd
Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:StatystykiHistoryczne/opis
local p = {}
-- =========================================================
-- MODUŁ: STATYSTYKIHISTORYCZNE
--
-- Łączne statystyki wszystkich zawodników Enea Basket Poznań
-- ze wszystkich sezonów znajdujących się w Cargo.
--
-- Rankingi:
-- MECZE
-- PUNKTY
-- MINUTY
-- ASYSTY
-- ZBIÓRKI
-- BLOKI
--
-- Dane:
-- UdzialZawodnika
--
-- Uwzględniani są WYŁĄCZNIE zawodnicy,
-- dla których:
--
-- druzyna = "Enea Basket Poznań"
--
-- =========================================================
-- =========================================================
-- USTAWIENIA
-- =========================================================
local NAZWA_DRUZYNY = "Enea Basket Poznań"
-- =========================================================
-- FUNKCJA BEZPIECZNEJ KONWERSJI NA LICZBĘ
-- =========================================================
local function liczba(value)
local n = tonumber(value)
if n then
return n
end
return 0
end
-- =========================================================
-- FORMATOWANIE MINUT
--
-- Cargo przechowuje minuty jako liczbę całkowitą.
--
-- Przykład:
--
-- 65 -> 1:05
-- 125 -> 2:05
-- =========================================================
local function formatujMinuty(minuty)
minuty = liczba(minuty)
local godziny =
math.floor(minuty / 60)
local min =
minuty % 60
return string.format(
"%d:%02d",
godziny,
min
)
end
-- =========================================================
-- POBIERANIE DANYCH
-- =========================================================
function p.main(frame)
-- =====================================================
-- POLA CARGO
-- =====================================================
local fields = table.concat({
"zawodnik=zawodnik",
"mecz=mecz",
"minuty=minuty",
"gol=gol",
"asysta=asysta",
"zbiorka=zbiorka",
"blok=blok"
}, ",")
-- =====================================================
-- ZAPYTANIE
--
-- TYLKO ENEA BASKET POZNAŃ
--
-- Nie ma tutaj filtra sezonu.
-- Dzięki temu pobierane są WSZYSTKIE
-- wprowadzone sezony.
-- =====================================================
local args = {
where =
"druzyna='"
.. NAZWA_DRUZYNY
.. "'",
limit = 50000
}
local rows =
mw.ext.cargo.query(
"UdzialZawodnika",
fields,
args
)
-- =====================================================
-- BRAK DANYCH
-- =====================================================
if not rows or #rows == 0 then
return
"''Brak danych statystycznych "
.. "dla Enea Basket Poznań w bazie.''"
end
-- =====================================================
-- TABLICA ZAWODNIKÓW
-- =====================================================
local zawodnicy = {}
-- =====================================================
-- GRUPOWANIE DANYCH
-- =====================================================
for _, row in ipairs(rows) do
local zawodnik =
mw.text.trim(
row.zawodnik or ""
)
if zawodnik ~= "" then
-- ---------------------------------------------
-- UTWORZENIE REKORDU ZAWODNIKA
-- ---------------------------------------------
if not zawodnicy[zawodnik] then
zawodnicy[zawodnik] = {
mecze = 0,
punkty = 0,
minuty = 0,
asysty = 0,
zbiorki = 0,
bloki = 0
}
end
local stat =
zawodnicy[zawodnik]
-- ---------------------------------------------
-- MECZ
--
-- Każdy rekord UdzialZawodnika odpowiada
-- jednemu występowi w meczu.
-- ---------------------------------------------
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 listaZawodnikow = {}
for zawodnik, stat in pairs(zawodnicy) do
table.insert(
listaZawodnikow,
{
zawodnik = zawodnik,
stat = stat
}
)
end
-- =====================================================
-- FUNKCJA TWORZĄCA RANKING
-- =====================================================
--
-- sortowane malejąco według wybranej statystyki.
--
-- Przy takiej samej wartości:
-- alfabetycznie według nazwiska.
--
-- Miejsce jest liczone jako ranking konkursowy:
--
-- 1.
-- 2.
-- 2.
-- 4.
--
-- =====================================================
local function ranking(pole)
local lista = {}
for _, rekord in ipairs(listaZawodnikow) do
table.insert(
lista,
{
zawodnik = rekord.zawodnik,
wartosc = rekord.stat[pole]
}
)
end
table.sort(
lista,
function(a, b)
if a.wartosc ~= b.wartosc then
return a.wartosc > b.wartosc
end
return
a.zawodnik < b.zawodnik
end
)
return lista
end
-- =====================================================
-- GENEROWANIE POJEDYNCZEJ TABELI
-- =====================================================
local function generujTabele(
tytul,
pole,
formatWartosci,
ikona
)
local lista =
ranking(pole)
local wynik = {}
-- -----------------------------------------------
-- KONTENER
-- -----------------------------------------------
table.insert(
wynik,
'<div style="'
.. 'border:1px solid #c8ccd1;'
.. 'border-radius:6px;'
.. 'padding:8px;'
.. 'margin-bottom:10px;'
.. 'background:#fff;'
.. '">'
)
-- -----------------------------------------------
-- TYTUŁ
-- -----------------------------------------------
table.insert(
wynik,
'<div style="'
.. 'font-weight:bold;'
.. 'font-size:110%;'
.. 'text-align:center;'
.. 'padding:5px;'
.. 'margin-bottom:5px;'
.. 'border-bottom:2px solid #eaecf0;'
.. '">'
.. (ikona or "")
.. ' '
.. tytul
.. '</div>'
)
-- -----------------------------------------------
-- TABELA
-- -----------------------------------------------
table.insert(
wynik,
'{| class="wikitable sortable" '
.. 'style="width:100%; margin:0; text-align:center;"'
)
table.insert(
wynik,
"! Miejsce"
)
table.insert(
wynik,
"! Zawodnik"
)
table.insert(
wynik,
"! " .. tytul
)
-- -----------------------------------------------
-- WIERSZE
-- -----------------------------------------------
local poprzedniaWartosc = nil
local miejsce = 0
local licznik = 0
for _, rekord in ipairs(lista) do
licznik =
licznik + 1
local wartosc =
rekord.wartosc
-- -------------------------------------------
-- RANKING KONKURSOWY
--
-- 1, 2, 2, 4
-- -------------------------------------------
if poprzedniaWartosc == nil then
miejsce = 1
elseif wartosc ~= poprzedniaWartosc then
miejsce = licznik
end
poprzedniaWartosc =
wartosc
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"| " .. miejsce
)
table.insert(
wynik,
"| '''"
.. rekord.zawodnik
.. "'''"
)
local wartoscTekst
if formatWartosci then
wartoscTekst =
formatWartosci(
wartosc
)
else
wartoscTekst =
tostring(wartosc)
end
table.insert(
wynik,
"| '''"
.. wartoscTekst
.. "'''"
)
end
table.insert(
wynik,
"|}"
)
table.insert(
wynik,
"</div>"
)
return table.concat(
wynik,
"\n"
)
end
-- =====================================================
-- POSZCZEGÓLNE RANKINGI
-- =====================================================
local tabelaMecze =
generujTabele(
"Mecze",
"mecze",
nil,
"🏀"
)
local tabelaPunkty =
generujTabele(
"Punkty",
"punkty",
nil,
"🏀"
)
local tabelaMinuty =
generujTabele(
"Minuty",
"minuty",
formatujMinuty,
"⏱"
)
local tabelaAsysty =
generujTabele(
"Asysty",
"asysty",
nil,
"🎯"
)
local tabelaZbiorki =
generujTabele(
"Zbiórki",
"zbiorki",
nil,
"🏀"
)
local tabelaBloki =
generujTabele(
"Bloki",
"bloki",
nil,
"🛡"
)
-- =====================================================
-- WYNIK KOŃCOWY
-- =====================================================
local wynik = {}
-- =====================================================
-- NAGŁÓWEK
-- =====================================================
table.insert(
wynik,
"== Statystyki historyczne Enea Basket Poznań =="
)
table.insert(
wynik,
""
)
table.insert(
wynik,
"''Łączne statystyki zawodników Enea Basket Poznań "
.. "ze wszystkich wprowadzonych sezonów.''"
)
table.insert(
wynik,
""
)
-- =====================================================
-- UKŁAD 3 × 2
-- =====================================================
table.insert(
wynik,
'<div style="'
.. 'display:grid;'
.. 'grid-template-columns:repeat(3, minmax(0, 1fr));'
.. 'gap:12px;'
.. 'width:100%;'
.. '">'
)
-- -----------------------------------------------------
-- 1. MECZE
-- -----------------------------------------------------
table.insert(
wynik,
tabelaMecze
)
-- -----------------------------------------------------
-- 2. PUNKTY
-- -----------------------------------------------------
table.insert(
wynik,
tabelaPunkty
)
-- -----------------------------------------------------
-- 3. MINUTY
-- -----------------------------------------------------
table.insert(
wynik,
tabelaMinuty
)
-- -----------------------------------------------------
-- 4. ASYSTY
-- -----------------------------------------------------
table.insert(
wynik,
tabelaAsysty
)
-- -----------------------------------------------------
-- 5. ZBIÓRKI
-- -----------------------------------------------------
table.insert(
wynik,
tabelaZbiorki
)
-- -----------------------------------------------------
-- 6. BLOKI
-- -----------------------------------------------------
table.insert(
wynik,
tabelaBloki
)
table.insert(
wynik,
"</div>"
)
-- =====================================================
-- KONIEC
-- =====================================================
return table.concat(
wynik,
"\n"
)
end
return p