Skip to content

Commit

Permalink
Added files and tests for a controller and helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorrill10 committed Nov 5, 2011
1 parent fbfc911 commit 94b1a4f
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 42 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -58,6 +58,9 @@ group :development, :test do

# Factory gem
gem 'factory_girl_rails'

# Higher level testing/acceptance requirements
gem 'cucumber'
end

# JavaScript runtime
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Expand Up @@ -53,6 +53,12 @@ GEM
execjs
coffee-script-source (1.1.2)
configuration (1.3.1)
cucumber (1.1.1)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
gherkin (~> 2.6.0)
json (>= 1.4.6)
term-ansicolor (>= 1.0.6)
daemon_controller (0.2.6)
diff-lcs (1.1.2)
em-websocket (0.3.1)
Expand All @@ -69,6 +75,8 @@ GEM
railties (>= 3.0.0)
fastthread (1.0.7)
ffi (1.0.9)
gherkin (2.6.2)
json (>= 1.4.6)
guard (0.8.8)
thor (~> 0.14.6)
guard-bundler (0.1.3)
Expand Down Expand Up @@ -173,6 +181,7 @@ GEM
stalker (0.9.0)
beanstalk-client
json_pure
term-ansicolor (1.0.7)
therubyracer (0.9.8)
libv8 (~> 3.3.10)
thor (0.14.6)
Expand All @@ -199,6 +208,7 @@ DEPENDENCIES
bson_ext (~> 1.4)
capybara
coffee-rails (~> 3.1.1)
cucumber
em-websocket
factory_girl_rails
guard-bundler
Expand Down
6 changes: 3 additions & 3 deletions Guardfile
@@ -1,8 +1,8 @@


guard 'rspec', :version => 2 do
#watch(%r{^spec/.+_spec\.rb$})
#watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
#watch('spec/spec_helper.rb') { "spec" }

# Rails example
Expand All @@ -13,7 +13,7 @@ guard 'rspec', :version => 2 do
# To watch lib/bots/proxy_bot/
watch(%r{^(spec/)?lib/bots/proxy_bot/(.*?/*?.+?)(_spec)?\.rb$}) { |m| "spec/lib/bots/proxy_bot/#{m[2]}_spec.rb" }

#watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
#watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
#watch('spec/spec_helper.rb') { "spec" }
#watch('config/routes.rb') { "spec/routing" }
Expand Down
22 changes: 15 additions & 7 deletions app/controllers/new_game_controller.rb
Expand Up @@ -6,6 +6,9 @@
require 'application_defs'
require 'application_helper'

# Local classes
require 'match'

# Controller for the 'start a new game' view.
class NewGameController < ApplicationController
include ApplicationDefs
Expand All @@ -19,15 +22,20 @@ def index

# Starts a new two-player limit game.
def two_player_limit
(@port_number,
@match_name,
@game_definition_file_name,
@number_of_hands,
@random_seed,
@player_names) = two_player_limit_params
@match_params = two_player_limit_params params

dealer_arguments = [@match_name, @game_definition_file_name.to_s, @number_of_hands.to_s, @random_seed.to_s, @player_names.split(/\s*,?\s+/)].flatten
dealer_arguments = [@match_params[:match_name],
@match_params[:game_definition_file_name].to_s,
@match_params[:number_of_hands].to_s,
@match_params[:random_seed].to_s,
@match_params[:player_names].split(/\s*,?\s+/)].flatten

# Initialize a match
match = Match.create(parameters: @match_parameters)

## Start the player that represents the browser operator
#Stalker.enqueue("Game.start", :id => match.id.to_s)
#
# TODO Make sure the background server is started at this point

# Start the dealer
Expand Down
5 changes: 1 addition & 4 deletions app/controllers/player_actions_controller.rb
Expand Up @@ -42,11 +42,8 @@ def index
:player_names => player_names
}

# Initialize a game table record
match = Match.create(game_arguments)

# Start the player that represents the browser operator
Stalker.enqueue("Game.start", :id => match.id.to_s)
#Stalker.enqueue("Game.start", :id => match.id.to_s)

# Wait for the player to start and catch errors

Expand Down
17 changes: 9 additions & 8 deletions app/helpers/application_helper.rb
Expand Up @@ -54,22 +54,23 @@ def hidden_game_parameter_form
end
end

# @return [Array] An array containing the game parameters.
def two_player_limit_params
port_number = if params[:port_number] then params[:port_number] else 18791 end
match_name = if params[:match_name] then params[:match_name] else 'default' end
# @param [Hash] params Parameters from the view.
# @return [Hash] A hash containing the game parameters.
def two_player_limit_params(params)
port_number = params[:port_number] || 18791
match_name = params[:match_name] || 'default'
game_definition_file_name = GAME_DEFINITION_FILE_NAMES[:two_player_limit_texas_holdem_poker]
number_of_hands = if params[:number_of_hands] then params[:number_of_hands] else 1 end
number_of_hands = params[:number_of_hands] || 1
random_seed = if params[:random_seed] then
params[:random_seed]
else
# TODO not sure what the maximum random seed should be
# @todo not sure what the maximum random seed should be
if NO_RANDOM then 1 else rand 100 end
end

player_names = if params[:player_names] then params[:player_names] else "user, p2" end
player_names = params[:player_names] || 'user, p2'

[port_number, match_name, game_definition_file_name, number_of_hands, random_seed, player_names]
{port_number: port_number, match_name: match_name, game_definition_file_name: game_definition_file_name, number_of_hands: number_of_hands, random_seed: random_seed, player_names: player_names}
end

end
4 changes: 4 additions & 0 deletions features/step_definitions/mongoid_steps.rb
@@ -0,0 +1,4 @@
Given(/^an? (.+) exists with an? (.+) of "([^"]*)"$/) do |model, field, value|
factory_name = model.gsub(' ', '_')
Factory factory_name, field => value
end
21 changes: 21 additions & 0 deletions features/support/env.rb
@@ -0,0 +1,21 @@
ENV["RAILS_ENV"] ||= "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')

require 'cucumber/formatter/unicode'
require 'cucumber/rails/rspec'
require 'cucumber/rails/world'
require 'cucumber/web/tableish'

require 'capybara/rails'
require 'capybara/cucumber'
require 'capybara/session'
require 'cucumber/rails/capybara_javascript_emulation'

Capybara.default_selector = :css

ActionController::Base.allow_rescue = false

require 'factory_girl'
require 'factory_girl/step_definitions'
Dir[File.expand_path(File.join(File.dirname(__FILE__),'..','..',
'spec','factories','*.rb'))].each {|f| require f}
3 changes: 3 additions & 0 deletions features/support/hooks.rb
@@ -0,0 +1,3 @@
Before do |scenario|
Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
end
2 changes: 1 addition & 1 deletion lib/application_defs.rb
Expand Up @@ -2,7 +2,7 @@
# Assortment of constant definitions and methods for generating default values.
module ApplicationDefs
# @return [Boolean] Debugging mode switch.
DEBUG = true
DEBUG = false

# @return [Integer] The user's index in the array of Player.
USERS_INDEX = 0;
Expand Down
5 changes: 4 additions & 1 deletion lib/background/worker.rb
Expand Up @@ -42,14 +42,17 @@
# Store the port numbers in the database so the web app. can access them
match = Match.find match_id
match.port_numbers = port_numbers

puts 'Started dealer and exiting from Stalker job'
end


Stalker.job('Start WebApplicationPlayerProxy') do |match_id, host_name, port_number, game_definition_file_name|
dealer_information = DealerInformation.new host_name, port_number

match_id = args[:match_id]
match = Match.find match_id
@match_id_to_web_application_player_proxy_map[match_id] = WebApplicationPlayerProxy.new match, dealer_information
@match_id_to_web_application_player_proxy_map[match_id] = WebApplicationPlayerProxy.new match, dealer_information, game_definition_file_name
end

# @todo Catch errors
Expand Down
60 changes: 47 additions & 13 deletions spec/controllers/new_game_controller_spec.rb
Expand Up @@ -2,19 +2,53 @@

describe NewGameController do

#describe "GET 'index'" do
# it "should be successful" do
# get 'index'
# response.should be_success
# end
#end
#
#describe "GET 'two_player_limit'" do
# it "should be successful" do
# get 'two_player_limit'
# response.should be_success
# end
#end
describe "GET 'index'" do
it "should be successful" do
get_page_and_check_success 'index'
end
end

describe "GET 'two_player_limit'" do
it 'should be successful' do
get_page_and_check_success 'two_player_limit'
end
it 'collects arguments from the view' do
pending 'set variables as if they would from the view and check @match_params'

get_page_and_check_success 'two_player_limit'


params).should be == (
{port_number: 18791, match_name: 'default',
game_definition_file_name: GAME_DEFINITION_FILE_NAMES[:two_player_limit_texas_holdem_poker],
number_of_hands: 1, random_seed: params[:random_seed], player_names: 'user, p2'})
end
end
it 'creates a new Match' do
pending 'figure out how to validate the Match input'

match = mock('Match')
Match.stubs(:create).returns(match)
Match.expects(:create).once.with()
end
it 'starts a dealer instance in the background' do
pending 'Try mocking Stalker.enqueue'
end
it 'queries the Match for the port numbers' do
pending
end
it 'starts a player proxy in the background' do
pending
end
it 'starts the other players in the match' do
pending
end
end

def get_page_and_check_success(page_name)
get page_name
response.should be_success
end
#
#describe "GET 'two_player_no_limit'" do
# it "should be successful" do
Expand Down
9 changes: 9 additions & 0 deletions spec/factories.rb
@@ -0,0 +1,9 @@

FactoryGirl.define do
factory :match do
port_numbers [9001, 9002]
parameters {}
state ''
pot {}
end
end
17 changes: 17 additions & 0 deletions spec/helpers/application_helper_spec.rb
@@ -0,0 +1,17 @@
require 'spec_helper'

require File.expand_path('../../../lib/application_defs', __FILE__)

describe ApplicationHelper do
include ApplicationDefs

describe '#two_player_limit_params' do
it 'returns a game parameter hash filled with all the necessary arguments to start a two-player limit match properly defined to defaults' do
params = {random_seed: 1}
helper.two_player_limit_params(params).should be == (
{port_number: 18791, match_name: 'default',
game_definition_file_name: GAME_DEFINITION_FILE_NAMES[:two_player_limit_texas_holdem_poker],
number_of_hands: 1, random_seed: params[:random_seed], player_names: 'user, p2'})
end
end
end
@@ -1,11 +1,11 @@
require 'spec_helper'

# Local classes
require File.expand_path('../../../../../lib/bots/proxy_bot/communication_logic/acpc_dealer_communicator', __FILE__)
require File.expand_path('../../../../../lib/bots/proxy_bot/communication_logic/action_sender', __FILE__)
require File.expand_path('../../../../../lib/bots/proxy_bot/domain_types/card', __FILE__)
require File.expand_path('../../../../../lib/bots/proxy_bot/communication_logic/matchstate_string_receiver', __FILE__)
require File.expand_path('../../../../../lib/game/dealer_information', __FILE__)
require File.expand_path('../../../../lib/bots/proxy_bot/communication_logic/acpc_dealer_communicator', __FILE__)
require File.expand_path('../../../../lib/bots/proxy_bot/communication_logic/action_sender', __FILE__)
require File.expand_path('../../../../lib/bots/proxy_bot/domain_types/card', __FILE__)
require File.expand_path('../../../../lib/bots/proxy_bot/communication_logic/matchstate_string_receiver', __FILE__)
require File.expand_path('../../../../lib/game/dealer_information', __FILE__)

describe WebApplicationPlayerProxy do
#before(:each) do
Expand Down

0 comments on commit 94b1a4f

Please sign in to comment.