Skip to content

Commit

Permalink
Latest matches between multiple participants, Latest matches for a pa…
Browse files Browse the repository at this point in the history
…rticipant, Upcoming matches for a participant
  • Loading branch information
lsv committed May 8, 2018
1 parent fb110ce commit 4aabd29
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
37 changes: 37 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

use SDM\Enetpulse\Configuration;
use SDM\Enetpulse\Generator;
use SDM\Enetpulse\Model\Event\Participant;

$dsn = include __DIR__ . '/dsn.php';

$config = new Configuration($dsn);
$generator = new Generator($config);
$event = $generator->getEventProvider()->getEvent(2680952);

echo "----- EVENT -----\n";
echo implode(' - ', array_map(function(Participant $part) { return $part->getName(); }, $event->getParticipants()));
echo "\n";
echo $event->getStartDate()->format('Y-m-d H:i');
echo "\n=================================";

// ordinare time - meaning in regular time
// CS: Hvis en kamp ender med 15-15 (så stoppes ordinær tid her, selvom kampen fortsætter i overtid)

Expand Down Expand Up @@ -278,3 +285,33 @@
echo $offer->getProvider()->getName() . ' - ' . $offer->getOdds() . "\n";
}
}

/**
* Upcoming matches for a participant (not started matches)
*/
echo "\n\nUpcoming matches for a participant (not started matches)\n-------------------\n\n";
// Can be either a integer or a participant object
$events = $generator->getEventProvider()->getUpcomingMatchesParticipant($event->getParticipants()[0], 5);
foreach ($events as $upcomingEvent) {
echo $upcomingEvent->getStartDate()->format('Y-m-d H:i') . ' - ' . implode(' - ', array_map(function(Participant $participant) { return $participant->getName(); }, $upcomingEvent->getParticipants())) . "\n";
}

/**
* Latest matches for a participant (finished matches)
*/
echo "\n\nLatest matches for a participant (finished matches)\n-------------------\n\n";
// Can be either a integer or a participant object
$events = $generator->getEventProvider()->getLatestMatchesParticipant($event->getParticipants()[0], 5);
foreach ($events as $latestEvent) {
echo $latestEvent->getStartDate()->format('Y-m-d H:i') . ' - ' . implode(' - ', array_map(function(Participant $participant) { return $participant->getName(); }, $latestEvent->getParticipants())) . "\n";
}

/**
* Latest matches between multiple participants (finished matches)
*/
echo "\n\nLatest matches between multiple participants (finished matches)\n-------------------\n\n";
// Can be either a array of integers or an array of participant objects
$events = $generator->getEventProvider()->getLatestMatchesBetween($event->getParticipants(), 5);
foreach ($events as $latestBetween) {
echo $latestBetween->getStartDate()->format('Y-m-d H:i') . ' - ' . implode(' - ', array_map(function(Participant $participant) { return $participant->getName(); }, $latestBetween->getParticipants())) . "\n";
}
65 changes: 65 additions & 0 deletions src/Provider/EventProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,71 @@ public function getTomorrowEvents(int $limit = 30, array $tournaments = [], arra
return $this->createEvents($qb);
}

/**
* @param int|Event\Participant $participant
* @param int $limit
*
* @return Event[]
*/
public function getUpcomingMatchesParticipant($participant, $limit = 10): array
{
$qb = $this->queryBuilder($limit);
$qb->innerJoin('e', 'event_participants', 'ep', 'e.id = ep.eventFK');
$qb->andWhere($qb->expr()->eq('ep.participantFK', ':participant'));
$qb->setParameter(':participant', $participant instanceof Event\Participant ? $participant->getId() : $participant);

$qb->andWhere($qb->expr()->eq('e.status_type', ':status'));
$qb->setParameter(':status', 'notstarted');
$qb->addOrderBy('e.startdate', 'ASC');
$qb->addOrderBy('e.id', 'DESC');

return $this->createEvents($qb);
}

/**
* @param int|Event\Participant $participant
* @param int $limit
*
* @return Event[]
*/
public function getLatestMatchesParticipant($participant, $limit = 10): array
{
$qb = $this->queryBuilder($limit);
$qb->innerJoin('e', 'event_participants', 'ep', 'e.id = ep.eventFK');
$qb->andWhere($qb->expr()->eq('ep.participantFK', ':participant'));
$qb->setParameter(':participant', $participant instanceof Event\Participant ? $participant->getId() : $participant);

$qb->andWhere($qb->expr()->eq('e.status_type', ':status'));
$qb->setParameter(':status', 'finished');
$qb->addOrderBy('e.startdate', 'DESC');
$qb->addOrderBy('e.id', 'DESC');

return $this->createEvents($qb);
}

/**
* @param int[]|Event\Participant[] $participants
* @param int $limit
*
* @return Event[]
*/
public function getLatestMatchesBetween(array $participants, $limit = 10): array
{
$qb = $this->queryBuilder($limit);
$qb->andWhere($qb->expr()->eq('e.status_type', ':status'));
$qb->setParameter(':status', 'finished');
$qb->addOrderBy('e.startdate', 'DESC');
$qb->addOrderBy('e.id', 'DESC');

foreach ($participants as $num => $participant) {
$qb->innerJoin('e', 'event_participants', 'ep_' . $num, 'e.id = ep_' . $num . '.eventFK');
$qb->andWhere($qb->expr()->eq('ep_' . $num . '.participantFK', ':participant_' . $num));
$qb->setParameter(':participant_' . $num, $participant instanceof Event\Participant ? $participant->getId() : $participant);
}

return $this->createEvents($qb);
}

public function getEvent(int $eventId): ?Event
{
$qb = $this->queryBuilder(null);
Expand Down

0 comments on commit 4aabd29

Please sign in to comment.