Перейти к содержанию

Модуль:Football: различия между версиями

Нет описания правки
Реализован вывод прогресса по турам
Строка 674: Строка 674:
end
end


-- =====================================================
-- ПРОГРЕСС ПО ТУРАМ
-- =====================================================
local function buildProgress()
local progress = {}
-- Собираем список туров
local tours = {}
for _, match in ipairs(matches) do
local tourNumber = tonumber(match.tour)
if tourNumber and not tours[tourNumber] then
tours[tourNumber] = true
end
end
-- Последовательно рассчитываем таблицу
-- после каждого тура
local currentTeams = {}
for _, code in ipairs(teamOrder) do
currentTeams[code] = newTeam(
code,
teams[code].name
)
end
-- Вспомогательная функция личных встреч
local function progressHeadToHead(code, group, lastTour)
local result = {
points = 0,
goalsFor = 0,
goalsAgainst = 0,
goalDifference = 0,
wins = 0
}
for _, match in ipairs(matches) do
local tourNumber = tonumber(match.tour)
if match.played and
  tourNumber and
  tourNumber <= lastTour and
  group[match.home] and
  group[match.away] then
if match.home == code then
result.goalsFor =
result.goalsFor + match.homeGoals
result.goalsAgainst =
result.goalsAgainst + match.awayGoals
if match.homeGoals > match.awayGoals then
result.points =
result.points + pointsWin
result.wins =
result.wins + 1
elseif match.homeGoals == match.awayGoals then
result.points =
result.points + pointsDraw
else
result.points =
result.points + pointsLoss
end
elseif match.away == code then
result.goalsFor =
result.goalsFor + match.awayGoals
result.goalsAgainst =
result.goalsAgainst + match.homeGoals
if match.awayGoals > match.homeGoals then
result.points =
result.points + pointsWin
result.wins =
result.wins + 1
elseif match.awayGoals == match.homeGoals then
result.points =
result.points + pointsDraw
else
result.points =
result.points + pointsLoss
end
end
end
end
result.goalDifference =
result.goalsFor - result.goalsAgainst
return result
end
-- Сравнение команд для конкретного тура
local function compareProgressTeams(a, b, lastTour)
for _, criterion in ipairs(criteria) do
if criterion == "очки" then
if a.points ~= b.points then
return a.points > b.points
end
elseif criterion == "разница" then
if a.goalDifference ~= b.goalDifference then
return a.goalDifference > b.goalDifference
end
elseif criterion == "забитые" then
if a.goalsFor ~= b.goalsFor then
return a.goalsFor > b.goalsFor
end
elseif criterion == "победы" then
if a.wins ~= b.wins then
return a.wins > b.wins
end
elseif criterion == "игры" then
if a.played ~= b.played then
return a.played > b.played
end
elseif criterion == "личные" then
local group = {}
for _, code in ipairs(teamOrder) do
local team = currentTeams[code]
if team.points == a.points then
group[code] = true
end
end
local ha =
progressHeadToHead(a.code, group, lastTour)
local hb =
progressHeadToHead(b.code, group, lastTour)
if ha.points ~= hb.points then
return ha.points > hb.points
end
if ha.goalDifference ~= hb.goalDifference then
return ha.goalDifference > hb.goalDifference
end
if ha.goalsFor ~= hb.goalsFor then
return ha.goalsFor > hb.goalsFor
end
if ha.wins ~= hb.wins then
return ha.wins > hb.wins
end
elseif criterion == "название" then
local aname =
mw.text.unstrip(a.name)
local bname =
mw.text.unstrip(b.name)
if aname ~= bname then
return aname < bname
end
end
end
return a.code < b.code
end
-- -------------------------------------------------
-- Обрабатываем туры по порядку
-- -------------------------------------------------
local tourNumbers = {}
for tourNumber in pairs(tours) do
table.insert(tourNumbers, tourNumber)
end
table.sort(tourNumbers)
for _, tourNumber in ipairs(tourNumbers) do
-- Добавляем матчи данного тура
for _, match in ipairs(matches) do
if tonumber(match.tour) == tourNumber
  and match.played then
local homeTeam =
currentTeams[match.home]
local awayTeam =
currentTeams[match.away]
homeTeam.played =
homeTeam.played + 1
awayTeam.played =
awayTeam.played + 1
homeTeam.goalsFor =
homeTeam.goalsFor + match.homeGoals
homeTeam.goalsAgainst =
homeTeam.goalsAgainst + match.awayGoals
awayTeam.goalsFor =
awayTeam.goalsFor + match.awayGoals
awayTeam.goalsAgainst =
awayTeam.goalsAgainst + match.homeGoals
if match.homeGoals > match.awayGoals then
homeTeam.wins =
homeTeam.wins + 1
awayTeam.losses =
awayTeam.losses + 1
homeTeam.points =
homeTeam.points + pointsWin
awayTeam.points =
awayTeam.points + pointsLoss
elseif match.homeGoals < match.awayGoals then
awayTeam.wins =
awayTeam.wins + 1
homeTeam.losses =
homeTeam.losses + 1
awayTeam.points =
awayTeam.points + pointsWin
homeTeam.points =
homeTeam.points + pointsLoss
else
homeTeam.draws =
homeTeam.draws + 1
awayTeam.draws =
awayTeam.draws + 1
homeTeam.points =
homeTeam.points + pointsDraw
awayTeam.points =
awayTeam.points + pointsDraw
end
end
end
-- Разница мячей
for _, code in ipairs(teamOrder) do
local team = currentTeams[code]
team.goalDifference =
team.goalsFor - team.goalsAgainst
end
-- Создаём таблицу после этого тура
local tourStandings = {}
for _, code in ipairs(teamOrder) do
table.insert(
tourStandings,
currentTeams[code]
)
end
table.sort(
tourStandings,
function(a, b)
return compareProgressTeams(
a,
b,
tourNumber
)
end
)
progress[tourNumber] = {}
for position, team in ipairs(tourStandings) do
progress[tourNumber][team.code] =
position
end
end
return progress, tourNumbers
end


-- =====================================================
-- =====================================================
Строка 1075: Строка 1441:


root:node(resultsTable)
root:node(resultsTable)
-- =====================================================
-- ПРОГРЕСС ПО ТУРАМ
-- =====================================================
if trim(args.show_progress) == "yes" then
local progress, tourNumbers =
buildProgress()
root:node(mw.html.create("br"))
local progressTitle =
mw.html.create("div")
:addClass("football-title")
:wikitext("'''Положение команд по турам'''")
root:node(progressTitle)
local progressTable =
mw.html.create("table")
:addClass("wikitable")
:addClass("football-progress")
:css("text-align", "center")
-- -------------------------------------------------
-- Цвета мест
-- -------------------------------------------------
local progressHighlights = {}
for i = 1, 10 do
local value =
trim(args["progress_highlight" .. i])
if value ~= "" then
local parts =
split(value, ";")
local place =
tonumber(parts[1])
local color =
parts[2] or ""
if place and color ~= "" then
progressHighlights[place] =
color
end
end
end
-- -------------------------------------------------
-- Заголовок
-- -------------------------------------------------
local progressHeader =
mw.html.create("tr")
progressHeader:node(
cell("th", "Команда")
)
for _, tourNumber in ipairs(tourNumbers) do
progressHeader:node(
cell(
"th",
"Тур " .. tourNumber
)
)
end
progressTable:node(progressHeader)
-- -------------------------------------------------
-- Строки команд
-- -------------------------------------------------
for _, code in ipairs(teamOrder) do
local row =
mw.html.create("tr")
row:node(
cell(
"th",
teams[code].name
)
)
for _, tourNumber in ipairs(tourNumbers) do
local position =
progress[tourNumber][code]
local resultCell =
cell("td", position)


local color =
progressHighlights[position]
if color and color ~= "" then
resultCell
:addClass("ui")
:addClass(color)
end
row:node(resultCell)
end
progressTable:node(row)
end
root:node(progressTable)
end


return tostring(root)
return tostring(root)