class PlaylyfeClient::V2::Player

Attributes

alias[R]
enabled[R]
id[R]

Public Class Methods

create(pl_hash, game) click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 9
def self.create(pl_hash, game)
  unless pl_hash.has_key?(:id) && pl_hash.has_key?(:alias)
    fail PlaylyfeClient::PlayerError.new("{\"error\": \"keys missing\", \"error_description\": \"Player's hash is missing values for keys :id and :alias!\"}")
  end  
  
  p=self.new( game.connection.post_create_player( pl_hash.select { |key, value| [:id, :alias].include?(key) } ), game )
  game.players.add(p)
  p
end
new(player_hash, game) click to toggle source
Calls superclass method PlaylyfeClient::Player::new
# File lib/playlyfe_client/v2/player.rb, line 71
def initialize(player_hash, game)
  super(game)
  @id=player_hash[:id] || player_hash["id"]
  @alias=player_hash[:alias] || player_hash["alias"]
  @enabled=player_hash[:enabled] || player_hash["enabled"] || false
end

Public Instance Methods

enabled?() click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 23
def enabled?
  enabled
end
events(start_time=nil,end_time=nil) click to toggle source

results are cached if start_time is nil, otherwise, direct call to Playlyfe is made

# File lib/playlyfe_client/v2/player.rb, line 61
def events(start_time=nil,end_time=nil)
  if start_time.nil?
    @events ||= PlaylyfeClient::V2::EventCollection.new(game, game.connection.get_player_events_array(self.id), self)
  else  
    PlaylyfeClient::V2::EventCollection.new(game, game.connection.get_player_events_array(self.id, start_time, end_time), self)
  end
end
name() click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 19
def name
  @alias
end
play(action, variables={}) click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 27
def play(action, variables={})
  action.play_by(self, variables)
  reload!
end
players_leaderboards() click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 56
def players_leaderboards
  game.leaderboards.for_players
end
reload!() click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 32
def reload!
  @profile_hash= game.connection.get_full_player_profile_hash(self.id)
  @scores=fill_scores
end
roles_in_team(team) click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 44
def roles_in_team(team)
  teams.empty? ? [] : (@teams_roles[team.id].nil? ? [] : @teams_roles[team.id])
end
scores(force_refill=false) click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 37
def scores(force_refill=false)
  if (!defined?(@scores) || force_refill)
    @scores=fill_scores
  end
  @scores  
end
teams() click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 48
def teams
  @teams||= fill_teams
end
teams_leaderboards() click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 52
def teams_leaderboards
  game.leaderboards.for_teams
end

Private Instance Methods

fill_scores() click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 100
def fill_scores
  scores={points: {} ,sets: {}, states: {}, compounds: {}}
  profile_hash["scores"].each do |score|
    case score["metric"]["type"]
      when "point"
        scores[:points][score["metric"]["id"].to_sym]=score["value"].to_i
      when "set"
        scores[:sets][score["metric"]["id"].to_sym]=sets_metric_value_from(score["value"])
      when "state"
        scores[:states][score["metric"]["id"].to_sym]=score["value"]["name"] #"value": { "name": "Hell", "description": "The plane of demons" }
      when "compound"
        scores[:compounds][score["metric"]["id"].to_sym]=score["value"].to_i
    end

  end  
  scores
end
fill_teams() click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 82
def fill_teams
  teams=[]
  @teams_roles={}
  profile_hash["teams"].each do |team_hash|
    team=game.teams.find(team_hash["id"])

    #all teams should be listed in game, so if nothing is found raise exception
    if team.nil?
      fail PlaylyfeClient::PlayerError.new("{\"error\": \"Team not found\", \"error_description\": \"Team '#{team_hash["id"]}' from #{self.id} player profile was not found between game.teams!\"}")
    end  
    
    teams << team
    @teams_roles[team.id]=team_hash["roles"]

  end  
  teams
end
profile_hash() click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 78
def profile_hash
  @profile_hash||= game.connection.get_full_player_profile_hash(self.id)
end
sets_metric_value_from(value) click to toggle source
# File lib/playlyfe_client/v2/player.rb, line 118
def sets_metric_value_from(value)
  (value.each.collect {|item| {name: item["name"], count: item["count"].to_i} } )
end