From 15a75bc46225973802c0a8a4c0e7bd9ebdf4d96d Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Sat, 23 Jan 2016 19:41:49 -0800 Subject: [PATCH] Add feature: Play now --- app/controllers/games_controller.rb | 7 +++++++ app/views/visitors/index.html.haml | 2 +- config/routes.rb | 2 +- spec/features/play_now_spec.rb | 28 ++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 spec/features/play_now_spec.rb diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 1baf14c..f89cbc1 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -95,6 +95,13 @@ class GamesController < ApplicationController respond_with(@game) end + def play_now + # First joinable game or create new game + @game = Game.where(status:nil).joins("LEFT OUTER JOIN players ON players.game_id = games.id").group("games.id").having("count(players) < 4").first || Game.create(title: "Game " + current_user.soft_token[0..4]) + @game.add_player_from_user current_user + redirect_to @game + end + def join @game.add_player_from_user(current_user) flash[:notice] = 'Successfully joined game...' diff --git a/app/views/visitors/index.html.haml b/app/views/visitors/index.html.haml index aca68a3..afc69dd 100644 --- a/app/views/visitors/index.html.haml +++ b/app/views/visitors/index.html.haml @@ -2,6 +2,6 @@ %h1 Thirteen
Tien Len #main-menu - = link_to 'Play Now', '#' + = link_to 'Play Now', play_now_path = link_to 'Browse Tables', '#' = link_to 'Tutorial', '#' diff --git a/config/routes.rb b/config/routes.rb index 154b01f..9502527 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,7 +13,7 @@ Rails.application.routes.draw do mount Upmin::Engine => '/admin' root to: 'visitors#index' - get '/quick-play', to: 'visitors#quick-play' + get '/quick-play', to: 'games#play_now', as: 'play_now' devise_for :users, controllers: { registrations: "users/registrations" } resources :users get '/loaderio-456644f4db1fcd1e8578be882d2fc476.txt', to: redirect('/assets/loaderio-456644f4db1fcd1e8578be882d2fc476.txt') diff --git a/spec/features/play_now_spec.rb b/spec/features/play_now_spec.rb new file mode 100644 index 0000000..dbdad75 --- /dev/null +++ b/spec/features/play_now_spec.rb @@ -0,0 +1,28 @@ +# Feature: Play now +# As a visitor +# I want to quickly join a game +# So I can play +feature 'Play now', :devise do + before { @game = FactoryGirl.create :game } + # Scenario: Visitor can play now + # Given I am not signed in + # When I click Play Now + # Then I join a game + scenario 'visitor can join a game', js: true do + visit root_path + click_link 'Play Now' + expect(page).to have_content "Test Game" + expect(current_path).to eq game_path(@game) + @player = @game.players.last + @playerComponent = page.find("#player-1") + expect(@playerComponent.text).to have_content("Guest#{@player.soft_token[0..5]}") + + # Add another player + @game.add_player_from_user FactoryGirl.create :user + #TODO: Remove race condition + visit current_path + click_button 'Start' + visit current_path + expect(page).to have_content 'Inventory' + end +end