From 611e034a853b5c41fea1bc77c110dd235a2e19aa Mon Sep 17 00:00:00 2001 From: Ralphos Date: Thu, 13 Dec 2012 19:26:32 +0800 Subject: [PATCH 1/6] Panda Level: Preview added to Page and edited content to make winner/loser clear --- Gemfile | 2 +- Gemfile.lock | 16 ++++++++-------- adventure.rb | 20 +++++++++++++------- db/migrate/001_creates_page.rb | 1 + models/book.rb | 4 ++-- spec/book_spec.rb | 6 ++++++ spec/page_spec.rb | 5 +++++ 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Gemfile b/Gemfile index 5ac1d52..267c362 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'http://rubygems.org' gem 'rake' -gem 'rspec' +gem 'rspec', '~> 2.11.0' gem 'activesupport' gem 'activerecord' gem 'pg' diff --git a/Gemfile.lock b/Gemfile.lock index 0facad0..ef75d28 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -19,14 +19,14 @@ GEM multi_json (1.3.4) pg (0.13.2) rake (0.9.2.2) - rspec (2.10.0) - rspec-core (~> 2.10.0) - rspec-expectations (~> 2.10.0) - rspec-mocks (~> 2.10.0) - rspec-core (2.10.0) - rspec-expectations (2.10.0) + rspec (2.11.0) + rspec-core (~> 2.11.0) + rspec-expectations (~> 2.11.0) + rspec-mocks (~> 2.11.0) + rspec-core (2.11.1) + rspec-expectations (2.11.2) diff-lcs (~> 1.1.3) - rspec-mocks (2.10.1) + rspec-mocks (2.11.1) tzinfo (0.3.33) PLATFORMS @@ -37,4 +37,4 @@ DEPENDENCIES activesupport pg rake - rspec + rspec (~> 2.11.0) diff --git a/adventure.rb b/adventure.rb index d65f616..5f0bc39 100644 --- a/adventure.rb +++ b/adventure.rb @@ -5,17 +5,25 @@ require_relative 'models/page' require_relative 'models/book' -page = Page.create(starting_point: true, content: "You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?") -Page.create(conclusion: true, parent_id: page.id, content: "Go into the forest") -Page.create(conclusion: true, parent_id: page.id, content: "Walk down the road") +page = Page.create(starting_point: true, + content: "You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?", + preview: "30 gold pieces would make you rich... But that bacon sandwich smells awfully good!") +Page.create(conclusion: true, + parent_id: page.id, + content: "You've won 30 pieces of gold!", + preview: "Some preview for option_a goes here...") +Page.create(conclusion: true, + parent_id: page.id, + content: "You ate the bacon sandwich you greedy buggar!", + preview: "Some preview for option_b goes here...") book = Book.new(page) until book.complete_game? do puts book.current_page.content puts "your options: " - puts " - [#{book.current_page.options.first.content}]" - puts " - [#{book.current_page.options.last.content}]" + puts "A - #{book.current_page.options.first.preview}" + puts "B - #{book.current_page.options.last.preview}" puts "What do you want to do? Enter A or B" book.input( gets ) @@ -31,5 +39,3 @@ puts book.current_page.content - -puts "hope you won!" diff --git a/db/migrate/001_creates_page.rb b/db/migrate/001_creates_page.rb index 8a293c0..7f0a3b8 100644 --- a/db/migrate/001_creates_page.rb +++ b/db/migrate/001_creates_page.rb @@ -1,6 +1,7 @@ class CreatesPage < ActiveRecord::Migration def change create_table :pages do |t| + t.text :preview t.text :content t.integer :parent_id t.boolean :starting_point, default: false diff --git a/models/book.rb b/models/book.rb index 5eb6f53..39e1709 100644 --- a/models/book.rb +++ b/models/book.rb @@ -7,9 +7,9 @@ def initialize(starting_page) end def input(input_string) - if input_string.chomp == "A" + if input_string.chomp.capitalize == "A" @current_page = current_page.options.first - elsif input_string.chomp == "B" + elsif input_string.chomp.capitalize == "B" @current_page = current_page.options.last end end diff --git a/spec/book_spec.rb b/spec/book_spec.rb index b429112..e352135 100644 --- a/spec/book_spec.rb +++ b/spec/book_spec.rb @@ -16,11 +16,17 @@ subject.input("A") subject.current_page.should eq(option_a) end + it "should receive input and opens page" do subject.input("B") subject.current_page.should eq(option_b) end + it "should still work with lower case inputs" do + subject.input("a") + subject.current_page.should eq(option_a) + end + end describe "#complete_game?" do diff --git a/spec/page_spec.rb b/spec/page_spec.rb index 5951cdd..f32595b 100644 --- a/spec/page_spec.rb +++ b/spec/page_spec.rb @@ -16,6 +16,11 @@ Page.find(page.id).content.should eq("The fox and hound get along") end + it "has a preview" do + page = Page.create(preview: "A light appears at the end of the road") + Page.find(page.id).preview.should eq("A light appears at the end of the road") + end + context "#options" do subject {Page.create} let(:option_a) {Page.create(parent_id: subject.id) } From fd1afc1c5eb19d644ba182d2b69f74d9a34496f7 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Thu, 13 Dec 2012 19:46:23 +0800 Subject: [PATCH 2/6] Tiger level: extracted Page.creates into seed file and loaded it --- adventure.rb | 14 ++------------ db/seed.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/adventure.rb b/adventure.rb index 5f0bc39..e83ae33 100644 --- a/adventure.rb +++ b/adventure.rb @@ -4,19 +4,9 @@ require_relative 'db/setup' require_relative 'models/page' require_relative 'models/book' +require_relative 'db/seed' -page = Page.create(starting_point: true, - content: "You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?", - preview: "30 gold pieces would make you rich... But that bacon sandwich smells awfully good!") -Page.create(conclusion: true, - parent_id: page.id, - content: "You've won 30 pieces of gold!", - preview: "Some preview for option_a goes here...") -Page.create(conclusion: true, - parent_id: page.id, - content: "You ate the bacon sandwich you greedy buggar!", - preview: "Some preview for option_b goes here...") - +page = Page.starting_point book = Book.new(page) until book.complete_game? do diff --git a/db/seed.rb b/db/seed.rb index 1abe902..25d25cd 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -1 +1,19 @@ # Cleaning Out +Page.delete_all + +# Current page / starting point +page = Page.create(starting_point: true, + content: "You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?", + preview: "30 gold pieces would make you rich... But that bacon sandwich smells awfully good!") + +# Child pages +Page.create(conclusion: true, + parent_id: page.id, + content: "You've won 30 pieces of gold!", + preview: "some preview for option_a goes here...") +Page.create(conclusion: true, + parent_id: page.id, + content: "You ate the bacon sandwich you greedy buggar!", + preview: "Some preview for option_b goes here...") + + From 6fee50db3b2d4b0dcefdc5a3c5de7d3a211a9489 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Thu, 13 Dec 2012 20:34:50 +0800 Subject: [PATCH 3/6] Tiger level: user now has to make two selections to determine conclusion --- adventure.rb | 21 ++++++++++++++------- db/migrate/001_creates_page.rb | 1 + db/seed.rb | 33 +++++++++++++++++++++++++++------ spec/page_spec.rb | 11 +++++++++++ 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/adventure.rb b/adventure.rb index e83ae33..d97f46a 100644 --- a/adventure.rb +++ b/adventure.rb @@ -11,21 +11,28 @@ until book.complete_game? do puts book.current_page.content - puts "your options: " - puts "A - #{book.current_page.options.first.preview}" - puts "B - #{book.current_page.options.last.preview}" + option_a = book.current_page.options.first + option_b = book.current_page.options.last + + puts "Your options: " + puts "A - #{option_a.preview}" + puts "B - #{option_b.preview}" puts "What do you want to do? Enter A or B" book.input( gets ) end + +puts +puts book.current_page.content +puts + +message = book.current_page.winner ? "YOU WIN" : "YOU LOSE" + puts "------------------------------------------" puts "| |" puts "| |" -puts "| ADVENTURE COMPLETE |" +puts " #{message} " puts "| |" puts "| |" puts "------------------------------------------" - - -puts book.current_page.content diff --git a/db/migrate/001_creates_page.rb b/db/migrate/001_creates_page.rb index 7f0a3b8..bef4f59 100644 --- a/db/migrate/001_creates_page.rb +++ b/db/migrate/001_creates_page.rb @@ -6,6 +6,7 @@ def change t.integer :parent_id t.boolean :starting_point, default: false t.boolean :conclusion, default: false + t.boolean :winner, default: false end end end diff --git a/db/seed.rb b/db/seed.rb index 25d25cd..d2ca2f9 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -7,13 +7,34 @@ preview: "30 gold pieces would make you rich... But that bacon sandwich smells awfully good!") # Child pages -Page.create(conclusion: true, +option_a = Page.create(conclusion: false, parent_id: page.id, - content: "You've won 30 pieces of gold!", - preview: "some preview for option_a goes here...") -Page.create(conclusion: true, + content: "You're rich! Or not... You must continue on your journey to cash your gold pieces in...", + preview: "A long journey awaits, but it could be worth it...") + +option_b = Page.create(conclusion: false, parent_id: page.id, - content: "You ate the bacon sandwich you greedy buggar!", - preview: "Some preview for option_b goes here...") + content: "You ate the bacon sandwich! Now you're even hungrier! Onwards march...", + preview: "Tired and hungry, this could be just what you need!") +Page.create(conclusion: true, + parent_id: option_a.id, + content: "You're seeing things. A bear appears and eats you. YOU DEAD!", + preview: "Walk towards the light... your destiny awaits!") +Page.create(conclusion: true, + parent_id: option_a.id, + content: "It is! You stumble across Bacon Land where bacon is free and everywhere! WIN!", + preview: "You smell more bacon... could it be true!?", + winner: true) + +Page.create(conclusion: true, + parent_id: option_b.id, + content: "No it isn't! FAIL. YOU LOSE!", + preview: "You're bacon sandwich it isn't sitting well. Is that a toilet nearby?") + +Page.create(conclusion: true, + parent_id: option_b.id, + content: "Well what do ya' know?? YOU WIN!", + preview: "A sign appears saying, 'Go this way and you will win big'..", + winner: true) diff --git a/spec/page_spec.rb b/spec/page_spec.rb index f32595b..98c8967 100644 --- a/spec/page_spec.rb +++ b/spec/page_spec.rb @@ -21,6 +21,17 @@ Page.find(page.id).preview.should eq("A light appears at the end of the road") end + it "knows if it's a winning page" do + page = Page.create(winner: true) + page.winner.should be_true + end + + it "is not a winner by default" do + Page.create.winner.should eq false + # Page.create.winner.should be_false => this passes when it shouldn't. + # What's the difference with eq false and be_false? + end + context "#options" do subject {Page.create} let(:option_a) {Page.create(parent_id: subject.id) } From 1b60a0af5673a55d187afd16dfe4fd34f746b9a4 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Thu, 13 Dec 2012 22:21:44 +0800 Subject: [PATCH 4/6] Eagle level: Page does not have a parent anymore and instead stores ids of its options --- adventure.rb | 4 +-- db/migrate/001_creates_page.rb | 3 ++- db/seed.rb | 46 +++++++++++++++++----------------- models/book.rb | 5 ++-- models/page.rb | 4 +-- spec/book_spec.rb | 6 ++--- spec/page_spec.rb | 11 ++++---- 7 files changed, 40 insertions(+), 39 deletions(-) diff --git a/adventure.rb b/adventure.rb index d97f46a..d0b6ec3 100644 --- a/adventure.rb +++ b/adventure.rb @@ -11,8 +11,8 @@ until book.complete_game? do puts book.current_page.content - option_a = book.current_page.options.first - option_b = book.current_page.options.last + option_a = Page.find(book.current_page.options.first) + option_b = Page.find(book.current_page.options.last) puts "Your options: " puts "A - #{option_a.preview}" diff --git a/db/migrate/001_creates_page.rb b/db/migrate/001_creates_page.rb index bef4f59..4ff76d5 100644 --- a/db/migrate/001_creates_page.rb +++ b/db/migrate/001_creates_page.rb @@ -3,10 +3,11 @@ def change create_table :pages do |t| t.text :preview t.text :content - t.integer :parent_id t.boolean :starting_point, default: false t.boolean :conclusion, default: false t.boolean :winner, default: false + t.integer :option_a_id + t.integer :option_b_id end end end diff --git a/db/seed.rb b/db/seed.rb index d2ca2f9..dfdac89 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -1,40 +1,40 @@ # Cleaning Out Page.delete_all -# Current page / starting point -page = Page.create(starting_point: true, - content: "You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?", - preview: "30 gold pieces would make you rich... But that bacon sandwich smells awfully good!") - # Child pages -option_a = Page.create(conclusion: false, - parent_id: page.id, - content: "You're rich! Or not... You must continue on your journey to cash your gold pieces in...", - preview: "A long journey awaits, but it could be worth it...") - -option_b = Page.create(conclusion: false, - parent_id: page.id, - content: "You ate the bacon sandwich! Now you're even hungrier! Onwards march...", - preview: "Tired and hungry, this could be just what you need!") - -Page.create(conclusion: true, - parent_id: option_a.id, +option_c = Page.create(conclusion: true, content: "You're seeing things. A bear appears and eats you. YOU DEAD!", preview: "Walk towards the light... your destiny awaits!") -Page.create(conclusion: true, - parent_id: option_a.id, +option_d = Page.create(conclusion: true, content: "It is! You stumble across Bacon Land where bacon is free and everywhere! WIN!", preview: "You smell more bacon... could it be true!?", winner: true) -Page.create(conclusion: true, - parent_id: option_b.id, +option_e = Page.create(conclusion: true, content: "No it isn't! FAIL. YOU LOSE!", preview: "You're bacon sandwich it isn't sitting well. Is that a toilet nearby?") -Page.create(conclusion: true, - parent_id: option_b.id, +option_f = Page.create(conclusion: true, content: "Well what do ya' know?? YOU WIN!", preview: "A sign appears saying, 'Go this way and you will win big'..", winner: true) + +option_a = Page.create(conclusion: false, + content: "You're rich! Or not... You must continue on your journey to cash your gold pieces in...", + preview: "A long journey awaits, but it could be worth it...", + option_a_id: option_c.id, + option_b_id: option_d.id) + +option_b = Page.create(conclusion: false, + content: "You ate the bacon sandwich! Now you're even hungrier! Onwards march...", + preview: "Tired and hungry, this could be just what you need!", + option_a_id: option_e.id, + option_b_id: option_f.id) + +# Current page / starting point +page = Page.create(starting_point: true, + content: "You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?", + preview: "30 gold pieces would make you rich... But that bacon sandwich smells awfully good!", + option_a_id: option_a.id, + option_b_id: option_b.id) diff --git a/models/book.rb b/models/book.rb index 39e1709..8dcb2c6 100644 --- a/models/book.rb +++ b/models/book.rb @@ -8,14 +8,13 @@ def initialize(starting_page) def input(input_string) if input_string.chomp.capitalize == "A" - @current_page = current_page.options.first + @current_page = Page.where(id: current_page.options.first).first elsif input_string.chomp.capitalize == "B" - @current_page = current_page.options.last + @current_page = Page.where(id: current_page.options.last).first end end def complete_game? current_page.conclusion? end - end diff --git a/models/page.rb b/models/page.rb index 2b88343..78319d7 100644 --- a/models/page.rb +++ b/models/page.rb @@ -5,7 +5,7 @@ def self.starting_point end def options - Page.where(:parent_id => id).limit(2) + options = Page.find([option_a_id, option_b_id]) + option_ids = options.map { |o| o.id } end - end diff --git a/spec/book_spec.rb b/spec/book_spec.rb index e352135..c10c57a 100644 --- a/spec/book_spec.rb +++ b/spec/book_spec.rb @@ -1,7 +1,9 @@ require_relative "spec_helper" describe Book do - let!(:page) {Page.create(starting_point: true)} + let(:option_a) { Page.create } + let(:option_b) { Page.create } + let!(:page) {Page.create(starting_point: true, option_a_id: option_a.id, option_b_id: option_b.id)} subject { Book.new(page) } it "should have a page" do @@ -9,8 +11,6 @@ end describe "#input" do - let!(:option_a) { Page.create(parent_id: page.id)} - let!(:option_b) { Page.create(parent_id: page.id)} it "should receive input and opens page" do subject.input("A") diff --git a/spec/page_spec.rb b/spec/page_spec.rb index 98c8967..501b7a9 100644 --- a/spec/page_spec.rb +++ b/spec/page_spec.rb @@ -6,6 +6,7 @@ Page.delete_all end + it "should know if it's at the end of the road" do page = Page.create(conclusion: true) page.conclusion?.should be_true @@ -33,13 +34,13 @@ end context "#options" do - subject {Page.create} - let(:option_a) {Page.create(parent_id: subject.id) } - let(:option_b) {Page.create(parent_id: subject.id) } - let(:option_c) {Page.create(parent_id: subject.id) } + + let(:option_a) { Page.create } + let(:option_b) { Page.create } + let(:page) { Page.create(option_a_id: option_a.id, option_b_id: option_b.id) } it "should have options for the next pages" do - subject.options.should eq([option_a, option_b]) + expect(page.options).to eq [option_a.id, option_b.id] end end From 567ed3e62a31afc1a316d8dbaf22916e12ef8440 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Thu, 13 Dec 2012 22:23:30 +0800 Subject: [PATCH 5/6] Refactored options method to simply return array of ids.. duh --- models/page.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/models/page.rb b/models/page.rb index 78319d7..6b6f660 100644 --- a/models/page.rb +++ b/models/page.rb @@ -5,7 +5,6 @@ def self.starting_point end def options - options = Page.find([option_a_id, option_b_id]) - option_ids = options.map { |o| o.id } + [option_a_id, option_b_id] end end From 550a55a46f6c719a4e3edd8eadc5cdf17ab00a3d Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sat, 15 Dec 2012 19:19:44 +0800 Subject: [PATCH 6/6] Eagle level: Either path now shares option_d on the 3rd tier --- db/seed.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/seed.rb b/db/seed.rb index dfdac89..a54b904 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -30,7 +30,7 @@ content: "You ate the bacon sandwich! Now you're even hungrier! Onwards march...", preview: "Tired and hungry, this could be just what you need!", option_a_id: option_e.id, - option_b_id: option_f.id) + option_b_id: option_d.id) # Current page / starting point page = Page.create(starting_point: true,