Class: Beintoo::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/beintoo/player.rb

Constant Summary collapse

RESOURCE =
"player"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Player

Returns a new instance of Player.



6
7
8
9
# File 'lib/beintoo/player.rb', line 6

def initialize(params = {})
  self.guid = params[:guid]
  self
end

Instance Attribute Details

#b_userObject

Returns the value of attribute b_user.



4
5
6
# File 'lib/beintoo/player.rb', line 4

def b_user
  @b_user
end

#guidObject

Returns the value of attribute guid.



4
5
6
# File 'lib/beintoo/player.rb', line 4

def guid
  @guid
end

#logged_inObject

Returns the value of attribute logged_in.



4
5
6
# File 'lib/beintoo/player.rb', line 4

def logged_in
  @logged_in
end

#scoreObject

Returns the value of attribute score.



4
5
6
# File 'lib/beintoo/player.rb', line 4

def score
  @score
end

Instance Method Details

#assign_rewardObject



100
101
102
# File 'lib/beintoo/player.rb', line 100

def assign_reward
  Beintoo::Vgood.byplayer(self)
end

#balance(contest = :default) ⇒ Object



96
97
98
# File 'lib/beintoo/player.rb', line 96

def balance(contest = :default)
  score[contest][:balance] rescue nil
end

#best_score(contest = :default) ⇒ Object



92
93
94
# File 'lib/beintoo/player.rb', line 92

def best_score(contest = :default)
  score[contest][:bestscore] rescue nil
end

#get_player(_guid = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/beintoo/player.rb', line 54

def get_player(_guid = nil)
  self.guid = _guid unless _guid.nil?
  raise Beintoo::ApiException, "No guid setted when calling get_player" if guid.nil?
  headers = Beintoo::build_headers
  result = Beintoo::get "#{RESOURCE}/byguid/#{guid}", headers
  if result.has_key? :playerScore
    self.score = result[:playerScore]
  end
end

#last_score(contest = :default) ⇒ Object



88
89
90
# File 'lib/beintoo/player.rb', line 88

def last_score(contest = :default)
  score[contest][:lastscore] rescue nil
end

#logged_in?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/beintoo/player.rb', line 40

def logged_in?
  !!logged_in
end

#login(_guid = nil, publicname = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/beintoo/player.rb', line 19

def (_guid = nil, publicname = nil)
  self.guid = _guid unless _guid.nil?
  headers = {}
  headers[:guid] = guid unless guid.nil?
  headers = Beintoo::build_headers headers

  params_get = {}
  params_get[:language] = 1
  params_get[:publicname] = publicname unless publicname.nil?
  result = Beintoo::get "#{RESOURCE}/login", headers, params_get
  self.guid = result["guid"]
  self.logged_in = true
  if result.has_key? :playerScore
    self.score = result[:playerScore]
  end
  if result.has_key? "user"
    self.b_user = Beintoo::User.new result["user"].merge(guid: guid)
    self.b_user.created = true
  end
end

#logoutObject



44
45
46
47
48
49
50
51
52
# File 'lib/beintoo/player.rb', line 44

def logout
  return true if !logged_in?
  headers = {}
  headers[:guid] = guid
  headers = Beintoo::build_headers headers

  result = Beintoo::get "#{RESOURCE}/logout", headers
  self.logged_in = false
end

#rewards_iframe_urlObject



104
105
106
# File 'lib/beintoo/player.rb', line 104

def rewards_iframe_url
  "http://www.beintoo.com/m/marketplace.html?apikey=#{Beintoo::apikey}&guid=#{guid}"
end

#submit_score(score, code_id = nil, balance = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/beintoo/player.rb', line 64

def submit_score(score, code_id = nil, balance = nil)
  raise Beintoo::ApiException, "User not logged in when trying to submit score" unless logged_in?
  headers = {
    guid:   guid,
  }
  headers[:codeID] = code_id unless code_id.nil?
  headers = Beintoo::build_headers headers
  body = {lastScore: score}
  body[:balance] = balance unless balance.nil?
  if Beintoo.debug
    Beintoo.log "Submitting score of #{score} for player with guid #{guid}"
    Beintoo.log body.inspect
  end
  result = Beintoo::get "#{RESOURCE}/submitscore/", headers, body
  if result.has_key? :playerScore
    self.score = result[:playerScore]
  end
  if result[:message] == "OK" && result[:messageID] == 0
    true
  else
    false
  end
end

#user(params = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/beintoo/player.rb', line 11

def user(params = {})
  raise Beintoo::ApiException, "No guid set when creating new Beintoo::User" if guid.nil?
  if b_user.nil?
    self.b_user = Beintoo::User.new params.merge(guid: guid)
  end
  b_user
end