public function index() { $game = Game::currentGame(); if ($game && $game->isStarted()) { $this->redirect(gameGamesPath()); } else { $this->view->render(); } }
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()]); }
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()]); }
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); } }
/** * @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); }
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' => '']); } }
/** * @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); }
/** * @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; }
/** * @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'); }
/** * @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()); }
/** * @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()); }
public function end_game() { $this->layout->setLayout('ajax_layout'); $game = Game::currentGame(); $game->endedByCurrentGameUser(); $this->view->game = $game; $this->view->render(); }