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..d0b6ec3 100644 --- a/adventure.rb +++ b/adventure.rb @@ -4,32 +4,35 @@ 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?") -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.starting_point 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}]" + 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}" + 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 - -puts "hope you won!" diff --git a/db/migrate/001_creates_page.rb b/db/migrate/001_creates_page.rb index 8a293c0..4ff76d5 100644 --- a/db/migrate/001_creates_page.rb +++ b/db/migrate/001_creates_page.rb @@ -1,10 +1,13 @@ 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 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 1abe902..a54b904 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -1 +1,40 @@ # Cleaning Out +Page.delete_all + +# Child pages +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!") + +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) + +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?") + +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_d.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 5eb6f53..8dcb2c6 100644 --- a/models/book.rb +++ b/models/book.rb @@ -7,15 +7,14 @@ def initialize(starting_page) end def input(input_string) - if input_string.chomp == "A" - @current_page = current_page.options.first - elsif input_string.chomp == "B" - @current_page = current_page.options.last + if input_string.chomp.capitalize == "A" + @current_page = Page.where(id: current_page.options.first).first + elsif input_string.chomp.capitalize == "B" + @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..6b6f660 100644 --- a/models/page.rb +++ b/models/page.rb @@ -5,7 +5,6 @@ def self.starting_point end def options - Page.where(:parent_id => id).limit(2) + [option_a_id, option_b_id] end - end diff --git a/spec/book_spec.rb b/spec/book_spec.rb index b429112..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,18 +11,22 @@ 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") 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..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 @@ -16,14 +17,30 @@ 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 + + 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) } - 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