Sha256: 332cdaba4f51311cbf477a1e85306e513dd457ac243359fd213082c163b4b253

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

require_relative "../leaderboard.rb"

module PlaylyfeClient
  module V2
    class Leaderboard < PlaylyfeClient::Leaderboard
      
      attr_reader :entity_type, :metric, :scope, :cycles

      private 
      
        def initialize(lbd_hash, game)
          super(game)

          @id=lbd_hash[:id] || lbd_hash["id"]
          @name=lbd_hash[:name] || lbd_hash["name"]
          @entity_type=lbd_hash[:entity_type] || lbd_hash["entity_type"]
          @metric=lbd_hash[:metric] || lbd_hash["metric"]
          @scope=lbd_hash[:scope] || lbd_hash["scope"]
          @cycles=lbd_hash[:cycles] || lbd_hash["cycles"]

          fill_positions(lbd_hash[:data] || lbd_hash["data"] || [])
        end
        
        #positions[x] is array of entities
        #If there are 3 entities at rank 4, there will be array with 3 items at positions[3]
        #Positions[4] and positions[5] will be []
        #Playlyfe have this style of ranking too ( #1, #2, #3, #4, #4, #4, #7)
        def fill_positions(data)  
          data.each do |pos|
            rank=(pos[:rank]).to_i - 1
            score=pos[:score] || 0
            entity= pos[:entity] || nil
            
            if @positions[rank].nil?
              @positions[rank] = [{entity: entity, score: score}]
            else
              @positions[rank] << {entity: entity, score: score}
            end  
          end  

          #fill empty positions with []
          @positions.each_with_index {|p,i| @positions[i]=[] if p.nil? }

          @positions
        end  

    end
  end
end  

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
playlyfe_client-1.1.6 lib/playlyfe_client/v2/leaderboard.rb
playlyfe_client-1.1.5 lib/playlyfe_client/v2/leaderboard.rb
playlyfe_client-1.1.3 lib/playlyfe_client/v2/leaderboard.rb
playlyfe_client-1.1.2 lib/playlyfe_client/v2/leaderboard.rb
playlyfe_client-1.1.1 lib/playlyfe_client/v2/leaderboard.rb
playlyfe_client-1.1.0 lib/playlyfe_client/v2/leaderboard.rb
playlyfe_client-1.0.6 lib/playlyfe_client/v2/leaderboard.rb