Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @link https://github.com/nicoSWD
* @author Nicolas Oelgart <nico@ifsc.stream>
*/
namespace SportClimbing\IfscCalendar\Domain\Round;
namespace SportClimbing\IfscCalendar\Domain\Athlete;

enum IFSCRoundCategory: string
enum IFSCAthleteGender: string
{
case MEN = 'men';
case WOMEN = 'women';
Expand Down
3 changes: 2 additions & 1 deletion src/Domain/Round/IFSCRound.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
namespace SportClimbing\IfscCalendar\Domain\Round;

use DateTimeImmutable;
use SportClimbing\IfscCalendar\Domain\Athlete\IFSCAthleteGender;
use SportClimbing\IfscCalendar\Domain\Discipline\IFSCDisciplines;
use SportClimbing\IfscCalendar\Domain\Stream\LiveStream;

final readonly class IFSCRound
{
/** @param IFSCRoundCategory[] $categories */
/** @param IFSCAthleteGender[] $categories */
public function __construct(
public string $name,
public array $categories,
Expand Down
5 changes: 3 additions & 2 deletions src/Domain/Round/IFSCSameStreamRoundsMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace SportClimbing\IfscCalendar\Domain\Round;

use DateTimeImmutable;
use SportClimbing\IfscCalendar\Domain\Athlete\IFSCAthleteGender;
use SportClimbing\IfscCalendar\Domain\Tags\IFSCTagsParser;

final readonly class IFSCSameStreamRoundsMerger
Expand Down Expand Up @@ -107,7 +108,7 @@ private function buildMergedName(array $rounds): string

/**
* @param IFSCRound[] $rounds
* @return IFSCRoundCategory[]
* @return IFSCAthleteGender[]
*/
private function mergedCategories(array $rounds): array
{
Expand All @@ -121,7 +122,7 @@ private function mergedCategories(array $rounds): array
}
}

usort($categories, static fn (IFSCRoundCategory $a, IFSCRoundCategory $b) => $a->value <=> $b->value);
usort($categories, static fn (IFSCAthleteGender $a, IFSCAthleteGender $b) => $a->value <=> $b->value);

return $categories;
}
Expand Down
84 changes: 74 additions & 10 deletions src/Domain/StartList/IFSCStartListGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
namespace SportClimbing\IfscCalendar\Domain\StartList;

use Closure;
use SportClimbing\IfscCalendar\Domain\Athlete\IFSCAthlete;
use SportClimbing\IfscCalendar\Domain\Athlete\IFSCAthleteException;
use SportClimbing\IfscCalendar\Domain\Athlete\IFSCAthleteService;
use SportClimbing\IfscCalendar\Domain\Ranking\IFSCAthleteRankingCalculator;

use SportClimbing\IfscCalendar\Domain\Round\IFSCRoundCategory;
use SportClimbing\IfscCalendar\Domain\Athlete\IFSCAthleteGender;

final readonly class IFSCStartListGenerator
{
private const int LIST_MAX_SIZE = 40;
private const int PER_GENDER_MAX = 20;

public function __construct(
private IFSCStartListProviderInterface $startListProvider,
Expand All @@ -37,25 +38,55 @@ public function buildStartList(int $eventId): IFSCStartListResult
$athlete = $this->athleteService->fetchAthlete($starter->athleteId);
$starter->score = $this->rankingCalculator->calculateScore($athlete);
$starter->photoUrl = $athlete->photoUrl;
$starter->instagram = $athlete->instagram;

$starter->category = match ($athlete->gender) {
'male' => IFSCRoundCategory::MEN,
'female' => IFSCRoundCategory::WOMEN,
default => null,
};
$starter->instagram = $this->normalizeInstagram($athlete->instagram);
$starter->gender = $this->getGender($athlete);

$startList[] = $starter;
}

usort($startList, $this->sortByScore());

return new IFSCStartListResult(
starters: array_slice($startList, 0, self::LIST_MAX_SIZE),
starters: $this->selectTopByGender($startList),
total: count($startList),
);
}

/**
* @param IFSCStarter[] $startList
* @return IFSCStarter[]
*/
private function selectTopByGender(array $startList): array
{
$men = $this->filterByGender($startList, IFSCAthleteGender::MEN);
$women = $this->filterByGender($startList, IFSCAthleteGender::WOMEN);

$selectedMen = $this->selectTopFromPool($men, array_slice($women, self::PER_GENDER_MAX));
$selectedWomen = $this->selectTopFromPool($women, array_slice($men, self::PER_GENDER_MAX));

$result = array_merge($selectedMen, $selectedWomen);
usort($result, $this->sortByScore());

return $result;
}

/**
* @param IFSCStarter[] $pool
* @param IFSCStarter[] $fillPool
* @return IFSCStarter[]
*/
private function selectTopFromPool(array $pool, array $fillPool): array
{
$selected = array_slice($pool, 0, self::PER_GENDER_MAX);
$shortfall = self::PER_GENDER_MAX - count($selected);

if ($shortfall > 0) {
$selected = array_merge($selected, array_slice($fillPool, 0, $shortfall));
}

return $selected;
}

private function sortByScore(): Closure
{
return static function (IFSCStarter $athlete1, IFSCStarter $athlete2): int {
Expand All @@ -69,6 +100,39 @@ private function sortByScore(): Closure
};
}

/** @return IFSCStarter[] */
public function filterByGender(array $startList, IFSCAthleteGender $gender): array
{
return array_values(array_filter($startList, fn (IFSCStarter $starter): bool => $starter->gender === $gender));
}

private function normalizeInstagram(?string $instagram): ?string
{
if ($instagram === null || $instagram === '') {
return null;
}

if (str_contains($instagram, 'instagram.com/')) {
preg_match('~instagram\.com/([^/?#]+)~', $instagram, $matches);
return $matches[1] ?? null;
}

return ltrim($instagram, '@');
}

/**
* @param IFSCAthlete $athlete
* @return IFSCAthleteGender|null
*/
private function getGender(IFSCAthlete $athlete): ?IFSCAthleteGender
{
return match ($athlete->gender) {
'male' => IFSCAthleteGender::MEN,
'female' => IFSCAthleteGender::WOMEN,
default => null,
};
}

/**
* @return IFSCStarter[]
* @throws IFSCStartListException
Expand Down
8 changes: 5 additions & 3 deletions src/Domain/StartList/IFSCStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
*/
namespace SportClimbing\IfscCalendar\Domain\StartList;

use SportClimbing\IfscCalendar\Domain\Round\IFSCRoundCategory;
use SportClimbing\IfscCalendar\Domain\Discipline\IFSCDiscipline;
use SportClimbing\IfscCalendar\Domain\Athlete\IFSCAthleteGender;

final class IFSCStarter
{
public ?IFSCRoundCategory $category = null;

/** @param IFSCDiscipline[] $disciplines */
public function __construct(
public readonly int $athleteId,
public readonly string $firstName,
public readonly string $lastName,
public readonly string $country,
public ?IFSCAthleteGender $gender = null,
public readonly array $disciplines = [],
public float $score = 0,
public ?string $photoUrl = null,
public ?string $instagram = null,
Expand Down
14 changes: 7 additions & 7 deletions src/Domain/Tags/IFSCParsedTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use SportClimbing\IfscCalendar\Domain\Discipline\IFSCDiscipline;
use SportClimbing\IfscCalendar\Domain\Event\IFSCEventTagsRegex as Tag;
use SportClimbing\IfscCalendar\Domain\Round\IFSCRoundCategory;
use SportClimbing\IfscCalendar\Domain\Athlete\IFSCAthleteGender;
use SportClimbing\IfscCalendar\Domain\Round\IFSCRoundKind;

final readonly class IFSCParsedTags
Expand All @@ -29,8 +29,8 @@
];

private const array CATEGORIES = [
IFSCRoundCategory::WOMEN->value => Tag::WOMEN,
IFSCRoundCategory::MEN->value => Tag::MEN,
IFSCAthleteGender::WOMEN->value => Tag::WOMEN,
IFSCAthleteGender::MEN->value => Tag::MEN,
];

/** @param Tag[] $tags */
Expand Down Expand Up @@ -75,20 +75,20 @@ public function getRoundKind(): ?IFSCRoundKind
return null;
}

/** @return IFSCRoundCategory[] */
/** @return IFSCAthleteGender[] */
public function getCategories(): array
{
$categories = [];

foreach (self::CATEGORIES as $name => $tag) {
if ($this->hasTag($tag)) {
$categories[] = IFSCRoundCategory::from($name);
$categories[] = IFSCAthleteGender::from($name);
}
}

if (empty($categories)) {
$categories[] = IFSCRoundCategory::WOMEN;
$categories[] = IFSCRoundCategory::MEN;
$categories[] = IFSCAthleteGender::WOMEN;
$categories[] = IFSCAthleteGender::MEN;
}

return $categories;
Expand Down
34 changes: 32 additions & 2 deletions src/Infrastructure/Calendar/ICalCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Eluceo\iCal\Domain\ValueObject\Uri;
use Exception;
use SportClimbing\IfscCalendar\Domain\Calendar\IFSCCalendarGeneratorInterface;
use SportClimbing\IfscCalendar\Domain\Discipline\IFSCDiscipline;
use SportClimbing\IfscCalendar\Domain\Event\IFSCEvent;
use SportClimbing\IfscCalendar\Domain\Round\IFSCRound;
use SportClimbing\IfscCalendar\Domain\StartList\IFSCStarter;
Expand Down Expand Up @@ -223,13 +224,42 @@ private function buildDescription(IFSCEvent $event, ?IFSCRound $round = null): s
/** @return IFSCStarter[] */
private function getFilteredStartList(IFSCEvent $event, ?IFSCRound $round): array
{
if (!$round || empty($round->categories)) {
if (!$round) {
return $event->startList;
}

if (empty($round->categories) && empty($round->disciplines->all())) {
return $event->startList;
}

return array_filter(
$event->startList,
fn (IFSCStarter $athlete): bool => $athlete->category === null || in_array($athlete->category, $round->categories, strict: true)
fn (IFSCStarter $athlete): bool =>
$this->matchesRoundCategory($athlete, $round) &&
$this->matchesRoundDiscipline($athlete, $round)
);
}

private function matchesRoundCategory(IFSCStarter $athlete, IFSCRound $round): bool
{
if (empty($round->categories)) {
return true;
}

return $athlete->gender === null || in_array($athlete->gender, $round->categories, strict: true);
}

private function matchesRoundDiscipline(IFSCStarter $athlete, IFSCRound $round): bool
{
$roundDisciplines = $round->disciplines->all();

if (empty($roundDisciplines)) {
return true;
}

return array_any(
$athlete->disciplines,
static fn (IFSCDiscipline $discipline): bool => in_array($discipline, $roundDisciplines, strict: true),
);
}

Expand Down
26 changes: 10 additions & 16 deletions src/Infrastructure/Calendar/JsonCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use SportClimbing\IfscCalendar\Domain\Discipline\IFSCDiscipline;
use SportClimbing\IfscCalendar\Domain\Event\IFSCEvent;
use SportClimbing\IfscCalendar\Domain\Round\IFSCRound;
use SportClimbing\IfscCalendar\Domain\Round\IFSCRoundCategory;
use SportClimbing\IfscCalendar\Domain\Athlete\IFSCAthleteGender;
use SportClimbing\IfscCalendar\Domain\StartList\IFSCStarter;
use Override;

Expand Down Expand Up @@ -99,9 +99,11 @@ private function formatStarters(array $starters): array
'athlete_id' => $starter->athleteId,
'first_name' => $starter->firstName,
'last_name' => $starter->lastName,
'gender' => $starter->gender,
'country' => $starter->country,
'photo_url' => $starter->photoUrl,
'instagram' => $this->normalizeInstagram($starter->instagram),
'instagram' => $starter->instagram,
'disciplines' => $this->buildStarterDisciplines($starter),
];

return array_map($format, $starters);
Expand Down Expand Up @@ -137,7 +139,7 @@ private function buildDisciplines(IFSCRound $round): array
/** @return string[] */
private function buildCategories(IFSCRound $round): array
{
return array_map(static fn (IFSCRoundCategory $category): string => $category->value, $round->categories);
return array_map(static fn (IFSCAthleteGender $category): string => $category->value, $round->categories);
}

private function countryName(string $countryCode): string
Expand All @@ -157,22 +159,14 @@ private function countryName(string $countryCode): string
return \Locale::getDisplayRegion("und-{$isoCode}", 'en');
}

private function normalizeInstagram(?string $instagram): ?string
private function formatDate(DateTimeInterface $dateTime): string
{
if ($instagram === null || $instagram === '') {
return null;
}

if (str_contains($instagram, 'instagram.com/')) {
preg_match('~instagram\.com/([^/?#]+)~', $instagram, $matches);
return $matches[1] ?? null;
}

return ltrim($instagram, '@');
return $dateTime->format(DateTimeInterface::RFC3339);
}

private function formatDate(DateTimeInterface $dateTime): string
/** @return string[] */
private function buildStarterDisciplines(IFSCStarter $starter): array
{
return $dateTime->format(DateTimeInterface::RFC3339);
return array_map(static fn (IFSCDiscipline $discipline): string => $discipline->value, $starter->disciplines);
}
}
Loading
Loading