# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' module App42 module Game # # 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 Game, RewardPoint, RewardPoint, Score # class ScoreBoardService # # 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/scoreboard" @version = "1.0" end # # Saves the User score for a game # # @param gameName # - Name of the game for which score has to be saved # @param gameUserName # - The user for which score has to be saved # @param gameScore # - The sore that has to be saved # # @return Returns the saved score for a game # # @raise App42Exception # 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 # # Retrieves the scores for a game for the specified name # # @param gameName # - Name of the game for which score has to be fetched # @param userName # - The user for which score has to be fetched # # @return Returns the game score for the specified user # # @raise App42Exception # 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 # # Retrieves the highest game score for the specified user # # @param gameName # - Name of the game for which highest score has to be fetched # @param userName # - The user for which highest score has to be fetched # # @return Returns the highest game score for the specified user # # @raise App42Exception # 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 # # Retrieves the lowest game score for the specified user # # @param gameName # - Name of the game for which lowest score has to be fetched # @param userName # - The user for which lowest score has to be fetched # # @return Returns the lowest game score for the specified user # # @raise App42Exception # 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 # # Retrieves the Top Rankings for the specified game # # @param gameName # - Name of the game for which ranks have to be fetched # # @return Returns the Top rankings for a game # # @raise App42Exception # 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 # # Retrieves the average game score for the specified user # # @param gameName # - Name of the game for which average score has to be fetched # @param userName # - The user for which average score has to be fetched # # @return Returns the average game score for the specified user # # @raise App42Exception # 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 # # Retrieves the Top N Rankings for the specified game # # @param gameName # - Name of the game for which ranks have to be fetched # # @return Returns the Top rankings for a game # # @raise App42Exception # 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 # # Retrieves the User Ranking for the specified game # # @param gameName # - Name of the game for which ranks have to be fetched # @param userName # - Name of the user for which ranks have to be fetched # # @return Returns the rank of the User # # @raise App42Exception # 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 end end end