question

wader avatar image
wader asked

Cloud Script - Query all players and character names for validation if character name already exists

Hi,

Question:

How to query all players and all created characters from CloudScript Server API.

Example:

I have game, where users can have multiple characters.

I need to validate if character name is unique in cloudscript.

Example of CreateNewCharacter cloud script function

var CreateNewCharacter = function (args: any, context: IPlayFabContext): boolean {
    let nickname = "";
    if (args && args.Nickname)
        nickname  = args.Nickname;
   

    if (nickname.length <= 4) {
        log.error("Nickname too short");
        return false;
    }


    if (nickname.length > 10) {
        log.error("Nickname too long");
        return false;
    }




    //Check if character already exists
    ????????????????????????????????????????????????????? 






    var result = server.GrantCharacterToUser({
        PlayFabId: currentPlayerId,
        CharacterName: nickname,
        CharacterType: "TestCharacter"
    });


    return true;

}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

No, that’s not supported or even possible. In your case, the “Create Character” player action results in iterating through all players, which means that each player could be attempting to make up to thousands or even millions of API calls – depending on your player base.

Instead, you should maintain a SET (take Javascript for instance) that stores unique “Character Name” values for your code to look up to. When a specific name the player inputted is not in that Set, you may create a character using that name, otherwise he/she should provide another names. Considering that it’s possible for multiple players to create characters using the same name at the same time, you may also need to implement concurrency control.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

wader avatar image wader commented ·

Hi,

thank you for your reply,

but where should I store the list od nicknames? The cloud script variable should be populated with data.

Where to store the list so in cloudscript I can populate variable and look up?

Should I use external database and create rest API for that. If yes, then why should I use Playfab when I'm able to create my own rest API ;D

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan wader commented ·

A external database/server would be favorable, and, you can also consider using Azure Function for this.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.