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