Class: Remember

Inherits:
Object
  • Object
show all
Defined in:
lib/game_codebreaker/remember.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRemember

Returns a new instance of Remember.



5
6
7
# File 'lib/game_codebreaker/remember.rb', line 5

def initialize
  @users = []
end

Instance Attribute Details

#usersObject

Returns the value of attribute users.



3
4
5
# File 'lib/game_codebreaker/remember.rb', line 3

def users
  @users
end

Class Method Details

.load(path) ⇒ Object



43
44
45
# File 'lib/game_codebreaker/remember.rb', line 43

def self.load(path)
  Marshal.load File.open(path, 'r').read if File.exists?(path)
end

Instance Method Details

#add_games(object) ⇒ Object



9
10
11
12
# File 'lib/game_codebreaker/remember.rb', line 9

def add_games( object )
  @users.each { |user| @user = user if user.hash == object.hash }
  object.games.each { |game| @user.games << game } 
end

#exists?(object) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/game_codebreaker/remember.rb', line 14

def exists?( object )
  @users.each { |user| return true if user.hash == object.hash }
  false
end

#get_user(object) ⇒ Object



19
20
21
22
# File 'lib/game_codebreaker/remember.rb', line 19

def get_user( object )
  @users.each { |user| @user = user if user.hash == object.hash }
  @user
end

#info(user) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/game_codebreaker/remember.rb', line 24

def info( user )
  a = []
  a << name = user.name
  a << surname = user.surname
  a << age = user.age
  a << total_game = user.games.size
  array_win = user.games.select { |game| game.win == true }
  a << total_win = array_win.size
  a << total_losses = ( user.games.select { |game| game.win == false } ).size
  average_turns = 0 and array_win.each { |game| average_turns += game.turns }
  a << average_turns /= total_win
  average_level = 0 and user.games.select { |game| average_level += game.level }
  a << average_level /= total_game
end

#save(path) ⇒ Object



39
40
41
# File 'lib/game_codebreaker/remember.rb', line 39

def save(path)
  File.open(path, 'w') { |f| f.write Marshal.dump self }
end