class PirateGame::Client
Constants
- STATES
Attributes
bridge[R]
command_start[R]
The time the last command was issued
completion_time[RW]
current_action[R]
The command the client is waiting for
log_book[R]
Log of messages sent
slop_bucket[R]
Bucket for data being sent from game master
state[R]
The state of the client. See STATES
.
Public Class Methods
default_name()
click to toggle source
# File lib/pirate_game/client.rb, line 55 def self.default_name "Blackbeard" end
new(opts={})
click to toggle source
Calls superclass method
# File lib/pirate_game/client.rb, line 38 def initialize(opts={}) opts[:protocol] ||= PirateGame::Protocol.default super(opts.merge({:verbose => true})) set_state :select_game @bridge = nil @command_start = nil @command_thread = nil @completion_time = PirateGame::Boot.config["action_duration"] @current_action = nil @log_book = PirateGame::LogBook.new @slop_bucket = {} end
Public Instance Methods
action_time_left()
click to toggle source
# File lib/pirate_game/client.rb, line 59 def action_time_left return 0 unless waiting? @command_start - Time.now + @completion_time end
broadcast(msg)
click to toggle source
# File lib/pirate_game/client.rb, line 125 def broadcast(msg) each_client {|remote| remote.say(msg, @name) } end
clicked(button)
click to toggle source
# File lib/pirate_game/client.rb, line 71 def clicked button renewer = Rinda::SimpleRenewer.new @completion_time @mothership.write [:button, button, Time.now.to_i, DRb.uri], renewer end
end_game(data)
click to toggle source
# File lib/pirate_game/client.rb, line 106 def end_game data set_state :end @slop_bucket[:end_game] = data end
issue_command(item=nil)
click to toggle source
# File lib/pirate_game/client.rb, line 77 def issue_command item=nil item ||= @bridge.sample_item if @bridge return unless item @command_thread = Thread.new do wait_for_action item end Thread.pass until @command_start # this should be a proper barrier @current_action = "#{PirateCommand.action} the #{item}" end
perform_action(item, time, from)
click to toggle source
Sends action message to Game Master indicating that action has been successfully performed
# File lib/pirate_game/client.rb, line 119 def perform_action item, time, from if @mothership @mothership.write [:action, item, time, from] end end
register()
click to toggle source
Calls superclass method
# File lib/pirate_game/client.rb, line 91 def register set_state :pub super end
renewer()
click to toggle source
# File lib/pirate_game/client.rb, line 133 def renewer PirateGame::TimeoutRenewer.new @completion_time end
return_to_pub()
click to toggle source
# File lib/pirate_game/client.rb, line 101 def return_to_pub @bridge = nil set_state :pub end
say(msg, name)
click to toggle source
# File lib/pirate_game/client.rb, line 129 def say(msg, name) @log_book.add msg, name || 'unknown' end
set_state(state)
click to toggle source
# File lib/pirate_game/client.rb, line 65 def set_state state raise RuntimeError, "invalid state #{state}" unless STATES.include? state @state = state end
start_stage(bridge, all_items)
click to toggle source
# File lib/pirate_game/client.rb, line 96 def start_stage(bridge, all_items) @bridge = PirateGame::Bridge.new(bridge, all_items) set_state :stage end
teammates()
click to toggle source
# File lib/pirate_game/client.rb, line 112 def teammates registered_services.collect{|name,_| name} end
wait_for_action(item)
click to toggle source
# File lib/pirate_game/client.rb, line 137 def wait_for_action item @command_start = Time.now now = @command_start.to_i Thread.pass from = nil Timeout.timeout @completion_time do _, _, _, from = @mothership.read [:button, item, (now...now + 30), nil], renewer end perform_action item, Time.now, from rescue Rinda::RequestExpiredError, Timeout::Error ensure @command_thread = nil @command_start = nil @current_action = nil end
waiting?()
click to toggle source
# File lib/pirate_game/client.rb, line 159 def waiting? @command_thread and @command_thread.alive? and @command_start end