Пример #1
0
 public function test_it_deserializes_from_json()
 {
     $factory = new BoardFactory('questions', 'tests/');
     $board = $factory->initialize();
     $this->assertEquals(3, $board->getContestants()->count());
     $this->assertEquals(2, $board->getCategories()->count());
     $this->assertInstanceOf(FinalJeopardyClue::class, $board->getFinalJeopardyClue());
     $this->assertEquals("mock_final_clue", $board->getFinalJeopardyClue()->getClue());
 }
Пример #2
0
 /**
  * @param BoardFactory $boardFactory
  *
  * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
  * @throws \React\Socket\ConnectionException
  */
 public function run(BoardFactory $boardFactory)
 {
     $emitter = new Emitter();
     $wamp = new WampConnector($emitter);
     $webSocket = new \React\Socket\Server($this->eventLoop);
     $webSocket->listen(self::SOCKET_LISTEN_PORT, '0.0.0.0');
     $board = $boardFactory->initialize();
     new IoServer(new HttpServer(new WsServer(new WampServer($wamp))), $webSocket);
     $emitter->addListener(QuestionSubscriptionEvent::class, function (QuestionSubscriptionEvent $event) use($wamp, $board) {
         $wamp->onQuestionSubscribe($board->getCategories(), $event->getSessionId());
     });
     $emitter->addListener(ContestantScoreSubscriptionEvent::class, function (ContestantScoreSubscriptionEvent $event) use($wamp, $board) {
         $wamp->onContestantScoreSubscription($board->getContestants(), $event->getSessionId());
     });
     $emitter->addListener(BuzzerStatusSubscriptionEvent::class, function (BuzzerStatusSubscriptionEvent $event) use($wamp, $board) {
         $wamp->onBuzzerStatusChange($board->getBuzzerStatus(), [], [$event->getSessionId()]);
     });
     $emitter->addListener(BuzzerResolutionEvent::class, function (BuzzerResolutionEvent $event) use($wamp) {
         //TODO Store this event
         echo 'Buzzer Resolution: ' . $event->getResolution()->getContestant()->getName() . " buzzed in ({$event->getResolution()->getTime()}ms)\n";
         $wamp->onBuzzerResolution($event->getResolution());
     });
     $emitter->addListener(QuestionAnswerEvent::class, function (QuestionAnswerEvent $event) use($wamp, $board, $emitter) {
         try {
             $questionAnswer = $event->getQuestionAnswer();
             $board->addScore($questionAnswer->getContestant(), $questionAnswer->getRealValue());
             $wamp->onQuestionAnswer($questionAnswer);
         } catch (ContestantNotFoundException $exception) {
             echo $exception->getMessage();
             // TODO handle exception
         }
         if ($questionAnswer->isCorrect()) {
             $emitter->emit(new QuestionDismissalEvent(new QuestionDismissal($questionAnswer->getCategory(), $questionAnswer->getValue())));
             return;
         }
         $emitter->emit(new BuzzerStatusChangeEvent(new BuzzerStatus(true)));
     });
     $emitter->addListener(QuestionDisplayRequestEvent::class, function (QuestionDisplayRequestEvent $event) use($wamp, $board) {
         try {
             $question = $board->getQuestionByCategoryAndValue($event->getCategoryName(), $event->getValue());
             $question->setUsed();
             $wamp->onQuestionDisplay($question, $event->getCategoryName());
         } catch (QuestionNotFoundException $exception) {
             //TODO log this somewhere.
             echo "Error occured, could not find question in category: {$event->getCategoryName()} valued at: {$event->getValue()}";
         }
     });
     $emitter->addListener(DailyDoubleBetEvent::class, function (DailyDoubleBetEvent $event) use($wamp, $board) {
         try {
             $question = $board->getQuestionByCategoryAndValue($event->getCategory(), $event->getValue());
         } catch (QuestionNotFoundException $e) {
             // TODO
         }
         $wamp->onDailyDoubleBetRecieved($question, $event->getCategory(), $event->getBet());
     });
     $emitter->addListener(QuestionDismissalEvent::class, function (QuestionDismissalEvent $event) use($wamp, $board) {
         $dismissal = $event->getDismissal();
         $wamp->onQuestionDismiss($dismissal);
     });
     $emitter->addListener(BuzzReceivedEvent::class, function (BuzzReceivedEvent $event) use($board, $emitter) {
         if (!$board->getBuzzerStatus()->isActive()) {
             // The buzzer isn't active, so there's nothing to do.
             return;
         }
         if ($board->getResolver()->isEmpty()) {
             // If this is the first buzz, then we want to resolve it after the timeout.
             $this->eventLoop->addTimer($this->buzzer_resolve_timeout, function () use($board, $emitter) {
                 $resolution = $board->resolveBuzzes();
                 $emitter->emit(new BuzzerResolutionEvent($resolution));
             });
         }
         // TODO store this event
         echo 'Buzz in (' . $event->getContestant()->getName() . '): ' . $event->getDifference() . "ms \n";
         $board->getResolver()->addBuzz($event);
     });
     $emitter->addListener(BuzzerStatusChangeEvent::class, function (BuzzerStatusChangeEvent $event) use($wamp, $board) {
         $board->setBuzzerStatus($event->getBuzzerStatus());
         $wamp->onBuzzerStatusChange($event->getBuzzerStatus());
     });
     $emitter->addListener(FinalJeopardyCategoryRequest::class, function (FinalJeopardyCategoryRequest $event) use($wamp, $board) {
         $wamp->onFinalJeopardyRequest('category', $board->getFinalJeopardyClue());
     });
     $emitter->addListener(FinalJeopardyClueRequest::class, function (FinalJeopardyClueRequest $event) use($wamp, $board) {
         $wamp->onFinalJeopardyBetCollectionRequest();
         // We're going to wait for a set time
         //TODO this doesn't work for some reason in every case, so we simply don't close the acceptance of responses.
         $this->eventLoop->addTimer($this->final_jeopardy_collection_timeout, function () use($wamp, $board) {
             if (!$board->getFinalJeopardy()->hasAllBets()) {
                 //TODO logging
                 echo "Did not recieve all bets.\n";
                 $missingbets = $board->getFinalJeopardy()->getMissingBets();
                 $missingbets = implode(', ', $missingbets);
                 echo "Require bets from: {$missingbets}\n";
             }
             $wamp->onFinalJeopardyRequest('clue', $board->getFinalJeopardyClue());
         });
     });
     $emitter->addListener(ContestantScoreChangeEvent::class, function (ContestantScoreChangeEvent $event) use($wamp, $board) {
         try {
             $board->addScore(new Contestant($event->getContestant()), $event->getScoreChange());
         } catch (ContestantNotFoundException $e) {
             //TODO
         }
         $contestant = $board->getContestants()->first(function ($key, Contestant $contestant) use($event) {
             return $contestant->getName() === $event->getContestant();
         });
         $wamp->onContestantScoreUpdate($contestant);
     });
     $emitter->addListener(FinalJeopardyBetEvent::class, function (FinalJeopardyBetEvent $event) use($wamp, $board) {
         $finalJeopardy = $board->getFinalJeopardy();
         $finalJeopardy->setBet($event->getContestant(), $event->getBet());
     });
     $emitter->addListener(FinalJeopardyAnswerRequest::class, function (FinalJeopardyAnswerRequest $event) use($wamp, $board) {
         $wamp->onFinalJeopardyAnswerCollectionRequest();
         $this->eventLoop->addTimer($this->final_jeopardy_collection_timeout, function () use($wamp, $board) {
             if (!$board->getFinalJeopardy()->hasAllAnswers()) {
                 //TODO logging
                 echo "Did not receive all final jeopardy answers!\n";
                 $missingAnswers = $board->getFinalJeopardy()->getMissingAnswers();
                 $missingAnswers = implode(', ', $missingAnswers);
                 echo "Require answers from: {$missingAnswers}\n";
             }
             $wamp->onFinalJeopardyRequest('answer', $board->getFinalJeopardyClue());
         });
     });
     $emitter->addListener(FinalJeopardyAnswerEvent::class, function (FinalJeopardyAnswerEvent $event) use($wamp, $board) {
         $finalJeopardy = $board->getFinalJeopardy();
         if ($finalJeopardy->hasAnswer($event->getContestant())) {
             //TODO logging
             echo "{$event->getContestant()} has already submitted a final answer";
             return;
         }
         $finalJeopardy->setAnswer($event->getContestant(), $event->getAnswer());
     });
     $emitter->addListener(FinalJeopardyResponseRequest::class, function (FinalJeopardyResponseRequest $event) use($wamp, $board) {
         $finalJeopardy = $board->getFinalJeopardy();
         if (!$finalJeopardy->hasAnswer($event->getContestant())) {
             //TODO logging
             $response = new FinalJeopardyQuestionResponse($event->getContestant(), 0, 'No answer, Troy');
             $wamp->onFinalJeopardyResponse($response);
             return;
         }
         $wamp->onFinalJeopardyResponse($finalJeopardy->getResponse($event->getContestant()));
     });
     $this->eventLoop->run();
 }