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

Requires gems ‘crack’ and ‘rest_client’, included on initialize.

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
# 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]

  require 'rest_client'
  require 'crack/xml'
  @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”



53
54
55
56
57
# File 'lib/chingu/online_high_score_list.rb', line 53

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



132
133
134
# File 'lib/chingu/online_high_score_list.rb', line 132

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chingu/online_high_score_list.rb', line 70

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



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

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

#each_with_indexObject

Iterate through @high_scores-array with each_with_index



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

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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chingu/online_high_score_list.rb', line 107

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)


93
94
95
96
97
98
99
100
# File 'lib/chingu/online_high_score_list.rb', line 93

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