Class: PirateGame::GameMaster

Inherits:
Shuttlecraft::Mothership
  • Object
show all
Defined in:
lib/pirate_game/game_master.rb

Constant Summary collapse

MIN_PLAYERS =

for now

1
MAX_PLAYERS =
4
STATES =
[:pending, :startable, :playing, :ended]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GameMaster

Returns a new instance of GameMaster.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pirate_game/game_master.rb', line 28

def initialize(opts={})
  opts[:protocol] ||= PirateGame::Protocol.default

  super(opts.merge({:verbose => true}))

  set_state :pending

  @last_update  = Time.at 0
  @num_players  = 0
  @player_names = []
  @stage        = nil
  @stage_ary    = []

  @action_watcher = create_action_watcher
end

Instance Attribute Details

#num_playersObject (readonly)

Number of players in the game. Call #update to refresh



21
22
23
# File 'lib/pirate_game/game_master.rb', line 21

def num_players
  @num_players
end

#player_namesObject (readonly)

Names of players in the game. Call #update to refresh



26
27
28
# File 'lib/pirate_game/game_master.rb', line 26

def player_names
  @player_names
end

#stageObject

Returns the value of attribute stage.



11
12
13
# File 'lib/pirate_game/game_master.rb', line 11

def stage
  @stage
end

#stage_aryObject

Returns the value of attribute stage_ary.



11
12
13
# File 'lib/pirate_game/game_master.rb', line 11

def stage_ary
  @stage_ary
end

#stateObject (readonly)

The state of the game. See STATES.



16
17
18
# File 'lib/pirate_game/game_master.rb', line 16

def state
  @state
end

Instance Method Details

#allow_registration?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/pirate_game/game_master.rb', line 104

def allow_registration?
  return (@stage.nil? && @num_players < MAX_PLAYERS)
end

#create_action_watcherObject



173
174
175
176
177
178
179
# File 'lib/pirate_game/game_master.rb', line 173

def create_action_watcher
  Thread.new do
    loop do
      handle_action @ts.take([:action, nil, nil, nil])
    end
  end
end

#game_infoObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pirate_game/game_master.rb', line 71

def game_info
  return if @stage_ary.empty?

  info = "Game Rundown:\n"
  gr = game_rundown

  info << "Total Actions: #{gr[:total_actions]}\n"

  gr[:player_breakdown].each do |player_uri, actions|
    info << "#{player_uri}: #{actions}\n"
  end

  info
end

#game_rundownObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pirate_game/game_master.rb', line 86

def game_rundown
  return {} if @stage_ary.empty?

  rundown = {
    :total_stages => @stage_ary.length,
    :total_actions => @stage_ary.inject(0) {|sum,stage| sum += stage.actions_completed},
    :player_breakdown => {}}

  for stage in @stage_ary
    stage.player_stats.each_pair do |key, value|
      rundown[:player_breakdown][key] ||= 0
      rundown[:player_breakdown][key] += value.size
    end
  end

  rundown
end

#handle_action(action_array) ⇒ Object



181
182
183
184
185
# File 'lib/pirate_game/game_master.rb', line 181

def handle_action action_array
  if @stage && @stage.in_progress?
    @stage.complete action_array[1], action_array[3]
  end
end

#increment_stageObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pirate_game/game_master.rb', line 115

def increment_stage
  @stage =
    if @stage
      @stage.increment
    else
      PirateGame::Stage.new 1, @num_players
    end

  @stage_ary << @stage
  @stage
end

#on_registrationObject



127
128
129
# File 'lib/pirate_game/game_master.rb', line 127

def on_registration
  set_state :startable if startable?
end

#registrations_textObject



44
45
46
# File 'lib/pirate_game/game_master.rb', line 44

def registrations_text
  "Num Players: #{@num_players}\n#{@player_names.join(', ')}\n"
end

#send_end_game_to_clientsObject



167
168
169
170
171
# File 'lib/pirate_game/game_master.rb', line 167

def send_end_game_to_clients
  each_client do |client|
    client.end_game game_rundown
  end
end

#send_return_to_pub_to_clientsObject



161
162
163
164
165
# File 'lib/pirate_game/game_master.rb', line 161

def send_return_to_pub_to_clients
  each_client do |client|
    client.return_to_pub
  end
end

#send_stage_info_to_clientsObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/pirate_game/game_master.rb', line 139

def send_stage_info_to_clients
  if @stage.in_progress?
    set_state :playing
    send_start_to_clients

  elsif @stage.success?
    set_state :startable
    send_return_to_pub_to_clients

  elsif @stage.failure?
    set_state :ended
    send_end_game_to_clients
  end
end

#send_start_to_clientsObject



154
155
156
157
158
159
# File 'lib/pirate_game/game_master.rb', line 154

def send_start_to_clients
  each_client do |client|
    bridge = @stage.bridge_for_player
    client.start_stage(bridge, @stage.all_items)
  end
end

#stage_infoObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pirate_game/game_master.rb', line 48

def stage_info
  return unless @stage

  info = "Stage #{@stage.level}: \n"
  if @stage.in_progress?
    info << "Actions: #{@stage.actions_completed}\n"
    info << "Time Left: #{@stage.time_left.to_i} seconds\n"
  else
    info << "Status: #{@stage.status}\n"

    rundown = @stage.rundown

    info << "Actions: #{rundown[:total_actions]}\n"

    rundown[:player_breakdown].each do |player_uri, actions|
      info << "#{player_uri}: #{actions}\n"
    end

  end

  info
end

#startObject



131
132
133
134
135
136
137
# File 'lib/pirate_game/game_master.rb', line 131

def start
  return unless startable?

  increment_stage

  return true
end

#startable?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
# File 'lib/pirate_game/game_master.rb', line 108

def startable?
  update!
  return (@stage.nil? || @stage.success?) &&
         @num_players >= MIN_PLAYERS &&
         @num_players <= MAX_PLAYERS
end

#updateObject

Retrieves the latest data from the TupleSpace.



190
191
192
193
194
195
196
197
198
199
# File 'lib/pirate_game/game_master.rb', line 190

def update
  ret = super
  return if ret.nil?

  @num_players = @registered_services_ary.length

  @player_names = @registered_services_ary.map { |name,| name }

  ret
end