src/BackendBundle/Helper/GamePointsHelper.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\BackendBundle\Helper;
  3. use DateTime;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\ORM\Query;
  7. use App\BackendBundle\Helper\MqttHelper;
  8. use App\Entity\Game;
  9. use App\Entity\GameCategory;
  10. use App\Entity\GameSchoolClassHighscore;
  11. use App\Entity\GameType;
  12. use App\Entity\SchoolClass;
  13. use App\Entity\SchoolClassGameHistory;
  14. use App\Entity\SchoolClassGameHistoryBonus;
  15. class GamePointsHelper {
  16.     private Connection $connection;
  17.     private EntityManagerInterface $em;
  18.     private MqttHelper $mqttHelper;
  19.     public function __construct(EntityManagerInterface $emMqttHelper $mqttHelper) {
  20.         $this->em $em;
  21.         $this->connection $this->em->getConnection();
  22.         $this->mqttHelper $mqttHelper;
  23.     }
  24.     public function getGameSchoolClassHighscore(SchoolClass $schoolClassGameType $gameTypeGameCategory $gameCategory) {
  25.         $highscore $this->em->getRepository(GameSchoolClassHighscore::class)->findOneBy(array(
  26.             'schoolClass' => $schoolClass'gameType' => $gameType'gameCategory' => $gameCategory
  27.         ));
  28.         if (!empty($highscore)) {
  29.             return $highscore;
  30.         }
  31.         return $this->createGameSchoolClassHighscore($schoolClass$gameType$gameCategory);
  32.     }
  33.     public function createGameSchoolClassHighscore(SchoolClass $schoolClassGameType $gameTypeGameCategory $gameCategory) {
  34.         $gameSchoolClassHighscore = new GameSchoolClassHighscore();
  35.         $gameSchoolClassHighscore->setSchoolClass($schoolClass);
  36.         $gameSchoolClassHighscore->setGameType($gameType);
  37.         $gameSchoolClassHighscore->setGameCategory($gameCategory);
  38.         $gameSchoolClassHighscore->setRoundNumber(0);
  39.         $gameSchoolClassHighscore->setPoints(0);
  40.         $gameSchoolClassHighscore->setUpdateAt(new DateTime());
  41.         $this->em->persist($gameSchoolClassHighscore);
  42.         $this->em->flush();
  43.         return $gameSchoolClassHighscore;
  44.     }
  45.     public function getClickThePicPointsForGame(Game $game) {
  46.         $gameID $game->getId();
  47.         $sql "SELECT sum(points) as points "
  48.                 "FROM game_ctp_question as ctpq "
  49.                 "WHERE game_id=$gameID AND answer_correct=1;";
  50.         $results $this->connection->query($sql)->fetchAllAssociative();
  51.         if (empty($results)) {
  52.             return 0;
  53.         }
  54.         return intval($results[0]['points']);
  55.     }
  56.     public function getClickThePicRoundsCorrectForGame(Game $game) {
  57.         $gameID $game->getId();
  58.         $sql "SELECT count(*) as rounds "
  59.                 "FROM game_ctp_question as ctpq "
  60.                 "WHERE game_id=$gameID AND answer_correct=1;";
  61.         $results $this->connection->query($sql)->fetchAllAssociative();
  62.         if (empty($results)) {
  63.             return 0;
  64.         }
  65.         return intval($results[0]['rounds']);
  66.     }
  67.     public function getQuizRoundPoints($roundNumber) {
  68.         switch ($roundNumber) {
  69.             case 1: return 5;
  70.             case 2: return 7;
  71.             case 3: return 10;
  72.             case 4: return 15;
  73.             case 5: return 30;
  74.             case 6: return 40;
  75.             case 7: return 55;
  76.             case 8: return 70;
  77.             case 9: return 150;
  78.             case 10: return 200;
  79.             case 11: return 300;
  80.             case 12: return 800;
  81.             default: return 0;
  82.         }
  83.         return 0;
  84.     }
  85.     public function getSchoolClassGamePoints(SchoolClass $schoolClassDateTime $end null) {
  86.         $pointsSum 0;
  87.         $start null;
  88.         /* @var $schoolClassGameHistoryLast SchoolClassGameHistory */
  89.         $schoolClassGameHistoryLast $this->getSchoolClassGameHistoryLast($schoolClass);
  90.         if (!empty($schoolClassGameHistoryLast)) {
  91.             $start $schoolClassGameHistoryLast->getCreatedAt();
  92.             $pointsSum += intval($schoolClassGameHistoryLast->getPoints());
  93.         }
  94.         $pointsGames $this->getSchoolClassGamePointsDate($schoolClass$start$end);
  95.         $pointsSum += $pointsGames;
  96.         return $pointsSum;
  97.     }
  98.     private function getSchoolClassGameHistoryLast(SchoolClass $schoolClass) {
  99.         $schoolClassID $schoolClass->getId();
  100.         $query "SELECT scgh "
  101.                 "FROM App\Entity\SchoolClassGameHistory scgh "
  102.                 "LEFT JOIN scgh.schoolClass sc "
  103.                 "WHERE sc.id = :scid";
  104.         /* @var $dqlQuery Query */
  105.         $dqlQuery $this->em->createQuery($query);
  106.         $dqlQuery->setParameter('scid'$schoolClassID);
  107.         $dqlQuery->setMaxResults(1);
  108.         $dqlResults $dqlQuery->execute();
  109.         if (count($dqlResults) > 0) {
  110.             return $dqlResults[0];
  111.         }
  112.         return null;
  113.     }
  114.     private function getSchoolClassGamePointsDate(SchoolClass $schoolClassDateTime $start nullDateTime $end null) {
  115.         $schoolClassID $schoolClass->getId();
  116.         $strWhere $this->getSchoolClassGamePointsWhere($start$end);
  117.         $sql "SELECT scc.school_class_id as scid, sum(g.points) as points "
  118.                 "FROM game as g "
  119.                 "LEFT JOIN game_state as gs ON g.game_state_id=gs.id "
  120.                 "LEFT JOIN school_class_client as scc ON g.school_class_client_id=scc.id "
  121.                 "WHERE scc.school_class_id=$schoolClassID AND gs.id IN (2,3,4) $strWhere"
  122.                 "GROUP BY scc.school_class_id;";
  123.         $results $this->connection->query($sql)->fetchAllAssociative();
  124.         if (count($results) > 0) {
  125.             return $results[0]['points'];
  126.         }
  127.         return 0;
  128.     }
  129.     public function getSchoolClassesGamePoints(DateTime $start nullDateTime $end null) {
  130.         $strWhere $this->getSchoolClassGamePointsWhere($start$end);
  131.         $sql "SELECT scc.school_class_id as scid, sum(g.points) as points "
  132.                 "FROM game as g "
  133.                 "LEFT JOIN game_state as gs ON g.game_state_id=gs.id "
  134.                 "LEFT JOIN school_class_client as scc ON g.school_class_client_id=scc.id "
  135.                 "LEFT JOIN school_class as sc ON scc.school_class_id=sc.id "
  136.                 "LEFT JOIN school as sch ON sc.school_id=sch.id "
  137.                 "WHERE g.game_timer_id IS NOT NULL AND gs.id IN (2,3,4) AND sch.id NOT IN (1,167) $strWhere"
  138.                 "GROUP BY scc.school_class_id "
  139.                 "ORDER BY points DESC;";
  140.         $results $this->connection->query($sql)->fetchAllAssociative();
  141.         return $results;
  142.     }
  143.     public function getSchoolClassesTrophyPoints() {
  144.         $sql "SELECT sc.id as scid, sum(gt.tropy_points) as points "
  145.                 "FROM game_trophy_progress as gtp "
  146.                 "LEFT JOIN game_trophy_progress_entry as gtpe ON gtpe.game_trophy_progress_id=gtp.id "
  147.                 "LEFT JOIN game_trophy as gt ON gtpe.game_trophy_id=gt.id "
  148.                 "LEFT JOIN school_class as sc ON gtp.school_class_id=sc.id "
  149.                 "LEFT JOIN school as sch ON sc.school_id=sch.id "
  150.                 "WHERE sch.id NOT IN (1,167) AND gtpe.finished_at IS NOT NULL "
  151.                 "GROUP BY sc.id;";
  152.         $results $this->connection->query($sql)->fetchAllAssociative();
  153.         return $results;
  154.     }
  155.     public function getSchoolClassGameHistoryBonus() {
  156.         return $this->em->getRepository(SchoolClassGameHistoryBonus::class)->findAll();
  157.     }
  158.     private function getSchoolClassGamePointsWhere(DateTime $start nullDateTime $end null) {
  159.         $strWhere '';
  160.         if (!empty($start)) {
  161.             $strStart $start->format('Y-m-d H:i:s');
  162.             $strWhere "AND g.created_at >= '$strStart' ";
  163.         }
  164.         if (empty($end)) {
  165.             $end = new DateTime();
  166.             $end->setTime(2359);
  167.             $strEnd $end->format('Y-m-d H:i:s');
  168.             $strWhere .= "AND g.created_at <= '$strEnd' ";
  169.         } else {
  170.             $strEnd $end->format('Y-m-d H:i:s');
  171.             $strWhere .= "AND g.created_at <= '$strEnd' ";
  172.         }
  173.         return $strWhere;
  174.     }
  175.     public function updateGameSchoolClassHighscore(Game $game$roundNumber$points) {
  176.         /* @var $schoolClass SchoolClass */
  177.         $schoolClass $game->getSchoolClassClient()->getSchoolClass();
  178.         $gameType $game->getGameType();
  179.         $gameCategory $game->getCategory();
  180.         $gameSchoolClassHighscore $this->em->getRepository(GameSchoolClassHighscore::class)->findOneBy(array(
  181.             'schoolClass' => $schoolClass,
  182.             'gameType' => $gameType,
  183.             'gameCategory' => $gameCategory
  184.         ));
  185.         if (empty($gameSchoolClassHighscore)) {
  186.             $gameSchoolClassHighscore $this->createGameSchoolClassHighscore($schoolClass$gameType$gameCategory);
  187.         }
  188.         if ($this->checkUpdateHighscore($gameSchoolClassHighscore$roundNumber$points)) {
  189.             $gameSchoolClassHighscore->setRoundNumber($roundNumber);
  190.             $gameSchoolClassHighscore->setPoints($points);
  191.             $this->em->persist($gameSchoolClassHighscore);
  192.             $this->em->flush();
  193.         }
  194.     }
  195.     private function checkUpdateHighscore(GameSchoolClassHighscore $gameSchoolClassHighscore$roundNumber$points) {
  196.         $currentRound $gameSchoolClassHighscore->getRoundNumber();
  197.         if ($roundNumber $currentRound) {
  198.             return false;
  199.         }
  200.         $currentPoints $gameSchoolClassHighscore->getPoints();
  201.         if ($points $currentPoints) {
  202.             return false;
  203.         }
  204.         return true;
  205.     }
  206.     public function publishSchoolClassPoints(Game $game) {
  207.         /* @var $schoolClass SchoolClass */
  208.         $schoolClass $game->getSchoolClassClient()->getSchoolClass();
  209.         $schoolClassPoints $this->getSchoolClassGamePoints($schoolClass);
  210.         $schoolClassID $schoolClass->getId();
  211.         $this->mqttHelper->sendSchoolClassPoints($schoolClassID$schoolClassPoints);
  212.     }
  213. }