Skip to content

Commit

Permalink
Merge pull request #33 from leoamigood/la_user_id
Browse files Browse the repository at this point in the history
Adding user id to guess for exact user matching
  • Loading branch information
leoamigood authored Feb 17, 2017
2 parents 04d9916 + 2571809 commit 39aff40
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 144 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ gem 'activerecord-import', '~> 0.4.0'
gem 'rack-cors', :require => 'rack/cors'
gem 'kaminari'
gem 'api-pagination'
gem 'attr_extras'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
140 changes: 76 additions & 64 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
api-pagination (4.5.1)
arel (6.0.3)
api-pagination (4.5.2)
arel (6.0.4)
attr_extras (5.2.0)
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (8.2.4)
builder (3.2.3)
byebug (9.0.6)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
coffee-rails (4.1.1)
Expand All @@ -56,54 +57,63 @@ GEM
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.10.0)
concurrent-ruby (1.0.1)
coffee-script-source (1.12.2)
concurrent-ruby (1.0.4)
debug_inspector (0.0.2)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
diff-lcs (1.2.5)
diff-lcs (1.3)
docile (1.1.5)
equalizer (0.0.11)
erubis (2.7.0)
execjs (2.6.0)
factory_girl (4.7.0)
execjs (2.7.0)
factory_girl (4.8.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.7.0)
factory_girl (~> 4.7.0)
factory_girl_rails (4.8.0)
factory_girl (~> 4.8.0)
railties (>= 3.0.0)
faraday (0.9.2)
faraday (0.11.0)
multipart-post (>= 1.2, < 3)
globalid (0.3.6)
globalid (0.3.7)
activesupport (>= 4.1.0)
i18n (0.7.0)
i18n (0.8.0)
ice_nine (0.11.2)
jbuilder (2.4.1)
jbuilder (2.6.1)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
jquery-rails (4.1.1)
jquery-rails (4.2.2)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
kaminari (0.17.0)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
json (1.8.6)
kaminari (1.0.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.0.1)
kaminari-activerecord (= 1.0.1)
kaminari-core (= 1.0.1)
kaminari-actionview (1.0.1)
actionview
kaminari-core (= 1.0.1)
kaminari-activerecord (1.0.1)
activerecord
kaminari-core (= 1.0.1)
kaminari-core (1.0.1)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
mime-types (3.0)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0221)
mini_portile2 (2.0.0)
minitest (5.8.4)
multi_json (1.11.2)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.1)
multi_json (1.12.1)
multipart-post (2.0.0)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
pg (0.17.1)
rack (1.6.4)
rack-cors (0.4.0)
nokogiri (1.7.0.1)
mini_portile2 (~> 2.1.0)
pg (0.19.0)
rack (1.6.5)
rack-cors (0.4.1)
rack-test (0.6.3)
rack (>= 1.0)
rails (4.2.5)
Expand All @@ -122,9 +132,9 @@ GEM
railties (>= 3.2.11)
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.7)
rails-dom-testing (1.0.8)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
nokogiri (~> 1.6)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
Expand All @@ -134,59 +144,60 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (12.0.0)
rdoc (4.2.2)
json (~> 1.4)
rspec-core (3.4.4)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
rdoc (4.3.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.1)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-rails (3.4.2)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
sass (3.4.22)
sass-rails (5.0.4)
railties (>= 4.0.0, < 5.0)
rspec-support (~> 3.5.0)
rspec-rails (3.5.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
sass (3.4.23)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
sass (~> 3.1)
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
sdoc (0.4.1)
sdoc (0.4.2)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
simplecov (0.12.0)
simplecov (0.13.0)
docile (~> 1.1.0)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
spring (1.7.1)
sprockets (3.6.0)
spring (2.0.1)
activesupport (>= 4.2)
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.0.4)
sprockets-rails (3.2.0)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
telegram-bot-ruby (0.5.2)
telegram-bot-ruby (0.7.2)
faraday
virtus
thor (0.19.1)
thor (0.19.4)
thread_safe (0.3.5)
tilt (2.0.2)
turbolinks (2.5.3)
coffee-rails
tilt (2.0.6)
turbolinks (5.0.1)
turbolinks-source (~> 5)
turbolinks-source (5.0.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (3.0.0)
uglifier (3.0.4)
execjs (>= 0.3.0, < 3)
virtus (1.0.5)
axiom-types (~> 0.1)
Expand All @@ -205,6 +216,7 @@ PLATFORMS
DEPENDENCIES
activerecord-import (~> 0.4.0)
api-pagination
attr_extras
byebug
coffee-rails (~> 4.1.0)
factory_girl_rails (~> 4.0)
Expand All @@ -226,4 +238,4 @@ DEPENDENCIES
web-console (~> 2.0)

BUNDLED WITH
1.13.6
1.14.3
2 changes: 1 addition & 1 deletion app/controllers/guesses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class GuessesController < BaseApiController

def create
game = GameService.find_by_id!(validate[:game_id])
guess = GameEngineService.guess(game, validate[:username], validate[:guess])
guess = GameEngineService.guess(game, User.new(id = Random.rand, name = validate[:username]), validate[:guess])

render json: {
guess: Responses::Guess.new(guess),
Expand Down
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class User

rattr_initialize :id, :name

def ==(other)
@id == other.id
end

end
8 changes: 4 additions & 4 deletions app/services/game_engine_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def create_by_options(channel, source, options)
GameService.create(channel, secret, source)
end

def guess(game, username, word)
def guess(game, user, word)
GameService.validate_game!(game)
GameService.validate_guess!(game, word)

GameService.guess(game, username, word)
GameService.guess(game, user, word)
end

def hint(game, letter = nil)
Expand All @@ -34,7 +34,7 @@ def open(game, number)
GameService.open(game, number)
end

def suggest(game, username, letters = nil)
def suggest(game, user, letters = nil)
GameService.validate_game!(game)
nouns = Noun.
where(dictionary_id: game.dictionary_id).
Expand All @@ -44,7 +44,7 @@ def suggest(game, username, letters = nil)
nouns = nouns.where("noun LIKE '%#{letters}%'") if letters.present?
suggestion = nouns.order('RANDOM()').first

GameService.guess(game, username, suggestion.noun.downcase, suggestion = true) if suggestion.present?
GameService.guess(game, user, suggestion.noun.downcase, suggestion = true) if suggestion.present?
end

def tries(channel)
Expand Down
5 changes: 3 additions & 2 deletions app/services/game_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ def find_games(options = {})
Game.all.where(options.compact)
end

def guess(game, username, word, suggestion = false)
def guess(game, user, word, suggestion = false)
guess = Guess.find_or_create_by(game_id: game.id, word: word) do |guess|
guess.attempts = 0
guess.username = username
guess.user_id = user.id
guess.username = user.name
guess.suggestion = suggestion

guess.update(match(word, game.secret))
Expand Down
2 changes: 1 addition & 1 deletion app/services/telegram/command/guess.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Guess
class << self
def execute(channel, message, word)
game = GameService.find_by_channel!(channel)
guess = GameEngineService.guess(game, message.from.username, word)
guess = GameEngineService.guess(game, User.new(message.from.id, message.from.username), word)
TelegramMessenger.guess(guess)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/telegram/command/suggest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Suggest
class << self
def execute(channel, message, letters)
game = GameService.find_by_channel!(channel)
guess = GameEngineService.suggest(game, message.from.username, letters)
guess = GameEngineService.suggest(game, User.new(message.from.id, message.from.username), letters)

guess.present? ? TelegramMessenger.suggestion(guess) : TelegramMessenger.no_suggestions(letters)
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170216144448_add_user_id_to_guess.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserIdToGuess < ActiveRecord::Migration
def change
add_column :guesses, :user_id, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20161223235906) do
ActiveRecord::Schema.define(version: 20170216144448) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -56,6 +56,7 @@
t.datetime "updated_at", null: false
t.string "username"
t.boolean "suggestion", default: false
t.integer "user_id"
end

create_table "hints", force: :cascade do |t|
Expand Down
2 changes: 1 addition & 1 deletion spec/services/game_engine_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rails_helper'

describe GameEngineService, type: :service do
let!(:channel) { Random.rand }
let!(:channel) { Random.rand(@MAX_INT_VALUE) }

it 'create a game with specified secret word' do
game = GameEngineService.create_by_word(channel, :telegram, 'magic')
Expand Down
4 changes: 2 additions & 2 deletions spec/services/game_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'rails_helper'

describe GameService, type: :service do
let!(:user) { '@Amig0' }
let!(:channel) { 169778030 }
let!(:channel) { Random.rand(@MAX_INT_VALUE) }
let!(:user) { User.new(Random.rand(@MAX_INT_VALUE), '@Amig0') }

context 'with game not started' do
context 'with a secret word' do
Expand Down
5 changes: 3 additions & 2 deletions spec/services/telegram/command/guess_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Telegram::Command::Guess, type: :service do
context 'given created game' do
let!(:user) { '@Amig0' }
let!(:user) { User.new(id = Random.rand, name = '@Amig0') }
let!(:channel) { 'telegram-game-channel' }

let!(:game) { create(:game, :telegram, secret: 'secret', channel: channel) }
Expand All @@ -13,7 +13,8 @@
allow(GameService).to receive(:find_by_channel!).and_return(game)

message.stub_chain(:chat, :id).and_return(channel)
message.stub_chain(:from, :username).and_return(user)
message.stub_chain(:from, :id).and_return(user.id)
message.stub_chain(:from, :username).and_return(user.name)

allow(TelegramMessenger).to receive(:guess)
end
Expand Down
Loading

0 comments on commit 39aff40

Please sign in to comment.