Moduł:H2H
Wygląd
Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:H2H/opis
local p = {}
function p.main(frame)
local team1 = frame.args.team1 or ""
local team2 = frame.args.team2 or ""
if team1 == "" or team2 == "" then
return "'''Błąd:''' należy podać team1 i team2."
end
-- =========================================================
-- FUNKCJE POMOCNICZE
-- =========================================================
local function esc(value)
value = tostring(value or "")
return mw.ustring.gsub(value, "'", "''")
end
local t1 = esc(team1)
local t2 = esc(team2)
local function num(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
local function wynik(row)
local g1 = num(row.gole1)
local g2 = num(row.gole2)
if row.gole1 ~= nil and row.gole2 ~= nil then
return g1, g2
end
local w = row.wynik or ""
local a, b = w:match("(%d+)%s*:%s*(%d+)")
if a and b then
return tonumber(a), tonumber(b)
end
return nil, nil
end
local function strona(row)
if row.druzyna1 == team1 then
return "dom"
end
if row.druzyna2 == team1 then
return "wyjazd"
end
return nil
end
local function nazwaMeczu(row)
local d1 = row.druzyna1 or ""
local d2 = row.druzyna2 or ""
local nazwa =
d1 .. " – " .. d2
if row.strona_meczu and row.strona_meczu ~= "" then
return "[[" ..
row.strona_meczu ..
"|" .. nazwa .. "]]"
end
return nazwa
end
local function linkOsoby(osoba)
if not osoba or osoba == "" then
return "—"
end
return "[[" .. osoba .. "]]"
end
-- =========================================================
-- MECZE H2H
--
-- KLUCZOWE:
-- używamy nazwa_statystyki1 / nazwa_statystyki2,
-- a nie aktualnej nazwy sponsorskiej drużyny.
-- =========================================================
local fieldsMecze = table.concat({
"Mecze.id=mecz",
"Mecze.data=data",
"Mecze.sezon=sezon",
"Mecze.druzyna1=druzyna1",
"Mecze.druzyna2=druzyna2",
"Mecze.nazwa_statystyki1=ns1",
"Mecze.nazwa_statystyki2=ns2",
"Mecze.gole1=gole1",
"Mecze.gole2=gole2",
"Mecze.wynik=wynik",
"Mecze.kolejka=kolejka",
"Mecze.hala=hala",
"Mecze.widzowie=widzowie",
"Mecze.sedziowie=sedziowie",
"Mecze.strona_meczu=strona_meczu",
"Mecze.kwarta1=kwarta1",
"Mecze.kwarta2=kwarta2",
"Mecze.kwarta3=kwarta3",
"Mecze.kwarta4=kwarta4",
"Mecze.dogrywka1=dogrywka1",
"Mecze.dogrywka2=dogrywka2",
"Mecze.dogrywka3=dogrywka3"
}, ",")
local whereH2H =
"(" ..
"(Mecze.nazwa_statystyki1='" .. t1 ..
"' AND Mecze.nazwa_statystyki2='" .. t2 .. "')" ..
" OR " ..
"(Mecze.nazwa_statystyki1='" .. t2 ..
"' AND Mecze.nazwa_statystyki2='" .. t1 .. "')" ..
")"
local whereH2H_M =
"(" ..
"(M.nazwa_statystyki1='" .. t1 ..
"' AND M.nazwa_statystyki2='" .. t2 .. "')" ..
" OR " ..
"(M.nazwa_statystyki1='" .. t2 ..
"' AND M.nazwa_statystyki2='" .. t1 .. "')" ..
")"
local mecze = mw.ext.cargo.query(
"Mecze",
fieldsMecze,
{
where = whereH2H,
orderBy = "Mecze.data ASC",
limit = 5000
}
)
if not mecze or #mecze == 0 then
return "'''Brak danych H2H dla podanych drużyn.'''"
end
-- =========================================================
-- BILANS
-- =========================================================
local bilans = {
mecze = 0,
wygrane1 = 0,
wygrane2 = 0,
punkty1 = 0,
punkty2 = 0
}
for _, row in ipairs(mecze) do
local g1, g2 = wynik(row)
if g1 and g2 then
bilans.mecze =
bilans.mecze + 1
if row.ns1 == team1 then
bilans.punkty1 =
bilans.punkty1 + g1
bilans.punkty2 =
bilans.punkty2 + g2
if g1 > g2 then
bilans.wygrane1 =
bilans.wygrane1 + 1
elseif g2 > g1 then
bilans.wygrane2 =
bilans.wygrane2 + 1
end
else
bilans.punkty1 =
bilans.punkty1 + g2
bilans.punkty2 =
bilans.punkty2 + g1
if g2 > g1 then
bilans.wygrane1 =
bilans.wygrane1 + 1
elseif g1 > g2 then
bilans.wygrane2 =
bilans.wygrane2 + 1
end
end
end
end
-- =========================================================
-- REKORDY MECZOWE
-- =========================================================
local rekordyMeczowe = {
zwyciestwo = {
dom = {},
wyjazd = {}
},
porazka = {
dom = {},
wyjazd = {}
}
}
local function dodajRekordMeczowy(tabela, miejsce, roznica, row)
local lista = tabela[miejsce]
if #lista == 0 then
table.insert(lista, {
row = row,
roznica = roznica
})
return
end
local obecna =
lista[1].roznica
if roznica > obecna then
tabela[miejsce] = {
{
row = row,
roznica = roznica
}
}
elseif roznica == obecna then
table.insert(lista, {
row = row,
roznica = roznica
})
end
end
-- =========================================================
-- SERIE
-- =========================================================
local serie = {
wszystkie = {
W = 0,
P = 0
},
dom = {
W = 0,
P = 0
},
wyjazd = {
W = 0,
P = 0
}
}
local aktualne = {
wszystkie = {
W = 0,
P = 0
},
dom = {
W = 0,
P = 0
},
wyjazd = {
W = 0,
P = 0
}
}
-- =========================================================
-- ANALIZA MECZÓW
-- =========================================================
for _, row in ipairs(mecze) do
local g1, g2 = wynik(row)
if g1 and g2 then
local mojaStrona
if row.ns1 == team1 then
mojaStrona = "dom"
else
mojaStrona = "wyjazd"
end
local mojePunkty
local rywalPunkty
if mojaStrona == "dom" then
mojePunkty = g1
rywalPunkty = g2
else
mojePunkty = g2
rywalPunkty = g1
end
local roznica =
mojePunkty - rywalPunkty
-- ZWYCIĘSTWO
if roznica > 0 then
dodajRekordMeczowy(
rekordyMeczowe.zwyciestwo,
mojaStrona,
roznica,
row
)
end
-- PORAŻKA
if roznica < 0 then
dodajRekordMeczowy(
rekordyMeczowe.porazka,
mojaStrona,
math.abs(roznica),
row
)
end
-- SERIA OGÓŁEM
if roznica > 0 then
aktualne.wszystkie.W =
aktualne.wszystkie.W + 1
aktualne.wszystkie.P = 0
if aktualne.wszystkie.W >
serie.wszystkie.W then
serie.wszystkie.W =
aktualne.wszystkie.W
end
elseif roznica < 0 then
aktualne.wszystkie.P =
aktualne.wszystkie.P + 1
aktualne.wszystkie.W = 0
if aktualne.wszystkie.P >
serie.wszystkie.P then
serie.wszystkie.P =
aktualne.wszystkie.P
end
end
-- SERIA DOMOWA
if mojaStrona == "dom" then
if roznica > 0 then
aktualne.dom.W =
aktualne.dom.W + 1
aktualne.dom.P = 0
if aktualne.dom.W >
serie.dom.W then
serie.dom.W =
aktualne.dom.W
end
elseif roznica < 0 then
aktualne.dom.P =
aktualne.dom.P + 1
aktualne.dom.W = 0
if aktualne.dom.P >
serie.dom.P then
serie.dom.P =
aktualne.dom.P
end
end
end
-- SERIA WYJAZDOWA
if mojaStrona == "wyjazd" then
if roznica > 0 then
aktualne.wyjazd.W =
aktualne.wyjazd.W + 1
aktualne.wyjazd.P = 0
if aktualne.wyjazd.W >
serie.wyjazd.W then
serie.wyjazd.W =
aktualne.wyjazd.W
end
elseif roznica < 0 then
aktualne.wyjazd.P =
aktualne.wyjazd.P + 1
aktualne.wyjazd.W = 0
if aktualne.wyjazd.P >
serie.wyjazd.P then
serie.wyjazd.P =
aktualne.wyjazd.P
end
end
end
end
end
-- =========================================================
-- REKORDY INDYWIDUALNE
-- =========================================================
local rekordy = {
punkty = {dom = {}, wyjazd = {}},
minuty = {dom = {}, wyjazd = {}},
asysty = {dom = {}, wyjazd = {}},
zbiorki = {dom = {}, wyjazd = {}},
eval = {dom = {}, wyjazd = {}},
plusminus = {dom = {}, wyjazd = {}}
}
local fieldsU = table.concat({
"U.zawodnik=zawodnik",
"U.druzyna=druzyna_zawodnika",
"U.gol=gol",
"U.minuty=minuty",
"U.minuty_format=minuty_format",
"U.asysta=asysta",
"U.zbiorka=zbiorka",
"U.eval=eval",
"U.plusminus=plusminus",
"M.id=mecz",
"M.data=data",
"M.druzyna1=druzyna1",
"M.druzyna2=druzyna2",
"M.nazwa_statystyki1=ns1",
"M.nazwa_statystyki2=ns2",
"M.wynik=wynik",
"M.strona_meczu=strona_meczu"
}, ",")
local zawodnicy = mw.ext.cargo.query(
"UdzialZawodnika=U,Mecze=M",
fieldsU,
{
join = "U.mecz=M.id",
where =
whereH2H ..
" AND U.minuty > 0",
orderBy = "M.data ASC",
limit = 20000
}
)
local function dodajRekord(tabela, miejsce, wartosc, item)
local lista = tabela[miejsce]
if #lista == 0 then
table.insert(lista, {
item = item,
wartosc = wartosc
})
return
end
local obecna =
lista[1].wartosc
if wartosc > obecna then
tabela[miejsce] = {
{
item = item,
wartosc = wartosc
}
}
elseif wartosc == obecna then
table.insert(lista, {
item = item,
wartosc = wartosc
})
end
end
if zawodnicy then
for _, row in ipairs(zawodnicy) do
local miejsce
-- Strona drużyny jest ustalana po
-- nazwa_statystyki, a nie po sponsorze.
if row.ns1 == team1 then
miejsce = "dom"
elseif row.ns2 == team1 then
miejsce = "wyjazd"
end
if miejsce then
local item = {
zawodnik = row.zawodnik or "",
mecz = row.mecz or "",
data = row.data or "",
wynik = row.wynik or "",
tekst =
(row.druzyna1 or "") ..
" – " ..
(row.druzyna2 or ""),
punkty = num(row.gol),
minuty = num(row.minuty),
minuty_format =
row.minuty_format or "",
asysty = num(row.asysta),
zbiorki = num(row.zbiorka),
eval = num(row.eval),
plusminus = num(row.plusminus)
}
dodajRekord(
rekordy.punkty,
miejsce,
item.punkty,
item
)
dodajRekord(
rekordy.minuty,
miejsce,
item.minuty,
item
)
dodajRekord(
rekordy.asysty,
miejsce,
item.asysty,
item
)
dodajRekord(
rekordy.zbiorki,
miejsce,
item.zbiorki,
item
)
dodajRekord(
rekordy.eval,
miejsce,
item.eval,
item
)
dodajRekord(
rekordy.plusminus,
miejsce,
item.plusminus,
item
)
end
end
end
-- =========================================================
-- TRENERZY
-- =========================================================
local fieldsSztab = table.concat({
"S.osoba=osoba",
"S.druzyna=druzyna",
"S.rola=rola",
"S.mecz=mecz",
"M.data=data",
"M.nazwa_statystyki1=ns1",
"M.nazwa_statystyki2=ns2",
"M.gole1=gole1",
"M.gole2=gole2"
}, ",")
local sztab = mw.ext.cargo.query(
"SztabMeczu=S,Mecze=M",
fieldsSztab,
{
join = "S.mecz=M.id",
where =
whereH2H ..
" AND S.rola IN ('Trener','II trener')",
orderBy = "M.data ASC",
limit = 20000
}
)
local trenerzy = {}
if sztab then
for _, row in ipairs(sztab) do
local klucz =
(row.osoba or "") ..
"|" ..
(row.druzyna or "") ..
"|" ..
(row.rola or "")
if not trenerzy[klucz] then
trenerzy[klucz] = {
osoba = row.osoba or "",
druzyna = row.druzyna or "",
rola = row.rola or "",
mecze = 0,
wygrane = 0,
przegrane = 0
}
end
local t = trenerzy[klucz]
t.mecze =
t.mecze + 1
local g1, g2 =
wynik(row)
if g1 and g2 then
local dlaDruzyny
if row.druzyna == row.ns1 then
dlaDruzyny = g1 - g2
elseif row.druzyna == row.ns2 then
dlaDruzyny = g2 - g1
end
if dlaDruzyny then
if dlaDruzyny > 0 then
t.wygrane =
t.wygrane + 1
elseif dlaDruzyny < 0 then
t.przegrane =
t.przegrane + 1
end
end
end
end
end
-- =========================================================
-- NAJLEPSI STRZELCY
--
-- SUMA PUNKTÓW ZOSTAJE.
-- =========================================================
local strzelcy = {
team1 = {},
team2 = {}
}
if zawodnicy then
for _, row in ipairs(zawodnicy) do
local stronaDruzyny
if row.ns1 == team1 then
if row.druzyna_zawodnika == row.druzyna1 then
stronaDruzyny = "team1"
end
elseif row.ns2 == team1 then
if row.druzyna_zawodnika == row.druzyna2 then
stronaDruzyny = "team1"
end
elseif row.ns1 == team2 then
if row.druzyna_zawodnika == row.druzyna1 then
stronaDruzyny = "team2"
end
elseif row.ns2 == team2 then
if row.druzyna_zawodnika == row.druzyna2 then
stronaDruzyny = "team2"
end
end
if stronaDruzyny then
local zawodnik =
row.zawodnik or ""
if not strzelcy[stronaDruzyny][zawodnik] then
strzelcy[stronaDruzyny][zawodnik] = 0
end
strzelcy[stronaDruzyny][zawodnik] =
strzelcy[stronaDruzyny][zawodnik] +
num(row.gol)
end
end
end
local function posortowaniStrzelcy(tabela)
local lista = {}
for zawodnik, punkty in pairs(tabela) do
table.insert(lista, {
zawodnik = zawodnik,
punkty = punkty
})
end
table.sort(lista, function(a,b)
if a.punkty == b.punkty then
return a.zawodnik < b.zawodnik
end
return a.punkty > b.punkty
end)
return lista
end
-- =========================================================
-- FORMATOWANIE REKORDÓW
-- =========================================================
local function meczeRekordowe(lista)
if not lista or #lista == 0 then
return "—"
end
local wynik = {}
for _, r in ipairs(lista) do
local row = r.row
table.insert(
wynik,
nazwaMeczu(row) ..
" (" ..
tostring(row.gole1 or "") ..
":" ..
tostring(row.gole2 or "") ..
", +" ..
tostring(r.roznica) ..
")"
)
end
return table.concat(
wynik,
"<br>"
)
end
local function rekordyZawodnikow(lista, typ)
if not lista or #lista == 0 then
return "—"
end
local wynik = {}
for _, r in ipairs(lista) do
local x = r.item
local wartosc
if typ == "punkty" then
wartosc = x.punkty .. " pkt"
elseif typ == "minuty" then
wartosc =
x.minuty_format ~= "" and
x.minuty_format or
tostring(x.minuty)
wartosc = wartosc .. " min"
elseif typ == "asysty" then
wartosc = x.asysty .. " as."
elseif typ == "zbiorki" then
wartosc = x.zbiorki .. " zb."
elseif typ == "eval" then
wartosc = x.eval .. " EVAL"
elseif typ == "plusminus" then
if x.plusminus >= 0 then
wartosc =
"+" .. x.plusminus
else
wartosc =
tostring(x.plusminus)
end
wartosc =
wartosc .. " +/-"
end
table.insert(
wynik,
linkOsoby(x.zawodnik) ..
" — " ..
wartosc ..
"<br>" ..
x.tekst ..
" (" ..
x.wynik ..
")"
)
end
return table.concat(
wynik,
"<br>"
)
end
-- =========================================================
-- WYNIK
-- =========================================================
local out = {}
table.insert(
out,
"== Bilans bezpośrednich spotkań =="
)
table.insert(
out,
'{| class="wikitable" style="width:100%; text-align:center;"'
)
table.insert(
out,
"! Drużyna !! Mecze !! Wygrane !! Porażki !! Punkty zdobyte"
)
table.insert(out, "|-")
table.insert(
out,
"| '''" .. team1 .. "'''"
)
table.insert(
out,
"| " .. bilans.mecze
)
table.insert(
out,
"| " .. bilans.wygrane1
)
table.insert(
out,
"| " .. bilans.wygrane2
)
table.insert(
out,
"| " .. bilans.punkty1
)
table.insert(out, "|-")
table.insert(
out,
"| '''" .. team2 .. "'''"
)
table.insert(
out,
"| " .. bilans.mecze
)
table.insert(
out,
"| " .. bilans.wygrane2
)
table.insert(
out,
"| " .. bilans.wygrane1
)
table.insert(
out,
"| " .. bilans.punkty2
)
table.insert(out, "|}")
table.insert(out, "")
-- =========================================================
-- WSZYSTKIE MECZE H2H
-- =========================================================
table.insert(
out,
"== Wszystkie mecze =="
)
table.insert(
out,
'{| class="wikitable sortable" style="width:100%;"'
)
table.insert(
out,
"! Nr !! Data !! Sezon !! Gospodarz !! Wynik !! Gość !! Hala"
)
for i = #mecze, 1, -1 do
local row = mecze[i]
table.insert(out, "|-")
table.insert(
out,
"| " .. (#mecze - i + 1)
)
table.insert(
out,
"| " .. (row.data or "")
)
table.insert(
out,
"| " .. (row.sezon or "")
)
table.insert(
out,
"| " .. (row.druzyna1 or "")
)
table.insert(
out,
"| '''" ..
(row.wynik or
((row.gole1 or "") ..
":" ..
(row.gole2 or ""))) ..
"'''"
)
table.insert(
out,
"| " .. (row.druzyna2 or "")
)
table.insert(
out,
"| " .. (row.hala or "")
)
end
table.insert(out, "|}")
-- =========================================================
-- TRENERZY
-- =========================================================
table.insert(out, "")
table.insert(
out,
"== Trenerzy w bezpośrednich spotkaniach =="
)
table.insert(
out,
'{| class="wikitable sortable" style="width:100%;"'
)
table.insert(
out,
"! Trener !! Drużyna !! Rola !! Mecze !! Wygrane !! Porażki !! % wygranych"
)
local listaTrenerow = {}
for _, t in pairs(trenerzy) do
table.insert(listaTrenerow, t)
end
table.sort(listaTrenerow, function(a,b)
return a.mecze > b.mecze
end)
for _, t in ipairs(listaTrenerow) do
local procent = 0
if t.mecze > 0 then
procent =
math.floor(
(t.wygrane / t.mecze) *
1000 + 0.5
) / 10
end
table.insert(out, "|-")
table.insert(
out,
"| " .. linkOsoby(t.osoba)
)
table.insert(
out,
"| " .. t.druzyna
)
table.insert(
out,
"| " .. t.rola
)
table.insert(
out,
"| " .. t.mecze
)
table.insert(
out,
"| " .. t.wygrane
)
table.insert(
out,
"| " .. t.przegrane
)
table.insert(
out,
"| " .. procent .. "%"
)
end
table.insert(out, "|}")
-- =========================================================
-- NAJLEPSI STRZELCY
-- =========================================================
table.insert(out, "")
table.insert(
out,
"== Najlepsi strzelcy rywalizacji =="
)
table.insert(
out,
'{| class="wikitable" style="width:100%;"'
)
table.insert(
out,
"! " .. team1 .. " !! Punkty !! " ..
team2 .. " !! Punkty"
)
local s1 =
posortowaniStrzelcy(
strzelcy.team1
)
local s2 =
posortowaniStrzelcy(
strzelcy.team2
)
local max =
math.max(#s1, #s2)
for i = 1, max do
table.insert(out, "|-")
if s1[i] then
table.insert(
out,
"| " ..
linkOsoby(s1[i].zawodnik)
)
table.insert(
out,
"| " ..
s1[i].punkty
)
else
table.insert(out, "|")
table.insert(out, "|")
end
if s2[i] then
table.insert(
out,
"| " ..
linkOsoby(s2[i].zawodnik)
)
table.insert(
out,
"| " ..
s2[i].punkty
)
else
table.insert(out, "|")
table.insert(out, "|")
end
end
table.insert(out, "|}")
-- =========================================================
-- REKORDY MECZOWE
-- =========================================================
table.insert(out, "")
table.insert(
out,
"== Rekordy meczowe – " .. team1 .. " =="
)
table.insert(
out,
'{| class="wikitable" style="width:100%; text-align:center;"'
)
table.insert(
out,
"! Rekord !! 🏠 Dom !! ✈️ Wyjazd"
)
table.insert(out, "|-")
table.insert(
out,
"! 🏆 Najwyższe zwycięstwo"
)
table.insert(
out,
"| " ..
meczeRekordowe(
rekordyMeczowe.zwyciestwo.dom
)
)
table.insert(
out,
"| " ..
meczeRekordowe(
rekordyMeczowe.zwyciestwo.wyjazd
)
)
table.insert(out, "|-")
table.insert(
out,
"! ❌ Najwyższa porażka"
)
table.insert(
out,
"| " ..
meczeRekordowe(
rekordyMeczowe.porazka.dom
)
)
table.insert(
out,
"| " ..
meczeRekordowe(
rekordyMeczowe.porazka.wyjazd
)
)
table.insert(out, "|}")
-- =========================================================
-- REKORDY INDYWIDUALNE
-- =========================================================
table.insert(out, "")
table.insert(
out,
"== Rekordy indywidualne – " .. team1 .. " =="
)
table.insert(
out,
'{| class="wikitable" style="width:100%; text-align:center;"'
)
table.insert(
out,
"! Rekord !! 🏠 Dom !! ✈️ Wyjazd"
)
local rekordyLista = {
{
"🏀 Najwięcej punktów",
rekordy.punkty,
"punkty"
},
{
"⏱️ Najwięcej minut",
rekordy.minuty,
"minuty"
},
{
"🎯 Najwięcej asyst",
rekordy.asysty,
"asysty"
},
{
"🏀 Najwięcej zbiórek",
rekordy.zbiorki,
"zbiorki"
},
{
"⭐ Najwyższy EVAL",
rekordy.eval,
"eval"
},
{
"➕ Najwyższy +/-",
rekordy.plusminus,
"plusminus"
}
}
for _, r in ipairs(rekordyLista) do
table.insert(out, "|-")
table.insert(
out,
"! " .. r[1]
)
table.insert(
out,
"| " ..
rekordyZawodnikow(
r[2].dom,
r[3]
)
)
table.insert(
out,
"| " ..
rekordyZawodnikow(
r[2].wyjazd,
r[3]
)
)
end
table.insert(out, "|}")
-- =========================================================
-- SERIE
-- =========================================================
table.insert(out, "")
table.insert(
out,
"== Serie – " .. team1 .. " =="
)
table.insert(
out,
'{| class="wikitable" style="width:100%; text-align:center;"'
)
table.insert(
out,
"! Seria !! Ogółem !! 🏠 Dom !! ✈️ Wyjazd"
)
table.insert(out, "|-")
table.insert(
out,
"! 🔥 Najdłuższa seria zwycięstw"
)
table.insert(
out,
"| " .. serie.wszystkie.W
)
table.insert(
out,
"| " .. serie.dom.W
)
table.insert(
out,
"| " .. serie.wyjazd.W
)
table.insert(out, "|-")
table.insert(
out,
"! 📉 Najdłuższa seria porażek"
)
table.insert(
out,
"| " .. serie.wszystkie.P
)
table.insert(
out,
"| " .. serie.dom.P
)
table.insert(
out,
"| " .. serie.wyjazd.P
)
table.insert(out, "|}")
return table.concat(out, "\n")
end
return p