Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Panda, Tiger and now Eagle #3

Closed
wants to merge 3 commits into from

2 participants

@jwo
Owner
jwo commented

Looks good.

@johnnygoodman johnnygoodman Eagle project. The flow always merges to a winner on the third level.…
… Page and Book classes replaced with a single DB class as I used the class as a...db.
69b746e
@jwo jwo commented on the diff
((3 lines not shown))
+require 'sinatra'
+require 'shotgun'
+require_relative 'db/seed'
+require_relative 'models/db'
+
+enable :sessions
+
+get '/adventure' do
+ @page = DB.starting_point
+ erb :adventure
+end
+
+post '/adventure' do
+ @page = DB.page_with_id(params[:choice_id].to_i)
+
+ if @page.conclusion == true
@jwo Owner
jwo added a note

Since this is ActiveRecord, you could probably simplify to @page.conclusion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo jwo commented on the diff
((1 lines not shown))
+require 'rubygems'
+require 'bundler/setup'
+require 'sinatra'
+require 'shotgun'
+require_relative 'db/seed'
+require_relative 'models/db'
+
+enable :sessions
+
+get '/adventure' do
+ @page = DB.starting_point
+ erb :adventure
+end
+
+post '/adventure' do
+ @page = DB.page_with_id(params[:choice_id].to_i)
@jwo Owner
jwo added a note

If I don't select anything, the params[:choice_id] is null, and .to_i becomes 0.... so you get ugliness. Ideally you'd either be OK with 0, or redirect back to tell them to do it right! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo jwo commented on the diff
db/seed.rb
((77 lines not shown))
+ conclusion: false)
+
+DB.add_page(choice_2d)
+
+choice_3 = DB.new(starting_point: false,
+ preview_a: "",
+ preview_b: "",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "You find the maiden, take back your sandwich and ride off into the sunset.",
+ winner: true,
+ conclusion: true)
+
+DB.add_page(choice_3)
+
+start_page.option_a_id = choice_1a.id
@jwo Owner
jwo added a note

Since this is an eagle project.... I probably would have a method on start_page something like:

def add_choice!(choice)
  options << choice
end

Or something... Seems like MAYBE there's too much being known about the inner workings of the Choice class outside of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo
Owner

Great job here! I enjoyed your adventure in web form!

@jwo jwo closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 28, 2012
  1. @johnnygoodman
  2. @johnnygoodman
Commits on Jun 10, 2012
  1. @johnnygoodman

    Eagle project. The flow always merges to a winner on the third level.…

    johnnygoodman authored
    … Page and Book classes replaced with a single DB class as I used the class as a...db.
This page is out of date. Refresh to see the latest.
View
5 Gemfile
@@ -4,3 +4,8 @@ gem 'rake'
gem 'activesupport'
gem 'sinatra'
gem 'sinatra-contrib'
+gem 'shotgun'
+gem 'heroku'
+gem 'sinatra-activerecord'
+gem 'pg'
+gem 'rspec'
View
48 Gemfile.lock
@@ -1,23 +1,65 @@
GEM
remote: http://rubygems.org/
specs:
+ activemodel (3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ activerecord (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
+ addressable (2.2.8)
+ arel (3.0.2)
backports (2.5.3)
+ builder (3.0.0)
+ diff-lcs (1.1.3)
eventmachine (0.12.10)
+ excon (0.13.4)
+ heroku (2.26.2)
+ heroku-api (~> 0.2.1)
+ launchy (>= 0.3.2)
+ netrc (~> 0.7.2)
+ rest-client (~> 1.6.1)
+ rubyzip
+ heroku-api (0.2.1)
+ excon (~> 0.13.3)
i18n (0.6.0)
+ launchy (2.1.0)
+ addressable (~> 2.2.6)
+ mime-types (1.18)
multi_json (1.3.5)
+ netrc (0.7.2)
+ pg (0.13.2)
rack (1.4.1)
rack-protection (1.2.0)
rack
rack-test (0.6.1)
rack (>= 1.0)
rake (0.9.2.2)
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
+ 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)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.10.1)
+ rubyzip (0.9.8)
+ shotgun (0.9)
+ rack (>= 1.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
+ sinatra-activerecord (1.0.1)
+ activerecord (~> 3.0)
+ sinatra (~> 1.0)
sinatra-contrib (1.3.1)
backports (>= 2.0)
eventmachine
@@ -26,12 +68,18 @@ GEM
sinatra (~> 1.3.0)
tilt (~> 1.3)
tilt (1.3.3)
+ tzinfo (0.3.33)
PLATFORMS
ruby
DEPENDENCIES
activesupport
+ heroku
+ pg
rake
+ rspec
+ shotgun
sinatra
+ sinatra-activerecord
sinatra-contrib
View
42 app.rb
@@ -0,0 +1,42 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'sinatra'
+require 'shotgun'
+require_relative 'db/seed'
+require_relative 'models/db'
+
+enable :sessions
+
+get '/adventure' do
+ @page = DB.starting_point
+ erb :adventure
+end
+
+post '/adventure' do
+ @page = DB.page_with_id(params[:choice_id].to_i)
@jwo Owner
jwo added a note

If I don't select anything, the params[:choice_id] is null, and .to_i becomes 0.... so you get ugliness. Ideally you'd either be OK with 0, or redirect back to tell them to do it right! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ if @page.conclusion == true
@jwo Owner
jwo added a note

Since this is ActiveRecord, you could probably simplify to @page.conclusion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ erb :adventure_complete
+ else
+ erb :adventure
+ end
+end
+
+post '/number' do
+ @number_of_randoms = session[:number_of_randoms] || 0
+ @number_of_randoms += 1
+ session[:number_of_randoms] = @number_of_randoms
+ number_as_string = params.fetch('number').to_i
+ @the_number = rand(number_as_string)
+ erb :number
+end
+
+get '/' do
+ about_me = [{url: 'http://www.cpap.com/aboutUsPerson.php?AboutUsID=6', anchor: 'CPAP.com About Us Page'},
+ {url: 'http://www.linkedin.com/profile/view?id=6892236', anchor: 'Linked In Profile'},
+ {url: 'https://github.com/johnnygoodman', anchor: 'Github Profile'}]
+ selected = about_me.shuffle.take(1).first
+ @about_me_url = selected.fetch(:url)
+ @about_me_anchor = selected.fetch(:anchor)
+ erb :about
+end
View
2  config.ru
@@ -1,2 +1,2 @@
-require "./theweb"
+require "./app.rb"
run Sinatra::Application
View
18 db/migrate/20120528145204_create_pages.rb
@@ -0,0 +1,18 @@
+class CreatePages < ActiveRecord::Migration
+ def up
+ create_table :pages do |t|
+ t.text :content
+ t.text :preview_a
+ t.text :preview_b
+ t.integer :option_a_id
+ t.integer :option_b_id
+ t.boolean :starting_point, default: false
+ t.boolean :winner, default: nil
+ t.boolean :conclusion, default: false
+ end
+ end
+
+ def down
+ drop table :pages
+ end
+end
View
107 db/seed.rb
@@ -0,0 +1,107 @@
+#require_relative '../spec/spec_helper.rb'
+require_relative '../models/db.rb'
+
+start_page = DB.new(starting_point: true,
+ preview_a: "30 gold pieces",
+ preview_b: "Bacon Sandwich",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "You wake up on a road. It's foggy and damp. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?",
+ winner: false,
+ conclusion: false)
+
+DB.add_page(start_page)
+
+choice_1a = DB.new(starting_point: false,
+ preview_a: "Plunge into the woods",
+ preview_b: "Run down the road toward to bridge",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "You put the 30 gold pieces in your pocket and toss the bacon sandwich on the ground. A fair maiden emerges from the woods, grabs your sandwich and takes off. You've got to stop her! Where will you go?",
+ winner: false,
+ conclusion: false)
+
+DB.add_page(choice_1a)
+
+choice_1b = DB.new(starting_point: false,
+ preview_a: "I think she's heading for the bubbling brook, after her!",
+ preview_b: "Climb the nearest tree and see if you can spot her.",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "As you toss the gold on the ground and begin to eat your bacon sandwich, a fair maiden emerges from the woods and says 'm'lord, can I have the rest of that sandwich?' Before you answer, she grabs the sandwich and takes off. She must be stopped! Where will you go?",
+ winner: false,
+ conclusion: false)
+
+DB.add_page(choice_1b)
+
+choice_2a = DB.new(starting_point: false,
+ preview_a: "Well, if the woods didn't work, maybe she's hiding under that bridge down the road.",
+ preview_b: "She is seriously hiding under the bridge down the road. I mean, who checks the woods first? Really. Just go look under the bridge.",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "You plunge into the woods. There are stickers everywhere. This is not worth a chunky bacon sandwich. Where do you go next?",
+ winner: false,
+ conclusion: false)
+
+DB.add_page(choice_2a)
+
+choice_2b = DB.new(starting_point: false,
+ preview_a: "You see the maiden! She's under the bridge. Pounce!",
+ preview_b: "Jump in the warp machine, see a command prompt: <br> $>gem install find_maiden <br> $>irb <br> $>require 'find_maiden' <br> $>m = Maiden.new <br> $>m.throw_net_over_and_get_back_sandwhich",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "You run down the road and come to a bridge.",
+ winner: false,
+ conclusion: false)
+
+DB.add_page(choice_2b)
+
+choice_2c = DB.new(starting_point: false,
+ preview_a: "Blink twice using your Google glasses to issue the 'Get GPS coordinates of Fair Maiden from your circling UAV' command.",
+ preview_b: "Charge the bridge.",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "You hear a bubbling brook and follow the sound down the road. You can see a bridge ahead and bread crumbs lay at your feet. What do you do?",
+ winner: false,
+ conclusion: false)
+
+DB.add_page(choice_2c)
+
+choice_2d = DB.new(starting_point: false,
+ preview_a: "Run quickly to the bridge.",
+ preview_b: "Run at Ent speed for the bridge.",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "The tree you try to climb is one of those Ents from Lord of the Rings. He ain't happy. After 20 minutes of tense negotiations, he tells you to run down to the bridge.",
+ winner: false,
+ conclusion: false)
+
+DB.add_page(choice_2d)
+
+choice_3 = DB.new(starting_point: false,
+ preview_a: "",
+ preview_b: "",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "You find the maiden, take back your sandwich and ride off into the sunset.",
+ winner: true,
+ conclusion: true)
+
+DB.add_page(choice_3)
+
+start_page.option_a_id = choice_1a.id
@jwo Owner
jwo added a note

Since this is an eagle project.... I probably would have a method on start_page something like:

def add_choice!(choice)
  options << choice
end

Or something... Seems like MAYBE there's too much being known about the inner workings of the Choice class outside of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+start_page.option_b_id = choice_1b.id
+
+choice_1a.option_a_id = choice_2a.id
+choice_1a.option_b_id = choice_2b.id
+choice_1b.option_a_id = choice_2c.id
+choice_1b.option_b_id = choice_2d.id
+
+choice_2a.option_a_id = choice_3.id
+choice_2a.option_b_id = choice_3.id
+choice_2b.option_a_id = choice_3.id
+choice_2b.option_b_id = choice_3.id
+choice_2c.option_a_id = choice_3.id
+choice_2c.option_b_id = choice_3.id
+choice_2d.option_a_id = choice_3.id
+choice_2d.option_b_id = choice_3.id
View
15 db/setup.rb
@@ -0,0 +1,15 @@
+require 'pg'
+require 'active_record'
+require 'yaml'
+
+connection_details = YAML::load(File.open('../config/database.yml'))
+
+# Setup out connection details
+ActiveRecord::Base.establish_connection(connection_details.merge({'database'=> 'postgres', 'schema_search_path'=> 'public'}))
+# create the 'episode5' database
+ActiveRecord::Base.connection.drop_database (connection_details.fetch('database')) rescue nil
+ActiveRecord::Base.connection.create_database(connection_details.fetch('database')) rescue nil
+# connect to it
+ActiveRecord::Base.establish_connection(connection_details)
+# Migrate all the things
+ActiveRecord::Migrator.migrate("db/migrate/")
View
44 models/db.rb
@@ -0,0 +1,44 @@
+class DB
+ attr_accessor :starting_point, :preview_a, :preview_b,
+ :option_a_id, :option_b_id,
+ :content, :winner, :conclusion, :id
+
+ @@id = 0
+ @@pages = []
+
+ def initialize(hash)
+ @@id = @@id + 1
+ @id = @@id
+ @option_a_id = hash.fetch(:option_a_id)
+ @option_b_id = hash.fetch(:option_b_id)
+ @starting_point = hash.fetch(:starting_point)
+ @preview_a = hash.fetch(:preview_a)
+ @preview_b = hash.fetch(:preview_b)
+ @content = hash.fetch(:content)
+ @winner = hash.fetch(:winner)
+ @conclusion = hash.fetch(:conclusion)
+ end
+
+ def self.add_page(page)
+ @@pages << page
+ end
+
+ def self.pages
+ @@pages
+ end
+
+ def self.starting_point
+ pages.each do |page|
+ return page if page.starting_point == true
+ end
+ end
+
+ def self.page_with_id(id)
+ pages.each do |page|
+ return page if page.id == id
+ end
+ end
+
+end
+
+
View
23 rakefile.rb
@@ -1,13 +1,16 @@
require "rubygems"
require "bundler/setup"
+require './app'
+require 'sinatra/activerecord/rake'
-require 'rspec/core/rake_task'
-
-desc 'Default: run specs.'
-task :default => :spec
-
-desc "Run specs"
-RSpec::Core::RakeTask.new do |t|
- t.pattern = "**/*_spec.rb" # don't need this, it's default.
- # Put spec opts in a file named .rspec in root
-end
+#
+# require 'rspec/core/rake_task'
+#
+# desc 'Default: run specs.'
+# task :default => :spec
+#
+# desc "Run specs"
+# RSpec::Core::RakeTask.new do |t|
+# t.pattern = "**/*_spec.rb" # don't need this, it's default.
+# # Put spec opts in a file named .rspec in root
+# end
View
27 spec/db_spec.rb
@@ -0,0 +1,27 @@
+require_relative "spec_helper"
+
+describe DB do
+ subject { DB.new(starting_point: true,
+ preview_a: "30 gold pieces",
+ preview_b: "Bacon Sandwich",
+ option_a_id: 0,
+ option_b_id: 0,
+ content: "You wake up on a road. It's foggy and damp. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?",
+ winner: false,
+ conclusion: false) }
+
+ it "should have a preview a" do
+ subject.preview_a.should eq("30 gold pieces")
+ end
+
+ it "should not be a winner" do
+ subject.winner.should eq(false)
+ end
+
+ it "should not be the winner" do
+ subject.conclusion.should eq(false)
+ end
+
+
+end
+
View
5 spec/spec_helper.rb
@@ -0,0 +1,5 @@
+require "rspec"
+require 'bundler/setup'
+require_relative '../db/seed'
+require_relative "../models/db"
+
View
18 theweb.rb
@@ -1,18 +0,0 @@
-require 'rubygems'
-require 'bundler/setup'
-require 'sinatra'
-require 'sinatra/reloader'
-enable :sessions
-
-get '/' do
- erb :dashboard
-end
-
-post '/number' do
- @number_of_randoms = session[:number_of_randoms] || 0
- @number_of_randoms += 1
- session[:number_of_randoms] = @number_of_randoms
- number_as_string = params.fetch('number').to_i
- @the_number = rand(number_as_string)
- erb :number
-end
View
4 views/about.erb
@@ -0,0 +1,4 @@
+<h1>About Johnny</h1>
+
+<br><br>
+One random thing about me: <a href="<%= @about_me_url %>"><%= @about_me_anchor %></a>
View
12 views/adventure.erb
@@ -0,0 +1,12 @@
+<h1>Adventure</h1>
+<br><br>
+<%= @page.content %>
+<br><br>
+<form method="POST" action="/adventure">
+ <label for="choice">Choose the next move:</label>
+ <input type="radio" value="<%= @page.option_a_id %>" name="choice_id"/> <%= @page.preview_a %>
+ <br>
+ <input type="radio" value="<%= @page.option_b_id %>" name="choice_id"/> <%= @page.preview_b %>
+ <br><br>
+ <input type="submit" class="btn btn-large btn-primary" value="Submit"/>
+</form>
View
3  views/adventure_complete.erb
@@ -0,0 +1,3 @@
+<h1>Adventure Complete</h1>
+<br>
+<%= @page.content %>
View
6 views/dashboard.erb
@@ -1,6 +0,0 @@
-<h1>OH HAI THERE</h1>
-<form method="POST" action="/number">
- <label for="number">Max number for Randomitude</label>
- <input type="text" id="number" name="number"/>
- <input type="submit" class="btn btn-large btn-primary" value="View Number"/>
-</form>
View
11 views/layout.erb
@@ -1,10 +1,8 @@
-
-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
- <title>Bootstrap, from Twitter</title>
+ <title>Adventure</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
@@ -42,12 +40,11 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
- <a class="brand" href="/">Randomizer</a>
<div class="nav-collapse">
<ul class="nav">
- <li class="active"><a href="#">Home</a></li>
- <li><a href="#about">About</a></li>
- <li><a href="#contact">Contact</a></li>
+ <li class="active"><a href="/">Home</a></li>
+ <li><a href="/">About</a></li>
+ <li><a href="/adventure">Adventure</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
Something went wrong with that request. Please try again.