From fa0e592c7dc5b41c9dcbf5337f890ed0f7f0ee0d Mon Sep 17 00:00:00 2001 From: sabih Date: Fri, 8 Jul 2022 15:30:53 +0100 Subject: [PATCH 1/6] Project Setup --- Gemfile | 4 +++- Gemfile.lock | 9 +++++++-- README.md | 2 +- app.rb | 17 +++++++++++++++-- spec/features/test_page_spec.rb | 2 ++ spec/name_spec.rb | 9 +++++++++ spec/spec_helper.rb | 3 ++- views/index.erb | 8 ++++++++ views/match.erb | 1 + views/play.erb | 18 ++++++++++++++++++ 10 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 spec/name_spec.rb create mode 100644 views/index.erb create mode 100644 views/match.erb create mode 100644 views/play.erb diff --git a/Gemfile b/Gemfile index 63415039..d6be8385 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,10 @@ source 'https://rubygems.org' -ruby '3.0.2' +ruby '3.0.0' gem 'sinatra' +gem 'webrick' +gem 'launchy' group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index b5673894..ba6d0e99 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -15,6 +15,8 @@ GEM xpath (~> 3.2) diff-lcs (1.4.4) docile (1.4.0) + launchy (2.5.0) + addressable (~> 2.7) mini_mime (1.1.1) mini_portile2 (2.6.1) mustermann (1.1.1) @@ -82,6 +84,7 @@ GEM unicode-display_width (>= 1.1.1, < 3) tilt (2.0.10) unicode-display_width (1.6.1) + webrick (1.7.0) xpath (3.2.0) nokogiri (~> 1.8) @@ -91,14 +94,16 @@ PLATFORMS DEPENDENCIES capybara + launchy rspec rubocop (= 1.20) simplecov simplecov-console sinatra + webrick RUBY VERSION - ruby 3.0.2p107 + ruby 3.0.0p0 BUNDLED WITH - 2.2.26 + 2.3.17 diff --git a/README.md b/README.md index dfbcae2f..60763ec1 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ require 'simplecov-console' SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, # Want a nice code coverage website? Uncomment this next line! - # SimpleCov::Formatter::HTMLFormatter + SimpleCov::Formatter::HTMLFormatter ]) SimpleCov.start ``` diff --git a/app.rb b/app.rb index 4abb71ec..e3e19a09 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,21 @@ require 'sinatra/base' class RockPaperScissors < Sinatra::Base get '/test' do - 'test page' + 'test page ' end - run! if app_file == $0 + # get '/' do + # erb :index + # end + # post '/action_page' do + # @player = params["name"] + # erb :play + # end + + # post '/game' do + # p params + # erb :match + # end + + run! if app_file == $0 end diff --git a/spec/features/test_page_spec.rb b/spec/features/test_page_spec.rb index 5d6e8919..72e8e2ab 100644 --- a/spec/features/test_page_spec.rb +++ b/spec/features/test_page_spec.rb @@ -1,3 +1,5 @@ + +require 'simplecov' feature 'test page' do scenario 'visit test page' do visit '/test' diff --git a/spec/name_spec.rb b/spec/name_spec.rb new file mode 100644 index 00000000..72682dd0 --- /dev/null +++ b/spec/name_spec.rb @@ -0,0 +1,9 @@ + +# feature 'Player name' do +# it 'dispaly player name' do +# visit '/' +# fill_in 'name', with: 'Sabih' +# click_button 'Submit' +# expect(page).to have_content('Sabih') +# end +# end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2177ec6a..f7117af5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,10 +2,11 @@ require 'simplecov' require 'simplecov-console' + SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, # Want a nice code coverage website? Uncomment this next line! - # SimpleCov::Formatter::HTMLFormatter + SimpleCov::Formatter::HTMLFormatter ]) SimpleCov.start diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..5c1463d0 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,8 @@ + +
+

Welcome to ROCK PAPER SCISSORS

+

Please Enter your details:

+
+
+ +
\ No newline at end of file diff --git a/views/match.erb b/views/match.erb new file mode 100644 index 00000000..6d92f44c --- /dev/null +++ b/views/match.erb @@ -0,0 +1 @@ +

Computer chooses Rock

\ No newline at end of file diff --git a/views/play.erb b/views/play.erb new file mode 100644 index 00000000..d344bbc6 --- /dev/null +++ b/views/play.erb @@ -0,0 +1,18 @@ +

+ <%=" Hello, #{@player} let's play RPS" %> +

+ +
+ + +
+ +
+ + +
+ +
+ + +
\ No newline at end of file From 79162ccdab92bf10eb647ae07e7e9a8e9ec62efb Mon Sep 17 00:00:00 2001 From: sabih Date: Fri, 8 Jul 2022 15:32:42 +0100 Subject: [PATCH 2/6] Player Name Displayed --- spec/name_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/name_spec.rb b/spec/name_spec.rb index 72682dd0..99c4daa6 100644 --- a/spec/name_spec.rb +++ b/spec/name_spec.rb @@ -1,9 +1,9 @@ -# feature 'Player name' do -# it 'dispaly player name' do -# visit '/' -# fill_in 'name', with: 'Sabih' -# click_button 'Submit' -# expect(page).to have_content('Sabih') -# end -# end \ No newline at end of file +feature 'Player name' do + it 'dispaly player name' do + visit '/' + fill_in 'name', with: 'Sabih' + click_button 'Submit' + expect(page).to have_content('Sabih') + end +end \ No newline at end of file From e516244e0c5dc21472cf810b5f409e940f711cfa Mon Sep 17 00:00:00 2001 From: sabih Date: Fri, 8 Jul 2022 17:42:34 +0100 Subject: [PATCH 3/6] Form added at Root --- app.rb | 29 +++++++++++++++++++-------- player.rb | 18 +++++++++++++++++ spec/features/name_spec.rb | 8 ++++++++ spec/name_spec.rb | 9 --------- views/display_action.erb | 1 + views/index.erb | 13 ++++++------ views/match.erb | 1 - views/{play.erb => select_action.erb} | 12 +++++------ 8 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 player.rb create mode 100644 spec/features/name_spec.rb delete mode 100644 spec/name_spec.rb create mode 100644 views/display_action.erb delete mode 100644 views/match.erb rename views/{play.erb => select_action.erb} (52%) diff --git a/app.rb b/app.rb index e3e19a09..42b68dce 100644 --- a/app.rb +++ b/app.rb @@ -1,21 +1,34 @@ require 'sinatra/base' class RockPaperScissors < Sinatra::Base + + enable :sessions get '/test' do 'test page ' end + get '/' do + erb :index + end + # post '/register-name' do + # p params + # session[:player] = Player.new(params[:player_name]) + # redirect '/select-action' + # end - # get '/' do - # erb :index + # get '/select-action' do + # @player_name = session[:player].name + # erb :select_action # end - # post '/action_page' do - # @player = params["name"] - # erb :play + + # post '/record-action' do + # session[:player].choose_action(params[:action]) + # redirect '/display-action' # end - # post '/game' do - # p params - # erb :match + # get '/display-action' do + # @player_action = session[:player].action + # erb :display_action # end + run! if app_file == $0 end diff --git a/player.rb b/player.rb new file mode 100644 index 00000000..57443269 --- /dev/null +++ b/player.rb @@ -0,0 +1,18 @@ +class Player + def initialize(name) + @name = name + @action = nil + end + + def name + return @name + end + + def choose_action(action) + @action = action + end + + def action + return @action + end + end \ No newline at end of file diff --git a/spec/features/name_spec.rb b/spec/features/name_spec.rb new file mode 100644 index 00000000..cd5fd74f --- /dev/null +++ b/spec/features/name_spec.rb @@ -0,0 +1,8 @@ + +feature 'Home Page' do + scenario 'submit button' do + visit '/' + expect(page).to have_button('Submit') + end +end + \ No newline at end of file diff --git a/spec/name_spec.rb b/spec/name_spec.rb deleted file mode 100644 index 99c4daa6..00000000 --- a/spec/name_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ - -feature 'Player name' do - it 'dispaly player name' do - visit '/' - fill_in 'name', with: 'Sabih' - click_button 'Submit' - expect(page).to have_content('Sabih') - end -end \ No newline at end of file diff --git a/views/display_action.erb b/views/display_action.erb new file mode 100644 index 00000000..22e03ba4 --- /dev/null +++ b/views/display_action.erb @@ -0,0 +1 @@ +<%= @player_action.upcase%> \ No newline at end of file diff --git a/views/index.erb b/views/index.erb index 5c1463d0..454515f0 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,8 +1,7 @@ -
-

Welcome to ROCK PAPER SCISSORS

-

Please Enter your details:

-
-
- -
\ No newline at end of file +

Welcome to ROCK PAPER SCISSORS

+
+
+
+ +
\ No newline at end of file diff --git a/views/match.erb b/views/match.erb deleted file mode 100644 index 6d92f44c..00000000 --- a/views/match.erb +++ /dev/null @@ -1 +0,0 @@ -

Computer chooses Rock

\ No newline at end of file diff --git a/views/play.erb b/views/select_action.erb similarity index 52% rename from views/play.erb rename to views/select_action.erb index d344bbc6..d42c8dcb 100644 --- a/views/play.erb +++ b/views/select_action.erb @@ -1,18 +1,18 @@ -

- <%=" Hello, #{@player} let's play RPS" %> -

+

Greetings, <%= @player_name %>!

+ +
Choose Rock, Paper or Scissors

- +
- +
- +
\ No newline at end of file From 1261dd4460bae0a8cfa623670cf08d6ed82481d3 Mon Sep 17 00:00:00 2001 From: sabih Date: Fri, 8 Jul 2022 18:10:36 +0100 Subject: [PATCH 4/6] Index page posting to action page --- app.rb | 6 +++--- spec/features/name_spec.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app.rb b/app.rb index 42b68dce..71b6a3ce 100644 --- a/app.rb +++ b/app.rb @@ -8,11 +8,11 @@ class RockPaperScissors < Sinatra::Base get '/' do erb :index end - # post '/register-name' do - # p params + post '/action_page' do + p params # session[:player] = Player.new(params[:player_name]) # redirect '/select-action' - # end + end # get '/select-action' do # @player_name = session[:player].name diff --git a/spec/features/name_spec.rb b/spec/features/name_spec.rb index cd5fd74f..a3e0db15 100644 --- a/spec/features/name_spec.rb +++ b/spec/features/name_spec.rb @@ -4,5 +4,15 @@ visit '/' expect(page).to have_button('Submit') end + scenario 'to fill the form' do + visit '/' + expect(page.fill_in 'Enter your name:', with: 'Sabih') + end + scenario 'to submit content' do + visit '/' + page.fill_in 'Enter your name:', with: 'Sabih' + page.click_on 'Submit' + expect(page).to have_content 'Sabih' + end end \ No newline at end of file From 0db426bf15fd252f030705204d071f173316fdf9 Mon Sep 17 00:00:00 2001 From: sabih Date: Fri, 8 Jul 2022 18:16:52 +0100 Subject: [PATCH 5/6] Files Updated --- app.rb | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/app.rb b/app.rb index 71b6a3ce..1cf80878 100644 --- a/app.rb +++ b/app.rb @@ -10,24 +10,9 @@ class RockPaperScissors < Sinatra::Base end post '/action_page' do p params - # session[:player] = Player.new(params[:player_name]) - # redirect '/select-action' + @name = player end - # get '/select-action' do - # @player_name = session[:player].name - # erb :select_action - # end - - # post '/record-action' do - # session[:player].choose_action(params[:action]) - # redirect '/display-action' - # end - - # get '/display-action' do - # @player_action = session[:player].action - # erb :display_action - # end run! if app_file == $0 From 766de1382296653033057f904d5a0d27dd04d2cd Mon Sep 17 00:00:00 2001 From: sabih Date: Fri, 8 Jul 2022 22:40:36 +0100 Subject: [PATCH 6/6] As far as Weapon choice --- app.rb | 21 ++++++++++++++++----- views/display_action.erb | 1 - views/{select_action.erb => play.erb} | 6 +++--- views/result.erb | 3 +++ 4 files changed, 22 insertions(+), 9 deletions(-) delete mode 100644 views/display_action.erb rename views/{select_action.erb => play.erb} (69%) create mode 100644 views/result.erb diff --git a/app.rb b/app.rb index 1cf80878..3b737f8d 100644 --- a/app.rb +++ b/app.rb @@ -9,11 +9,22 @@ class RockPaperScissors < Sinatra::Base erb :index end post '/action_page' do - p params - @name = player + session[:name] = params[:name] + redirect '/play' end - - - + get '/play' do + @player_name = session[:name] + erb :play + end + post '/record-action' do + session[:action] = params[:action] + redirect '/result' + end + get '/result' do + @selection = session[:action] + @opponent = [:Rock, :Paper, :Scissors].sample + erb :result + end + run! if app_file == $0 end diff --git a/views/display_action.erb b/views/display_action.erb deleted file mode 100644 index 22e03ba4..00000000 --- a/views/display_action.erb +++ /dev/null @@ -1 +0,0 @@ -<%= @player_action.upcase%> \ No newline at end of file diff --git a/views/select_action.erb b/views/play.erb similarity index 69% rename from views/select_action.erb rename to views/play.erb index d42c8dcb..ea20388e 100644 --- a/views/select_action.erb +++ b/views/play.erb @@ -3,16 +3,16 @@
Choose Rock, Paper or Scissors

- +
- +
- +
\ No newline at end of file diff --git a/views/result.erb b/views/result.erb new file mode 100644 index 00000000..6d0de3e5 --- /dev/null +++ b/views/result.erb @@ -0,0 +1,3 @@ +<%= "You have selected #{@selection} as your weapon"%>
+<%= "Your opponent have selected #{@opponent} as weapon"%>
+

Good Choice

\ No newline at end of file