Class: App42::Game::ScoreBoardService

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

Overview

ScoreBoard allows storing, retrieving, querying and ranking scores for users and Games across Game Session.

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, Score

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ ScoreBoardService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL


36
37
38
39
40
41
42
43
# File 'lib/game/ScoreBoardService.rb', line 36

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/scoreboard"
  @version = "1.0"
end

Instance Method Details

#get_average_score_by_user(gameName, userName) ⇒ Object

Retrieves the average game score for the specified user

Parameters:

  • gameName
    • Name of the game for which average score has to be fetched

  • userName
    • The user for which average score has to be fetched

Returns:

  • Returns the average game score for the specified user

Raises:

  • App42Exception



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/game/ScoreBoardService.rb', line 301

def get_average_score_by_user(gameName, userName)
  puts "Get Top Rankings Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreboardObj = nil;
  scoreboardObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("name", gameName)
    params.store("userName", userName);
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{gameName}/#{userName}/average"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreboardObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreboardObj
end

#get_highest_score_by_user(gameName, userName) ⇒ Object

Retrieves the highest game score for the specified user

Parameters:

  • gameName
    • Name of the game for which highest score has to be fetched

  • userName
    • The user for which highest score has to be fetched

Returns:

  • Returns the highest game score for the specified user

Raises:

  • App42Exception



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/game/ScoreBoardService.rb', line 161

def get_highest_score_by_user(gameName, userName)
  puts "Get Highest Score By User Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreboardObj = nil;
  scoreboardObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("name", gameName)
    params.store("userName", userName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{gameName}/#{userName}/highest"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreboardObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreboardObj
end

#get_lowest_score_by_user(gameName, userName) ⇒ Object

Retrieves the lowest game score for the specified user

Parameters:

  • gameName
    • Name of the game for which lowest score has to be fetched

  • userName
    • The user for which lowest score has to be fetched

Returns:

  • Returns the lowest game score for the specified user

Raises:

  • App42Exception



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/game/ScoreBoardService.rb', line 209

def get_lowest_score_by_user(gameName, userName)
  puts "Get Lowest Score By User Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreboardObj = nil;
  scoreboardObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("name", gameName)
    params.store("userName", userName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{gameName}/#{userName}/lowest"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreboardObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreboardObj
end

#get_scores_by_user(gameName, userName) ⇒ Object

Retrieves the scores for a game for the specified name

Parameters:

  • gameName
    • Name of the game for which score has to be fetched

  • userName
    • The user for which score has to be fetched

Returns:

  • Returns the game score for the specified user

Raises:

  • App42Exception



113
114
115
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
# File 'lib/game/ScoreBoardService.rb', line 113

def get_scores_by_user(gameName, userName)
  puts "Get Score By User Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreboardObj = nil;
  scoreboardObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("name", gameName)
    params.store("userName", userName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{gameName}/#{userName}"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreboardObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreboardObj
end

#get_top_n_rankings(gameName, max) ⇒ Object

Retrieves the Top N Rankings for the specified game

Parameters:

  • gameName
    • Name of the game for which ranks have to be fetched

Returns:

  • Returns the Top rankings for a game

Raises:

  • App42Exception



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/game/ScoreBoardService.rb', line 347

def get_top_n_rankings(gameName, max)
  puts "Get Top N Rankings Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreboardObj = nil;
  scoreboardObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  util.throwExceptionIfNullOrBlank(max, "Max");
  util.validateMax(max);
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("name", gameName);
    params.store("max", ""+ max.to_s);
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{gameName}/ranking/#{max.to_s}"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreboardObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreboardObj
end

#get_top_rankings(gameName) ⇒ Object

Retrieves the Top Rankings for the specified game

Parameters:

  • gameName
    • Name of the game for which ranks have to be fetched

Returns:

  • Returns the Top rankings for a game

Raises:

  • App42Exception



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/game/ScoreBoardService.rb', line 255

def get_top_rankings(gameName)
  puts "Get Top Rankings Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreboardObj = nil;
  scoreboardObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("name", gameName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{gameName}/ranking"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreboardObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreboardObj
end

#get_user_ranking(gameName, userName) ⇒ Object

Retrieves the User Ranking for the specified game

Parameters:

  • gameName
    • Name of the game for which ranks have to be fetched

  • userName
    • Name of the user for which ranks have to be fetched

Returns:

  • Returns the rank of the User

Raises:

  • App42Exception



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/game/ScoreBoardService.rb', line 396

def get_user_ranking(gameName, userName)
  puts "get_user_ranking Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreboardObj = nil;
  scoreboardObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  util.throwExceptionIfNullOrBlank(userName, "User Name");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    query_params = Hash.new
    params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    query_params = params.clone
    params.store("name", gameName);
    params.store("userName", ""+userName);
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{gameName}/#{userName}/ranking"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreboardObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreboardObj
end

#save_user_score(gameName, gameUserName, gameScore) ⇒ Object

Saves the User score for a game

Parameters:

  • gameName
    • Name of the game for which score has to be saved

  • gameUserName
    • The user for which score has to be saved

  • gameScore
    • The sore that has to be saved

Returns:

  • Returns the saved score for a game

Raises:

  • App42Exception



60
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
# File 'lib/game/ScoreBoardService.rb', line 60

def save_user_score(gameName, gameUserName, gameScore)
  puts "Save User Score Called "
  puts "Base url #{@base_url}"
  response = nil;
  scoreboardObj = nil;
  scoreboardObj = 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}"
    response = connection.post(signature, resource_url, query_params, body)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    scoreboardObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return scoreboardObj
end