<commit_before><?php

namespace TomGud\FoosLeader\CoreBundle\Entity;


use Doctrine\ORM\EntityRepository;
use TomGud\FoosLeader\UserBundle\Entity\User;
use TomGud\FoosLeader\CoreBundle\Model\PlayerGoalStatistics;
use TomGud\FoosLeader\CoreBundle\Model\PlayerGameStatistics;

/**
 * ResultRepository
 */
class ResultRepository extends EntityRepository
{
	public function getLatestForUser(User $user) {
        $q = $this
            ->createQueryBuilder('r')
            ->where('(r.player1 = :user OR r.player2 = :user OR r.player3 = :user OR r.player4 = :user)
                AND (r.team1Confirmed = true AND r.team2Confirmed = true)')
            ->orderBy('r.submitted', 'DESC')
            ->setParameter('user', $user)
            ->getQuery();

		$results = $q->getResult();
        return $results;

	}
	public function getLatestForAll() {
		$q = $this
            ->createQueryBuilder('r')
            ->where('r.team1Confirmed = true and r.team2Confirmed = true')
            ->orderBy('r.submitted', 'DESC')
            ->getQuery();

		$results = $q->getResult();
        return $results;
	}

    public function getUnconfirmedResultsForUser(User $user) {
        $qb = $this
            ->createQueryBuilder('r')
            ->where('((r.player1 = :user OR r.player3 = :user) AND r.team1Confirmed = false) OR
                    ((r.player2 = :user OR r.player4 = :user) AND r.team2Confirmed = false)')
            ->orderBy('r.submitted', 'DESC')
            ->setParameter('user', $user);

        $results = $qb->getQuery()->getResult();
        return $results;
    }


    /**
     * Get goal statistics for a player
     * @param  User   $player
     * @return PlayerGoalStatistics
     */
    public function getGoalStatisticsForPlayer(User $player) {
        $qT1 = $this
            ->createQueryBuilder('r')
            ->add('select', 'SUM(r.team1Score) as scored, SUM(r.team2Score) as conceded')
            ->add('from', 'FoosLeaderCoreBundle:Result r')
            ->add('where', '((r.player1 = :user OR r.player3 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true)')
            ->setParameter('user', $player)
            ->getQuery();
        $qT2 = $this
            ->createQueryBuilder('r')
            ->add('select', 'SUM(r.team2Score) as scored, SUM(r.team1Score) as conceded')
            ->add('from', 'FoosLeaderCoreBundle:Result r')
            ->add('where', '((r.player2 = :user OR r.player4 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true)')
            ->setParameter('user', $player)
            ->getQuery();

        $resultT1 = $qT1->getResult();
        $resultT2 = $qT2->getResult();

        if ((is_array($resultT1) && count($resultT1) === 0) || (is_array($resultT2) && count($resultT2) === 0)) {
            return null;
        } else {
            $statistics = new PlayerGoalStatistics(
                $player,
                ($resultT1[0]['scored'] + $resultT2[0]['scored']),
                ($resultT1[0]['conceded'] + $resultT2[0]['conceded'])
            );
            return $statistics;
        }
    }

    public function getGameStatisticsForPlayer(User $player)
    {
        $qT1 = $this
            ->createQueryBuilder('r')
            ->add('select', 'COUNT(r.id) as won')
            ->add('from', 'FoosLeaderCoreBundle:Result r')
            ->add('where', '((r.player1 = :user OR r.player3 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true AND r.team1Score > r.team2Score) OR ' .
                '((r.player2 = :user OR r.player4 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true AND r.team2Score > r.team1Score)')
            ->setParameter('user', $player)
            ->getQuery();
        $qT2 = $this
            ->createQueryBuilder('r')
            ->add('select', 'COUNT(r.id) as games')
            ->add('from', 'FoosLeaderCoreBundle:Result r')
            ->add('where', '((r.player2 = :user OR r.player4 = :user OR r.player1 = :user OR r.player3 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true)')
            ->setParameter('user', $player)
            ->getQuery();

        $resultT1 = $qT1->getResult();
        $resultT2 = $qT2->getResult();

        if ((is_array($resultT1) && count($resultT1) === 0) || (is_array($resultT2) && count($resultT2) === 0)) {
            return null;
        } else {
            $statistics = new PlayerGameStatistics(
                $player,
                $resultT2[0]['games'],
                $resultT1[0]['won']
            );
            return $statistics;
        }
    }

    /**
     * @param Result $result
     * @return Result[]
     */
    public function getAllAfter(Result $result) {
        $qb = $this->createQueryBuilder('r')
            ->where('r.id > :id')
            ->andWhere('r.player1 IS NOT NULL OR r.player2 IS NOT NULL OR r.player3 IS NOT NULL OR r.player4 IS NOT NULL')
            ->orderBy('r.id', 'ASC')
            ->setParameter('id', $result->getId());
        $results = $qb->getQuery()->getResult();
        return $results;
    }

    /**
     * @return Result[]
     */
    public function getAvailableResults() {
        $qb = $this->createQueryBuilder('r')
            ->where('r.player1 IS NULL AND r.player2 IS NULL AND r.player3 IS NULL AND r.player4 IS NULL')
            ->andWhere('r.team1Confirmed = 0 AND r.team2Confirmed = 0')
            ->orderBy('r.submitted', 'DESC');
        return $qb->getQuery()->getResult();
    }
}<commit_msg>Fetch results between two users<commit_after><?php

namespace TomGud\FoosLeader\CoreBundle\Entity;


use Doctrine\ORM\EntityRepository;
use TomGud\FoosLeader\UserBundle\Entity\User;
use TomGud\FoosLeader\CoreBundle\Model\PlayerGoalStatistics;
use TomGud\FoosLeader\CoreBundle\Model\PlayerGameStatistics;

/**
 * ResultRepository
 */
class ResultRepository extends EntityRepository
{
	public function getLatestForUser(User $user) {
        $q = $this
            ->createQueryBuilder('r')
            ->where('(r.player1 = :user OR r.player2 = :user OR r.player3 = :user OR r.player4 = :user)
                AND (r.team1Confirmed = true AND r.team2Confirmed = true)')
            ->orderBy('r.submitted', 'DESC')
            ->setParameter('user', $user)
            ->getQuery();

		$results = $q->getResult();
        return $results;

	}
	public function getLatestForAll() {
		$q = $this
            ->createQueryBuilder('r')
            ->where('r.team1Confirmed = true and r.team2Confirmed = true')
            ->orderBy('r.submitted', 'DESC')
            ->getQuery();

		$results = $q->getResult();
        return $results;
	}

    public function getUnconfirmedResultsForUser(User $user) {
        $qb = $this
            ->createQueryBuilder('r')
            ->where('((r.player1 = :user OR r.player3 = :user) AND r.team1Confirmed = false) OR
                    ((r.player2 = :user OR r.player4 = :user) AND r.team2Confirmed = false)')
            ->orderBy('r.submitted', 'DESC')
            ->setParameter('user', $user);

        $results = $qb->getQuery()->getResult();
        return $results;
    }


    /**
     * Get goal statistics for a player
     * @param  User   $player
     * @return PlayerGoalStatistics
     */
    public function getGoalStatisticsForPlayer(User $player) {
        $qT1 = $this
            ->createQueryBuilder('r')
            ->add('select', 'SUM(r.team1Score) as scored, SUM(r.team2Score) as conceded')
            ->add('from', 'FoosLeaderCoreBundle:Result r')
            ->add('where', '((r.player1 = :user OR r.player3 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true)')
            ->setParameter('user', $player)
            ->getQuery();
        $qT2 = $this
            ->createQueryBuilder('r')
            ->add('select', 'SUM(r.team2Score) as scored, SUM(r.team1Score) as conceded')
            ->add('from', 'FoosLeaderCoreBundle:Result r')
            ->add('where', '((r.player2 = :user OR r.player4 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true)')
            ->setParameter('user', $player)
            ->getQuery();

        $resultT1 = $qT1->getResult();
        $resultT2 = $qT2->getResult();

        if ((is_array($resultT1) && count($resultT1) === 0) || (is_array($resultT2) && count($resultT2) === 0)) {
            return null;
        } else {
            $statistics = new PlayerGoalStatistics(
                $player,
                ($resultT1[0]['scored'] + $resultT2[0]['scored']),
                ($resultT1[0]['conceded'] + $resultT2[0]['conceded'])
            );
            return $statistics;
        }
    }

    public function getGameStatisticsForPlayer(User $player)
    {
        $qT1 = $this
            ->createQueryBuilder('r')
            ->add('select', 'COUNT(r.id) as won')
            ->add('from', 'FoosLeaderCoreBundle:Result r')
            ->add('where', '((r.player1 = :user OR r.player3 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true AND r.team1Score > r.team2Score) OR ' .
                '((r.player2 = :user OR r.player4 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true AND r.team2Score > r.team1Score)')
            ->setParameter('user', $player)
            ->getQuery();
        $qT2 = $this
            ->createQueryBuilder('r')
            ->add('select', 'COUNT(r.id) as games')
            ->add('from', 'FoosLeaderCoreBundle:Result r')
            ->add('where', '((r.player2 = :user OR r.player4 = :user OR r.player1 = :user OR r.player3 = :user) AND r.team1Confirmed = true AND r.team2Confirmed = true)')
            ->setParameter('user', $player)
            ->getQuery();

        $resultT1 = $qT1->getResult();
        $resultT2 = $qT2->getResult();

        if ((is_array($resultT1) && count($resultT1) === 0) || (is_array($resultT2) && count($resultT2) === 0)) {
            return null;
        } else {
            $statistics = new PlayerGameStatistics(
                $player,
                $resultT2[0]['games'],
                $resultT1[0]['won']
            );
            return $statistics;
        }
    }

    /**
     * @param Result $result
     * @return Result[]
     */
    public function getAllAfter(Result $result) {
        $qb = $this->createQueryBuilder('r')
            ->where('r.id > :id')
            ->andWhere('r.player1 IS NOT NULL OR r.player2 IS NOT NULL OR r.player3 IS NOT NULL OR r.player4 IS NOT NULL')
            ->orderBy('r.id', 'ASC')
            ->setParameter('id', $result->getId());
        $results = $qb->getQuery()->getResult();
        return $results;
    }

    /**
     * @return Result[]
     */
    public function getAvailableResults() {
        $qb = $this->createQueryBuilder('r')
            ->where('r.player1 IS NULL AND r.player2 IS NULL AND r.player3 IS NULL AND r.player4 IS NULL')
            ->andWhere('r.team1Confirmed = 0 AND r.team2Confirmed = 0')
            ->orderBy('r.submitted', 'DESC');
        return $qb->getQuery()->getResult();
    }

    public function getResultsIncludingPlayers(User $player1, User $player2)
    {
        $qb = $this->createQueryBuilder('r')
            ->where('r.player1 = :player1 AND r.player2 = :player2')
            ->orWhere('r.player1 = :player2 AND r.player1 = :player1')
            ->orWhere('(r.player1 = :player1 OR r.player3 = :player1) AND (r.player2 = :player2 OR r.player4 = :player2)')
            ->orWhere('(r.player1 = :player2 OR r.player3 = :player2) AND (r.player2 = :player1 OR r.player4 = :player1)')
            ->andWhere('r.team1Confirmed = 1 AND r.team2Confirmed = 1')
            ->setParameter('player1', $player1)
            ->setParameter('player2', $player2)
            ->orderBy('r.submitted', 'DESC');
        return $qb->getQuery()->getResult();
    }
}
{'fraction_non_alphanumeric': 0.15945349626612357, 'fraction_numerical': 0.018499660556687034, 'mean_word_length': 2.193766937669377, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 138, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4042349', 'n_tokens_mistral': 3773, 'n_tokens_neox': 3672, 'n_words': 787}