Sign In Register

How can we help you today?

Start a new topic
Answered

Challenge to a player with username

Hi, I am creating a new Challenge, and would like to send it to a player, but only his username is known. Is it possible? Can I find out playerId based on his username?

Does GS provide any mechanism for that or I am going to have to create a database in the cloud adding players Ids and names everytime they register?


Best Answer

Thanks Sean.


I was able to implement what I needed, I created a custom collection as you suggested and then created a custom event that returns a playerId when called with username parameter.

I still think, this should be implemented by default in cloud platform, since every time a player wants to create a challenge, he doesn't knows players Ids, but he likely knows usernames. (unless I am misunderstanding the most popular use of Challenges).


Thanks again for your help.


Hey Roman,

Unfortunately, access to player collections is not possible at the moment. We consider this system-critical information, so restricting access is to protect altering information that might break your game.
The way we usually suggest getting around this is to have your own player collection where a new player is added either on authentication or registration. Its up how your game is going to work as to which one is best. At registration have the benefit of an entry to the collection only happening once for each player, and less calls might be better.

So, you'll want to put this in the RegistrationResponse cloud-code.
In there you can also get any data that is returned in the response as normal, such as player id info etc [docs here].

So, for example, you can get the player info in the response with ... Spark.getData().Player;

-Sean

 

Thanks Sean for a quick reply. So basically, you are saying that every time a game creates new challenge, it has to access its own database on the cloud. Hmmm ... maybe I am missing something, but wouldn't it be a nice feature if  CreateChallengeRequest   would take a players name instead of players Id? Your challenge cloud code could go into players data and search out player based on the name - id match?

Oh,

Sorry, i misunderstood you there Roman. I though you wanted just to access the player details in a collection.

So, yes, it would be nice if we could just throw some player names at those request so it would be a bot easier to see at first glance what is going on. The reason we dont so this is because it is very possible for two players to have the same username or display name. Since the playerId is always unique to the player's document in the collection, its safest to use playerIds, especially if you got >1million players there will certainly be duplicates.

However, if this is not a problem for you, you can always write a method which will return the playerId from any name found in the collection? Does that suit?

Sean

 

Thanks Sean,


1. I thought that when player is registering with a username that is already in the database, registration fails and return NAME_TAKEN error or something similar, at least that's what I was getting when I was playing with registrations. So how could there be two identical usernames in the player database?


2. Basically, what I am doing at this point is, one player wants to play a game together with another player that is his friend and he knows his display name. So he challenges him and code uses CreateChallengeRequest. But that function requires the other player's Id, that the challenging client doesn't know. As you suggested in your first response, I could create my own database and every time some player registers, I would add to it, and later retrieve player Id using his username. That seems doable, can you please educate me :-), where in the cloud code would that database/collection be located/created? Can I override default CreateChallengeRequest and add onto that code my own code that retrieves data from my own collection? Do I have access to your CreateChallengeRequest code so I can modify it?


3. In your second replay, you suggest to write a method that will return an Id from any name in collection. That also looks suitable for me. Again, I guess I could add/override default CreateChallengeRequest code. Would you mind showing my an example what collection with player's info and how to access it? Or just if there is some reference in your documentations or on the forum, that would be great to, too. 


Thanks so much for you help,



Sorry Roman,

You are right, i was thinking of multiple display names rather than usernames.

So if you want to go with a search and return option, you are going to have to store player information in a collection when a player registers.
Doing this in the response also gives you access to some other player details...


 

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()
    })
}

 

So, now you have a list of player details, you can check if that is working on the NoSQL explorer.

Next, in you event request or wherever you are calling your CreateChallenge from you just need to search the player-list for a document containing that username or display name.


 

var playerData1 = Spark.runtimeCollection("playerList").findOne({ "displayName" : "opponent-name" });
var playerData2 = Spark.runtimeCollection("playerList").findOne({ "userName" : "opponent-name" });

 

This will get you all their player info you have stored in the "playerList" collection.

So, if you want to get just the ID, you can use...

playerData1.id, playerData2.id

Hope that helps,
Sean


 

Answer

Thanks Sean.


I was able to implement what I needed, I created a custom collection as you suggested and then created a custom event that returns a playerId when called with username parameter.

I still think, this should be implemented by default in cloud platform, since every time a player wants to create a challenge, he doesn't knows players Ids, but he likely knows usernames. (unless I am misunderstanding the most popular use of Challenges).


Thanks again for your help.

i'm running into a similar problem loading player information using his/her username. i also feel that Spark.loadPlayer (or a username counterpart) would greatly benefit from having a username version in addition to playerId.

Hi Ham, 
The situation is quite the same since this post was made. To do this you are required to mirror the player collection. You can then query the collection by userName and get the "id" field of that document. This will allow you access to the player id through the userName. Thanks for your suggestion we'll keep that in mind for future releases. 

Does that help clear things up you ? 

Regards Patrick.

yes, thank you for your quick reply. keep up the good work!

Thank you Hamlet, much appreciated. 

As always contact us if you need assistance. 

Regards Patrick. 

Login to post a comment