Sign In Register

How can we help you today?

Start a new topic

Creating a player-list for searching and storing player info

So, we get a lot of forum posts related to searching and retrieving player information like playerId, username, etc.
This is pretty simple for the player currently logged in, (you can see the documentation here) but what if you want to search for other player's details given only limited information like their id, username, etc you currently cannot do so, as the player lists are protected collections.


There is a way around this though. Specifically by creating your own player-collection.

This gives you the freedom to  add whatever player information you like to your player lists.


To do this you will have to add some code to the registration response  cloud-code.




The code you need to add will check to see if the registered player is a new player first, and if so we will insert a new document into the player-list collection with their important details like, password, id, username, etc. For this example i havent included everything, but play around with it to see what you need. Check out the SparkPlayer docs to see what can be added.



 

var playerID = Spark.getData().userId;
var displayName = Spark.getData().displayName;

if(Spark.getData().newPlayer) // only save if it is a new player
{
    Spark.runtimeCollection("playerList").insert({
        "id" : playerID,
        "displayName" : displayName,
        "userName" : Spark.getPlayer().getUserName()
    })
}

 

Now we want to query this from other events or modules.
To do this we use the mongo 'find' method on our collection.
The query is structured so that you ask what element you want to search for, in this example, the user-name, and then the actual name.
This query will return the player's document in the 'playerList' collection.



var playerDetails = Spark.runtimeCollection("playerList").findOne({ "userName" : "someUserName" });

 



1 person has this question

 Note, that in my example i used the 'findOne' method, as i only want to find one player's details. You could also use the 'find' to, for example, return all players with a certain achievement. Its up to you.

Now, we have the playerDetails variable, this contains all the player details. you can get them by calling...

playerDetails.id        ....... for their id
playerDetails.userName   ......... for their user name
playerDetails.displayName  ....... for their display name


1 person likes this

Is there something like this for challenges as well?  I started to look into it in order to create a workaround for the fact that I can't read nextPlayer or turns from a SparkChallenge, but I also can't use Spark.runtimeCollection("challengeInstance").findOne() --- I get a Java error that something can't be serialized.

Nevermind, I see what you did there, you created a runtime collection mirroring the system collection.  That's not going to work since I still can't access nextPlayer and turns from code.

Hello, we are having some trouble to follow your answer as we are not very skilled with scripts.

We have created a Registration Request in Cloud Code with the code as you said:


var playerID = Spark.getData().userId;
var displayName = Spark.getData().displayName;
 
if(Spark.getData().newPlayer) // only save if it is a new player
{
  Spark.runtimeCollection("playerList").insert({
  "id" : playerID,
  "displayName" : displayName,
  "userName" : Spark.getPlayer().getUserName()
  })
}



After that we have created a runtime collection ("playerList") in NoSQL tab.

We don´t know if we have mirrored correctly our system collection, because when we press 'Find' we get an Output "No results"

Do we have to write anything in the 'Query' field, or in 'Fields' field?


Thank you in advance.


Hi Jose


No you don't need to put anything in the query or field.

What script have you put the code into?


Regards

Katie

Hello, we have put the code into


Configurator > Cloud Code > Bindings > Request > REGISTRATION REQUEST


var playerID = Spark.getData().userId;
var displayName = Spark.getData().displayName;
 
if(Spark.getData().newPlayer) // only save if it is a new player
{
  Spark.runtimeCollection("playerList").insert({
  "id" : playerID,
  "displayName" : displayName,
  "userName" : Spark.getPlayer().getUserName()
  })
}



Is this not correct?


Thanks, Jose


Hi Jose 


I would recommend moving the code to the registration response.


Cheers

Katie 

Hi Jose,


I don't know if you have resolved this problem but you would need to select your collection name "script.playerList" incase of your example above.


Regards

Saad

I tried this method, adding a specific team type to the runtime collection. When i add the teamId to the collection, it is null. Why is this so?

On the client side, i do receive the response with a team id. The client does not send a team id up, as i want to use the generated id from the server.

Please advice. Thanks.

can you show your code on client and cloud code?

 There is no code client-side for this. All action takes place on the server.

Sean

Hi Everyone here, as Katie said:


"I would recommend moving the code to the registration response."


Could you guy tell me, when i will put my code into response cloud code and when into request cloud code for good praticles? 

Thanks you

Login to post a comment