Sign In Register

How can we help you today?

Start a new topic
Answered

Check if player exist by displayname or username

 Hi all,


I wonder what's the best / easiest way to check if a player exists.


- I like to check inside unity if a certain displayname is used allready.

I don't want to do this on registration request. I need to do this check before registering/authentication


- I like to do the same with username. Check if a certain username exists in the game.


I'm new to Gamesparks so any code snippets to get me on my way would be highly appreciated.


Best Answer

 Hi again,

This is related to the same subject so i gues i just post it here and not start a new thread...


I have a simple cloudcode script attached to DeviceAuthenticationRequest.

It makes a runtimeCollection with displaynames so i can check later for duplicates.

This works fine.


But i would like to add the username or id from the player to this list but it keeps returning null for id or username, so i'm retrieving it wrong.


Can anyone please help me how to retrieve username or id in DeviceAuthenticationRequest ?


thanks


// ====================================================================================================
//
// Cloud Code for DeviceAuthenticationRequest, write your code here to customise the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://portal.gamesparks.net/docs.htm			
//
// ====================================================================================================
//load the displayNames collection
var displayNameCol = Spark.runtimeCollection("playerList")

//get the displayName passed in from the request
var displayName = Spark.getData().displayName;

//get the user id
var userId = Spark.getData().userId;

//check the custom collection to see if the current displayName is already in use
var displayNameCheck = displayNameCol.findOne({"displayName":displayName})


if(displayNameCheck === null){ // displayname doesnt exist so we make a new list
    Spark.setScriptData("Success", displayName + " is Unique")
    // add the displayname to the list    
        Spark.runtimeCollection("playerList").insert({
        "displayName" : displayName,
        "userId" : userId,
    })
}

 

 


Hi Ward,


We have some documentation regarding this here (https://docs2.gamesparks.com/tutorials/database-access-and-cloud-storage/searching-for-players-and-teams.html#searching-with-cloud-code) using some of the sample code here I'm sure you can write an event that would facilitate this for you. If there's anything else we're happy to help you further.


Kind regards,

 - Steve

 

// Search DB for playerId based on username, displayname, or email address.
var playerId = Spark.systemCollection("player").findOne({ "$or" : [
        {"userName": { "$regex" : input, "$options": "i"} }
        , {"displayName": { "$regex" : input, "$options": "i"} }
        , {"privateData.email": { "$regex": input, "$options": "i"} }
    ] }, {"_id": 1, "lastSeen": 1});

 This is what I use: input variable being the search query. So the input in my case would be "username", "displayname", or "email@address.com". This searches the player collection (case-insensitive, hence regex use) based on your query and returns one or more playerId results. YMMV.

 

Thanks for the reply.
If i would like to get this information inside unity, how would i do this ?

Mind me i'm completely new to Gamesparks and Cloudcode.

 

Thanks both to Steve and Christopher for the help.
 I got it working now ;)

 

You're welcome. I am new as of a few weeks ago now, the learning curve is steep at first but becomes easy enough when you understand the architecture. I'm sure Customer support is tired of all the tickets I send in asking for clarifications ;)

Answer

 Hi again,

This is related to the same subject so i gues i just post it here and not start a new thread...


I have a simple cloudcode script attached to DeviceAuthenticationRequest.

It makes a runtimeCollection with displaynames so i can check later for duplicates.

This works fine.


But i would like to add the username or id from the player to this list but it keeps returning null for id or username, so i'm retrieving it wrong.


Can anyone please help me how to retrieve username or id in DeviceAuthenticationRequest ?


thanks


// ====================================================================================================
//
// Cloud Code for DeviceAuthenticationRequest, write your code here to customise the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://portal.gamesparks.net/docs.htm			
//
// ====================================================================================================
//load the displayNames collection
var displayNameCol = Spark.runtimeCollection("playerList")

//get the displayName passed in from the request
var displayName = Spark.getData().displayName;

//get the user id
var userId = Spark.getData().userId;

//check the custom collection to see if the current displayName is already in use
var displayNameCheck = displayNameCol.findOne({"displayName":displayName})


if(displayNameCheck === null){ // displayname doesnt exist so we make a new list
    Spark.setScriptData("Success", displayName + " is Unique")
    // add the displayname to the list    
        Spark.runtimeCollection("playerList").insert({
        "displayName" : displayName,
        "userId" : userId,
    })
}

 

 

@ward, its fine. The approval process on this forum is pretty dumb. Luckily you can see unapproved posts as they come in via email. (ie, your post technically isn't even visible to me right now, only saw the message via email.

 

I was wondering if there is a way to check for a username or displayname (as described above) without being authenticated as a player.

Ii need to check if a display name is taken before authenticating as device, later if the player wants to make a account it gets upgraded to one.

Right now i created a 'login account' that users first login to so they can check if a name exists. Then if it doesn't, i create a new account and authenticate with that one.

This login account gets used by all new players. If this bad practice to do it this way ?

 

It's better to do DeviceAuthentication as startup regardless, then convert DeviceAuth to a login via ChangeAccountDetailsRequest when the user wants to register an account.

 

I am doing that, but i want to have my display names unique.
Even if the player is authenticated as device.

That's why i need to check if the displayname is available.

 

If the Display Name needs to be unique (which, by the way, usernames already are), then set the username and displayname to be equal, and on Cloudcode just do a case-insensitive check if username == displayname. If the username is already taken, you'll get a script error.
I don't want them to be equal because some reason i saw on another post in this forum...

- If a player tries to login a few times with the wrong password the account gets locked.
This is for safety reasons so other players have more trouble hacking or guessing in another players account. But this allows trolls to screw other players over.
- a player wants to troll another player and just uses that others player's displany/username  and fails a few times logging in. That other player's account gets locked for multiple failed logins.
I don't say this will happen but it could happen once players know how easy it is to lock other players account.



 

Well, the code I provided above will search username, displayname, and privateData.email - copy or remove whatever query part you don't need.
I managed to have that part working, Thanks again for putting me on the right path..

I just wondered if it was possible to do this without being logged in/authenticated.
It's not a big thing because i got it working as it is. I was just wondering if it was bad practice to do this with a 'login account' to do the check.

I think i should most likely leave it alone since it's working fine :)
Don't fix it if it's not broken, right ?


 

Login to post a comment