Class: App42::Game::GameService

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

Overview

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 Reward, RewardPoint, Score, ScoreBoard

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ GameService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL


37
38
39
40
41
42
43
44
# File 'lib/game/GameService.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"
  @version = "1.0"
end

Instance Method Details

#create_game(gameName, gameDescription) ⇒ Object

Creates game on the cloud

@param gameName
    - Name of the game that has to be created
@param gameDescription
    - Description of the game to be created

@return Game object containing the game which has been created

@raise App42Exception


58
59
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
# File 'lib/game/GameService.rb', line 58

def create_game(gameName, gameDescription)
  puts "Create Game Called "
  puts "Base url #{@base_url}"
  response = nil;
  gameObj = nil;
  gameObj = Game.new
  util = Util.new
  util.throwExceptionIfNullOrBlank(gameName, "Game Name");
  util.throwExceptionIfNullOrBlank(gameDescription, "Game Description");
  begin
    connection = App42::Connection::RESTConnection.new(@base_url)
    body = {'app42' => {"game"=> {
      "name" => gameName,
      "description" => gameDescription
      }}}.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()
    gameObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return gameObj
end

#get_all_gamesObject

Fetches all games for the App

Returns:

  • ArrayList of Game object containing all the games for the App

Raises:

  • App42Exception



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/game/GameService.rb', line 105

def get_all_games()
  puts "Get All Games Called "
  puts "Base url #{@base_url}"
  response = nil;
  gameList = nil;
  gameList = Array.new
  util = Util.new
  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
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new
    gameList = game.buildArrayResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return gameList
end

#get_all_games_by_paging(max, offset) ⇒ Object

Fetches all games for the App by paging. * @param max Maximum number of records to be fetched

Parameters:

  • offset
    • From where the records are to be fetched

Returns:

  • ArrayList of Game object containing all the games for the App

Raises:

  • App42Exception



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/game/GameService.rb', line 147

def get_all_games_by_paging(max, offset)
  puts "get_all_games_by_paging Called "
  puts "Base url #{@base_url}"
  response = nil;
  gameList = nil;
  gameList = Array.new
  util = Util.new
  util.validateMax(max);
  util.throwExceptionIfNullOrBlank(max, "Max");
  util.throwExceptionIfNullOrBlank(offset, "Offset");
  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("max", "" + (max.to_i).to_s)
    params.store("offset", "" + (offset.to_i).to_s)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/paging/#{(max.to_i).to_s}/#{(offset.to_i).to_s}"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new
    gameList = game.buildArrayResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return gameList
end

#get_all_games_countObject

Fetches the count of all games for the App

Returns:

  • App42Response object containing count of all the Game for the App

Raises:

  • App42Exception



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/game/GameService.rb', line 235

def get_all_games_count()
  puts "get_all_games_count Called "
  puts "Base url #{@base_url}"
  response = nil;
  responseObj = App42Response.new
  util = Util.new
  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
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/count"
    response = connection.get(signature, resource_url, query_params)
    responseObj.strResponse=(response)
    responseObj.isResponseSuccess=(true)
    responseObj = GameResponseBuilder.new()
    responseObj.getTotalRecords(response);
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return responseObj
end

#get_game_by_name(gameName) ⇒ Object

Retrieves the game by the specified name

Parameters:

  • gameName
    • Name of the game that has to be fetched

Returns:

  • Game object containing the game which has been created

Raises:

  • App42Exception



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/game/GameService.rb', line 193

def get_game_by_name(gameName)
  puts "Get Games By Name Called "
  puts "Base url #{@base_url}"
  response = nil;
  gameObj = nil;
  gameObj = 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
    puts params
    params.store("name", gameName)
    puts query_params
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/#{gameName}"
    response = connection.get(signature, resource_url, query_params)
    puts "Response is #{response}"
    game = GameResponseBuilder.new()
    gameObj = game.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return gameObj
end