From fae80db3ca228a7c5500533d18054e9c8d84ae45 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Mon, 8 Feb 2016 15:00:33 -0800 Subject: [PATCH 01/37] added gmes and routes for Oauth --- .gitignore | 1 + Gemfile | 4 +++- Gemfile.lock | 26 ++++++++++++++++++++++++++ config/initializers/omniauth.rb | 6 ++++++ config/routes.rb | 2 ++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 config/initializers/omniauth.rb diff --git a/.gitignore b/.gitignore index 76b696b..9500a34 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ /log/* !/log/.keep /tmp +/.env # From https://github.com/github/gitignore/blob/master/Rails.gitignore diff --git a/Gemfile b/Gemfile index cab0e4f..403f7ba 100644 --- a/Gemfile +++ b/Gemfile @@ -20,6 +20,8 @@ gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc +gem 'omniauth-facebook' + group :production do gem 'pg' gem 'rails_12factor' @@ -31,6 +33,7 @@ group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' gem 'rspec-rails' + gem 'dotenv-rails' end group :development do @@ -43,4 +46,3 @@ group :development do # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end - diff --git a/Gemfile.lock b/Gemfile.lock index fe2d2c4..644766a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,10 +56,17 @@ GEM concurrent-ruby (1.0.0) debug_inspector (0.0.2) diff-lcs (1.2.5) + dotenv (2.1.0) + dotenv-rails (2.1.0) + dotenv (= 2.1.0) + railties (>= 4.0, < 5.1) erubis (2.7.0) execjs (2.6.0) + faraday (0.9.2) + multipart-post (>= 1.2, < 3) globalid (0.3.6) activesupport (>= 4.1.0) + hashie (3.4.3) i18n (0.7.0) jbuilder (2.4.0) activesupport (>= 3.0.0, < 5.1) @@ -69,6 +76,7 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.3) + jwt (1.5.1) loofah (2.0.3) nokogiri (>= 1.5.9) mail (2.6.3) @@ -78,8 +86,24 @@ GEM mini_portile2 (2.0.0) minitest (5.8.4) multi_json (1.11.2) + multi_xml (0.5.5) + multipart-post (2.0.0) nokogiri (1.6.7.2) mini_portile2 (~> 2.0.0.rc2) + oauth2 (1.1.0) + faraday (>= 0.8, < 0.10) + jwt (~> 1.0, < 1.5.2) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (>= 1.2, < 3) + omniauth (1.3.1) + hashie (>= 1.2, < 4) + rack (>= 1.0, < 3) + omniauth-facebook (3.0.0) + omniauth-oauth2 (~> 1.2) + omniauth-oauth2 (1.4.0) + oauth2 (~> 1.0) + omniauth (~> 1.2) pg (0.18.4) pry (0.10.3) coderay (~> 1.1.0) @@ -179,8 +203,10 @@ DEPENDENCIES binding_of_caller byebug coffee-rails (~> 4.1.0) + dotenv-rails jbuilder (~> 2.0) jquery-rails + omniauth-facebook pg pry rails (= 4.2.5) diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..2db6833 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,6 @@ +# what is this? +OmniAuth.config.logger = Rails.logger + +Rails.application.config.middleware.use OmniAuth::Builder do + provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET'] +end diff --git a/config/routes.rb b/config/routes.rb index 2beb77c..05ec0da 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do root "site#index" + get "/auth/facebook" + get "/auth/facebook/callback" to: "sessions#create" end From c4c1890781406d7291e3ab6b0157e2280e2a5488 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Mon, 8 Feb 2016 15:09:07 -0800 Subject: [PATCH 02/37] omniauth gem and sessions controller --- Gemfile | 1 + Gemfile.lock | 1 + app/assets/javascripts/sessions.coffee | 3 +++ app/assets/stylesheets/sessions.scss | 3 +++ app/controllers/sessions_controller.rb | 21 ++++++++++++++++++++ app/helpers/sessions_helper.rb | 2 ++ config/routes.rb | 2 +- spec/controllers/sessions_controller_spec.rb | 5 +++++ spec/helpers/sessions_helper_spec.rb | 15 ++++++++++++++ 9 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/sessions.coffee create mode 100644 app/assets/stylesheets/sessions.scss create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/helpers/sessions_helper.rb create mode 100644 spec/controllers/sessions_controller_spec.rb create mode 100644 spec/helpers/sessions_helper_spec.rb diff --git a/Gemfile b/Gemfile index 403f7ba..14cef06 100644 --- a/Gemfile +++ b/Gemfile @@ -20,6 +20,7 @@ gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc +gem 'omniauth' gem 'omniauth-facebook' group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 644766a..2b6cb4f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -206,6 +206,7 @@ DEPENDENCIES dotenv-rails jbuilder (~> 2.0) jquery-rails + omniauth omniauth-facebook pg pry diff --git a/app/assets/javascripts/sessions.coffee b/app/assets/javascripts/sessions.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/sessions.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/sessions.scss b/app/assets/stylesheets/sessions.scss new file mode 100644 index 0000000..7bef9cf --- /dev/null +++ b/app/assets/stylesheets/sessions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..e85886f --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,21 @@ +class SessionsController < ApplicationController + skip_before_filter :verify_authenticity_token + + def new + provider = params[:format] + redirect_to "/auth/facebook" + end + + def create + auth_hash = request.env['omniauth.auth'] +# create method for line below + @user = User.find_or_create_from_omniauth(auth_hash) + session[:user_id] = @user.id + redirect_to root_path + end + + def destroy + reset_session + redirect_to root_path + end +end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/config/routes.rb b/config/routes.rb index 05ec0da..c92c7d1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,5 +2,5 @@ root "site#index" get "/auth/facebook" - get "/auth/facebook/callback" to: "sessions#create" + get "/auth/facebook/callback", to: "sessions#create" end diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb new file mode 100644 index 0000000..003bede --- /dev/null +++ b/spec/controllers/sessions_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SessionsController, type: :controller do + +end diff --git a/spec/helpers/sessions_helper_spec.rb b/spec/helpers/sessions_helper_spec.rb new file mode 100644 index 0000000..9484198 --- /dev/null +++ b/spec/helpers/sessions_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the SessionsHelper. For example: +# +# describe SessionsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe SessionsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end From 2e4454baf5857b9f19f304c19b51f34c930650bd Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Mon, 8 Feb 2016 15:23:54 -0800 Subject: [PATCH 03/37] generated user model --- app/models/user.rb | 3 +++ config/initializers/omniauth.rb | 3 --- db/migrate/20160208232259_create_users.rb | 12 ++++++++++++ spec/models/user_spec.rb | 5 +++++ 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20160208232259_create_users.rb create mode 100644 spec/models/user_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..50c4d3a --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,3 @@ +class User < ActiveRecord::Base + +end diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index 2db6833..adef7e2 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -1,6 +1,3 @@ -# what is this? -OmniAuth.config.logger = Rails.logger - Rails.application.config.middleware.use OmniAuth::Builder do provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET'] end diff --git a/db/migrate/20160208232259_create_users.rb b/db/migrate/20160208232259_create_users.rb new file mode 100644 index 0000000..6f90b0e --- /dev/null +++ b/db/migrate/20160208232259_create_users.rb @@ -0,0 +1,12 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :uid + t.string :name + t.string :oauth_token + t.datetime :oauth_expires_at + + t.timestamps null: false + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..47a31bb --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe User, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From fb9cd1ec64d401855513647c7e922f8b1d367357 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Mon, 8 Feb 2016 15:48:43 -0800 Subject: [PATCH 04/37] can login with facebook. yay --- app/controllers/application_controller.rb | 5 +++++ app/controllers/sessions_controller.rb | 15 +++++---------- app/models/user.rb | 20 +++++++++++++++++++- app/views/site/index.html.erb | 10 +++++++++- config/routes.rb | 2 ++ db/schema.rb | 11 ++++++++++- 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..f08a607 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,9 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + helper_method :current_user + + def current_user + @current_user ||= User.find(session[:user_id]) if session[:user_id] + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index e85886f..c9e7550 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,21 +1,16 @@ class SessionsController < ApplicationController skip_before_filter :verify_authenticity_token - def new - provider = params[:format] - redirect_to "/auth/facebook" - end - def create - auth_hash = request.env['omniauth.auth'] -# create method for line below - @user = User.find_or_create_from_omniauth(auth_hash) - session[:user_id] = @user.id + user = User.from_omniauth(env["omniauth.auth"]) + session[:user_id] = user.id redirect_to root_path end def destroy - reset_session + session[:user_id] = nil redirect_to root_path end + + end diff --git a/app/models/user.rb b/app/models/user.rb index 50c4d3a..71a7746 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,21 @@ class User < ActiveRecord::Base - + def self.from_omniauth(auth) + where(uid: auth.uid).first_or_create do |user| + user.uid = auth.uid + user.name = auth.info.name + user.oauth_token = auth.credentials.token + user.oauth_expires_at = Time.at(auth.credentials.expires_at) + user.save! + end + end end + + +# def self.from_omniauth(auth) +# where(provider: auth.provider, uid: auth.uid).first_or_create do |user| +# user.provider = auth.provider +# user.uid = auth.uid +# user.name = auth.info.name +# user.save +# end +# end diff --git a/app/views/site/index.html.erb b/app/views/site/index.html.erb index 277b756..28f1c47 100644 --- a/app/views/site/index.html.erb +++ b/app/views/site/index.html.erb @@ -1 +1,9 @@ -<%= render file: "public/game.html" %> +
+ <% if current_user %> + Signed in as <%= current_user.name %>! + <%= render file: "public/game.html" %> + <%= link_to "Sign out", signout_path, id: "sign_out" %> + <% else %> + <%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %> + <% end %> +
diff --git a/config/routes.rb b/config/routes.rb index c92c7d1..ed14351 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,4 +3,6 @@ root "site#index" get "/auth/facebook" get "/auth/facebook/callback", to: "sessions#create" + get "signout", to: "sessions#destroy" + end diff --git a/db/schema.rb b/db/schema.rb index 4dfbb16..0ea71d1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,6 +11,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 20160208232259) do + + create_table "users", force: :cascade do |t| + t.string "uid" + t.string "name" + t.string "oauth_token" + t.datetime "oauth_expires_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end end From 72bffd8dce09e9e73fe0e3916c8cf9a6565830da Mon Sep 17 00:00:00 2001 From: Brittany Date: Mon, 8 Feb 2016 16:24:35 -0800 Subject: [PATCH 05/37] Adding nav, needs to be fixed --- app/assets/stylesheets/application.css | 4 ++++ app/views/layouts/application.html.erb | 8 ++++++++ app/views/site/index.html.erb | 28 ++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index f9cd5b3..413fec8 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,3 +13,7 @@ *= require_tree . *= require_self */ + +.navbar-collapse { + margin-top: 0px; +} diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index d24dd07..98bac6b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,6 +5,14 @@ <%= stylesheet_link_tag 'application', media: 'all' %> <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> + + + + + + + + diff --git a/app/views/site/index.html.erb b/app/views/site/index.html.erb index 28f1c47..bb551a1 100644 --- a/app/views/site/index.html.erb +++ b/app/views/site/index.html.erb @@ -1,8 +1,32 @@
<% if current_user %> - Signed in as <%= current_user.name %>! + + <%= render file: "public/game.html" %> - <%= link_to "Sign out", signout_path, id: "sign_out" %> <% else %> <%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %> <% end %> From 2e3a84f38c624ae29ecfaa738090ee90705476dd Mon Sep 17 00:00:00 2001 From: Brittany Date: Mon, 8 Feb 2016 16:56:17 -0800 Subject: [PATCH 06/37] Fixing nav --- app/assets/stylesheets/application.css | 8 +++++-- app/controllers/site_controller.rb | 3 +++ app/views/shared/_nav.html.erb | 33 ++++++++++++++++++++++++++ app/views/site/index.html.erb | 27 +-------------------- public/style/main.css | 2 +- 5 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 app/views/shared/_nav.html.erb diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 413fec8..192a3b5 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,7 +13,11 @@ *= require_tree . *= require_self */ +a { + text-decoration: none; +} -.navbar-collapse { - margin-top: 0px; +.restart-button:hover { + color: #fff; + text-decoration: none; } diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index 9004665..602a884 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -1,5 +1,8 @@ class SiteController < ApplicationController def index + if current_user + @username = current_user.name + end end end diff --git a/app/views/shared/_nav.html.erb b/app/views/shared/_nav.html.erb new file mode 100644 index 0000000..8aeff01 --- /dev/null +++ b/app/views/shared/_nav.html.erb @@ -0,0 +1,33 @@ + + + + diff --git a/app/views/site/index.html.erb b/app/views/site/index.html.erb index bb551a1..da4d97e 100644 --- a/app/views/site/index.html.erb +++ b/app/views/site/index.html.erb @@ -1,31 +1,6 @@
<% if current_user %> - - + <%= render "shared/nav" %> <%= render file: "public/game.html" %> <% else %> <%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %> diff --git a/public/style/main.css b/public/style/main.css index ea3cb19..5633321 100644 --- a/public/style/main.css +++ b/public/style/main.css @@ -103,7 +103,7 @@ p { a { color: #776e65; font-weight: bold; - text-decoration: underline; + /*text-decoration: underline;*/ cursor: pointer; } strong.important { From 38d99f2f2dac2e0b11e7daa189e8a57008da952f Mon Sep 17 00:00:00 2001 From: Brittany Date: Tue, 9 Feb 2016 09:38:17 -0800 Subject: [PATCH 07/37] Fixing typo in nav --- app/views/shared/_nav.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/shared/_nav.html.erb b/app/views/shared/_nav.html.erb index 8aeff01..493c40e 100644 --- a/app/views/shared/_nav.html.erb +++ b/app/views/shared/_nav.html.erb @@ -24,7 +24,7 @@ <% if current_user %>
  • <%= link_to "Leaderboard", root_path %>
  • <%= link_to "Saved Games", root_path %>
  • -
  • <%= link_to "Current Games", root_path %>
  • +
  • <%= link_to "Current Game", root_path %>
  • <%= link_to "Log Out", signout_path %>
  • <% end %> From 8bc3fa94c6a1f1733267ac16b484f921ac23f48b Mon Sep 17 00:00:00 2001 From: Brittany Date: Tue, 9 Feb 2016 10:13:56 -0800 Subject: [PATCH 08/37] Adding save function, need to fill in API call --- app/models/game.rb | 2 ++ db/migrate/20160209175301_create_games.rb | 11 +++++++++++ db/migrate/20160209175724_change_column_default.rb | 5 +++++ db/schema.rb | 10 +++++++++- public/game.html | 1 + public/js/game_manager.js | 6 ++++++ public/js/keyboard_input_manager.js | 6 ++++++ spec/models/game_spec.rb | 5 +++++ 8 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 app/models/game.rb create mode 100644 db/migrate/20160209175301_create_games.rb create mode 100644 db/migrate/20160209175724_change_column_default.rb create mode 100644 spec/models/game_spec.rb diff --git a/app/models/game.rb b/app/models/game.rb new file mode 100644 index 0000000..a181c26 --- /dev/null +++ b/app/models/game.rb @@ -0,0 +1,2 @@ +class Game < ActiveRecord::Base +end diff --git a/db/migrate/20160209175301_create_games.rb b/db/migrate/20160209175301_create_games.rb new file mode 100644 index 0000000..822019c --- /dev/null +++ b/db/migrate/20160209175301_create_games.rb @@ -0,0 +1,11 @@ +class CreateGames < ActiveRecord::Migration + def change + create_table :games do |t| + t.integer :score + t.string :board_state + t.boolean :lost + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160209175724_change_column_default.rb b/db/migrate/20160209175724_change_column_default.rb new file mode 100644 index 0000000..0070a6b --- /dev/null +++ b/db/migrate/20160209175724_change_column_default.rb @@ -0,0 +1,5 @@ +class ChangeColumnDefault < ActiveRecord::Migration + def change + change_column_default(:games, :lost, false) + end +end diff --git a/db/schema.rb b/db/schema.rb index 0ea71d1..bec45be 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160208232259) do +ActiveRecord::Schema.define(version: 20160209175724) do + + create_table "games", force: :cascade do |t| + t.integer "score" + t.string "board_state" + t.boolean "lost", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end create_table "users", force: :cascade do |t| t.string "uid" diff --git a/public/game.html b/public/game.html index bcdf162..38b9d86 100644 --- a/public/game.html +++ b/public/game.html @@ -31,6 +31,7 @@

    2048

    Join the numbers and get to the 2048 tile!

    New Game + Save Game
    diff --git a/public/js/game_manager.js b/public/js/game_manager.js index 1c13d15..b4f6e8f 100644 --- a/public/js/game_manager.js +++ b/public/js/game_manager.js @@ -7,12 +7,18 @@ function GameManager(size, InputManager, Actuator, StorageManager) { this.startTiles = 2; this.inputManager.on("move", this.move.bind(this)); + this.inputManager.on("save", this.save.bind(this)); this.inputManager.on("restart", this.restart.bind(this)); this.inputManager.on("keepPlaying", this.keepPlaying.bind(this)); this.setup(); } +// Save the game +GameManager.prototype.save = function () { + // access API to save game somehow +}; + // Restart the game GameManager.prototype.restart = function () { this.storageManager.clearGameState(); diff --git a/public/js/keyboard_input_manager.js b/public/js/keyboard_input_manager.js index ca01b3c..e91bc10 100644 --- a/public/js/keyboard_input_manager.js +++ b/public/js/keyboard_input_manager.js @@ -72,6 +72,7 @@ KeyboardInputManager.prototype.listen = function () { this.bindButtonPress(".retry-button", this.restart); this.bindButtonPress(".restart-button", this.restart); this.bindButtonPress(".keep-playing-button", this.keepPlaying); + this.bindButtonPress(".save-button", this.save); // Respond to swipe events var touchStartClientX, touchStartClientY; @@ -127,6 +128,11 @@ KeyboardInputManager.prototype.listen = function () { }); }; +KeyboardInputManager.prototype.save = function (event) { + event.preventDefault(); + this.emit("save"); +}; + KeyboardInputManager.prototype.restart = function (event) { event.preventDefault(); this.emit("restart"); diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb new file mode 100644 index 0000000..7713645 --- /dev/null +++ b/spec/models/game_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Game, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 1cfb0be2bb7ac4a062b331841fc3e976af62bacb Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Tue, 9 Feb 2016 10:49:29 -0800 Subject: [PATCH 09/37] generated game controller --- app/assets/javascripts/game.coffee | 3 +++ app/assets/stylesheets/game.scss | 3 +++ app/controllers/game_controller.rb | 10 ++++++++++ app/helpers/game_helper.rb | 2 ++ config/routes.rb | 3 ++- public/js/game_manager.js | 1 + spec/controllers/game_controller_spec.rb | 5 +++++ spec/helpers/game_helper_spec.rb | 15 +++++++++++++++ 8 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/game.coffee create mode 100644 app/assets/stylesheets/game.scss create mode 100644 app/controllers/game_controller.rb create mode 100644 app/helpers/game_helper.rb create mode 100644 spec/controllers/game_controller_spec.rb create mode 100644 spec/helpers/game_helper_spec.rb diff --git a/app/assets/javascripts/game.coffee b/app/assets/javascripts/game.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/game.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/game.scss b/app/assets/stylesheets/game.scss new file mode 100644 index 0000000..4a613ae --- /dev/null +++ b/app/assets/stylesheets/game.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the game controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb new file mode 100644 index 0000000..816044d --- /dev/null +++ b/app/controllers/game_controller.rb @@ -0,0 +1,10 @@ +class GameController < ApplicationController + + def save +# does the game exist or do we need to create a new one? + game = Game.new + game.score = 2048 #something we get from JS + game.board_state = "" #some kind of sting + game.lost = false + end +end diff --git a/app/helpers/game_helper.rb b/app/helpers/game_helper.rb new file mode 100644 index 0000000..34198e3 --- /dev/null +++ b/app/helpers/game_helper.rb @@ -0,0 +1,2 @@ +module GameHelper +end diff --git a/config/routes.rb b/config/routes.rb index ed14351..5afa4ed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ root "site#index" get "/auth/facebook" get "/auth/facebook/callback", to: "sessions#create" - get "signout", to: "sessions#destroy" + get "signout", to: "sessions#destroy" + post "save", to: "game#save" end diff --git a/public/js/game_manager.js b/public/js/game_manager.js index b4f6e8f..280630f 100644 --- a/public/js/game_manager.js +++ b/public/js/game_manager.js @@ -17,6 +17,7 @@ function GameManager(size, InputManager, Actuator, StorageManager) { // Save the game GameManager.prototype.save = function () { // access API to save game somehow + }; // Restart the game diff --git a/spec/controllers/game_controller_spec.rb b/spec/controllers/game_controller_spec.rb new file mode 100644 index 0000000..31f3f56 --- /dev/null +++ b/spec/controllers/game_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe GameController, type: :controller do + +end diff --git a/spec/helpers/game_helper_spec.rb b/spec/helpers/game_helper_spec.rb new file mode 100644 index 0000000..da11225 --- /dev/null +++ b/spec/helpers/game_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the GameHelper. For example: +# +# describe GameHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe GameHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end From d0415231e9528c41188034592676233779a00868 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Tue, 9 Feb 2016 11:15:52 -0800 Subject: [PATCH 10/37] some ajax added. not working --- public/js/game_manager.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/public/js/game_manager.js b/public/js/game_manager.js index 280630f..0bf94ac 100644 --- a/public/js/game_manager.js +++ b/public/js/game_manager.js @@ -17,7 +17,26 @@ function GameManager(size, InputManager, Actuator, StorageManager) { // Save the game GameManager.prototype.save = function () { // access API to save game somehow - + $(document).ready(function(){ + // var url = "https://localhost:3000/save"; + // var data = {score: '2048'}; + + $.ajax({ + method: "POST", + url: "https://localhost:3000/save", + data: {score: '2048'} + }) + .done(function(msg) { + console.log("DONE!"); + console.log(msg); + }) + .fail(function(){ + console.log("fail"); + }) + .always(function(){ + console.log("always"); + }); + }); }; // Restart the game From 1ba9e11be0ca183eb8e80cd649e8efd1c9439ff3 Mon Sep 17 00:00:00 2001 From: Brittany Date: Tue, 9 Feb 2016 11:34:54 -0800 Subject: [PATCH 11/37] Posting to rails save api endpoint is working, need to get actual game data to save, not hardcoded values --- app/controllers/game_controller.rb | 1 + public/js/game_manager.js | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index 816044d..429a2f6 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -6,5 +6,6 @@ def save game.score = 2048 #something we get from JS game.board_state = "" #some kind of sting game.lost = false + render :json => [], :status => :ok end end diff --git a/public/js/game_manager.js b/public/js/game_manager.js index 0bf94ac..d4c4a6b 100644 --- a/public/js/game_manager.js +++ b/public/js/game_manager.js @@ -16,6 +16,12 @@ function GameManager(size, InputManager, Actuator, StorageManager) { // Save the game GameManager.prototype.save = function () { + $.ajaxSetup({ + headers: { + 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') + } + }); + // access API to save game somehow $(document).ready(function(){ // var url = "https://localhost:3000/save"; @@ -23,7 +29,7 @@ GameManager.prototype.save = function () { $.ajax({ method: "POST", - url: "https://localhost:3000/save", + url: "/save", data: {score: '2048'} }) .done(function(msg) { From 44e04f0532a076d8eb572a07f529b975999469e1 Mon Sep 17 00:00:00 2001 From: Brittany Date: Tue, 9 Feb 2016 11:52:50 -0800 Subject: [PATCH 12/37] Values are now saving in rails game model for save API endpoint, need to determine how to get correct values on javascript side --- app/controllers/game_controller.rb | 7 ++++--- public/js/game_manager.js | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index 429a2f6..d3c25f1 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -3,9 +3,10 @@ class GameController < ApplicationController def save # does the game exist or do we need to create a new one? game = Game.new - game.score = 2048 #something we get from JS - game.board_state = "" #some kind of sting - game.lost = false + game.score = params[:score] #something we get from JS + game.board_state = params[:board_state] #some kind of string + game.lost = params[:lost] + game.save render :json => [], :status => :ok end end diff --git a/public/js/game_manager.js b/public/js/game_manager.js index d4c4a6b..459cb3b 100644 --- a/public/js/game_manager.js +++ b/public/js/game_manager.js @@ -21,7 +21,7 @@ GameManager.prototype.save = function () { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') } }); - + // access API to save game somehow $(document).ready(function(){ // var url = "https://localhost:3000/save"; @@ -30,7 +30,7 @@ GameManager.prototype.save = function () { $.ajax({ method: "POST", url: "/save", - data: {score: '2048'} + data: {score: '2048', board_state: "hello", lost: "false"} }) .done(function(msg) { console.log("DONE!"); From 87aa00bc85b61e22f3044820ff1f4e86012cea27 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Tue, 9 Feb 2016 12:27:42 -0800 Subject: [PATCH 13/37] created remote storage manager and copied stuff in it --- public/js/application.js | 2 +- public/js/remote_storage_manager.js | 63 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 public/js/remote_storage_manager.js diff --git a/public/js/application.js b/public/js/application.js index 2c1108e..4d4e73f 100644 --- a/public/js/application.js +++ b/public/js/application.js @@ -1,4 +1,4 @@ // Wait till the browser is ready to render the game (avoids glitches) window.requestAnimationFrame(function () { - new GameManager(4, KeyboardInputManager, HTMLActuator, LocalStorageManager); + new GameManager(4, KeyboardInputManager, HTMLActuator, RemoteStorageManager); }); diff --git a/public/js/remote_storage_manager.js b/public/js/remote_storage_manager.js new file mode 100644 index 0000000..19408e4 --- /dev/null +++ b/public/js/remote_storage_manager.js @@ -0,0 +1,63 @@ +window.fakeStorage = { + _data: {}, + + setItem: function (id, val) { + return this._data[id] = String(val); + }, + + getItem: function (id) { + return this._data.hasOwnProperty(id) ? this._data[id] : undefined; + }, + + removeItem: function (id) { + return delete this._data[id]; + }, + + clear: function () { + return this._data = {}; + } +}; + +function RemoteStorageManager() { + this.bestScoreKey = "bestScore"; + this.gameStateKey = "gameState"; + + var supported = this.remoteStorageSupported(); + this.storage = supported ? window.localStorage : window.fakeStorage; +} + +RemoteStorageManager.prototype.remoteStorageSupported = function () { + var testKey = "test"; + var storage = window.localStorage; + + try { + storage.setItem(testKey, "1"); + storage.removeItem(testKey); + return true; + } catch (error) { + return false; + } +}; + +// Best score getters/setters +RemoteStorageManager.prototype.getBestScore = function () { + return this.storage.getItem(this.bestScoreKey) || 0; +}; + +RemoteStorageManager.prototype.setBestScore = function (score) { + this.storage.setItem(this.bestScoreKey, score); +}; + +// Game state getters/setters and clearing +RemoteStorageManager.prototype.getGameState = function () { + var stateJSON = this.storage.getItem(this.gameStateKey); + return stateJSON ? JSON.parse(stateJSON) : null; +}; + +RemoteStorageManager.prototype.setGameState = function (gameState) { + this.storage.setItem(this.gameStateKey, JSON.stringify(gameState)); +}; + +RemoteStorageManager.prototype.clearGameState = function () { + this.storage.removeItem(this.gameStateKey); +}; From 6850417588b36d3d8857861c672399d809657c2e Mon Sep 17 00:00:00 2001 From: Brittany Date: Tue, 9 Feb 2016 13:44:50 -0800 Subject: [PATCH 14/37] requiring new remote_storage_manager file --- public/game.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/public/game.html b/public/game.html index 38b9d86..f8173ed 100644 --- a/public/game.html +++ b/public/game.html @@ -95,8 +95,10 @@

    2048

    - + + + From a61d2695cb479bff70906a37eb8de2fd77bc6b3d Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Tue, 9 Feb 2016 14:08:18 -0800 Subject: [PATCH 15/37] we can now access info from the game and save it to our database --- public/js/game_manager.js | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/public/js/game_manager.js b/public/js/game_manager.js index 459cb3b..ad790b0 100644 --- a/public/js/game_manager.js +++ b/public/js/game_manager.js @@ -21,27 +21,25 @@ GameManager.prototype.save = function () { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') } }); - +//to access 'this' inside of ajax/jquery stuff: + var self = this; // access API to save game somehow $(document).ready(function(){ - // var url = "https://localhost:3000/save"; - // var data = {score: '2048'}; - - $.ajax({ - method: "POST", - url: "/save", - data: {score: '2048', board_state: "hello", lost: "false"} - }) - .done(function(msg) { - console.log("DONE!"); - console.log(msg); + $.ajax({ + method: "POST", + url: "/save", + data: {score: self.score, board_state: self.storageManager.getGameState(), lost: "false"} }) - .fail(function(){ - console.log("fail"); - }) - .always(function(){ - console.log("always"); - }); + .done(function(msg) { + console.log("DONE!"); + console.log(msg); + }) + .fail(function(){ + console.log("fail"); + }) + .always(function(){ + console.log("always"); + }); }); }; From e1c496bc5038de6e2a5ec6c07b008f6df5855bf9 Mon Sep 17 00:00:00 2001 From: Brittany Date: Tue, 9 Feb 2016 14:38:38 -0800 Subject: [PATCH 16/37] Adding view for saved games --- app/assets/stylesheets/site.scss | 4 ++++ app/controllers/game_controller.rb | 1 + app/controllers/site_controller.rb | 7 ++++++- app/models/game.rb | 1 + app/models/user.rb | 2 ++ app/views/shared/_nav.html.erb | 2 +- app/views/site/games.html.erb | 7 +++++++ config/routes.rb | 2 ++ db/migrate/20160209222022_add_user_id.rb | 5 +++++ db/schema.rb | 3 ++- public/js/game_manager.js | 4 ++-- 11 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 app/views/site/games.html.erb create mode 100644 db/migrate/20160209222022_add_user_id.rb diff --git a/app/assets/stylesheets/site.scss b/app/assets/stylesheets/site.scss index b5f45cc..407c0b2 100644 --- a/app/assets/stylesheets/site.scss +++ b/app/assets/stylesheets/site.scss @@ -1,3 +1,7 @@ // Place all the styles related to the site controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ + +.game-list { + margin-top: 80px; +} diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index d3c25f1..a782614 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -6,6 +6,7 @@ def save game.score = params[:score] #something we get from JS game.board_state = params[:board_state] #some kind of string game.lost = params[:lost] + game.user_id = current_user.id game.save render :json => [], :status => :ok end diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index 602a884..6dd2c69 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -3,6 +3,11 @@ class SiteController < ApplicationController def index if current_user @username = current_user.name - end + end + end + + def games + @username = current_user.name + @games = current_user.games end end diff --git a/app/models/game.rb b/app/models/game.rb index a181c26..f04c47d 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,2 +1,3 @@ class Game < ActiveRecord::Base + belongs_to :user end diff --git a/app/models/user.rb b/app/models/user.rb index 71a7746..9e6a713 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,6 @@ class User < ActiveRecord::Base + has_many :games + def self.from_omniauth(auth) where(uid: auth.uid).first_or_create do |user| user.uid = auth.uid diff --git a/app/views/shared/_nav.html.erb b/app/views/shared/_nav.html.erb index 493c40e..b057447 100644 --- a/app/views/shared/_nav.html.erb +++ b/app/views/shared/_nav.html.erb @@ -23,7 +23,7 @@
    - - - - - - - - - - - + + + + + + + + + + + diff --git a/public/js/remote_storage_manager.js b/public/js/remote_storage_manager.js index 19408e4..f48bb65 100644 --- a/public/js/remote_storage_manager.js +++ b/public/js/remote_storage_manager.js @@ -50,7 +50,30 @@ RemoteStorageManager.prototype.setBestScore = function (score) { // Game state getters/setters and clearing RemoteStorageManager.prototype.getGameState = function () { - var stateJSON = this.storage.getItem(this.gameStateKey); + var stateJSON; +//not sure if we need commented out code: + // $.ajaxSetup({ + // headers: { + // 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') + // } + // }); + var url = "/chosen-game"; + // $(document).ready(function(){ + $.ajax(url, { + method: "GET" + }) + .done(function(data) { + console.log("DONE!"); + console.log(data); + stateJSON = data; + }) + .fail(function(){ + console.log("fail"); + }) + .always(function(){ + console.log("always"); + }); + // var stateJSON = this.storage.getItem(this.gameStateKey); return stateJSON ? JSON.parse(stateJSON) : null; }; From 2c5b013fa0ab624dd33731e59aebca3e9a896c85 Mon Sep 17 00:00:00 2001 From: Brittany Date: Wed, 10 Feb 2016 11:19:09 -0800 Subject: [PATCH 19/37] Updated paths, nested games in users, updating API endpoint for getting games --- app/controllers/game_controller.rb | 22 ++++++++++++++-------- app/views/site/games.html.erb | 2 +- config/routes.rb | 10 +++++++--- public/js/remote_storage_manager.js | 4 +++- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index c8321be..4448ac9 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -11,14 +11,20 @@ def save render :json => [], :status => :ok end - def chosen_game - game = Game.find_by(id: params[:id]) - if game - render :json => game.board_state.as_json(except: [:created_at, :updated_at]), :status => :ok - else -# the request itself was fine, but there was just no content associated with the response, so use status code 204 - render :json => [], :status => 204 + def show + respond_to do |format| + format.json { + game = Game.find_by(id: params[:id]) + if game + render :json => game.board_state.as_json(except: [:created_at, :updated_at]), :status => :ok + else + # the request itself was fine, but there was just no content associated with the response, so use status code 204 + render :json => [], :status => 204 + end + } + format.html { + render template: "site/index" + } end - end end diff --git a/app/views/site/games.html.erb b/app/views/site/games.html.erb index 432aa87..0c2642c 100644 --- a/app/views/site/games.html.erb +++ b/app/views/site/games.html.erb @@ -2,6 +2,6 @@
    <% @games.each do |game| %> - <%= link_to "Game", "/#{game.id}" %> + <%= link_to "Game", "/users/#{current_user.id}/game/#{game.id}.html" %> <% end %>
    diff --git a/config/routes.rb b/config/routes.rb index 19aa4b7..95d8026 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,10 +4,14 @@ get "/auth/facebook" get "/auth/facebook/callback", to: "sessions#create" get "signout", to: "sessions#destroy" - get "/saved-games", to: "site#games" + get "/saved-games", to: "site#games" #move to game#index post "save", to: "game#save" - get "chosen-game", to: "game#chosen_game" - get "/:id", to: "site#index" + # get "get-game", to: "game#get_game" + resources :users do + resources :game do + end + end + diff --git a/public/js/remote_storage_manager.js b/public/js/remote_storage_manager.js index f48bb65..4754fbe 100644 --- a/public/js/remote_storage_manager.js +++ b/public/js/remote_storage_manager.js @@ -57,7 +57,9 @@ RemoteStorageManager.prototype.getGameState = function () { // 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') // } // }); - var url = "/chosen-game"; + var userID = "1"; + var gameID = "1"; + var url = "/users/" + userID + "/game/" + gameID; // $(document).ready(function(){ $.ajax(url, { method: "GET" From f83fdaae6718aecb00ba9d9d56478894ac173c2f Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Wed, 10 Feb 2016 16:11:13 -0800 Subject: [PATCH 20/37] Kai helping us debug things. We can now load a saved game. Asynchronous api calls, tile positions, etc. --- public/js/game_manager.js | 7 ++++--- public/js/grid.js | 3 +++ public/js/remote_storage_manager.js | 16 +++++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/public/js/game_manager.js b/public/js/game_manager.js index 7f1ab90..66ad03e 100644 --- a/public/js/game_manager.js +++ b/public/js/game_manager.js @@ -28,7 +28,7 @@ GameManager.prototype.save = function () { $.ajax({ method: "POST", url: "/save", - data: {score: self.score, board_state: self.storageManager.getGameState(), lost: "false"} + data: {score: self.score, board_state: JSON.stringify(self.grid), lost: "false"} }) .done(function(msg) { console.log("DONE!"); @@ -64,11 +64,12 @@ GameManager.prototype.isGameTerminated = function () { // Set up the game GameManager.prototype.setup = function () { var previousState = this.storageManager.getGameState(); + console.log(previousState + "prev state"); // Reload the game from a previous game if present if (previousState) { - this.grid = new Grid(previousState.grid.size, - previousState.grid.cells); // Reload grid + this.grid = new Grid(previousState.size, + previousState.cells); // Reload grid this.score = previousState.score; this.over = previousState.over; this.won = previousState.won; diff --git a/public/js/grid.js b/public/js/grid.js index 29f0821..0fea2e5 100644 --- a/public/js/grid.js +++ b/public/js/grid.js @@ -26,6 +26,9 @@ Grid.prototype.fromState = function (state) { for (var y = 0; y < this.size; y++) { var tile = state[x][y]; + if (tile) { + tile.position = {x: tile.x, y: tile.y}; + } row.push(tile ? new Tile(tile.position, tile.value) : null); } } diff --git a/public/js/remote_storage_manager.js b/public/js/remote_storage_manager.js index 4754fbe..d28322f 100644 --- a/public/js/remote_storage_manager.js +++ b/public/js/remote_storage_manager.js @@ -58,25 +58,31 @@ RemoteStorageManager.prototype.getGameState = function () { // } // }); var userID = "1"; - var gameID = "1"; - var url = "/users/" + userID + "/game/" + gameID; + var gameID = "5"; + var url = "/users/" + userID + "/game/" + gameID + ".json"; + console.log(url); // $(document).ready(function(){ $.ajax(url, { - method: "GET" + method: "GET", + async: false }) .done(function(data) { console.log("DONE!"); console.log(data); stateJSON = data; }) - .fail(function(){ + .fail(function(one, two, three){ + console.log(one); + console.log(two); + console.log(three); console.log("fail"); }) .always(function(){ console.log("always"); }); // var stateJSON = this.storage.getItem(this.gameStateKey); - return stateJSON ? JSON.parse(stateJSON) : null; + console.log(stateJSON); + return stateJSON; }; RemoteStorageManager.prototype.setGameState = function (gameState) { From ad93390cd91194f8f90adf1314253f6192dc58ec Mon Sep 17 00:00:00 2001 From: Brittany Date: Wed, 10 Feb 2016 16:42:02 -0800 Subject: [PATCH 21/37] Created hidden divs to dynamically pull correct data for selected game --- app/controllers/game_controller.rb | 2 ++ public/{game.html => game.html.erb} | 8 ++++++++ public/js/remote_storage_manager.js | 4 ++-- public/style/main.css | 9 +++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) rename public/{game.html => game.html.erb} (97%) diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index 4448ac9..568c3e8 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -23,6 +23,8 @@ def show end } format.html { + @user_id = current_user.id + @game_id = params[:id] render template: "site/index" } end diff --git a/public/game.html b/public/game.html.erb similarity index 97% rename from public/game.html rename to public/game.html.erb index 19b406f..f4cdd71 100644 --- a/public/game.html +++ b/public/game.html.erb @@ -34,6 +34,14 @@

    2048

    Save Game
    +
    + <%= @user_id %> +
    + +
    + <%= @game_id %> +
    +

    diff --git a/public/js/remote_storage_manager.js b/public/js/remote_storage_manager.js index d28322f..2cd02fc 100644 --- a/public/js/remote_storage_manager.js +++ b/public/js/remote_storage_manager.js @@ -57,8 +57,8 @@ RemoteStorageManager.prototype.getGameState = function () { // 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') // } // }); - var userID = "1"; - var gameID = "5"; + var userID = $(".user-id").text(); + var gameID = $(".game-id").text(); var url = "/users/" + userID + "/game/" + gameID + ".json"; console.log(url); // $(document).ready(function(){ diff --git a/public/style/main.css b/public/style/main.css index 5633321..09efc62 100644 --- a/public/style/main.css +++ b/public/style/main.css @@ -1,4 +1,13 @@ @import url(fonts/clear-sans.css); + +.user-id { + display: none; +} + +.game-id { + display: none; +} + html, body { margin: 0; padding: 0; From bb2d0910c1e3d3755e95ba959402c586b635600c Mon Sep 17 00:00:00 2001 From: Brittany Date: Wed, 10 Feb 2016 16:55:55 -0800 Subject: [PATCH 22/37] Updating saving/retrieving game data so that score now shows up --- app/controllers/game_controller.rb | 2 +- public/js/game_manager.js | 4 ++-- public/js/remote_storage_manager.js | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index 568c3e8..bc0b32c 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -16,7 +16,7 @@ def show format.json { game = Game.find_by(id: params[:id]) if game - render :json => game.board_state.as_json(except: [:created_at, :updated_at]), :status => :ok + render :json => game.as_json(except: [:created_at, :updated_at]), :status => :ok else # the request itself was fine, but there was just no content associated with the response, so use status code 204 render :json => [], :status => 204 diff --git a/public/js/game_manager.js b/public/js/game_manager.js index 66ad03e..1ffd18c 100644 --- a/public/js/game_manager.js +++ b/public/js/game_manager.js @@ -68,8 +68,8 @@ GameManager.prototype.setup = function () { // Reload the game from a previous game if present if (previousState) { - this.grid = new Grid(previousState.size, - previousState.cells); // Reload grid + this.grid = new Grid(previousState.board_state.size, + previousState.board_state.cells); // Reload grid this.score = previousState.score; this.over = previousState.over; this.won = previousState.won; diff --git a/public/js/remote_storage_manager.js b/public/js/remote_storage_manager.js index 2cd02fc..87dcdcd 100644 --- a/public/js/remote_storage_manager.js +++ b/public/js/remote_storage_manager.js @@ -82,6 +82,7 @@ RemoteStorageManager.prototype.getGameState = function () { }); // var stateJSON = this.storage.getItem(this.gameStateKey); console.log(stateJSON); + stateJSON.board_state = JSON.parse(stateJSON.board_state); return stateJSON; }; From ae73a367e23a5ba946e7ff9fef1b71e158f45e14 Mon Sep 17 00:00:00 2001 From: Brittany Date: Thu, 11 Feb 2016 09:21:44 -0800 Subject: [PATCH 23/37] Fixing storage manager logic, now new game will load --- public/js/remote_storage_manager.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/public/js/remote_storage_manager.js b/public/js/remote_storage_manager.js index 87dcdcd..2b4beb0 100644 --- a/public/js/remote_storage_manager.js +++ b/public/js/remote_storage_manager.js @@ -82,7 +82,9 @@ RemoteStorageManager.prototype.getGameState = function () { }); // var stateJSON = this.storage.getItem(this.gameStateKey); console.log(stateJSON); - stateJSON.board_state = JSON.parse(stateJSON.board_state); + if (stateJSON) { + stateJSON.board_state = JSON.parse(stateJSON.board_state); + } return stateJSON; }; From c2a934919f4375e228ecd671f384e8233e7db5cc Mon Sep 17 00:00:00 2001 From: Brittany Date: Thu, 11 Feb 2016 09:38:07 -0800 Subject: [PATCH 24/37] Created leaderboard --- app/controllers/site_controller.rb | 6 +++++- app/views/shared/_nav.html.erb | 2 +- app/views/site/leaderboard.html.erb | 9 +++++++++ config/routes.rb | 7 +------ 4 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 app/views/site/leaderboard.html.erb diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index c6f5fb8..9e49022 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -5,7 +5,7 @@ def index @username = current_user.name end if params[:id] - + end end @@ -13,4 +13,8 @@ def games @username = current_user.name @games = current_user.games end + + def leaderboard + @games = Game.order(score: :desc).limit(10) + end end diff --git a/app/views/shared/_nav.html.erb b/app/views/shared/_nav.html.erb index b057447..c673b93 100644 --- a/app/views/shared/_nav.html.erb +++ b/app/views/shared/_nav.html.erb @@ -22,7 +22,7 @@