src/BackendBundle/Helper/GamePointsHelper.php line 23

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