src/BackendBundle/Helper/GamePointsHelper.php line 21

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