Moduł:H2H
Wygląd
Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:H2H/opis
local p = {}
-- =========================================================
-- Module:H2H
--
-- Historia bezpośrednich spotkań dwóch drużyn.
--
-- Drużyny są rozpoznawane po:
-- Mecze.nazwa_statystyki1
-- Mecze.nazwa_statystyki2
--
-- Dzięki temu zmiana sponsora / nazwy drużyny nie rozbija H2H.
--
-- Funkcje:
-- 1. bilans spotkań
-- 2. suma punktów
-- 3. wszystkie mecze
-- 4. najlepsi strzelcy
-- 5. trenerzy
-- 6. rekordy meczowe
-- 7. rekordy indywidualne
-- 8. serie zwycięstw / porażek
-- 9. kwartowe porównanie wyników
--
-- =========================================================
function p.main(frame)
-- =====================================================
-- PARAMETRY
-- =====================================================
local team1 =
mw.text.trim(frame.args.team1 or "")
local team2 =
mw.text.trim(frame.args.team2 or "")
if team1 == "" or team2 == "" then
return "'''Błąd:''' należy podać obie drużyny."
end
-- =====================================================
-- BEZPIECZEŃSTWO SQL
-- =====================================================
local function sql(value)
return mw.ustring.gsub(
value,
"'",
"''"
)
end
local t1 = sql(team1)
local t2 = sql(team2)
-- =====================================================
-- WARUNEK H2H DLA TABELI Mecze
--
-- WAŻNE:
-- tutaj używamy nazwa_statystyki.
-- Dzięki temu zmiana nazwy sponsorskiej nie rozbija H2H.
-- =====================================================
local whereMecze =
"(" ..
"M.nazwa_statystyki1='" .. t1 ..
"' AND M.nazwa_statystyki2='" .. t2 ..
"')" ..
" OR " ..
"(" ..
"M.nazwa_statystyki1='" .. t2 ..
"' AND M.nazwa_statystyki2='" .. t1 ..
"')"
-- =====================================================
-- FUNKCJE POMOCNICZE
-- =====================================================
local function number(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 parseScore(value)
if not value then
return nil
end
local a, b =
tostring(value):match(
"(%d+)%s*:%s*(%d+)"
)
if a and b then
return tonumber(a), tonumber(b)
end
return nil, nil
end
local function matchScore(row)
local g1 =
tonumber(row.gole1 or "")
local g2 =
tonumber(row.gole2 or "")
if g1 and g2 then
return g1, g2
end
return parseScore(row.wynik)
end
local function location(row)
local d1 =
row.nazwa_statystyki1 or ""
local d2 =
row.nazwa_statystyki2 or ""
if d1 == team1 then
return "dom"
end
if d2 == team1 then
return "wyjazd"
end
return nil
end
local function teamScore(row)
local g1, g2 =
matchScore(row)
if not g1 or not g2 then
return nil, nil
end
if location(row) == "dom" then
return g1, g2
end
return g2, g1
end
local function result(row)
local zdobyte, stracone =
teamScore(row)
if not zdobyte or not stracone then
return nil
end
if zdobyte > stracone then
return "W"
end
if zdobyte < stracone then
return "P"
end
return "R"
end
local function matchName(row)
local d1 =
row.druzyna1 or ""
local d2 =
row.druzyna2 or ""
local page =
row.strona_meczu or ""
local text =
d1 ..
" – " ..
d2
if page ~= "" then
return
"[[" ..
page ..
"|" ..
text ..
"]]"
end
return text
end
local function formatPlusMinus(value)
value =
number(value)
if value > 0 then
return "+" .. tostring(value)
end
return tostring(value)
end
-- =====================================================
-- POBRANIE MECZÓW
-- =====================================================
local fieldsMecze =
table.concat({
"M.id=mecz",
"M.data=data",
"M.sezon=sezon",
"M.druzyna1=druzyna1",
"M.druzyna2=druzyna2",
"M.nazwa_statystyki1=ns1",
"M.nazwa_statystyki2=ns2",
"M.gole1=gole1",
"M.gole2=gole2",
"M.wynik=wynik",
"M.kwart1=kwarta1",
"M.kwart2=kwarta2",
"M.kwart3=kwarta3",
"M.kwart4=kwarta4",
"M.dogrywka1=dogrywka1",
"M.dogrywka2=dogrywka2",
"M.hala=hala",
"M.widzowie=widzowie",
"M.sedziowie=sedziowie",
"M.strona_meczu=strona_meczu"
}, ",")
local mecze =
mw.ext.cargo.query(
"Mecze=M",
fieldsMecze,
{
where = whereMecze,
orderBy = "M.data ASC",
limit = 20000
}
)
if not mecze or #mecze == 0 then
return
"'''Brak wyników.'''"
end
-- =====================================================
-- BILANS
-- =====================================================
local liczbaMeczow = 0
local wygrane1 = 0
local wygrane2 = 0
local remisy = 0
local punkty1 = 0
local punkty2 = 0
for _, row in ipairs(mecze) do
local g1, g2 =
matchScore(row)
if g1 and g2 then
liczbaMeczow =
liczbaMeczow + 1
local d1 =
row.nazwa_statystyki1 or ""
local d2 =
row.nazwa_statystyki2 or ""
if d1 == team1 then
punkty1 =
punkty1 + g1
punkty2 =
punkty2 + g2
if g1 > g2 then
wygrane1 =
wygrane1 + 1
elseif g2 > g1 then
wygrane2 =
wygrane2 + 1
else
remisy =
remisy + 1
end
elseif d1 == team2 then
punkty1 =
punkty1 + g2
punkty2 =
punkty2 + g1
if g2 > g1 then
wygrane1 =
wygrane1 + 1
elseif g1 > g2 then
wygrane2 =
wygrane2 + 1
else
remisy =
remisy + 1
end
end
end
end
-- =====================================================
-- WYNIK PODSUMOWANIA
-- =====================================================
local output = {}
table.insert(
output,
"== Bilans bezpośrednich spotkań =="
)
table.insert(
output,
""
)
table.insert(
output,
'{| class="wikitable" style="width:100%; text-align:center;"'
)
table.insert(
output,
"! Drużyna !! Mecze !! Wygrane !! Remisy !! Porażki !! Punkty"
)
table.insert(
output,
"|-"
)
table.insert(
output,
"| '''" ..
team1 ..
"''' || " ..
liczbaMeczow ..
" || " ..
wygrane1 ..
" || " ..
remisy ..
" || " ..
wygrane2 ..
" || " ..
punkty1
)
table.insert(
output,
"|-"
)
table.insert(
output,
"| '''" ..
team2 ..
"''' || " ..
liczbaMeczow ..
" || " ..
wygrane2 ..
" || " ..
remisy ..
" || " ..
wygrane1 ..
" || " ..
punkty2
)
table.insert(
output,
"|}"
)
-- =====================================================
-- HISTORIA SPOTKAŃ
-- =====================================================
table.insert(
output,
""
)
table.insert(
output,
"== Historia spotkań =="
)
table.insert(
output,
""
)
table.insert(
output,
'{| class="wikitable sortable" style="width:100%;"'
)
table.insert(
output,
"! Lp. !! Data !! Sezon !! Gospodarz !! Wynik !! Gość !! Hala"
)
local lp = 0
for i = #mecze, 1, -1 do
local row =
mecze[i]
local g1, g2 =
matchScore(row)
if g1 and g2 then
lp =
lp + 1
table.insert(
output,
"|-"
)
table.inser