Class: PirateGame::Stage

Inherits:
Object
  • Object
show all
Defined in:
lib/pirate_game/stage.rb

Constant Summary collapse

ITEMS_PER_BRIDGE =
6
DURATION =
PirateGame::Boot.config["stage_duration"]
IN_PROGRESS =
'In Progress'
SUCCESS =
'Success'
FAILURE =
'Failure'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level, players) ⇒ Stage

Returns a new instance of Stage.



19
20
21
22
23
24
25
26
27
# File 'lib/pirate_game/stage.rb', line 19

def initialize(level, players)
  @level = level
  @players = players
  @actions_completed = 0
  @player_stats = {}
  generate_all_items

  @begin_time = Time.now
end

Instance Attribute Details

#actions_completedObject

Returns the value of attribute actions_completed.



5
6
7
# File 'lib/pirate_game/stage.rb', line 5

def actions_completed
  @actions_completed
end

#all_itemsObject

Returns the value of attribute all_items.



7
8
9
# File 'lib/pirate_game/stage.rb', line 7

def all_items
  @all_items
end

#begin_timeObject

Returns the value of attribute begin_time.



8
9
10
# File 'lib/pirate_game/stage.rb', line 8

def begin_time
  @begin_time
end

#levelObject

Returns the value of attribute level.



9
10
11
# File 'lib/pirate_game/stage.rb', line 9

def level
  @level
end

#player_statsObject

Returns the value of attribute player_stats.



6
7
8
# File 'lib/pirate_game/stage.rb', line 6

def player_stats
  @player_stats
end

#playersObject

Returns the value of attribute players.



10
11
12
# File 'lib/pirate_game/stage.rb', line 10

def players
  @players
end

Instance Method Details

#bridge_for_playerObject



67
68
69
# File 'lib/pirate_game/stage.rb', line 67

def bridge_for_player
  @boards.shift
end

#complete(action, from) ⇒ Object



71
72
73
74
75
# File 'lib/pirate_game/stage.rb', line 71

def complete action, from
  @actions_completed += 1
  @player_stats[from] ||= []
  @player_stats[from] << action
end

#failure?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/pirate_game/stage.rb', line 53

def failure?
  status == FAILURE
end

#generate_all_itemsObject



57
58
59
60
61
62
63
64
65
# File 'lib/pirate_game/stage.rb', line 57

def generate_all_items
  @all_items = []

  while @all_items.length < @players*ITEMS_PER_BRIDGE
    thing = PirateCommand.thing
    @all_items << thing unless @all_items.include?(thing)
  end
  @boards = @all_items.each_slice(ITEMS_PER_BRIDGE).to_a
end

#in_progress?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/pirate_game/stage.rb', line 45

def in_progress?
  status == IN_PROGRESS
end

#incrementObject



29
30
31
# File 'lib/pirate_game/stage.rb', line 29

def increment
  PirateGame::Stage.new self.level + 1, self.players
end

#passed?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/pirate_game/stage.rb', line 81

def passed?
  @actions_completed >= required_actions
end

#required_actionsObject



77
78
79
# File 'lib/pirate_game/stage.rb', line 77

def required_actions
  @level * 2 + 1
end

#rundownObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/pirate_game/stage.rb', line 85

def rundown
  return if status == IN_PROGRESS

  rundown = {stage: @level, total_actions: @actions_completed}
  rundown[:player_breakdown] = {}

  @player_stats.each {|p,v| rundown[:player_breakdown][p] = v.size}

  rundown
end

#statusObject



37
38
39
40
41
42
43
# File 'lib/pirate_game/stage.rb', line 37

def status
  if time_left > 0
    IN_PROGRESS
  else
    passed? ? SUCCESS : FAILURE
  end
end

#success?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/pirate_game/stage.rb', line 49

def success?
  status == SUCCESS
end

#time_leftObject



33
34
35
# File 'lib/pirate_game/stage.rb', line 33

def time_left
  [0, (begin_time + DURATION) - Time.now].max
end