# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection' require 'util/util' require 'json/pure' require 'App42_Ruby_API/App42Response' require 'game/GameResponseBuilder' require 'game/Game' module App42 module Game # # 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 # class GameService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # 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 # 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 # 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 # # Fetches all games for the App # # @return ArrayList of Game object containing all the games for the App # # @raise App42Exception # 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 # # Fetches all games for the App by paging. * @param max Maximum number of # records to be fetched # # @param offset # - From where the records are to be fetched # # @return ArrayList of Game object containing all the games for the App # # @raise App42Exception # 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 # # Retrieves the game by the specified name # # @param gameName # - Name of the game that has to be fetched # # @return Game object containing the game which has been created # # @raise App42Exception # 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 # # Fetches the count of all games for the App # # @return App42Response object containing count of all the Game for the App # # @raise App42Exception # 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 end end end