class App42::Game::ScoreService

Allows ingame scoring. It has to be used for scoring for a parituclar Game Session. If scores have to be stored across Game sessions then the service ScoreBoard has to be used. It is especially useful for Multiplayer online or mobile games. The Game service allows Game, User, Score and ScoreBoard Management on the Cloud. The service allows Game Developer to create a Game and then do in Game Scoring using the Score service. It also allows to maintain a Scoreboard across game sessions using the ScoreBoard service. One can query for average or highest score for user for a Game and highest and average score across users for a Game. It also gives ranking of the user against other users for a particular game. The Reward and RewardPoints allows the Game Developer to assign rewards to a user and redeem the rewards. E.g. One can give Swords or Energy etc. The services Game, Score, ScoreBoard, Reward, RewardPoints can be used in Conjunction for complete Game Scoring and Reward Management.

@see Game, RewardPoint, RewardPoint, ScoreBoard

Public Class Methods

new(api_key, secret_key, base_url) click to toggle source

this is a constructor that takes

@param apiKey @param secretKey @param baseURL

# File lib/game/ScoreService.rb, line 37
def initialize(api_key, secret_key, base_url)
  puts "Game->initialize"
  @api_key = api_key
  @secret_key = secret_key
  @base_url = base_url
  @resource = "game/score"
  @version = "1.0"
end

Public Instance Methods

addScore(gameName, gameUserName, gameScore) click to toggle source

Adds game score for the specified user.

@param gameName

- Name of the game for which scores have to be added

@param gameUserName

- The user for whom scores have to be added

@param gameScore

- nThe scores that have to be added

@return Game object containing the scores that has been added

@raise App42Exception

# File lib/game/ScoreService.rb, line 61
def addScore(gameName, gameUserName, gameScore)
  puts "Add score Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreObj = nil;
  scoreObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  util.throwExceptionIfNullOrBlank(gameUserName, "User Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"game"=> {
      "name" => gameName,"scores" => { "score" => {
      "value" => gameScore,
      "userName" => gameUserName
      }}}}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("body", body)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/add"
    response = connection.post(signature, resource_url, query_params, body)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreObj
end
deductScore(gameName, gameUserName, gameScore) click to toggle source

Deducts the score from users account for a particular Game

@param gameName

- Name of the game for which scores have to be deducted

@param gameUserName

- The user for whom scores have to be deducted

@param gameScore

- The scores that have to be deducted

@return Game object containing the scores that has been deducted

@raise App42Exception

# File lib/game/ScoreService.rb, line 116
def deductScore(gameName, gameUserName, gameScore)
  puts "Deduct Score Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreObj = nil;
  scoreObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  util.throwExceptionIfNullOrBlank(gameUserName, "User Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"game"=> {
      "name" => gameName,"scores" => { "score" => {
      "value" => gameScore,
      "userName" => gameUserName
      }}}}}.to_json
    puts "Body #{body}"
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    query_params = params.clone
    params.store("body", body)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/deduct"
    response = connection.post(signature, resource_url, query_params,body)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreObj
end