Class: Mastermind::Player

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/mastermind/player.rb,
lib/mastermind/extensions/continue_later.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#get_input, #send_message

Constructor Details

#initialize(response: nil, player: nil) ⇒ Player

attr_accessor :name, :trials, :time_taken, :date_played



7
8
9
10
# File 'lib/mastermind/player.rb', line 7

def initialize(response: nil, player: nil)
  @response = response || Message.new
  set_attr player if player
end

Instance Attribute Details

#color_comboObject (readonly)

Returns the value of attribute color_combo.



66
67
68
# File 'lib/mastermind/extensions/continue_later.rb', line 66

def color_combo
  @color_combo
end

#date_playedObject (readonly)

Returns the value of attribute date_played.



4
5
6
# File 'lib/mastermind/player.rb', line 4

def date_played
  @date_played
end

#guessesObject (readonly)

Returns the value of attribute guesses.



4
5
6
# File 'lib/mastermind/player.rb', line 4

def guesses
  @guesses
end

#guesses_at_pause_countObject (readonly)

Returns the value of attribute guesses_at_pause_count.



66
67
68
# File 'lib/mastermind/extensions/continue_later.rb', line 66

def guesses_at_pause_count
  @guesses_at_pause_count
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/mastermind/player.rb', line 4

def name
  @name
end

#recordObject (readonly)

Returns the value of attribute record.



4
5
6
# File 'lib/mastermind/player.rb', line 4

def record
  @record
end

#save_game_workerObject (readonly)

Returns the value of attribute save_game_worker.



66
67
68
# File 'lib/mastermind/extensions/continue_later.rb', line 66

def save_game_worker
  @save_game_worker
end

#time_at_game_saveObject (readonly)

Returns the value of attribute time_at_game_save.



66
67
68
# File 'lib/mastermind/extensions/continue_later.rb', line 66

def time_at_game_save
  @time_at_game_save
end

#time_startedObject (readonly)

Returns the value of attribute time_started.



66
67
68
# File 'lib/mastermind/extensions/continue_later.rb', line 66

def time_started
  @time_started
end

#time_takenObject (readonly)

Returns the value of attribute time_taken.



4
5
6
# File 'lib/mastermind/player.rb', line 4

def time_taken
  @time_taken
end

Instance Method Details

#make_save_attr(time_started, color_combo, guesses_at_pause_count) ⇒ Object



76
77
78
79
80
81
# File 'lib/mastermind/extensions/continue_later.rb', line 76

def make_save_attr(time_started, color_combo, guesses_at_pause_count)
  @time_started = time_started
  @color_combo = color_combo
  @guesses_at_pause_count = guesses_at_pause_count
  @time_at_game_save = Time.now.to_i
end

#old_to_hObject



67
68
69
70
71
72
73
74
# File 'lib/mastermind/extensions/continue_later.rb', line 67

def to_h
  player = Hash.new(nil)
  player[:name] = @name
  player[:guesses] = @guesses
  player[:time_taken] = @time_taken
  player[:date_played] = @date_played
  player
end

#save_game(*game_data) ⇒ Object



69
70
71
72
73
74
# File 'lib/mastermind/extensions/continue_later.rb', line 69

def save_game(*game_data)
  @save_game_worker ||= SaveGame.new
  make_save_attr(game_data[0], game_data[1], game_data[2]) unless game_data.empty?
  @save_game_worker.save_record(self)

end

#set_attr(input) ⇒ Object



12
13
14
15
16
17
# File 'lib/mastermind/player.rb', line 12

def set_attr(input)
  @name = input[:name] if input[:name]
  @guesses = input[:guesses] if input[:guesses]
  @time_taken = input[:time_taken] if input[:time_taken]
  @date_played = input[:date_played] if input[:date_played]
end

#set_save_attr(game_data = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/mastermind/extensions/continue_later.rb', line 92

def set_save_attr(game_data = {})
  # check if game_data is a game or a hash
  @name = game_data[:name]
  @time_started = game_data[:time_started]
  @color_combo = game_data[:color_combo]
  @guesses_at_pause_count = game_data[:guesses_at_pause_count]
  @time_at_game_save = game_data[:time_at_game_save]
  @guesses = @time_taken = nil
end

#timeObject



23
24
25
26
27
28
29
# File 'lib/mastermind/player.rb', line 23

def time
  if @time_taken
    mins = @time_taken/60
    secs = @time_taken%60
    "#{mins}m#{secs}s"
  end
end

#to_hObject



31
32
33
34
35
36
37
38
# File 'lib/mastermind/player.rb', line 31

def to_h
  player = Hash.new(nil)
  player[:name] = @name
  player[:guesses] = @guesses
  player[:time_taken] = @time_taken
  player[:date_played] = @date_played
  player
end

#winner_responseObject



19
20
21
# File 'lib/mastermind/player.rb', line 19

def winner_response
  @response.winner(@name, @guesses, time).message
end