Class: App42::Game::ScoreService

Inherits:
Object
  • Object
show all
Defined in:
lib/game/ScoreService.rb

Overview

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 Also:

  • RewardPoint, RewardPoint, ScoreBoard

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ ScoreService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL


37
38
39
40
41
42
43
44
# 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

Instance Method Details

#addScore(gameName, gameUserName, gameScore) ⇒ Object

Adds game score for the specified user.

Parameters:

  • gameName
    • Name of the game for which scores have to be added

  • gameUserName
    • The user for whom scores have to be added

  • gameScore
    • nThe scores that have to be added

Returns:

  • Game object containing the scores that has been added

Raises:

  • App42Exception



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object

Deducts the score from users account for a particular Game

Parameters:

  • gameName
    • Name of the game for which scores have to be deducted

  • gameUserName
    • The user for whom scores have to be deducted

  • gameScore
    • The scores that have to be deducted

Returns:

  • Game object containing the scores that has been deducted

Raises:

  • App42Exception



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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