Class: Chingu::HighScoreList

Inherits:
Object
  • Object
show all
Defined in:
lib/chingu/high_score_list.rb

Overview

Highscore-class

  • Keeps a local YAML file with highscores, default highscores.yml in root game dir.

  • Add, delete, clear highscores

  • Iterate through highscores with simple HighScores#each

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HighScoreList

Create a new high score list with 0 entries



36
37
38
39
40
41
# File 'lib/chingu/high_score_list.rb', line 36

def initialize(options = {})
  @file = options[:file] || "high_score_list.yml"
  @size = options[:size] || 100
  @sort_on = options[:sort_on] || :score
  @high_scores = Array.new      
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file



31
32
33
# File 'lib/chingu/high_score_list.rb', line 31

def file
  @file
end

#high_scoresObject (readonly)

Returns the value of attribute high_scores



31
32
33
# File 'lib/chingu/high_score_list.rb', line 31

def high_scores
  @high_scores
end

Class Method Details

.load(options = {}) ⇒ Object

Create a new high score list and try to load content from :file-parameter If no :file is given, HighScoreList tries to load from file “high_score_list.yml”



47
48
49
50
51
52
# File 'lib/chingu/high_score_list.rb', line 47

def self.load(options = {})
  require 'yaml'
  high_score_list = HighScoreList.new(options)
  high_score_list.load
  return high_score_list
end

Instance Method Details

#[](index) ⇒ Object

Direct access to invidual high scores



101
102
103
# File 'lib/chingu/high_score_list.rb', line 101

def [](index)
  @high_scores[index]
end

#add(data) ⇒ Object Also known as: <<

Adda a new high score to the local file 'data' is a hash of key/value-pairs that needs to contain at least the keys :name and :score Returns the position it got in the list, with 1 beeing the first positions



59
60
61
62
63
64
65
# File 'lib/chingu/high_score_list.rb', line 59

def add(data)
  raise "No :name value in high score!"   if data[:name].nil?
  raise "No :score value in high score!"  if data[:score].nil?
  add_to_list(force_symbol_hash(data))
  save_to_file
  position_by_score(data[:score])
end

#eachObject

Iterate through all high scores



108
109
110
# File 'lib/chingu/high_score_list.rb', line 108

def each
  @high_scores.each { |high_score| yield high_score }
end

#each_with_indexObject



112
113
114
# File 'lib/chingu/high_score_list.rb', line 112

def each_with_index
  @high_scores.each_with_index { |high_score, index| yield high_score, index }
end

#loadObject

Load data from previously specified @file



93
94
95
96
# File 'lib/chingu/high_score_list.rb', line 93

def load
  @high_scores = YAML.load_file(@file)  if File.exists?(@file)
  @high_scores = @high_scores[0..@size]
end

#position_by_data(data) ⇒ Object

Returns the position of full data-hash data entry, used internally



71
72
73
74
# File 'lib/chingu/high_score_list.rb', line 71

def position_by_data(data)
  position = @high_scores.rindex(data)
  position += 1 if position
end

#position_by_score(score) ⇒ Object

Returns the position 'score' would get in among the high scores:

@high_score_list.position_by_score(999999999) # most likely returns 1 for the number one spot
@high_score_list.position_by_score(1)         # most likely returns nil since no placement is found (didn't make it to the high scores)


81
82
83
84
85
86
87
88
# File 'lib/chingu/high_score_list.rb', line 81

def position_by_score(score)
  position = 1
  @high_scores.each do |high_score|
    return position   if score >= high_score[:score]
    position += 1
  end
  return nil
end

#save_to_fileObject

Save high score data into previously specified @file



119
120
121
122
123
124
# File 'lib/chingu/high_score_list.rb', line 119

def save_to_file
  require 'yaml'
  File.open(@file, 'w') do |out|
    YAML.dump(@high_scores, out)
  end
end