How to query all players to check if nickname of character already exists in cloud code?


#1

Hi,
I have possibility in my game to create multiple characters for one Player.
Now I’m wondering how to check if nickname is free to use.
Is there any method to query all players data in cloud script?

var sdk = ChilliConnect.getSdk("2.18.0");
try {
    var nickname = ChilliConnect.Request.nickname;
    //validate nickname
    if(nickname.length < 4) {
        return { "Error" : "Nickname too short" };
    }
    if(nickname.length > 20) {
        return { "Error" : "Nickname too long" };
    }
    var key = "characters";
    var getPlayerDataResponse = sdk.CloudData.getPlayerData( [key] );
    var characters = [];
    if(getPlayerDataResponse.Values[0]){
        characters = getPlayerDataResponse.Values[0].Value;
    }
    var newCharacter = {
        "nickname" : nickname,
        "level" : 0,
        "pos": {
            "x": 0,
            "y": 0,
            "z": 0
        },
        "gender": "male"
    }
    characters.push( newCharacter );
    sdk.CloudData.setPlayerData( key, characters );
    return {
    "Success": true,
    "NewCharacter": newCharacter
    };
}
catch(e)
{
    ChilliConnect.Logger.error("Error:" + e );
    return { "Error" : "CREATE_NEW_CHARACTER" + e.code + ": " + e.message }
}

Example player’s data:


#2

Hi Michal

There’s no way search across player data - however, for creating profiles or player characters, there is another way you can do this with cloud code.

With this method, you use CloudCode to create new profiles as separate ChilliConnect accounts:

try {
var sdk = ChilliConnect.getSdk("2.8.0");

//Create a new player representing the profile
var response = sdk.PlayerAccounts.createPlayer(null, ChilliConnect.Request.Name);

//Load existing profiles
var data = sdk.CloudData.getPlayerData(['PROFILES']);

//Add new profiles
var profiles = (data.Values.length > 0) ? data.Values[0].Value : {};
profiles[ChilliConnect.Request.Name] = response.ChilliConnectID;

//Save profiles
sdk.CloudData.setPlayerData('PROFILES', profiles);

return { "Profiles" : profiles };
}
catch(e) {
    ChilliConnect.Logger.info(e);
    return "Error";
}

You could use the Username field to store nicknames, which would enforce uniqueness. To save data against a profile, or access data from a profile, you use wrapper CloudCode scripts that update the profile account using the asPlayer method (the attached sample shows updating a leaderboard score but this can be adapted to other methods).

try {
var sdk = ChilliConnect.getSdk("2.8.0");

//Load existing profile
var data = sdk.CloudData.getPlayerData(['PROFILES']);

//Make sure requested profile name exists
if(data.Values.length === 0 || !data.Values[0].Value[ChilliConnect.Request.Name]) {
    return "Profile not found";
}

var profileChilliConnectId = data.Values[0].Value[ChilliConnect.Request.Name];

//Add score 
return sdk.PlayerAccounts.asPlayer(profileChilliConnectId, function() {
    return sdk.Leaderboards.addScore(ChilliConnect.Request.Score, "MY_SCORES"); 
});


}
catch(e) {
    ChilliConnect.Logger.info(e);
    return "Error";
}