Add games per week to season object and preferred day of week for team object
This commit is contained in:
@@ -13,6 +13,7 @@ class SeasonDto
|
||||
public array $facilityDtoList;
|
||||
public int $leagueId;
|
||||
public int $seasonNumber;
|
||||
public int $gamesPerWeek;
|
||||
public string $leagueName;
|
||||
public int $pointsPerWin;
|
||||
public int $pointsPerLoss;
|
||||
@@ -27,6 +28,7 @@ class SeasonDto
|
||||
return [
|
||||
'id' => $this->id ?? null,
|
||||
'seasonNumber' => $this->seasonNumber ?? null,
|
||||
'gamesPerWeek' => $this->gamesPerWeek ?? null,
|
||||
'dateStart' => !empty($this->dateStart) ? $this->dateStart->format('Y-m'): null,
|
||||
'leagueId' => $this->leagueId ?? null,
|
||||
'leagueName' => $this->leagueName ?? null,
|
||||
@@ -51,6 +53,10 @@ class SeasonDto
|
||||
$this->dateStart = $dateStart;
|
||||
}
|
||||
}
|
||||
if (isset($dataList['gamesPerWeek']))
|
||||
{
|
||||
$this->gamesPerWeek = (int) $dataList['gamesPerWeek'];
|
||||
}
|
||||
if (isset($dataList['pointsPerWin']))
|
||||
{
|
||||
$this->pointsPerWin = (int) $dataList['pointsPerWin'];
|
||||
@@ -115,6 +121,10 @@ class SeasonDto
|
||||
{
|
||||
$this->active = $leagueObj->isActive();
|
||||
}
|
||||
if ($leagueObj->getGamesPerWeek() !== null)
|
||||
{
|
||||
$this->gamesPerWeek = $leagueObj->getGamesPerWeek();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,7 @@ class TeamDto
|
||||
{
|
||||
public int $id;
|
||||
public string $name;
|
||||
public string $dayOfWeekForHomeGame;
|
||||
public bool $active;
|
||||
public array $seasonDtoList;
|
||||
public array $playerDtoList;
|
||||
@@ -37,6 +38,7 @@ class TeamDto
|
||||
}
|
||||
return [
|
||||
'name' => $this->name ?? '',
|
||||
'dayOfWeekForHomeGame' => $this->dayOfWeekForHomeGame ?? '',
|
||||
'playerList' => $playerList,
|
||||
'seasonList' => $seasonList,
|
||||
'captainId' => $this->captainDto?->id,
|
||||
@@ -54,6 +56,10 @@ class TeamDto
|
||||
{
|
||||
$this->name = $dataList['name'];
|
||||
}
|
||||
if (!empty($dataList['dayOfWeekForHomeGame']))
|
||||
{
|
||||
$this->dayOfWeekForHomeGame = $dataList['dayOfWeekForHomeGame'];
|
||||
}
|
||||
if (isset($dataList['active']))
|
||||
{
|
||||
$this->active = $dataList['active'];
|
||||
@@ -98,6 +104,10 @@ class TeamDto
|
||||
{
|
||||
$this->name = $teamEntity->getName();
|
||||
}
|
||||
if ($teamEntity->getDayOfWeekForHomeGame())
|
||||
{
|
||||
$this->dayOfWeekForHomeGame = $teamEntity->getDayOfWeekForHomeGame();
|
||||
}
|
||||
if ($teamEntity->getPlayers())
|
||||
{
|
||||
foreach ($teamEntity->getPlayers() as $playerEntity)
|
||||
|
||||
+15
-12
@@ -53,6 +53,9 @@ class Season
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $pointsPerLoss = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $gamesPerWeek = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->teams = new ArrayCollection();
|
||||
@@ -102,18 +105,6 @@ class Season
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDateEnd(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->dateEnd;
|
||||
}
|
||||
|
||||
public function setDateEnd(?\DateTimeInterface $dateEnd): static
|
||||
{
|
||||
$this->dateEnd = $dateEnd;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isActive(): ?bool
|
||||
{
|
||||
return $this->active;
|
||||
@@ -287,4 +278,16 @@ class Season
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getGamesPerWeek(): ?int
|
||||
{
|
||||
return $this->gamesPerWeek;
|
||||
}
|
||||
|
||||
public function setGamesPerWeek(?int $gamesPerWeek): static
|
||||
{
|
||||
$this->gamesPerWeek = $gamesPerWeek;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ class Team
|
||||
#[ORM\ManyToOne(inversedBy: 'teams')]
|
||||
private ?Facility $homeFacility = null;
|
||||
|
||||
#[ORM\Column(length: 255, nullable: true)]
|
||||
private ?string $dayOfTheWeekForHomeGame = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->seasons = new ArrayCollection();
|
||||
@@ -199,4 +202,16 @@ class Team
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDayOfTheWeekForHomeGame(): ?string
|
||||
{
|
||||
return $this->dayOfTheWeekForHomeGame;
|
||||
}
|
||||
|
||||
public function setDayOfTheWeekForHomeGame(?string $dayOfTheWeekForHomeGame): static
|
||||
{
|
||||
$this->dayOfTheWeekForHomeGame = $dayOfTheWeekForHomeGame;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,7 +46,12 @@ class HandleCreateGameCalendarRequest
|
||||
'league' => $leagueEntity
|
||||
]);
|
||||
$this->validateEntities($leaguePresidentEntity, $leagueEntity, $leagueId, $seasonEntity);
|
||||
$teamEntityList = ($seasonEntity->getTeams())->toArray();
|
||||
$teamEntityList = ($seasonEntity->getTeams())?->toArray();
|
||||
if (empty($teamEntityList))
|
||||
{
|
||||
throw new HttpException(Response::HTTP_BAD_REQUEST, 'Temporada sin equipos.');
|
||||
}
|
||||
|
||||
$requestArray = $request->toArray();
|
||||
$numberOfTeams = count($teamEntityList);
|
||||
$numberOfWeeks = (2 * ($numberOfTeams * ($numberOfTeams - 1))) / $requestArray['gamesPerWeek'];
|
||||
@@ -175,6 +180,115 @@ class HandleCreateGameCalendarRequest
|
||||
// return $game;
|
||||
}
|
||||
|
||||
private function createArrayOfMatches(array $teamEntityList): array
|
||||
{
|
||||
$matches = [];
|
||||
$numTeams = count($teamEntityList);
|
||||
for ($i = 0; $i < $numTeams; $i++)
|
||||
{
|
||||
for ($j = $i + 1; $j < $numTeams; $j++)
|
||||
{
|
||||
// Home game
|
||||
$matches[] = [
|
||||
'home' => $teamEntityList[$i],
|
||||
'away' => $teamEntityList[$j]
|
||||
];
|
||||
// Away game
|
||||
$matches[] = [
|
||||
'home' => $teamEntityList[$j],
|
||||
'away' => $teamEntityList[$i]
|
||||
];
|
||||
}
|
||||
}
|
||||
return $matches;
|
||||
}
|
||||
|
||||
private function scheduleMatches($matches, $facilities, $teamPreferences, $gamesPerWeek, $startDate): array
|
||||
{
|
||||
$scheduledMatches = [];
|
||||
$weekNumber = 0;
|
||||
$dateIterator = new \DateTime($startDate);
|
||||
|
||||
shuffle($matches); // Randomize matches to avoid favoring certain teams
|
||||
|
||||
foreach ($matches as $match)
|
||||
{
|
||||
$homeTeam = $match['home'];
|
||||
$awayTeam = $match['away'];
|
||||
$homeFacility = $$homeTeam->getHomeFacility();
|
||||
$preferredDay = $teamPreferences[$homeTeam]['preferred_day'];
|
||||
|
||||
$scheduled = false;
|
||||
while (!$scheduled) {
|
||||
// Move to preferred day of the week
|
||||
$dateIterator->modify('next ' . $preferredDay);
|
||||
|
||||
// Check if we've started a new week
|
||||
if ($dateIterator->format('w') == 1) { // Monday
|
||||
$weekNumber++;
|
||||
}
|
||||
|
||||
// Check if we've exceeded games per week
|
||||
if (countGamesInWeek($scheduledMatches, $weekNumber) >= $gamesPerWeek) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if either team has already played this week
|
||||
if (hasTeamPlayedThisWeek($scheduledMatches, $homeTeam, $weekNumber) ||
|
||||
hasTeamPlayedThisWeek($scheduledMatches, $awayTeam, $weekNumber)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find an available time slot
|
||||
foreach ($homeFacility['schedule'] as $timeSlot) {
|
||||
if (isTimeSlotAvailable($scheduledMatches, $homeFacility['name'], $dateIterator, $timeSlot)) {
|
||||
$scheduledMatches[] = [
|
||||
'home' => $homeTeam,
|
||||
'away' => $awayTeam,
|
||||
'facility' => $homeFacility['name'],
|
||||
'date' => clone $dateIterator,
|
||||
'time' => $timeSlot,
|
||||
'week' => $weekNumber
|
||||
];
|
||||
$scheduled = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $scheduledMatches;
|
||||
}
|
||||
|
||||
private function countGamesInWeek($scheduledMatches, $weekNumber): int
|
||||
{
|
||||
return count(array_filter($scheduledMatches, function($match) use ($weekNumber)
|
||||
{
|
||||
return $match['week'] == $weekNumber;
|
||||
}));
|
||||
}
|
||||
|
||||
function hasTeamPlayedThisWeek($scheduledMatches, $team, $weekNumber): bool
|
||||
{
|
||||
return !empty(array_filter($scheduledMatches, function($match) use ($team, $weekNumber)
|
||||
{
|
||||
return ($match['home'] == $team || $match['away'] == $team) && $match['week'] == $weekNumber;
|
||||
}));
|
||||
}
|
||||
|
||||
function isTimeSlotAvailable($scheduledMatches, $facility, $date, $timeSlot): bool
|
||||
{
|
||||
foreach ($scheduledMatches as $match)
|
||||
{
|
||||
if ($match['facility'] == $facility &&
|
||||
$match['date']->format('Y-m-d') == $date->format('Y-m-d') &&
|
||||
$match['time'] == $timeSlot) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function createGameDto(Team $homeTeamEntity, Team $awayTeamEntity): GameDto
|
||||
{
|
||||
$gameDto = new GameDto();
|
||||
|
||||
Reference in New Issue
Block a user