コード例 #1
0
 public function index()
 {
     $game = Game::currentGame();
     if ($game && $game->isStarted()) {
         $this->redirect(gameGamesPath());
     } else {
         $this->view->render();
     }
 }
コード例 #2
0
 public function setUp()
 {
     parent::setUp();
     $user = User::create(['login' => 'test', 'password' => 'a']);
     $game = Game::create(['type' => 'cricket']);
     $game->addPlayer($user->getId());
     $this->gameUser = $gameUser = Arrays::first($game->game_users);
     $game->updateAttributes(['current_game_user_id' => $gameUser->getId()]);
 }
コード例 #3
0
ファイル: Game101Test.php プロジェクト: thuliumcc/dartboard
 public function setUp()
 {
     parent::setUp();
     /** @var User $user */
     $user = User::create(['login' => 'A']);
     $this->game = $game = Game::create(['type' => '101']);
     $this->gameUser = $gameUser = GameUser::create(['game_id' => $game->getId(), 'user_id' => $user->getId()]);
     $game->updateAttributes(['current_game_user_id' => $gameUser->getId()]);
 }
コード例 #4
0
ファイル: Game.php プロジェクト: thuliumcc/dartboard
 public static function restart()
 {
     $lastGame = self::lastGame();
     /** @var GameUser[] */
     $lastGameUsers = GameUser::where(['game_id' => $lastGame->getId()])->order(['score DESC', 'ordinal'])->fetchAll();
     if (!$lastGame->isFinished()) {
         $lastGame->delete();
     }
     $newGame = Game::create(['type' => $lastGame->type]);
     foreach ($lastGameUsers as $lastGameUser) {
         $newGame->addPlayer($lastGameUser->user_id);
     }
 }
コード例 #5
0
ファイル: UserTest.php プロジェクト: thuliumcc/dartboard
 /**
  * @test
  */
 public function shouldReturnMostFrequentlyHitField()
 {
     //given
     $user = User::create(['login' => 'A']);
     $game = Game::create(['type' => 'cricket']);
     $gameUser = $game->addPlayer($user->getId());
     Hit::createFor('15t', $gameUser);
     Hit::createFor('16s', $gameUser);
     Hit::createFor('16s', $gameUser);
     //when
     $field = $user->mostFrequentlyHitField();
     //then
     $this->assertEquals('16', $field);
 }
コード例 #6
0
 public function index()
 {
     $field = $this->params['field'];
     $game = Game::currentGame();
     $currentGameUser = $game->current_game_user;
     $leftShoots = $currentGameUser->getLeftShoots();
     if ($leftShoots) {
         $hit = Hit::createFor($field, $currentGameUser);
         $params = Json::encode(['game' => $game->type, 'field' => $hit->field, 'multiplier' => $hit->multiplier, 'scored' => $hit->handleScore(), 'winner' => $currentGameUser->isWinner(), 'shots_left' => $leftShoots - 1, 'shitty_round' => $currentGameUser->isShittyRound()]);
         Event::create(['name' => 'hit', 'params' => $params]);
     } else {
         Event::create(['name' => 'next_player', 'params' => '']);
     }
 }
コード例 #7
0
ファイル: CricketTest.php プロジェクト: thuliumcc/dartboard
 /**
  * @test
  */
 public function shouldCalculateIsNotShittyRound()
 {
     //given
     $user = User::create(['login' => 'A']);
     $game = Game::create(['type' => 'cricket']);
     $gameUser = GameUser::create(['game_id' => $game->id, 'user_id' => $user->getId()]);
     $game->updateAttributes(['current_game_user_id' => $gameUser->getId()]);
     Hit::createFor('15t', $gameUser);
     Hit::createFor('16t', $gameUser);
     Hit::createFor('17t', $gameUser);
     //when
     $isShittyRound = $gameUser->isShittyRound();
     //then
     $this->assertFalse($isShittyRound);
 }
コード例 #8
0
ファイル: User.php プロジェクト: thuliumcc/dartboard
 /**
  * @return int
  */
 public function maxWonStreak()
 {
     $userIds = Game::where(['finished' => true])->innerJoin('winner_game_user')->order('games.id')->select(['user_id'])->fetchAll();
     $maxWonStreak = 0;
     $tmpStreak = 0;
     foreach ($userIds as $userId) {
         if ($userId[0] == $this->id) {
             $tmpStreak++;
         } else {
             $maxWonStreak = $tmpStreak > $maxWonStreak ? $tmpStreak : $maxWonStreak;
             $tmpStreak = 0;
         }
     }
     return $maxWonStreak;
 }
コード例 #9
0
ファイル: HitTest.php プロジェクト: thuliumcc/dartboard
 /**
  * @test
  */
 public function shouldGetHitsOnlyForPlayersInCurrentGame()
 {
     //given
     $user = User::create(['login' => 'test', 'password' => 'a']);
     $game1 = Game::create();
     $game1->addPlayer($user->getId());
     /** @var GameUser $gameUser1 */
     $gameUser1 = Arrays::first($game1->game_users);
     $game2 = Game::create();
     $game2->addPlayer($user->getId());
     /** @var GameUser $gameUser2 */
     $gameUser2 = Arrays::first($game2->game_users);
     Hit::createFor('4d', $gameUser1);
     Hit::createFor('15t', $gameUser2);
     //when
     $hits = Hit::findForGame($game1);
     //then
     Assert::thatArray($hits)->onProperty('field')->containsExactly('4');
 }
コード例 #10
0
ファイル: GameUserTest.php プロジェクト: thuliumcc/dartboard
 /**
  * @test
  * @dataProvider hits
  */
 public function shouldReturnIsScoredIfFieldIsInRange($hit, $isScored)
 {
     //given
     $user = User::create(['login' => 'A']);
     $game = Game::create(['type' => 'cricket']);
     $gameUser = GameUser::create(['game_id' => $game->id, 'user_id' => $user->getId()]);
     $game->updateAttributes(['current_game_user_id' => $gameUser->getId()]);
     //when
     $hit = Hit::createFor($hit, $gameUser);
     //then
     $this->assertEquals($isScored, $hit->isScored());
 }
コード例 #11
0
ファイル: GameTest.php プロジェクト: thuliumcc/dartboard
 /**
  * @test
  */
 public function shouldRestartLastGame()
 {
     //given
     /** @var User $user1 */
     $user1 = User::create(['login' => 'A']);
     /** @var User $user2 */
     $user2 = User::create(['login' => 'B']);
     $game1 = Game::create();
     $game1->addPlayer($user1->getId());
     $game1->addPlayer($user2->getId());
     $game1->updateAttributes(['finished' => true]);
     //when
     Game::restart();
     //then
     $game = Game::findUnfinishedGame();
     $this->assertFalse($game->isFinished());
     $this->assertTrue($game->isStarted());
     Assert::thatArray(GameUser::where(['game_id' => $game->getId()])->fetchAll())->hasSize(2)->onProperty('user_id')->containsExactly($user1->getId(), $user2->getId());
 }
コード例 #12
0
 public function end_game()
 {
     $this->layout->setLayout('ajax_layout');
     $game = Game::currentGame();
     $game->endedByCurrentGameUser();
     $this->view->game = $game;
     $this->view->render();
 }