Sign In Register

How can we help you today?

Start a new topic
Answered

Saving a list of playerIds from a list of runtime collection data in Cloud Code

I have a runtimeCollection with all of the players UserIds, a bool that is set when the player is in challenger mode, and other data. I'd like to send a message to all players that are currently in challenger mode. I create a list of all runtimeCollection data for the players that are in challenger mode, but I am not sure how to save just the playerIds from this list in to another list that can be passed in to the Spark.Message().setPlayerIds();


 

var playerDataList = Spark.runtimeCollection("playerData"); // this will get the collection of player data
var challengerStatus = Spark.getData().challengerStatus;
var playerID = Spark.getPlayer().getPlayerId(); // first we get the id of the current player


var challengerPlayersData = playerDataList.find({
	"challengerStatus": 1
}); // search the collection data for the entry with the same id as the player
if(challengerPlayersData.count() === null){
}else{
    var challengerPlayersId = [];
    for(var i; i < challengerPlayersData.count(); i++){
        challengerPlayersId[i] = [challengerPlayersData[i].playerID];
    }
    
    var currentChallenger = playerDataList.findOne({
	    "playerID": Spark.getPlayer().getPlayerId()
    }); // search the collection data for the entry with the same id as the player

    var messageForm = {};
        messageForm.playerID = currentChallenger.playerID;
        messageForm.playerName = currentChallenger.playerName;
        messageForm.playerHealth = currentChallenger.playerHealth;
        messageForm.playerMana = currentChallenger.playerMana;
        messageForm.playerChar = currentChallenger.playerChar;
    
    Spark.message("ChallengerStatus").setMessageData(messageForm)
    .setPlayerIds(challengerPlayersId).send();
}
// var messageForm = {};
// Spark.message("ChallengerStatus").setMessageData(messageForm).setPlayerIds(Spark.getPlayer().getPlayerId()).send();


var currentPlayer = {
	"challengerStatus": challengerStatus
}; // we construct a new player from the data we are about to input into the player data
playerDataList.update({
	"playerID": playerID
}, //Looks for a doc with the id of the current player
{
	"$set": currentPlayer
}, // Uses the $set mongo modifier to set old player data to the current player data
true, // Create the document if it does not exist (upsert)
false // This query will only affect a single object (multi)
);

 Thanks for any help!


Best Answer

Hi Ian,


If I'm looking at the correct collection in your game (playerData ?) the following distinct query will get the playerIDs that you require to use in the SparkMessage you are sending.

 

var playerIDs = Spark.runtimeCollection("playerData").distinct("playerID",{"challengerStatus":0});

   

Distinct will return an array of unique values that match the given field in a collection, by adding a query to it also you can refine your results even further. So this query will return an array of unique player IDs for players who are currently not in a challenge. Hope that helps, if you have any further questions just let me know.


Thanks,

Liam


Fantastic! thank you Liam!


Is there documentation that explains this API that I can study? I have not been able to find it.


My only other question within this script example is saving entire GSData variables to my message and retrieving them in Unity. 

Where  have:

 

var messageForm = {};
        messageForm.playerID = currentChallenger.playerID;
        messageForm.playerName = currentChallenger.playerName;
        messageForm.playerHealth = currentChallenger.playerHealth;
        messageForm.playerMana = currentChallenger.playerMana;
        messageForm.playerChar = currentChallenger.playerChar;
     
    Spark.message("ChallengerStatus").setMessageData(messageForm)
    .setPlayerIds(challengerPlayersId).send();

 

I'd like to be able to save just the currentChallenger runtimeCollectionData instead of each variable individually. Or, ideally I'd like to save the entire challenge object to the message so I can access it similar to message.challenge. from challenge messages (like ChallengeAcceptedMessage) in Unity. 


Here is a link to the topic I started on the subject. May be best to respond there if your so inclined.


Thanks again Liam, huge help!

Answer

Hi Ian,


If I'm looking at the correct collection in your game (playerData ?) the following distinct query will get the playerIDs that you require to use in the SparkMessage you are sending.

 

var playerIDs = Spark.runtimeCollection("playerData").distinct("playerID",{"challengerStatus":0});

   

Distinct will return an array of unique values that match the given field in a collection, by adding a query to it also you can refine your results even further. So this query will return an array of unique player IDs for players who are currently not in a challenge. Hope that helps, if you have any further questions just let me know.


Thanks,

Liam

Login to post a comment