Skip to content

Instantly share code, notes, and snippets.

@Sephi-Chan
Created August 26, 2013 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sephi-Chan/1efbb48fcb54a152dae0 to your computer and use it in GitHub Desktop.
Save Sephi-Chan/1efbb48fcb54a152dae0 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe Dispatcher do
let(:communication_client) { stub }
let(:format_2_3) { FactoryGirl.build(:format, :two_teams_of_three) }
let(:corwin) { FactoryGirl.create(:player, name: 'Corwin') }
let(:mandor) { FactoryGirl.create(:player, name: 'Mandor') }
let(:caine) { FactoryGirl.create(:player, name: 'Caine') }
let(:corwin_invitation) { FactoryGirl.build(:invitation, :accepted, player: corwin) }
let(:mandor_invitation) { FactoryGirl.build(:invitation, :accepted, player: mandor) }
let(:caine_invitation) { FactoryGirl.build(:invitation, :accepted, player: caine) }
let(:first_game_search) { FactoryGirl.create(:game_search, :ready, invitations: [ corwin_invitation, mandor_invitation, caine_invitation ], format: format_2_3) }
let(:brand) { FactoryGirl.create(:player, name: 'Brand') }
let(:bleys) { FactoryGirl.create(:player, name: 'Bleys') }
let(:julian) { FactoryGirl.create(:player, name: 'Julian') }
let(:brand_invitation) { FactoryGirl.build(:invitation, :accepted, player: brand) }
let(:bleys_invitation) { FactoryGirl.build(:invitation, :accepted, player: bleys) }
let(:julian_invitation) { FactoryGirl.build(:invitation, :accepted, player: julian) }
let(:second_game_search) { FactoryGirl.create(:game_search, :ready, invitations: [ brand_invitation, bleys_invitation, julian_invitation ], format: format_2_3) }
let(:fiona) { FactoryGirl.create(:player, name: 'Fiona') }
let(:deirdre) { FactoryGirl.create(:player, name: 'Deirdre') }
let(:flora) { FactoryGirl.create(:player, name: 'Flora') }
let(:fiona_invitation) { FactoryGirl.build(:invitation, :accepted, player: fiona) }
let(:deirdre_invitation) { FactoryGirl.build(:invitation, :accepted, player: deirdre) }
let(:flora_invitation) { FactoryGirl.build(:invitation, :accepted, player: flora) }
let(:third_game_search) { FactoryGirl.create(:game_search, :ready, invitations: [ fiona_invitation, deirdre_invitation, flora_invitation ], format: format_2_3) }
let(:random) { FactoryGirl.create(:player, name: 'Random') }
let(:gerard) { FactoryGirl.create(:player, name: 'Gerard') }
let(:dworkin) { FactoryGirl.create(:player, name: 'Dworkin') }
let(:random_invitation) { FactoryGirl.build(:invitation, :accepted, player: random) }
let(:gerard_invitation) { FactoryGirl.build(:invitation, :accepted, player: gerard) }
let(:dworkin_invitation) { FactoryGirl.build(:invitation, :accepted, player: dworkin) }
let(:fourth_game_search) { FactoryGirl.create(:game_search, :ready, invitations: [ random_invitation, gerard_invitation, dworkin_invitation ], format: format_2_3) }
it 'composes no game when there is only 1 ready team' do
games = Dispatcher.new([ first_game_search ], communication_client).dispatch!
games.should be_empty
end
it 'composes 1 game when 2 teams are ready for the same 2-teams format' do
common_keys = { event: Queues::MatchMaker::GAME_SEARCH_SUCCEEDED, status: GameSearch::SUCCEEDED, game_id: anything }
expected_hash_1 = hash_including(common_keys.merge(game_search_id: first_game_search.id, players: [ { id: corwin.id, name: corwin.name }, { id: mandor.id, name: mandor.name }, { id: caine.id, name: caine.name } ]))
communication_client.should_receive(:push).with(corwin, expected_hash_1)
communication_client.should_receive(:push).with(mandor, expected_hash_1)
communication_client.should_receive(:push).with(caine, expected_hash_1)
expected_hash_2 = hash_including(common_keys.merge(game_search_id: second_game_search.id, players: [ { id: brand.id, name: brand.name }, { id: bleys.id, name: bleys.name }, { id: julian.id, name: julian.name } ]))
communication_client.should_receive(:push).with(brand, expected_hash_2)
communication_client.should_receive(:push).with(bleys, expected_hash_2)
communication_client.should_receive(:push).with(julian, expected_hash_2)
game_searches = [ first_game_search, second_game_search ]
games = Dispatcher.new(game_searches, communication_client).dispatch!
games[0].teams[0].players.should == [ corwin, mandor, caine ]
games[0].teams[0].position.should == 0
games[0].teams[1].players.should == [ brand, bleys, julian ]
games[0].teams[1].position.should == 1
end
it 'compose 2 games when 4 teams are ready for the same 2-teams format' do
common_keys = { event: Queues::MatchMaker::GAME_SEARCH_SUCCEEDED, status: GameSearch::SUCCEEDED, game_id: anything }
expected_hash_1 = hash_including(common_keys.merge(game_search_id: first_game_search.id, players: [ { id: corwin.id, name: corwin.name }, { id: mandor.id, name: mandor.name }, { id: caine.id, name: caine.name } ]))
communication_client.should_receive(:push).with(corwin, expected_hash_1)
communication_client.should_receive(:push).with(mandor, expected_hash_1)
communication_client.should_receive(:push).with(caine, expected_hash_1)
expected_hash_2 = hash_including(common_keys.merge(game_search_id: second_game_search.id, players: [ { id: brand.id, name: brand.name }, { id: bleys.id, name: bleys.name }, { id: julian.id, name: julian.name } ]))
communication_client.should_receive(:push).with(brand, expected_hash_2)
communication_client.should_receive(:push).with(bleys, expected_hash_2)
communication_client.should_receive(:push).with(julian, expected_hash_2)
expected_hash_3 = hash_including(common_keys.merge(game_search_id: third_game_search.id, players: [ { id: fiona.id, name: fiona.name }, { id: deirdre.id, name: deirdre.name }, { id: flora.id, name: flora.name } ]))
communication_client.should_receive(:push).with(fiona, expected_hash_3)
communication_client.should_receive(:push).with(deirdre, expected_hash_3)
communication_client.should_receive(:push).with(flora, expected_hash_3)
expected_hash_4 = hash_including(common_keys.merge(game_search_id: fourth_game_search.id, players: [ { id: random.id, name: random.name }, { id: gerard.id, name: gerard.name }, { id: dworkin.id, name: dworkin.name } ]))
communication_client.should_receive(:push).with(random, expected_hash_4)
communication_client.should_receive(:push).with(gerard, expected_hash_4)
communication_client.should_receive(:push).with(dworkin, expected_hash_4)
game_searches = [ first_game_search, second_game_search, third_game_search, fourth_game_search ]
games = Dispatcher.new(game_searches, communication_client).dispatch!
games[0].teams[0].players.should == [ corwin, mandor, caine ]
games[0].teams[0].position.should == 0
games[0].teams[1].players.should == [ brand, bleys, julian ]
games[0].teams[1].position.should == 1
games[1].teams[0].players.should == [ fiona, deirdre, flora ]
games[1].teams[0].position.should == 0
games[1].teams[1].players.should == [ random, gerard, dworkin ]
games[1].teams[1].position.should == 1
end
it 'compose 1 games when 3 teams are ready for the same 2-teams format' do
common_keys = { event: Queues::MatchMaker::GAME_SEARCH_SUCCEEDED, status: GameSearch::SUCCEEDED, game_id: anything }
expected_hash_1 = hash_including(common_keys.merge(game_search_id: first_game_search.id, players: [ { id: corwin.id, name: corwin.name }, { id: mandor.id, name: mandor.name }, { id: caine.id, name: caine.name } ]))
communication_client.should_receive(:push).with(corwin, expected_hash_1)
communication_client.should_receive(:push).with(mandor, expected_hash_1)
communication_client.should_receive(:push).with(caine, expected_hash_1)
expected_hash_2 = hash_including(common_keys.merge(game_search_id: second_game_search.id, players: [ { id: brand.id, name: brand.name }, { id: bleys.id, name: bleys.name }, { id: julian.id, name: julian.name } ]))
communication_client.should_receive(:push).with(brand, expected_hash_2)
communication_client.should_receive(:push).with(bleys, expected_hash_2)
communication_client.should_receive(:push).with(julian, expected_hash_2)
game_searches = [ first_game_search, second_game_search, third_game_search ]
games = Dispatcher.new(game_searches, communication_client).dispatch!
games[0].teams[0].players.should == [ corwin, mandor, caine ]
games[0].teams[1].players.should == [ brand, bleys, julian ]
games[0].game_searches.should == [ first_game_search, second_game_search]
end
it 'composes no game since the 2 teams has players in common' do
corwin_invitation = FactoryGirl.build(:invitation, :accepted, player: corwin)
mandor_invitation = FactoryGirl.build(:invitation, :accepted, player: mandor)
caine_invitation = FactoryGirl.build(:invitation, :accepted, player: caine)
first_game_search = FactoryGirl.create(:game_search, :ready, invitations: [ corwin_invitation, mandor_invitation, caine_invitation ], format: format_2_3)
corwin_other_invitation = FactoryGirl.build(:invitation, :accepted, player: corwin)
bleys_invitation = FactoryGirl.build(:invitation, :accepted, player: bleys)
julian_invitation = FactoryGirl.build(:invitation, :accepted, player: julian)
second_game_search = FactoryGirl.create(:game_search, :ready, invitations: [ corwin_other_invitation, bleys_invitation, julian_invitation ], format: format_2_3)
game_searches = [ first_game_search, second_game_search ]
games = Dispatcher.new(game_searches, communication_client).dispatch!
games.should be_empty
end
it 'composes one game with searches 1 and 3 since team 2 has players in common with 1' do
communication_client.should_receive(:push).exactly(6).times
corwin_invitation = FactoryGirl.build(:invitation, :accepted, player: corwin)
mandor_invitation = FactoryGirl.build(:invitation, :accepted, player: mandor)
caine_invitation = FactoryGirl.build(:invitation, :accepted, player: caine)
first_game_search = FactoryGirl.create(:game_search, :ready, invitations: [ corwin_invitation, mandor_invitation, caine_invitation ], format: format_2_3)
corwin_other_invitation = FactoryGirl.build(:invitation, :accepted, player: corwin)
bleys_invitation = FactoryGirl.build(:invitation, :accepted, player: bleys)
julian_invitation = FactoryGirl.build(:invitation, :accepted, player: julian)
second_game_search = FactoryGirl.create(:game_search, :ready, invitations: [ corwin_other_invitation, bleys_invitation, julian_invitation ], format: format_2_3)
fiona_invitation = FactoryGirl.build(:invitation, :accepted, player: fiona)
deirdre_invitation = FactoryGirl.build(:invitation, :accepted, player: deirdre)
flora_invitation = FactoryGirl.build(:invitation, :accepted, player: flora)
third_game_search = FactoryGirl.create(:game_search, :ready, invitations: [ fiona_invitation, deirdre_invitation, flora_invitation ], format: format_2_3)
game_searches = [ first_game_search, second_game_search, third_game_search ]
games = Dispatcher.new(game_searches, communication_client).dispatch!
games[0].teams[0].players.should == [ corwin, mandor, caine ]
games[0].teams[1].players.should == [ fiona, deirdre, flora ]
end
end
require 'spec_helper'
describe Handlers::NewGameSearch do
before { FactoryGirl.create(:format, :two_teams_of_three) }
let(:communication_client) { stub }
let(:handler) { Handlers::NewGameSearch.new(communication_client) }
let(:corwin) { FactoryGirl.create(:player, name: 'Corwin') }
let(:mandor) { FactoryGirl.create(:player, name: 'Mandor') }
let(:caine) { FactoryGirl.create(:player, name: 'Caine') }
let(:message) { {
'sender_id' => corwin.id,
'player_ids' => [ mandor.id, caine.id ],
'format' => { 'teams_count' => 2, 'teams_size' => 3 }
} }
it 'creates a new invitation and notify players' do
sender_hash = hash_including(event: Queues::MatchMaker::GAME_SEARCH_CREATED, game_search_id: anything)
guest_hash = hash_including(event: Queues::MatchMaker::GAME_SEARCH_CREATED, game_search_id: anything, invitation_id: anything, sender: { id: corwin.id, name: corwin.name }, format: { teams_count: 2, teams_size: 3 })
communication_client.should_receive(:push).with(corwin, sender_hash)
communication_client.should_receive(:push).with(mandor, guest_hash)
communication_client.should_receive(:push).with(caine, guest_hash)
handler.handle(message)
Format.count.should == 1
invitation = GameSearch.first
invitation.should be_waiting
invitation.invitations[0].player.should == corwin
invitation.invitations[1].player.should == mandor
invitation.invitations[2].player.should == caine
invitation.format.teams_size.should == 3
invitation.format.teams_count.should == 2
end
it 'fails when these exact same players already share an invitation' do
communication_client.should_receive(:push).exactly(3).times # The messages expected in the previous test.
communication_client.should_receive(:push).with(corwin, hash_including(event: Queues::MatchMaker::GAME_SEARCH_NOT_CREATED, error: Errors::MatchMaker::PLAYERS_LOCKED))
handler.handle(message)
handler.handle(message)
GameSearch.count.should == 1
end
context 'Self invitation' do
it 'fails when a player invites himself' do
message = {
'sender_id' => corwin.id,
'player_ids' => [ corwin.id, mandor.id ],
'format' => { 'teams_count' => 2, 'teams_size' => 3 }
}
communication_client.should_receive(:push).with(corwin, hash_including(event: Queues::MatchMaker::GAME_SEARCH_NOT_CREATED, error: Errors::MatchMaker::SELF_INVITATION))
handler.handle(message)
GameSearch.count.should == 0
end
end
context 'Number of guests do not match the format' do
it 'fails when a player invites too few players' do
message = {
'sender_id' => corwin.id,
'player_ids' => [ mandor.id ],
'format' => { 'teams_count' => 2, 'teams_size' => 3 }
}
communication_client.should_receive(:push).with(corwin, hash_including(event: Queues::MatchMaker::GAME_SEARCH_NOT_CREATED, error: Errors::MatchMaker::TEAM_SIZE_MISMATCH))
handler.handle(message)
GameSearch.count.should == 0
end
it 'fails when a same guest is given many times' do
message = {
'sender_id' => corwin.id,
'player_ids' => [ mandor.id, mandor.id ],
'format' => { 'teams_count' => 2, 'teams_size' => 3 }
}
communication_client.should_receive(:push).with(corwin, hash_including(event: Queues::MatchMaker::GAME_SEARCH_NOT_CREATED, error: Errors::MatchMaker::TEAM_SIZE_MISMATCH))
handler.handle(message)
GameSearch.count.should == 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment