Class: Flox

Inherits:
Object
  • Object
show all
Defined in:
lib/flox.rb,
lib/flox/version.rb

Overview

The main class used to interact with the Flox cloud service. Create an instance of Flox using the game ID and key acquired from the web interface, then login with a "Hero" key. That way, you will be able to access the data of all players.

Defined Under Namespace

Classes: Entity, Error, Player, Query, RestService, Score, ServiceError

Constant Summary collapse

DEFAULT_URL =

The URL where the Flox servers are found.

"https://www.flox.cc"
VERSION =

The current version of the Flox SDK.

'0.4.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_id, game_key, base_url = Flox::DEFAULT_URL) ⇒ Flox

Creates a new instance with a certain game ID and key. Per default, a guest player will be logged in. You probably need to call 'login_with_key' with a Hero-key to access your data.



23
24
25
26
# File 'lib/flox.rb', line 23

def initialize(game_id, game_key, base_url=Flox::DEFAULT_URL)
  @service = RestService.new(game_id, game_key, base_url)
  self.
end

Instance Attribute Details

#current_playerFlox::Player (readonly)

Returns The player that is currently logged in.

Returns:

  • (Flox::Player)

    The player that is currently logged in.



18
19
20
# File 'lib/flox.rb', line 18

def current_player
  @current_player
end

Instance Method Details

#base_urlString

The base URL of the Flox service.

Returns:



225
226
227
# File 'lib/flox.rb', line 225

def base_url
  service.base_url
end

#delete_entity(entity) ⇒ Object #delete_entity(type, id) ⇒ Object

Deletes the given entity from the database.

Overloads:

  • #delete_entity(entity) ⇒ Object

    Parameters:

    • entity (Flox:Entity)

      the entity to delete

  • #delete_entity(type, id) ⇒ Object

    Parameters:

    • type (String)

      the type of the entity

    • id (String)

      the id of the entity



82
83
84
85
86
87
88
89
90
# File 'lib/flox.rb', line 82

def delete_entity(*entity)
  if entity.length > 1
    type, id = entity[0], entity[1]
  else
    type, id = entity[0].type, entity[0].id
  end
  service.delete(entity_path(type, id))
  nil
end

#find_entities(query) ⇒ Object #find_entities(entity_type, constraints = nil, *args) ⇒ Object

Executes a query over Entities, using a simple SQL-like syntax.

Overloads:

  • #find_entities(query) ⇒ Object

    Parameters:

  • #find_entities(entity_type, constraints = nil, *args) ⇒ Object

    Parameters:

    • type (String, Symbol, Class)

      the type of the entity

    • query (String)

      the query string with optional '?' placeholders

See Also:



185
186
187
188
189
190
191
# File 'lib/flox.rb', line 185

def find_entities(*query)
  query = create_query(*query)
  ids = find_entity_ids(query)
  load_resources "entities/#{query.type}", ids do |id, data|
    create_entity(query.type, id, data)
  end
end

#find_entity_ids(*query) ⇒ Array<String>

Executes a query over Entities, using a simple SQL-like syntax.

Returns:

See Also:



197
198
199
200
201
202
203
# File 'lib/flox.rb', line 197

def find_entity_ids(*query)
  query = create_query(*query)
  path = "entities/#{query.type}"
  data = { where: query.constraints, offset: query.offset,
           limit: query.limit, orderBy: query.order_by }
  service.post(path, data).map {|e| e[:id]}
end

#find_log_ids(query = nil, limit = nil) ⇒ Array<String>

Finds just the IDs of the logs, defined by a certain query.

Returns:

See Also:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/flox.rb', line 160

def find_log_ids(query=nil, limit=nil)
  log_ids = []
  cursor = nil
  begin
    args = {}
    args[:q] = query  if query
    args[:l] = limit  if limit
    args[:c] = cursor if cursor

    result = service.get "logs", args
    cursor = result[:cursor]
    log_ids += result[:ids]
    limit -= log_ids.length if limit
  end while !cursor.nil? and (limit.nil? or limit > 0)
  log_ids
end

#find_logs(query = nil, limit = nil) ⇒ Array<Hash>

Finds logs defined by a certain query. Here are some sample queries:

  • day:2014-02-20 → all logs of a certain day
  • severity:warning → all logs of type warning & error
  • severity:error → all logs of type error
  • day:2014-02-20 severity:error → all error logs from February 20th.

Returns:

  • (Array<Hash>)


152
153
154
155
# File 'lib/flox.rb', line 152

def find_logs(query=nil, limit=nil)
  log_ids = find_log_ids(query, limit)
  load_resources('logs', log_ids)
end

#game_idString

The ID of the game you are accessing.

Returns:



213
214
215
# File 'lib/flox.rb', line 213

def game_id
  service.game_id
end

#game_keyString

The key of the game you are accessing.

Returns:



219
220
221
# File 'lib/flox.rb', line 219

def game_key
  service.game_key
end

#inspectString

Returns a string representation of the object.

Returns:

  • (String)

    a string representation of the object.



230
231
232
# File 'lib/flox.rb', line 230

def inspect
  "[Flox game_id: '#{game_id}', base_url: '#{base_url}']"
end

#load_entity(type, id) ⇒ Flox::Entity

Loads an entity with a certain type and id from the server. Normally, the type is the class name you used for the entity in your game.

Returns:



55
56
57
58
59
# File 'lib/flox.rb', line 55

def load_entity(type, id)
  path = entity_path(type, id)
  data = service.get(path)
  create_entity(type, id, data)
end

#load_log(log_id) ⇒ Hash

Loads a log with a certain ID. A log is a Hash instance.

Returns:

  • (Hash)


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

def load_log(log_id)
  service.get log_path(log_id)
end

#load_player(id) ⇒ Flox::Player

Loads an entity with type '.player'.

Returns:



63
64
65
# File 'lib/flox.rb', line 63

def load_player(id)
  load_entity('.player', id)
end

#load_resource(path, args = nil) ⇒ Hash

Loads a JSON object from the given path. This works with any resource e.g. entities, logs, etc.

Returns:

  • (Hash)


120
121
122
# File 'lib/flox.rb', line 120

def load_resource(path, args=nil)
  service.get(path, args)
end

#load_resources(path, ids) {|id, resource| ... } ⇒ Array

Loads a bulk of resources from a certain path, optionally feeding them through a block.

Yields:

  • (id, resource)

    called on every resource; the return-value is feeded into the result array.

Returns:

  • (Array)


129
130
131
132
133
134
135
# File 'lib/flox.rb', line 129

def load_resources(path, ids)
  ids.map do |id|
    resource = load_resource "#{path}/#{id}"
    resource = yield id, resource if block_given?
    resource
  end
end

#load_scores(leaderboard_id, scope) ⇒ Array<Flox::Score>

Loads all scores of a leaderboard, sorted by rank. 'scope' can either be one of the symbols +:today, :this_week, :all_time+ or an array of player IDs.

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/flox.rb', line 103

def load_scores(leaderboard_id, scope)
  path = leaderboard_path(leaderboard_id)
  args = {}

  if scope.is_a?(Array)
    args[:p] = scope
  else
    args[:t] = scope.to_s.to_camelcase
  end

  raw_scores = service.get(path, args)
  raw_scores.collect { |raw_score| Score.new(raw_score) }
end

#login_guestFlox::Player Also known as: logout

Creates a new guest player and logs it in. After the login, current_player will point to this player.

Returns:



39
40
41
# File 'lib/flox.rb', line 39

def ()
  (:guest)
end

#login_with_key(key) ⇒ Flox::Player

Makes a key-login on the server. It is recommended to create a 'hero' player in the web interface and use that for the login. After the login, current_player will point to this player.

Returns:



32
33
34
# File 'lib/flox.rb', line 32

def (key)
  (:key, key)
end

#post_score(leaderboard_id, score, player_name) ⇒ Object

Posts a score to a certain leaderboard. Beware that only the top score of a player will appear on the leaderboard.



94
95
96
97
98
# File 'lib/flox.rb', line 94

def post_score(leaderboard_id, score, player_name)
  path = leaderboard_path(leaderboard_id)
  data = { playerName: player_name, value: score }
  service.post(path, data)
end

#save_entity(entity) ⇒ Flox::Entity

Stores an entity on the server.

Returns:



69
70
71
72
73
74
# File 'lib/flox.rb', line 69

def save_entity(entity)
  result = service.put(entity.path, entity)
  entity[:updatedAt] = result[:updatedAt]
  entity[:createdAt] = result[:createdAt]
  entity
end

#statusHash

Loads the status of the Flox service.

Returns:

  • (Hash)

    with the keys 'status' and 'version'.



207
208
209
# File 'lib/flox.rb', line 207

def status
  service.get("")
end