Moduł:HistoriaSztabu
Wygląd
Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:HistoriaSztabu/opis
local p = {}
-- =========================================================
-- Moduł: HistoriaSztabu
--
-- Historia kariery członka sztabu:
-- - Trener
-- - II trener
-- - Asystent
--
-- Dla każdej roli:
-- - wszystkie mecze
-- - zwycięstwa
-- - porażki
-- - remisy
-- - % wygranych
-- - dom / wyjazd
-- - podsumowanie sezonów
-- - podsumowanie klubów
-- - numerowana lista spotkań
-- - jubileusze 50 / 100 / 150 / ...
--
-- Dane:
-- SztabMeczu
-- Mecze
-- =========================================================
function p.main(frame)
-- =====================================================
-- ZAWODNIK / OSOBA
-- =====================================================
local osoba = frame.args.osoba
if not osoba or osoba == "" then
osoba = mw.title.getCurrentTitle().text
end
local osobaSQL =
mw.ustring.gsub(
osoba,
"'",
"''"
)
-- =====================================================
-- POBRANIE DANYCH
-- =====================================================
local fields = table.concat({
"SztabMeczu.mecz=mecz",
"SztabMeczu.druzyna=druzyna",
"SztabMeczu.rola=rola",
"Mecze.data=data",
"Mecze.sezon=sezon",
"Mecze.druzyna1=druzyna1",
"Mecze.druzyna2=druzyna2",
"Mecze.gole1=gole1",
"Mecze.gole2=gole2",
"Mecze.wynik=wynik",
"Mecze.strona_meczu=strona_meczu"
}, ",")
local args = {
join =
"SztabMeczu.mecz=Mecze.id",
where =
"SztabMeczu.osoba='" ..
osobaSQL ..
"'",
orderBy =
"Mecze.data ASC",
limit = 10000
}
local rows =
mw.ext.cargo.query(
"SztabMeczu,Mecze",
fields,
args
)
-- =====================================================
-- BRAK DANYCH
-- =====================================================
if not rows or #rows == 0 then
return
"''Brak wprowadzonych spotkań sztabowych w bazie.''"
end
-- =====================================================
-- FUNKCJE POMOCNICZE
-- =====================================================
local function liczba(value)
if value == nil or value == "" then
return 0
end
value = tostring(value)
value =
mw.ustring.gsub(
value,
"%s+",
""
)
return tonumber(value) or 0
end
-- =====================================================
-- NOWE PODSUMOWANIE
-- =====================================================
local function nowePodsumowanie()
return {
mecze = 0,
zwyciestwa = 0,
porazki = 0,
remisy = 0
}
end
-- =====================================================
-- OKREŚLENIE MIEJSCA
-- =====================================================
local function okreslMiejsce(row)
local druzyna =
row.druzyna or ""
local d1 =
row.druzyna1 or ""
local d2 =
row.druzyna2 or ""
if druzyna == d1 then
return "dom"
end
if druzyna == d2 then
return "wyjazd"
end
return nil
end
-- =====================================================
-- OKREŚLENIE WYNIKU
-- =====================================================
local function okreslWynik(row)
local d1 =
row.druzyna1 or ""
local d2 =
row.druzyna2 or ""
local druzyna =
row.druzyna or ""
local g1 =
tonumber(row.gole1 or "")
local g2 =
tonumber(row.gole2 or "")
if not g1 or not g2 then
local wynik =
row.wynik or ""
local a, b =
wynik:match(
"(%d+)%s*:%s*(%d+)"
)
if not a or not b then
return nil
end
g1 =
tonumber(a)
g2 =
tonumber(b)
end
if druzyna == d1 then
if g1 > g2 then
return "W"
elseif g1 < g2 then
return "P"
else
return "R"
end
elseif druzyna == d2 then
if g2 > g1 then
return "W"
elseif g2 < g1 then
return "P"
else
return "R"
end
end
return nil
end
-- =====================================================
-- PROCENT WYGRANYCH
-- =====================================================
local function procentWygranych(stat)
if not stat or stat.mecze == 0 then
return "0,0%"
end
local procent =
stat.zwyciestwa /
stat.mecze *
100
return string.format(
"%.1f%%",
procent
):gsub(
"%.",
","
)
end
-- =====================================================
-- NAZWA MECZU
--
-- WAŻNE:
-- gospodarze zawsze są pierwsi.
-- =====================================================
local function nazwaMeczu(row)
local d1 =
row.druzyna1 or ""
local d2 =
row.druzyna2 or ""
local strona =
row.strona_meczu or ""
local tekst =
d1 ..
" – " ..
d2
if strona ~= "" then
return
"[[" ..
strona ..
"|" ..
tekst ..
"]]"
end
return tekst
end
-- =====================================================
-- GRUPY RÓL
-- =====================================================
local role = {}
for _, row in ipairs(rows) do
local rola =
row.rola or ""
if rola == "" then
rola = "Brak określonej roli"
end
if not role[rola] then
role[rola] = {
mecze = {},
suma = nowePodsumowanie(),
dom = nowePodsumowanie(),
wyjazd = nowePodsumowanie(),
sezony = {},
kluby = {}
}
end
table.insert(
role[rola].mecze,
row
)
end
-- =====================================================
-- KOLEJNOŚĆ RÓL
-- =====================================================
local kolejnoscRol = {
"Trener",
"II trener",
"Asystent"
}
-- dodajemy również ewentualne inne role
for rola, _ in pairs(role) do
local znaleziono = false
for _, r in ipairs(kolejnoscRol) do
if r == rola then
znaleziono = true
break
end
end
if not znaleziono then
table.insert(
kolejnoscRol,
rola
)
end
end
-- =====================================================
-- PRZETWARZANIE POSZCZEGÓLNYCH RÓL
-- =====================================================
for _, rola in ipairs(kolejnoscRol) do
local dane =
role[rola]
if dane then
for _, row in ipairs(dane.mecze) do
local wynik =
okreslWynik(row)
local miejsce =
okreslMiejsce(row)
-- =========================================
-- OGÓŁEM
-- =========================================
dane.suma.mecze =
dane.suma.mecze + 1
if wynik == "W" then
dane.suma.zwyciestwa =
dane.suma.zwyciestwa + 1
elseif wynik == "P" then
dane.suma.porazki =
dane.suma.porazki + 1
elseif wynik == "R" then
dane.suma.remisy =
dane.suma.remisy + 1
end
-- =========================================
-- DOM / WYJAZD
-- =========================================
if miejsce == "dom" then
dane.dom.mecze =
dane.dom.mecze + 1
if wynik == "W" then
dane.dom.zwyciestwa =
dane.dom.zwyciestwa + 1
elseif wynik == "P" then
dane.dom.porazki =
dane.dom.porazki + 1
elseif wynik == "R" then
dane.dom.remisy =
dane.dom.remisy + 1
end
elseif miejsce == "wyjazd" then
dane.wyjazd.mecze =
dane.wyjazd.mecze + 1
if wynik == "W" then
dane.wyjazd.zwyciestwa =
dane.wyjazd.zwyciestwa + 1
elseif wynik == "P" then
dane.wyjazd.porazki =
dane.wyjazd.porazki + 1
elseif wynik == "R" then
dane.wyjazd.remisy =
dane.wyjazd.remisy + 1
end
end
-- =========================================
-- SEZON
-- =========================================
local sezon =
row.sezon or ""
if sezon == "" then
sezon = "Brak sezonu"
end
if not dane.sezony[sezon] then
dane.sezony[sezon] =
nowePodsumowanie()
end
local sezonStat =
dane.sezony[sezon]
sezonStat.mecze =
sezonStat.mecze + 1
if wynik == "W" then
sezonStat.zwyciestwa =
sezonStat.zwyciestwa + 1
elseif wynik == "P" then
sezonStat.porazki =
sezonStat.porazki + 1
elseif wynik == "R" then
sezonStat.remisy =
sezonStat.remisy + 1
end
-- =========================================
-- KLUB
-- =========================================
local klub =
row.druzyna or ""
if klub == "" then
klub = "Brak klubu"
end
if not dane.kluby[klub] then
dane.kluby[klub] =
nowePodsumowanie()
end
local klubStat =
dane.kluby[klub]
klubStat.mecze =
klubStat.mecze + 1
if wynik == "W" then
klubStat.zwyciestwa =
klubStat.zwyciestwa + 1
elseif wynik == "P" then
klubStat.porazki =
klubStat.porazki + 1
elseif wynik == "R" then
klubStat.remisy =
klubStat.remisy + 1
end
end
end
end
-- =====================================================
-- SORTOWANIE SEZONÓW
-- =====================================================
local function listaSezonow(sezony)
local lista = {}
for sezon, _ in pairs(sezony) do
table.insert(
lista,
sezon
)
end
table.sort(
lista,
function(a, b)
return a > b
end
)
return lista
end
-- =====================================================
-- SORTOWANIE KLUBÓW
-- =====================================================
local function listaKlubow(kluby)
local lista = {}
for klub, _ in pairs(kluby) do
table.insert(
lista,
klub
)
end
table.sort(
lista,
function(a, b)
return a < b
end
)
return lista
end
-- =====================================================
-- GENEROWANIE WYNIKU
-- =====================================================
local wynik = {}
table.insert(
wynik,
"== Historia kariery sztabowej =="
)
table.insert(
wynik,
""
)
-- =====================================================
-- POSZCZEGÓLNE ROLE
-- =====================================================
for _, rola in ipairs(kolejnoscRol) do
local dane =
role[rola]
if dane then
-- =============================================
-- NAGŁÓWEK ROLI
-- =============================================
table.insert(
wynik,
"=== " .. rola .. " ==="
)
table.insert(
wynik,
""
)
-- =============================================
-- PODSUMOWANIE
-- =============================================
table.insert(
wynik,
"==== Podsumowanie ===="
)
table.insert(
wynik,
""
)
table.insert(
wynik,
'{| class="wikitable" style="width:100%; text-align:center;"'
)
table.insert(
wynik,
"! !! Wszystkie mecze !! 🏠 Dom !! ✈️ Wyjazd"
)
-- MECZE
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"! Mecze"
)
table.insert(
wynik,
"| '''" ..
dane.suma.mecze ..
"'''"
)
table.insert(
wynik,
"| '''" ..
dane.dom.mecze ..
"'''"
)
table.insert(
wynik,
"| '''" ..
dane.wyjazd.mecze ..
"'''"
)
-- ZWYCIĘSTWA
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"! Zwycięstwa"
)
table.insert(
wynik,
"| '''" ..
dane.suma.zwyciestwa ..
"'''"
)
table.insert(
wynik,
"| '''" ..
dane.dom.zwyciestwa ..
"'''"
)
table.insert(
wynik,
"| '''" ..
dane.wyjazd.zwyciestwa ..
"'''"
)
-- PORAŻKI
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"! Porażki"
)
table.insert(
wynik,
"| '''" ..
dane.suma.porazki ..
"'''"
)
table.insert(
wynik,
"| '''" ..
dane.dom.porazki ..
"'''"
)
table.insert(
wynik,
"| '''" ..
dane.wyjazd.porazki ..
"'''"
)
-- REMISY
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"! Remisy"
)
table.insert(
wynik,
"| '''" ..
dane.suma.remisy ..
"'''"
)
table.insert(
wynik,
"| '''" ..
dane.dom.remisy ..
"'''"
)
table.insert(
wynik,
"| '''" ..
dane.wyjazd.remisy ..
"'''"
)
-- % WYGRANYCH
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"! % wygranych"
)
table.insert(
wynik,
"| '''" ..
procentWygranych(dane.suma) ..
"'''"
)
table.insert(
wynik,
"| '''" ..
procentWygranych(dane.dom) ..
"'''"
)
table.insert(
wynik,
"| '''" ..
procentWygranych(dane.wyjazd) ..
"'''"
)
table.insert(
wynik,
"|}"
)
table.insert(
wynik,
""
)
-- =============================================
-- PODSUMOWANIE SEZONÓW
-- =============================================
table.insert(
wynik,
"==== Podsumowanie sezonów ===="
)
table.insert(
wynik,
""
)
table.insert(
wynik,
'{| class="wikitable sortable" style="width:100%; text-align:center;"'
)
table.insert(
wynik,
"! Sezon !! Mecze !! W !! P !! R !! % wygranych"
)
for _, sezon in ipairs(
listaSezonow(dane.sezony)
) do
local stat =
dane.sezony[sezon]
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"| '''" ..
sezon ..
"'''"
)
table.insert(
wynik,
"| " ..
stat.mecze
)
table.insert(
wynik,
"| " ..
stat.zwyciestwa
)
table.insert(
wynik,
"| " ..
stat.porazki
)
table.insert(
wynik,
"| " ..
stat.remisy
)
table.insert(
wynik,
"| '''" ..
procentWygranych(stat) ..
"'''"
)
end
table.insert(
wynik,
"|}"
)
table.insert(
wynik,
""
)
-- =============================================
-- PODSUMOWANIE KLUBÓW
-- =============================================
table.insert(
wynik,
"==== Podsumowanie według klubów ===="
)
table.insert(
wynik,
""
)
table.insert(
wynik,
'{| class="wikitable sortable" style="width:100%; text-align:center;"'
)
table.insert(
wynik,
"! Klub !! Mecze !! W !! P !! R !! % wygranych"
)
for _, klub in ipairs(
listaKlubow(dane.kluby)
) do
local stat =
dane.kluby[klub]
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"| '''" ..
klub ..
"'''"
)
table.insert(
wynik,
"| " ..
stat.mecze
)
table.insert(
wynik,
"| " ..
stat.zwyciestwa
)
table.insert(
wynik,
"| " ..
stat.porazki
)
table.insert(
wynik,
"| " ..
stat.remisy
)
table.insert(
wynik,
"| '''" ..
procentWygranych(stat) ..
"'''"
)
end
table.insert(
wynik,
"|}"
)
table.insert(
wynik,
""
)
-- =============================================
-- LISTA MECZÓW
-- =============================================
table.insert(
wynik,
"==== Wszystkie spotkania ===="
)
table.insert(
wynik,
""
)
table.insert(
wynik,
'{| class="wikitable sortable" style="width:100%;"'
)
table.insert(
wynik,
"! Nr !! Sezon !! Data !! Klub !! Miejsce !! Przeciwnik !! Wynik !! Jubileusz"
)
-- =============================================
-- MECZE
-- =============================================
for i, row in ipairs(dane.mecze) do
local miejsce =
okreslMiejsce(row)
local d1 =
row.druzyna1 or ""
local d2 =
row.druzyna2 or ""
local klub =
row.druzyna or ""
local przeciwnik = ""
if miejsce == "dom" then
przeciwnik =
d2
elseif miejsce == "wyjazd" then
przeciwnik =
d1
else
przeciwnik =
d1
end
local wynikMeczu =
row.wynik or ""
local strona =
row.strona_meczu or ""
local meczTekst =
nazwaMeczu(row)
local jubileusz = ""
if i % 50 == 0 then
jubileusz =
"'''🏆 " ..
i ..
". mecz'''"
end
table.insert(
wynik,
"|-"
)
table.insert(
wynik,
"| " ..
i
)
table.insert(
wynik,
"| " ..
(row.sezon or "")
)
table.insert(
wynik,
"| " ..
(row.data or "")
)
table.insert(
wynik,
"| " ..
klub
)
if miejsce == "dom" then
table.insert(
wynik,
"| 🏠 Dom"
)
elseif miejsce == "wyjazd" then
table.insert(
wynik,
"| ✈️ Wyjazd"
)
else
table.insert(
wynik,
"| "
)
end
table.insert(
wynik,
"| " ..
przeciwnik
)
table.insert(
wynik,
"| " ..
wynikMeczu
)
table.insert(
wynik,
"| " ..
jubileusz
)
end
table.insert(
wynik,
"|}"
)
table.insert(
wynik,
""
)
end
end
-- =====================================================
-- KONIEC
-- =====================================================
return table.concat(
wynik,
"\n"
)
end
return p