-
Couldn't load subscription status.
- Fork 17
Panda, Tiger, and Eagle #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7b3ce24
31c1707
50fbe65
7efffb7
31cd9ef
def1990
f62050b
4aec166
c72cd89
83b5322
9c3ab6e
e31e80c
df5c2d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| keep the config directory |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class CreatesPage < ActiveRecord::Migration | ||
| def change | ||
| create_table :pages do |t| | ||
| t.text :content | ||
| t.text :preview | ||
| t.integer :parent_id | ||
| t.integer :option_a_id | ||
| t.integer :option_b_id | ||
| t.boolean :starting_point, default: false | ||
| t.boolean :conclusion, default: false | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Page.delete_all | ||
|
|
||
| campfire = Page.create(conclusion: true, preview: "Approach the campfire", | ||
| content: "Bandits steal your bag. You lose everything!!") | ||
| sit = Page.create(conclusion: true, preview: "Sit down to eat your sandwich", | ||
| content: "You are attacked by a hungry bear who smells bacon. You lose.") | ||
| walk = Page.create(conclusion: true, preview: "Keep walking", | ||
| content: "You made it back to the inn safely. You win 100 gold pieces!!") | ||
|
|
||
| forest = Page.create(option_a_id: sit.id, option_b_id: campfire.id, | ||
| preview: "Go into the forest", | ||
| content: "You see a campfire in the distance.") | ||
| road = Page.create(option_a_id: sit.id, option_b_id: walk.id, | ||
| preview: "Walk down the road", | ||
| content: "You are hungry.") | ||
|
|
||
| start = Page.create(starting_point: true, option_a_id: forest.id, option_b_id: road.id, | ||
| preview: "Welcome, adventurer.", | ||
| content: "You wake up on a road. It's foggy and damp. Your bag conatains 30 gold pieces and a bacon sandwich.") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| require 'pg' | ||
| require 'active_record' | ||
|
|
||
| if ENV['DATABASE_URL'].nil? | ||
| 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 'tv' 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/") | ||
| else | ||
|
|
||
| db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb') | ||
|
|
||
| ActiveRecord::Base.establish_connection( | ||
| :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme, | ||
| :host => db.host, | ||
| :username => db.user, | ||
| :password => db.password, | ||
| :database => db.path[1..-1], | ||
| :encoding => 'utf8' | ||
| ) | ||
| ActiveRecord::Base.connection.drop_database (db.path[1..-1]) rescue nil | ||
| ActiveRecord::Base.connection.create_database(db.path[1..-1]) rescue nil | ||
| # connect to it | ||
| ActiveRecord::Base.establish_connection( | ||
| :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme, | ||
| :host => db.host, | ||
| :username => db.user, | ||
| :password => db.password, | ||
| :database => db.path[1..-1], | ||
| :encoding => 'utf8' | ||
| ) | ||
| # Migrate all the things | ||
| ActiveRecord::Migrator.migrate("db/migrate/") | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Book | ||
|
|
||
| attr_reader :current_page | ||
|
|
||
| def initialize(starting_page) | ||
| @current_page = 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 | ||
| end | ||
| end | ||
|
|
||
| def complete_game? | ||
| current_page.conclusion? | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class Page < ActiveRecord::Base | ||
|
|
||
| attr_reader :options | ||
|
|
||
| def self.starting_point | ||
| Page.where(starting_point: true).first | ||
| end | ||
|
|
||
| def options | ||
| options = Page.find(option_a_id, option_b_id) | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,67 @@ | |
| require 'bundler/setup' | ||
| require 'sinatra' | ||
| require 'sinatra/reloader' | ||
|
|
||
| #ENV.to_hash.each do |key, value| | ||
| # puts("#{key}\t#{value}") | ||
| #end | ||
| require_relative 'db/setup' | ||
| require_relative 'models/page' | ||
| require_relative 'models/book' | ||
| require "./db/seed" | ||
|
|
||
| enable :sessions | ||
|
|
||
| get '/' do | ||
| erb :dashboard | ||
| @about_text = [] | ||
| @about_text << "I've been learning Ruby for about five months." | ||
| @about_text << "I live in Atlanta, Georgia." | ||
| @about_text << "I like reading, knitting, and gardening." | ||
| @about_text << "I went to Clemson University. Go Tigers!!" | ||
| session[:random_text] = @about_text | ||
| @random_text = session[:random_text].sample | ||
| erb :about | ||
| end | ||
|
|
||
| get '/about' do | ||
| @random_text = session[:random_text].sample | ||
| erb :about | ||
| end | ||
|
|
||
| get '/random' do | ||
| erb :random | ||
| end | ||
|
|
||
| get '/adventure' do | ||
| session[:book] = book = Book.new(Page.starting_point) | ||
| @content = session[:book].current_page.content | ||
| @option_1 = session[:book].current_page.options.first.preview | ||
| @option_2 = session[:book].current_page.options.last.preview | ||
| erb :adventure | ||
| end | ||
|
|
||
| post '/option_1' do | ||
| session[:book].input("A") | ||
| @content = session[:book].current_page.content | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like how you have the option_1 and option_2... however, since the options are the same, I'd recommend moving the rest of the method out into another method. could be (maybe?) def process_option(choice)
@content = session[:book].current_page.content
if session[:book].complete_game?
erb :conclusion
else
@option_1 = session[:book].current_page.options.first.preview
@option_2 = session[:book].current_page.options.last.preview
erb :adventure
end
endAlso, you could do the following to only have one endpoint. The choice becomes a param. post '/options/:choice' do
session[:book].input(params[:choice])
# existing stuff
endThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. I'll work on reducing the repeated lines since each page has the same basic logic. |
||
| if session[:book].complete_game? | ||
| erb :conclusion | ||
| else | ||
| @option_1 = session[:book].current_page.options.first.preview | ||
| @option_2 = session[:book].current_page.options.last.preview | ||
| erb :adventure | ||
| end | ||
| end | ||
|
|
||
| post '/option_2' do | ||
| session[:book].input("B") | ||
| @content = session[:book].current_page.content | ||
| if session[:book].complete_game? | ||
| erb :conclusion | ||
| else | ||
| @option_1 = session[:book].current_page.options.first.preview | ||
| @option_2 = session[:book].current_page.options.last.preview | ||
| erb :adventure | ||
| end | ||
| end | ||
|
|
||
| post '/number' do | ||
|
|
@@ -16,3 +73,4 @@ | |
| @the_number = rand(number_as_string) | ||
| erb :number | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <h1>About me:</h1> | ||
| <h2>Hi! I'm Melissa, and <%= @random_text %></h2> | ||
| <break> | ||
| <br> | ||
| <form method="GET" action="/adventure"> | ||
| <label for="adventure"><h2>Are you ready for an adventure?</h2></label> | ||
| <input type="submit" class="btn btn-large btn-primary" value="Yes!"/> | ||
| </form> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <h1>Your story so far:</h1> | ||
|
|
||
| <h2><%= @content %></h2> | ||
| <br> | ||
| <h2>What do you want to do next?</h2> | ||
|
|
||
| <form method="POST" action="/option_1"> | ||
| <input type="submit" class="btn btn-large btn-primary" value="<%= @option_1 %>"/> | ||
| </form> | ||
|
|
||
| <form method="POST" action="/option_2"> | ||
| <input type="submit" class="btn btn-large btn-primary" value="<%= @option_2 %>"/> | ||
| </form> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <h1>Your story so far:</h1> | ||
|
|
||
| <h2><%= @content %></h2> | ||
| <br> | ||
| <h2>ADVENTURE COMPLETE</h2> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| <h1>OH HAI THERE</h1> | ||
| <h2>The number is <%= @the_number %> | ||
| <h1>Random number:</h1> | ||
| <h2>The number is <%= @the_number %></h2> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious -- why event machine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first thing I had to do after cloning the repository and trying to run theweb.rb was handle this error:
http://stackoverflow.com/questions/6927907/ruby-problem-installing-eventmachine-under-windows-7