# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'connection/RESTConnection' require 'util/util' require 'json/pure' module App42 module Game # # 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 Game, RewardPoint, RewardPoint, ScoreBoard # class ScoreService # # 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/score" @version = "1.0" end # # Adds game score for the specified user. # # @param gameName # - Name of the game for which scores have to be added # @param gameUserName # - The user for whom scores have to be added # @param gameScore # - nThe scores that have to be added # # @return Game object containing the scores that has been added # # @raise App42Exception # 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 # # Deducts the score from users account for a particular Game # # @param gameName # - Name of the game for which scores have to be deducted # @param gameUserName # - The user for whom scores have to be deducted # @param gameScore # - The scores that have to be deducted # # @return Game object containing the scores that has been deducted # # @raise App42Exception # 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 end end end