When a player submits a Registration Request to GameSparks, a check is made to ensure the username and password fields that the player uses are unique. However, other fields a player uses when registering don't need to be unique. For your game, you might want to impose uniqueness for fields a player uses, such as the displayName.
The following example guides you through a registration flow with some added checks for unique fields prior to allowing a player to register. In this example, we'll have two fields which must be unique in order for your player's registration to be successful. One of them, the displayName field, is a mandatory RegistrationRequest field. The other, the email field, is an optional supplemental field we've decided to include in the request's scriptData.
To achieve this uniqueness check example for player fields, we'll make use of two GameSparks features:
- Game Data Service
- Cloud Code
Game Data Service
The first thing we need to do is use the Game Data Service to set up a Data Type to store the data we'll be checking against in our Registration flow. To do this, navigate to Configurator > Game Data and select the Add button:
Because we'll be querying two fields in the Data Type, we'll need to add two indexes for our playerData Data Type. Both indexes should be of type String. Both fields are root-level fields in our document, so the Path value should equal the name of the field we'll be using to store our data:
We will only need two Cloud Code scripts for this flow.
First, we attach a Cloud Code script to the RegistrationResponse. To do this, go to Configurator> Cloud Code > Responses > RegistrationResponse. This Cloud Code script will take the displayName and email each player has used when registering and store them using the Game Data Service in our playerData Data Type:
Secondly, we attach a Cloud Code script to the RegistrationRequest. To do this, go to Configurator > Cloud Code > Requests >RegistrationRequest. This Cloud Code script checks the displayName and email fields used by a player when attempting to register, and throws an error if they've already been taken:
We can now test out this set up using the GameSparks Test Harness. First, we'll register a player by sending a RegistrationRequest, including an email address in the request's scriptData:
We can see from the response that the registration was successful and a new player has been created:
If we now navigate to the Data Explorer and open the playerData Data Type, we can verify that the player's data was persisted successfully by entering the userId returned in the RegistrationResponse in the Find tab:
Now, using the Test Harness we'll attempt to register a second player using the same displayName as our first player, which was p1:
This time the error we built into in the Cloud Code we attached to the RegistrationRequest is returned and no new player is created:
Note in this response the absence of the player fields (userId, displayName, and so on) when compared to the successful response we got back for player1.
We repeat this step with a different displayName but the same email as player1, which results in much the same response, with a slightly different error message:
Sending it with both displayName and email fields changed results in a successful registration and a new player being created:
Lastly, we can see that registering with a previously unused displayName and no email address also succeeds, as expected: