Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added scores

  • Loading branch information...
commit ce12b0e9939a5cd5d0c10b506091d3e5e5273657 1 parent 5270cc2
@allonmj allonmj authored
View
19 app/models/game.rb
@@ -1,6 +1,8 @@
class Game < ActiveRecord::Base
belongs_to :word
- has_and_belongs_to_many :users
+ #has_and_belongs_to_many :users
+ has_many :plays
+ has_many :users, :through => :plays
has_many :guesses
validates_presence_of :word
@@ -28,18 +30,29 @@ def guess(user, letter)
hit = true
end
end
+ if hit
+ play_for(user).update_attribute(:score, play_for(user).score + 1)
+ else
+ play_for(user).update_attribute(:score, play_for(user).score - 1)
+ end
reload
update_attribute :masked_word, new_masked_word
guesses.create! :user => user, :letter => letter, :hit => hit
end
def guess_word(user, w)
+ hit = false
if w == word.text
update_attribute :masked_word, w
+ hit = true
+ play_for(user).update_attribute(:score, play_for(user).score + 5)
end
- guesses.create! :user => user, :letter => w, :hit => true
+ guesses.create! :user => user, :letter => w, :hit => hit
+ end
+
+ def play_for(user)
+ plays.where(:user_id => user.id).first
end
-
def complete?
guesses.count >= 4
View
2  app/models/guess.rb
@@ -1,6 +1,6 @@
class Guess < ActiveRecord::Base
belongs_to :user
belongs_to :game
-
+
scope :wrong, where(:hit => false)
end
View
4 app/models/play.rb
@@ -0,0 +1,4 @@
+class Play < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :game
+end
View
3  app/models/user.rb
@@ -1,7 +1,8 @@
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
- has_and_belongs_to_many :games
+ has_many :plays
+ has_many :games, :through => :plays
has_many :guesses
validates :username, :presence => true, :uniqueness => true
View
4 app/views/games/show.html.erb
@@ -7,11 +7,11 @@
<p>
<b>Hint:</b>
<%= @game.word.meaning %>
+ <b> Score: </b>
+ <%= @game.plays[0].score%>
<% if @game.guesses.count >=3 %>
<br/>
<b>Guess the word!</b>
- <% else %>
-
<% end %>
</p>
View
13 db/migrate/20120326201825_create_plays.rb
@@ -0,0 +1,13 @@
+class CreatePlays < ActiveRecord::Migration
+ def change
+ create_table :plays do |t|
+ t.references :user
+ t.references :game
+ t.integer :score
+
+ t.timestamps
+ end
+ add_index :plays, :user_id
+ add_index :plays, :game_id
+ end
+end
View
8 db/migrate/20120326205948_update_plays_score.rb
@@ -0,0 +1,8 @@
+class UpdatePlaysScore < ActiveRecord::Migration
+ def up
+ change_column :plays, :score, :integer, :default => 0, :null => false
+ end
+
+ def down
+ end
+end
View
13 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120325142732) do
+ActiveRecord::Schema.define(:version => 20120326205948) do
create_table "games", :force => true do |t|
t.integer "word_id"
@@ -39,6 +39,17 @@
add_index "guesses", ["game_id"], :name => "index_guesses_on_game_id"
add_index "guesses", ["user_id"], :name => "index_guesses_on_user_id"
+ create_table "plays", :force => true do |t|
+ t.integer "user_id"
+ t.integer "game_id"
+ t.integer "score", :default => 0, :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "plays", ["game_id"], :name => "index_plays_on_game_id"
+ add_index "plays", ["user_id"], :name => "index_plays_on_user_id"
+
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
Please sign in to comment.
Something went wrong with that request. Please try again.