# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'App42Response.rb' require 'game/RewardResponseBuilder.rb' require 'game/Reward.rb' module App42 module Game # # Define a Reward e.g. Sword, Energy etc. Is needed for Reward Points # 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, Score, ScoreBoard # class RewardService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "Reward->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "game/reward" @version = "1.0" end # # Creates Reward. Reward can be Sword, Energy etc. When Reward Points have to be # added the Reward name created using this method has to be specified. # # @param rewardName # - The reward that has to be created # @param rewardDescription # - The description of the reward to be created # # @return Reward object containing the reward that has been created # # @raise App42Exception # def create_reward(rewardName, rewardDescription) puts "Create Reward Called " puts "Base url #{@base_url}" response = nil; rewardObj = nil; rewardObj = Reward.new() util = Util.new util.throwExceptionIfNullOrBlank(rewardName, "Reward Name"); util.throwExceptionIfNullOrBlank(rewardDescription, "Reward Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"rewards"=> { "reward"=> { "name" => rewardName, "description" => rewardDescription }}}}.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}" reward = RewardResponseBuilder.new() rewardObj = reward.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return rewardObj end # # Fetches all the Rewards # # @return ArrayList of Reward objects containing all the rewards of the App # # @raise App42Exception # # def get_all_rewards() puts "Get All Rewards Called " puts "Base url #{@base_url}" response = nil; rewardList = nil; rewardList = 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}" reward = RewardResponseBuilder.new() rewardList = reward.buildArrayRewards(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return rewardList end # # Fetches all the Rewards by paging. # # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return ArrayList of Reward objects containing all the rewards of the App # # @raise App42Exception # def get_all_rewards_by_paging(max, offset) puts "get_all_rewards_by_paging Called " puts "Base url #{@base_url}" response = nil; rewardList = nil; rewardList = 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}" reward = RewardResponseBuilder.new() rewardList = reward.buildArrayRewards(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return rewardList end # # Retrieves the reward for the specified name # # @param rewardName # - Name of the reward that has to be fetched # # @return Reward object containing the reward based on the rewardName # # @raise App42Exception # def get_reward_by_name(rewardName) puts "Get reward By Name Called " puts "Base url #{@base_url}" response = nil; rewardObj = nil; rewardObj = Reward.new() util = Util.new util.throwExceptionIfNullOrBlank(rewardName, "Reward 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", rewardName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{rewardName}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" reward = RewardResponseBuilder.new() rewardObj = reward.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return rewardObj end # # Adds the reward points to an users account. Reward Points can be earned by the user # which can be redeemed later. # # @param gameName # - Name of the game for which reward points have to be added # @param gameUserName # - The user for whom reward points have to be added # @param rewardName # - The rewards for which reward points have to be added # @param rewardsPoints # - The points that have to be added # # @return Reward object containing the rewardpoints that has been added # # @raise App42Exception # def earn_rewards(gameName,gameUserName,rewardName,rewardPoints) puts "Earn Rewards Called " puts "Base url #{@base_url}" response = nil; rewardObj = nil; rewardObj = Reward.new() util = Util.new util.throwExceptionIfNullOrBlank(gameName, "Game Name"); util.throwExceptionIfNullOrBlank(gameUserName, "User Name"); util.throwExceptionIfNullOrBlank(rewardName, "Reward Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"rewards"=> { "reward"=> { "gameName" => gameName, "userName" => gameUserName, "name" => rewardName, "points" => rewardPoints }}}}.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}/earn" response = connection.post(signature, resource_url, query_params, body) puts "Response is #{response}" reward = RewardResponseBuilder.new() rewardObj = reward.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return rewardObj end # # Deducts the rewardpoints from the earned rewards by a user. # # @param gameName # - Name of the game for which reward points have to be deducted # @param gameUserName # - The user for whom reward points have to be deducted # @param rewardName # - The rewards for which reward points have to be deducted # @param rewardsPoints # - The points that have to be deducted # # @return Reward object containing the rewardpoints that has been deducted # # @raise App42Exception # def redeem_rewards(gameName,gameUserName,rewardName,rewardPoints) puts "Redeem Rewards Called " puts "Base url #{@base_url}" response = nil; rewardObj = nil; rewardObj = Reward.new() util = Util.new util.throwExceptionIfNullOrBlank(gameName, "Game Name"); util.throwExceptionIfNullOrBlank(gameUserName, "Game User Name"); util.throwExceptionIfNullOrBlank(rewardName, "Reward Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"rewards"=> { "reward"=> { "gameName" => gameName, "userName" => gameUserName, "name" => rewardName, "points" => rewardPoints }}}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts query_params params.store("body", body) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/redeem" response = connection.post(signature, resource_url, query_params, body) puts "Response is #{response}" reward = RewardResponseBuilder.new() rewardObj = reward.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return rewardObj end # # Fetches the reward points for a particular user # # @param gameName # - Name of the game for which reward points have to be fetched # @param userName # - The user for whom reward points have to be fetched # # @return Reward object containing the reward points for the specified user # # @raise App42Exception # def get_game_reward_points_for_user(gameName, userName) puts "Get Game Reward Points For User Called " puts "Base url #{@base_url}" response = nil; rewardObj = nil; rewardObj = Reward.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("gameName", 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}" reward = RewardResponseBuilder.new() rewardObj = reward.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return rewardObj end # # Fetches all the Rewards # # @return ArrayList of Reward objects containing all the rewards of the App # # @raise App42Exception # def get_all_rewards_count() puts "get_all_rewards_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 = RewardResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end end end end