class Flox

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.

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.

Constants

DEFAULT_URL

The URL where the Flox servers are found.

VERSION

The current version of the Flox SDK.

Attributes

current_player[R]

@return [Flox::Player] The player that is currently logged in.

service[R]

@private

Public Class Methods

new(game_id, game_key, base_url=Flox::DEFAULT_URL) click to toggle source

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.

# 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.login_guest
end

Public Instance Methods

base_url() click to toggle source

The base URL of the Flox service. @return [String]

# File lib/flox.rb, line 225
def base_url
  service.base_url
end
delete_entity(*entity) click to toggle source

Deletes the given entity from the database. @overload delete_entity(entity)

@param [Flox:Entity] entity the entity to delete

@overload delete_entity(type, id)

@param [String] type the type of the entity
@param [String] id the id of the entity
# 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) click to toggle source

Executes a query over Entities, using a simple SQL-like syntax. @see Flox::Query

@overload find_entities(query)

@param query [Flox::Query] the query to execute

@overload find_entities(entity_type, constraints=nil, *args)

@param type [String, Symbol, Class] the type of the entity
@param query [String] the query string with optional '?' placeholders
# 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) click to toggle source

Executes a query over Entities, using a simple SQL-like syntax. @return [Array<String>] @see find_entities @see Flox::Query

# 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) click to toggle source

Finds just the IDs of the logs, defined by a certain query. @see find_logs @return [Array<String>]

# 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) click to toggle source

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.

@return [Array<Hash>]

# 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_id() click to toggle source

The ID of the game you are accessing. @return [String]

# File lib/flox.rb, line 213
def game_id
  service.game_id
end
game_key() click to toggle source

The key of the game you are accessing. @return [String]

# File lib/flox.rb, line 219
def game_key
  service.game_key
end
inspect() click to toggle source

@return [String] a string representation of the object.

# File lib/flox.rb, line 230
def inspect
  "[Flox game_id: '#{game_id}', base_url: '#{base_url}']"
end
load_entity(type, id) click to toggle source

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. @return [Flox::Entity]

# 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) click to toggle source

Loads a log with a certain ID. A log is a Hash instance. @return [Hash]

# File lib/flox.rb, line 139
def load_log(log_id)
  service.get log_path(log_id)
end
load_player(id) click to toggle source

Loads an entity with type '.player'. @return [Flox::Player]

# File lib/flox.rb, line 63
def load_player(id)
  load_entity('.player', id)
end
load_resource(path, args=nil) click to toggle source

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

# File lib/flox.rb, line 120
def load_resource(path, args=nil)
  service.get(path, args)
end
load_resources(path, ids) { |id, resource| ... } click to toggle source

Loads a bulk of resources from a certain path, optionally feeding them through a block. @yield [id, resource] called on every resource; the return-value is feeded

into the result array.

@return [Array]

# 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) click to toggle source

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. @return [Array<Flox::Score>]

# 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(auth_type, auth_id=nil, auth_token=nil) click to toggle source

@private

# File lib/flox.rb, line 47
def login(auth_type, auth_id=nil, auth_token=nil)
    data = service.login(auth_type, auth_id, auth_token)
    @current_player = Player.new(data[:id], data[:entity])
end
login_guest() click to toggle source

Creates a new guest player and logs it in. After the login, `current_player` will point to this player. @return [Flox::Player]

# File lib/flox.rb, line 39
def login_guest()
  login(:guest)
end
Also aliased as: logout
login_with_key(key) click to toggle source

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. @return [Flox::Player]

# File lib/flox.rb, line 32
def login_with_key(key)
  login(:key, key)
end
logout()

Logging out the current player automatically logs in a new guest.

Alias for: login_guest
post_score(leaderboard_id, score, player_name) click to toggle source

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

# 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) click to toggle source

Stores an entity on the server. @return [Flox::Entity]

# 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
status() click to toggle source

Loads the status of the Flox service. @return [Hash] with the keys 'status' and 'version'.

# File lib/flox.rb, line 207
def status
  service.get("")
end

Private Instance Methods

create_entity(type, id, data) click to toggle source
# File lib/flox.rb, line 248
def create_entity(type, id, data)
  if (type == '.player' or type == Flox::Player)
    Player.new(id, data)
  else
    Entity.new(type, id, data)
  end
end
create_query(*query) click to toggle source
# File lib/flox.rb, line 256
def create_query(*query)
  if query[0].kind_of? Flox::Query then query[0]
  else Flox::Query.new(query[0], query[1], *query[2..-1])
  end
end
entity_path(type, id) click to toggle source
# File lib/flox.rb, line 236
def entity_path(type, id)
  "entities/#{type}/#{id}"
end
leaderboard_path(leaderboard_id) click to toggle source
# File lib/flox.rb, line 240
def leaderboard_path(leaderboard_id)
  "leaderboards/#{leaderboard_id}"
end
log_path(log_id) click to toggle source
# File lib/flox.rb, line 244
def log_path(log_id)
  "logs/#{log_id}"
end