diff --git a/Gemfile b/Gemfile index 416fe44..18bfb52 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,6 @@ gem 'rake' gem 'activesupport' gem 'sinatra' gem 'sinatra-contrib' +gem 'eventmachine', '~> 1.0.0.beta.4.1' +gem 'activerecord' +gem 'pg' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 3d79568..5dae344 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,13 +1,25 @@ 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) + arel (3.0.2) backports (2.5.3) - eventmachine (0.12.10) + builder (3.0.4) + eventmachine (1.0.3) + eventmachine (1.0.3-x86-mingw32) i18n (0.6.0) multi_json (1.3.5) + pg (0.15.0-x86-mingw32) rack (1.4.1) rack-protection (1.2.0) rack @@ -26,12 +38,17 @@ GEM sinatra (~> 1.3.0) tilt (~> 1.3) tilt (1.3.3) + tzinfo (0.3.37) PLATFORMS ruby + x86-mingw32 DEPENDENCIES + activerecord activesupport + eventmachine (~> 1.0.0.beta.4.1) + pg rake sinatra sinatra-contrib diff --git a/config/.gitkeep b/config/.gitkeep new file mode 100644 index 0000000..8af715f --- /dev/null +++ b/config/.gitkeep @@ -0,0 +1 @@ +keep the config directory \ No newline at end of file diff --git a/db/migrate/001_creates_page.rb b/db/migrate/001_creates_page.rb new file mode 100644 index 0000000..0f3b6bd --- /dev/null +++ b/db/migrate/001_creates_page.rb @@ -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 diff --git a/db/seed.rb b/db/seed.rb new file mode 100644 index 0000000..74df529 --- /dev/null +++ b/db/seed.rb @@ -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.") diff --git a/db/setup.rb b/db/setup.rb new file mode 100644 index 0000000..8290b78 --- /dev/null +++ b/db/setup.rb @@ -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 \ No newline at end of file diff --git a/models/book.rb b/models/book.rb new file mode 100644 index 0000000..5eb6f53 --- /dev/null +++ b/models/book.rb @@ -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 diff --git a/models/page.rb b/models/page.rb new file mode 100644 index 0000000..d695df2 --- /dev/null +++ b/models/page.rb @@ -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 diff --git a/theweb.rb b/theweb.rb index a531ce4..942e62c 100644 --- a/theweb.rb +++ b/theweb.rb @@ -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 + 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 + diff --git a/views/about.erb b/views/about.erb new file mode 100644 index 0000000..0ba9d58 --- /dev/null +++ b/views/about.erb @@ -0,0 +1,8 @@ +
Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.
- -Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.
- -Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.
- -