Class: Chingu::OnlineHighScoreList

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

Overview

Online high score list, remotely synced to www.gamvercv.com’s RESTful web api.

  • fetch high scores, add new ones

  • Iterate through highscores with each and each_with_index

This class is the reason for the 'crack' and 'rest_client' dependencies.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ OnlineHighScoreList

Returns a new instance of OnlineHighScoreList



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chingu/online_high_score_list.rb', line 34

def initialize(options = {})
  @limit = options[:limit] || 100
  @sort_on = options[:sort_on] || :score
  @login = options[:login] || options[:user]
  @password = options[:password]
  @game_id = options[:game_id]
  
  begin
    require 'rest_client'
    require 'crack/xml'
  rescue
    puts "HighScoreList requires 2 gems, please install with:"
    puts "gem install rest-client"
    puts "gem install crack"
    exit
  end
  
  @resource = RestClient::Resource.new("http://api.gamercv.com/games/#{@game_id}/high_scores", 
                                          :user => @login, :password => @password, :timeout => 20, :open_timeout => 5)
                                          
  @high_scores = Array.new  # Keeping a local copy in a ruby array
end

Instance Attribute Details

#high_scoresObject (readonly)

Returns the value of attribute high_scores



32
33
34
# File 'lib/chingu/online_high_score_list.rb', line 32

def high_scores
  @high_scores
end

#resourceObject (readonly)

Returns the value of attribute resource



32
33
34
# File 'lib/chingu/online_high_score_list.rb', line 32

def resource
  @resource
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”



61
62
63
64
65
# File 'lib/chingu/online_high_score_list.rb', line 61

def self.load(options = {})
  high_score_list = OnlineHighScoreList.new(options)
  high_score_list.load
  return high_score_list     
end

Instance Method Details

#[](index) ⇒ Object

Direct access to @high_scores-array



140
141
142
# File 'lib/chingu/online_high_score_list.rb', line 140

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

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

POSTs a new high score to the remote web service

'data' is a hash of key/value-pairs that can contain :name - player-name, could be “AAA” or “Aaron Avocado” :score - the score :text - free text, up to 255 chars,

Returns the position the new score got in the high score list. return 1 for number one spot. returns -1 if it didn't quallify as a high scores.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chingu/online_high_score_list.rb', line 78

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?
  begin
    @res = @resource.post({:high_score => data})
    data = Crack::XML.parse(@res)
    add_to_list(force_symbol_hash(data["high_score"]))
  rescue RestClient::RequestFailed
    puts "RequestFailed: couldn't add high score"
  rescue RestClient::ResourceNotFound
    return -1
  rescue RestClient::Unauthorized
    puts "Unauthorized to add high score (check :login and :password arguments)"
  end
  return data["high_score"]["position"]
end

#eachObject

Iterate through @high_scores-array with each



147
148
149
# File 'lib/chingu/online_high_score_list.rb', line 147

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

#each_with_indexObject

Iterate through @high_scores-array with each_with_index



154
155
156
# File 'lib/chingu/online_high_score_list.rb', line 154

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

#loadObject

Load data from remove web service. Under the hood, this is accomplished through a simple REST-interface The returned XML-data is converted into a simple Hash (@high_scores), which is also returned from this method.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chingu/online_high_score_list.rb', line 115

def load
  raise "You need to specify a Game_id to load a remote high score list"    unless defined?(@game_id)
  raise "You need to specify a Login to load a remote high score list"      unless defined?(@login)
  raise "You need to specify a Password to load a remote high score list"   unless defined?(@password)
  
  @high_scores.clear
  begin
    res = @resource.get
    data = Crack::XML.parse(res)
    if data["high_scores"]
      data["high_scores"].each do |high_score|
        @high_scores.push(force_symbol_hash(high_score))
      end
    end
  rescue RestClient::ResourceNotFound
    puts "Couldn't find Resource, did you specify a correct :game_id ?"
  end
  
  @high_scores = @high_scores[0..@limit-1] unless @high_scores.empty?
  return @high_scores
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)


101
102
103
104
105
106
107
108
# File 'lib/chingu/online_high_score_list.rb', line 101

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