playlyfe_client 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +27 -0
  3. data/Gemfile +9 -0
  4. data/Gemfile.lock +50 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +97 -0
  7. data/Rakefile +10 -0
  8. data/lib/playlyfe_client/action.rb +32 -0
  9. data/lib/playlyfe_client/connection.rb +200 -0
  10. data/lib/playlyfe_client/errors.rb +42 -0
  11. data/lib/playlyfe_client/game.rb +33 -0
  12. data/lib/playlyfe_client/leaderboard.rb +27 -0
  13. data/lib/playlyfe_client/metric.rb +14 -0
  14. data/lib/playlyfe_client/player.rb +80 -0
  15. data/lib/playlyfe_client/team.rb +26 -0
  16. data/lib/playlyfe_client/v2/action.rb +59 -0
  17. data/lib/playlyfe_client/v2/collection/action_collection.rb +30 -0
  18. data/lib/playlyfe_client/v2/collection/leaderboard_collection.rb +43 -0
  19. data/lib/playlyfe_client/v2/collection/metric_collection.rb +30 -0
  20. data/lib/playlyfe_client/v2/collection/player_collection.rb +30 -0
  21. data/lib/playlyfe_client/v2/collection/team_collection.rb +31 -0
  22. data/lib/playlyfe_client/v2/collection.rb +60 -0
  23. data/lib/playlyfe_client/v2/connection.rb +109 -0
  24. data/lib/playlyfe_client/v2/game.rb +71 -0
  25. data/lib/playlyfe_client/v2/leaderboard/players_leaderboard.rb +30 -0
  26. data/lib/playlyfe_client/v2/leaderboard/teams_leaderboard.rb +30 -0
  27. data/lib/playlyfe_client/v2/leaderboard/unknown_leaderboard.rb +23 -0
  28. data/lib/playlyfe_client/v2/leaderboard.rb +38 -0
  29. data/lib/playlyfe_client/v2/metric/compound_metric.rb +31 -0
  30. data/lib/playlyfe_client/v2/metric/point_metric.rb +27 -0
  31. data/lib/playlyfe_client/v2/metric/set_metric.rb +41 -0
  32. data/lib/playlyfe_client/v2/metric/state_metric.rb +26 -0
  33. data/lib/playlyfe_client/v2/metric.rb +47 -0
  34. data/lib/playlyfe_client/v2/player.rb +135 -0
  35. data/lib/playlyfe_client/v2/team.rb +58 -0
  36. data/lib/playlyfe_client/version.rb +3 -0
  37. data/lib/playlyfe_client.rb +11 -0
  38. data/lib/string_addons.rb +9 -0
  39. data/playlyfe_client.gemspec +30 -0
  40. metadata +194 -0
@@ -0,0 +1,26 @@
1
+ require_relative "../metric.rb"
2
+
3
+ module PlaylyfeClient
4
+ module V2
5
+ class StateMetric < PlaylyfeClient::V2::Metric
6
+ attr_reader :states
7
+
8
+ private
9
+
10
+ def initialize(metric_hash, game)
11
+ super(metric_hash, game)
12
+
13
+ fill_states(metric_hash["states"])
14
+ end
15
+
16
+
17
+ def fill_states(states_hash)
18
+ @states=[]
19
+ states_hash.each_pair do |key, value|
20
+ @states << {name: key , description: value["description"]}
21
+ end
22
+ @states
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,47 @@
1
+ require_relative "../metric.rb"
2
+
3
+ module PlaylyfeClient
4
+ module V2
5
+ class Metric < PlaylyfeClient::Metric
6
+
7
+ attr_reader :id, :name, :type, :description
8
+
9
+ def self.create_from(metric_hash,game)
10
+ require_relative "./metric/compound_metric.rb"
11
+ require_relative "./metric/point_metric.rb"
12
+ require_relative "./metric/set_metric.rb"
13
+ require_relative "./metric/state_metric.rb"
14
+
15
+ type=metric_hash[:type] || metric_hash["type"]
16
+ case type
17
+ when "set"
18
+ PlaylyfeClient::V2::SetMetric.new(metric_hash,game)
19
+ when "state"
20
+ PlaylyfeClient::V2::StateMetric.new(metric_hash,game)
21
+ when "point"
22
+ PlaylyfeClient::V2::PointMetric.new(metric_hash,game)
23
+ when "compound"
24
+ PlaylyfeClient::V2::CompoundMetric.new(metric_hash,game)
25
+ else
26
+ fail PlaylyfeClient::MetricError.new("{\"error\": \"Unrecognized type\", \"error_description\": \"Class for metric type: '#{type}' from #{metric_hash} is unrecognized!\"}")
27
+ end
28
+
29
+ end
30
+
31
+ def apply_reward(reward, scores)
32
+ fail PlaylyfeClient::MetricError.new("{\"error\": \"Rewards can not be applyed!\", \"error_description\": \"For these metric, direct action rewards cannot be applied. Probably some rules based awards are used.\"}")
33
+ end
34
+
35
+ private
36
+
37
+ def initialize(metric_hash, game)
38
+ super(game)
39
+ @id=metric_hash[:id] || metric_hash["id"]
40
+ @name=metric_hash[:name] || metric_hash["name"]
41
+ @type=metric_hash[:type] || metric_hash["type"]
42
+ @description=metric_hash[:description] || metric_hash["description"]
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,135 @@
1
+ require_relative "../player.rb"
2
+
3
+ module PlaylyfeClient
4
+ module V2
5
+ class Player < PlaylyfeClient::Player
6
+
7
+ attr_reader :enabled ,:alias, :id
8
+
9
+ def self.create(pl_hash, game)
10
+ unless pl_hash.has_key?(:id) && pl_hash.has_key?(:alias)
11
+ fail PlaylyfeClient::PlayerError.new("{\"error\": \"keys missing\", \"error_description\": \"Player's hash is missing values for keys :id and :alias!\"}")
12
+ end
13
+
14
+ p=self.new( game.connection.post_create_player( pl_hash.select { |key, value| [:id, :alias].include?(key) } ), game )
15
+ game.players.add(p)
16
+ p
17
+ end
18
+
19
+ def enabled?
20
+ enabled
21
+ end
22
+
23
+ def play(action)
24
+ begin
25
+ game.connection.post_play_action(action.id, self.id)
26
+ @profile_hash= game.connection.get_full_player_profile_hash(self.id)
27
+ @scores=fill_scores
28
+ rescue PlaylyfeClient::ActionRateLimitExceededError => e
29
+ unless game.ignore_rate_limit_errors
30
+ fail e
31
+ end
32
+ end
33
+ end
34
+
35
+ def scores
36
+ @scores||=fill_scores
37
+ end
38
+
39
+ def roles_in_team(team)
40
+ teams.empty? ? [] : (@teams_roles[team.id].nil? ? [] : @teams_roles[team.id])
41
+ end
42
+
43
+ def teams
44
+ @teams||= fill_teams
45
+ end
46
+
47
+ def teams_leaderboards
48
+ game.leaderboards.for_teams
49
+ end
50
+
51
+ def players_leaderboards
52
+ game.leaderboards.for_players
53
+ end
54
+
55
+ def activities(start_time=nil,end_time=nil)
56
+ unless defined?(@activities)
57
+ #loading all activities for player (not restricted to some time period, ALL of them!)
58
+ @activities = build_activities_from(game.connection.get_full_activity_feed_array(self.id,self.game.created_at,Time.now.utc))
59
+ end
60
+
61
+ activities=@activities
62
+
63
+ if start_time
64
+ activities=activities.select {|act| act["timestamp"] > start_time.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ") }
65
+ end
66
+
67
+ if end_time
68
+ activities=activities.select {|act| act["timestamp"] < end_time.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ") }
69
+ end
70
+
71
+ return activities
72
+ end
73
+
74
+ private
75
+
76
+ def initialize(player_hash, game)
77
+ super(game)
78
+ @id=player_hash[:id] || player_hash["id"]
79
+ @alias=player_hash[:alias] || player_hash["alias"]
80
+ @enabled=player_hash[:enabled] || player_hash["enabled"] || false
81
+ end
82
+
83
+ def profile_hash
84
+ @profile_hash||= game.connection.get_full_player_profile_hash(self.id)
85
+ end
86
+
87
+ def fill_teams
88
+ teams=[]
89
+ @teams_roles={}
90
+ profile_hash["teams"].each do |team_hash|
91
+ team=game.teams.find(team_hash["id"])
92
+
93
+ #all teams should be listed in game, so if nothing is found raise exception
94
+ if team.nil?
95
+ 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!\"}")
96
+ end
97
+
98
+ teams << team
99
+ @teams_roles[team.id]=team_hash["roles"]
100
+
101
+ end
102
+ teams
103
+ end
104
+
105
+ def fill_scores
106
+ scores={points: {} ,sets: {}, states: {}, compounds: {}}
107
+ profile_hash["scores"].each do |score|
108
+ case score["metric"]["type"]
109
+ when "point"
110
+ scores[:points][score["metric"]["id"].to_sym]=score["value"].to_i
111
+ when "set"
112
+ scores[:sets][score["metric"]["id"].to_sym]=sets_metric_value_from(score["value"])
113
+ when "state"
114
+ scores[:states][score["metric"]["id"].to_sym]=score["value"]["name"] #"value": { "name": "Hell", "description": "The plane of demons" }
115
+ when "compound"
116
+ scores[:compounds][score["metric"]["id"].to_sym]=score["value"].to_i
117
+ end
118
+
119
+ end
120
+ scores
121
+ end
122
+
123
+ def sets_metric_value_from(value)
124
+ (value.each.collect {|item| {name: item["name"], count: item["count"].to_i} } )
125
+ end
126
+
127
+ def build_activities_from(array_of_hashes)
128
+ #https://dev.playlyfe.com/docs/events.html
129
+ #TODO: DO a Events :: now, just simple hashes
130
+ array_of_hashes
131
+ end
132
+
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,58 @@
1
+ require_relative "../team.rb"
2
+
3
+ module PlaylyfeClient
4
+ module V2
5
+ class Team < PlaylyfeClient::Team
6
+
7
+ attr_reader :definition, :access, :game_id, :roles, :owner
8
+
9
+ def members
10
+ @members ||= fill_members
11
+ end
12
+
13
+ def leaderboards
14
+ @game.leaderboards.for_teams
15
+ end
16
+
17
+ private
18
+
19
+ def initialize(team_hash, game)
20
+ super(game)
21
+
22
+ @id=team_hash[:id] || team_hash["id"]
23
+ @name=team_hash[:name] || team_hash["name"]
24
+ @access=team_hash[:access] || team_hash["access"]
25
+ @game_id=team_hash[:game_id] || team_hash["game_id"]
26
+ @roles=team_hash[:roles] || team_hash["roles"]
27
+ @definition=team_hash[:definition] || team_hash["definition"]
28
+
29
+ @created_at=team_hash[:created_at] || team_hash["created_at"] #created_at should be Time instance
30
+ @created_at=(Time.parse(team_hash[:created] || team_hash["created"]) ) if @created_at.nil? && !(team_hash[:created] || team_hash["created"]).nil? #created_at is string representation of time
31
+
32
+ own=team_hash[:owner] || team_hash["owner"]
33
+ @owner=nil
34
+
35
+ unless own.nil? || own.empty?
36
+ @owner=game.players.find(own[:id] || own["id"])
37
+ end
38
+ end
39
+
40
+ def fill_members
41
+ @members=[]
42
+
43
+ game.connection.get_team_members_hash_array(self.id).each do |player_hash|
44
+ player=game.players.find(player_hash["id"])
45
+
46
+ #all players should be listed in game, so if nothing is found raise exception
47
+ if player.nil?
48
+ fail PlaylyfeClient::TeamError.new("{\"error\": \"Player not found\", \"error_description\": \"Player '#{player_hash["id"]}' from '#{self.name}'[#{self.id}] team was not found between game.players!\"}")
49
+ end
50
+
51
+ @members << player
52
+ end
53
+
54
+ @members
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module PlaylyfeClient
2
+ VERSION = "1.0.2"
3
+ end
@@ -0,0 +1,11 @@
1
+
2
+ require "time"
3
+ require_relative "./string_addons.rb"
4
+
5
+ module PlaylyfeClient
6
+ require "playlyfe_client/version"
7
+ require "playlyfe_client/errors.rb"
8
+ require "playlyfe_client/connection.rb"
9
+ require "playlyfe_client/game.rb"
10
+ require "playlyfe_client/v2/connection.rb"
11
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+ def underscore
3
+ self.gsub(/::/, '/').
4
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
+ tr("-", "_").
7
+ downcase
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'playlyfe_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "playlyfe_client"
8
+ spec.version = PlaylyfeClient::VERSION
9
+ spec.authors = ["Foton"]
10
+ spec.email = ["foton@centrum.cz"]
11
+ spec.summary = "Ruby client to connect to Playlyfe.com game."
12
+ spec.description = "Ruby client to connect to Playlyfe.com game and play actions from game."
13
+ spec.homepage = 'https://github.com/foton/playlyfe-ruby-client'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "json"
22
+ spec.add_dependency "jwt"
23
+ spec.add_dependency "rest_client"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest"
28
+ spec.add_development_dependency "minitest-reporters"
29
+ spec.add_development_dependency "pry"
30
+ end
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: playlyfe_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Foton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest_client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-reporters
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Ruby client to connect to Playlyfe.com game and play actions from game.
126
+ email:
127
+ - foton@centrum.cz
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - Gemfile
134
+ - Gemfile.lock
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - lib/playlyfe_client.rb
139
+ - lib/playlyfe_client/action.rb
140
+ - lib/playlyfe_client/connection.rb
141
+ - lib/playlyfe_client/errors.rb
142
+ - lib/playlyfe_client/game.rb
143
+ - lib/playlyfe_client/leaderboard.rb
144
+ - lib/playlyfe_client/metric.rb
145
+ - lib/playlyfe_client/player.rb
146
+ - lib/playlyfe_client/team.rb
147
+ - lib/playlyfe_client/v2/action.rb
148
+ - lib/playlyfe_client/v2/collection.rb
149
+ - lib/playlyfe_client/v2/collection/action_collection.rb
150
+ - lib/playlyfe_client/v2/collection/leaderboard_collection.rb
151
+ - lib/playlyfe_client/v2/collection/metric_collection.rb
152
+ - lib/playlyfe_client/v2/collection/player_collection.rb
153
+ - lib/playlyfe_client/v2/collection/team_collection.rb
154
+ - lib/playlyfe_client/v2/connection.rb
155
+ - lib/playlyfe_client/v2/game.rb
156
+ - lib/playlyfe_client/v2/leaderboard.rb
157
+ - lib/playlyfe_client/v2/leaderboard/players_leaderboard.rb
158
+ - lib/playlyfe_client/v2/leaderboard/teams_leaderboard.rb
159
+ - lib/playlyfe_client/v2/leaderboard/unknown_leaderboard.rb
160
+ - lib/playlyfe_client/v2/metric.rb
161
+ - lib/playlyfe_client/v2/metric/compound_metric.rb
162
+ - lib/playlyfe_client/v2/metric/point_metric.rb
163
+ - lib/playlyfe_client/v2/metric/set_metric.rb
164
+ - lib/playlyfe_client/v2/metric/state_metric.rb
165
+ - lib/playlyfe_client/v2/player.rb
166
+ - lib/playlyfe_client/v2/team.rb
167
+ - lib/playlyfe_client/version.rb
168
+ - lib/string_addons.rb
169
+ - playlyfe_client.gemspec
170
+ homepage: https://github.com/foton/playlyfe-ruby-client
171
+ licenses:
172
+ - MIT
173
+ metadata: {}
174
+ post_install_message:
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubyforge_project:
190
+ rubygems_version: 2.4.5.1
191
+ signing_key:
192
+ specification_version: 4
193
+ summary: Ruby client to connect to Playlyfe.com game.
194
+ test_files: []