Sign In Register

How can we help you today?

Start a new topic
Answered

Sending Requests from CloudScript

This is copy/paste from TestHarness's JSON-view of my event:

{
 "@class": ".LogEventRequest",
 "eventKey": "SEND_GIFT",
 "currencyType": "Diamonds",
 "count": 1,
 "reciever": "55bc30fb845fb88eba4dfceb",
 "sender": "55bc30fb845fb88eba4dfceb",
 "action": "ask"
}

 It's working as well, but how call this event from CloudScript?

I'm trying this:

Spark.sendRequest({
     "@class": ".LogEventRequest",
     "eventKey": "SEND_GIFT",
     "currencyType": "Diamonds",
     "count": 1,
     "reciever": "55bc30fb845fb88eba4dfceb",
     "sender": "55bc30fb845fb88eba4dfceb",
     "action": "ask"
});

 but it's doing nothing


Best Answer

 This also doing nothing:

Spark.logEvent("SEND_GIFT", gift);

 and this too:

var request = new SparkRequests.LogEventRequest();
request.shortCode = "SEND_GIFT";
request.currencyType = "Diamonds";
request.count = 7777777;
request.reciever = "55bc30fb845fb88eba4dfceb";
request.sender = "55bc30fb845fb88eba4dfceb";
request.action = "ask";
request.Send();

But i'm find the solution: use Modules

Create Module in CloudCode section -> Modules, write your code, and you can now use this in your other scripts, just add line like this:

require("YourModuleName");

 It's not answer on our question, but it's works at least :)


Or how can i call another scripts to avoid code duplications?
(Sorry for my horrible English :) )

 

 This also giving no effect:

var request = new SparkRequests.LogEventRequest();
request.shortCode = "SEND_GIFT";
request.currencyType = "Diamonds";
request.count = 7777777;
request.reciever = "55bc30fb845fb88eba4dfceb";
request.sender = "55bc30fb845fb88eba4dfceb";
request.action = "ask";
request.Send();

 and this too:

var gift = {
     "currencyType": "Diamonds",
     "count": 123,
     "reciever": "55bc30fb845fb88eba4dfceb",
     "sender": "55bc30fb845fb88eba4dfceb",
     "action": "ask"
}
Spark.logEvent("SEND_GIFT", gift);

 

 this also giving no effect:

var request = new SparkRequests.LogEventRequest();
request.shortCode = "SEND_GIFT";
request.currencyType = "Diamonds";
request.count = 7777777;
request.reciever = "55bc30fb845fb88eba4dfceb";
request.sender = "55bc30fb845fb88eba4dfceb";
request.action = "ask";
request.Send();

 and this too:

 

var gift = {
     "@class": ".LogEventRequest",
     "eventKey": "SEND_GIFT",
     "currencyType": "Diamonds",
     "count": 123,
     "reciever": "55bc30fb845fb88eba4dfceb",
     "sender": "55bc30fb845fb88eba4dfceb",
     "action": "ask"
}
Spark.logEvent("SEND_GIFT", gift);

 

I have a similar problem.


What specifically are the limitations on Spark.sendRequest and Spark.sendRequestAs?


I have a request that I want to call server side to update a player's Clash of Clans style trophy score and then post it to a leaderboard.

Sending this from the client is really not ideal as the values are calculated server-side and would have to be sent to the client and then sent back to the server.


I tried to update attacker and defender trophies and leaderboard values using requests like this: 

Spark.sendRequest({
"@class": ".LogEventRequest",
"eventKey": "RANK_UPDATE",
"TROPHIES": playerLeague["trophies"],
"COUNTRY": country,
"PLAYER_ID" : "skip" });

Spark.sendRequestAs({
"@class": ".LogEventRequest",
"eventKey": "RANK_UPDATE",
"TROPHIES": enemyLeague["trophies"],
"COUNTRY": enemyCountry,
"PLAYER_ID" : "skip" },
playerBattle["enemyId"]);

 

However, these requests appear to do nothing.



Answer

 This also doing nothing:

Spark.logEvent("SEND_GIFT", gift);

 and this too:

var request = new SparkRequests.LogEventRequest();
request.shortCode = "SEND_GIFT";
request.currencyType = "Diamonds";
request.count = 7777777;
request.reciever = "55bc30fb845fb88eba4dfceb";
request.sender = "55bc30fb845fb88eba4dfceb";
request.action = "ask";
request.Send();

But i'm find the solution: use Modules

Create Module in CloudCode section -> Modules, write your code, and you can now use this in your other scripts, just add line like this:

require("YourModuleName");

 It's not answer on our question, but it's works at least :)

In my case I cannot use module code because I need to post values to a leaderboard which must be done using a request as far as I know.

I also want to be able to send a logEventRequest from cloudcode to update a leaderboard and do not see how the answer for this question could solve that problem. Has anyone found a way to do this?

Hi Jaayden,


You can do this by using a SparkRequest. A SparkRequest allows you to send an event in Cloud Code as the current player or as a given player if you pass in a playerId. If you had a basic score event called "postScore" with a score number attribute you would use the following to do this.

 

//this example assumes you have a score value and playerId to pass in.

var request = new SparkRequests.LogEventRequest();
request.eventKey = "postscore";
request.score = scoreValue;
var response = request.SendAs(playerID);

.SendAs(playerID); // will send the event as the given player
.ExecuteAs(playerID); // will send the event and execute any cloud code for that event in the context of the given player.

.Send(); & .Execute(); //will do the same as above but in the context of the currently authenticated player 

  

Try that and if you have any further questions just let us know. To see how other requests are sent in cloud Code you can reference our Client API here.


Regards,

Liam

With LogEventRequest, you pretty much always want to use Execute or ExecuteAs, and not Send or SendAs, since LogEventRequests almost always have accompanying Cloud Code that needs executing.


Login to post a comment