Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lockhart committed Jan 6, 2012
1 parent 9efea05 commit 18c7785
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions client_base.rb
Expand Up @@ -47,6 +47,7 @@ def get_discovery_capability
#Registers a user with the given name and waits for the server to respond with the created user
def create_user(name)
@mutex.synchronize do
@name = name
@reg_response = new_sub(@discovery['registration_response']['url'],
@discovery['registration_response']['capability'])
@command_id = rand(99999999)
Expand All @@ -62,6 +63,10 @@ def create_user(name)
end
@create_player_channel.publish(hsh.to_json)
end
wait_for_player_to_be_created
end

def wait_for_player_to_be_created
while true
should_exit = false
@mutex.synchronize do
Expand All @@ -70,6 +75,7 @@ def create_user(name)
break if should_exit
sleep 1
end
@reg_response.stop_listening if @reg_response
end

#Callback for whenever a user is created. If the created user was created by this client,
Expand Down Expand Up @@ -154,7 +160,7 @@ def winner_update(parsed)
#Callback for a table being created.. joins the table if we created it
def my_table_created(m)
resp = JSON.parse(m)
if resp['command_id'] == @table_command_id
if resp['command_id'] == @player_id
join_specific_table(resp)
end
end
Expand All @@ -165,16 +171,14 @@ def my_table_created(m)
# @param [String] blinds Starting small blind for the table
def create_table(name, min_players = 2, blinds = 1)
raise "No player yet!" unless @player_id
@reg_response.stop_listening if @reg_response
@table_command_id = rand(99999999)
@table_response = new_sub(@discovery['tables']['url'],
@discovery['tables']['capability'])
@create_table_channel = new_channel(@discovery['create_table']['url'],
@discovery['create_table']['capability'])
@table_response.last = (Time.now.to_i * 1000) - 5
@table_response.add_listener('table_response') {|m| my_table_created(m)}
@table_response.start_listening
@create_table_channel.publish({'name' => name, 'id' => @table_command_id,
@create_table_channel.publish({'name' => name, 'id' => @player_id,
'min_players' => min_players, 'blinds' => blinds}.to_json)
end

Expand Down
4 changes: 2 additions & 2 deletions poker.rb
Expand Up @@ -53,10 +53,10 @@ def send_game_state(no_active = false)
base_hash = {'hand_number' => @hand_number, 'table_id' => @table_id,
'state' => @table.table_state_hash(no_active), 'type' => 'game_state'}
@channel_hash.each do |player, channel|
player_hash = {}
player_hash = {'player' => player.to_hash}
if !@table.queue.include?(player) and @table.hands[player]
hand = @table.hands[player].map {|x| x.to_hash}
player_hash = {'hand' => hand, 'seat_number' => @table.player_position(player), 'player' => player.to_hash}
player_hash.merge!({'hand' => hand, 'seat_number' => @table.player_position(player)})
end
channel.publish(base_hash.merge(player_hash).to_json)
end
Expand Down

0 comments on commit 18c7785

Please sign in to comment.