/// <summary> /// Grants the specified character type to the user. /// </summary> public static void GrantCharacterToUser(GrantCharacterToUserRequest request, GrantCharacterToUserCallback resultCallback, ErrorCallback errorCallback) { if (PlayFabSettings.DeveloperSecretKey == null) throw new Exception ("Must have PlayFabSettings.DeveloperSecretKey set to call this method"); string serializedJSON = JsonConvert.SerializeObject(request, Util.JsonFormatting, Util.JsonSettings); Action<string,string> callback = delegate(string responseStr, string errorStr) { GrantCharacterToUserResult result = null; PlayFabError error = null; ResultContainer<GrantCharacterToUserResult>.HandleResults(responseStr, errorStr, out result, out error); if(error != null && errorCallback != null) { errorCallback(error); } if(result != null) { if(resultCallback != null) { resultCallback(result); } } }; PlayFabHTTP.Post(PlayFabSettings.GetURL()+"/Server/GrantCharacterToUser", serializedJSON, "X-SecretKey", PlayFabSettings.DeveloperSecretKey, callback); }
public void UserCharacter() { var request = new ServerModels.ListUsersCharactersRequest(); request.PlayFabId = playFabId; // Received from client upon login var getCharsTask = PlayFabServerAPI.GetAllUsersCharactersAsync(request); getCharsTask.Wait(); UUnitAssert.Null(getCharsTask.Result.Error, "Failed to GetChars"); UUnitAssert.NotNull(getCharsTask.Result.Result, "Failed to GetChars"); UUnitAssert.NotNull(getCharsTask.Result.Result.Characters, "Failed to GetChars"); ServerModels.CharacterResult targetCharacter = null; foreach (var eachCharacter in getCharsTask.Result.Result.Characters) if (eachCharacter.CharacterName == CHAR_NAME) targetCharacter = eachCharacter; if (targetCharacter == null) { // Create the targetCharacter since it doesn't exist var grantRequest = new ServerModels.GrantCharacterToUserRequest(); grantRequest.PlayFabId = playFabId; grantRequest.CharacterName = CHAR_NAME; grantRequest.CharacterType = CHAR_TEST_TYPE; var grantTask = PlayFabServerAPI.GrantCharacterToUserAsync(grantRequest); grantTask.Wait(); UUnitAssert.Null(grantTask.Result.Error, "Grant character failed"); UUnitAssert.NotNull(grantTask.Result.Result, "Grant character failed"); UUnitAssert.NotNull(grantTask.Result.Result.CharacterId, "Grant character failed"); // Attempt to get characters again getCharsTask = PlayFabServerAPI.GetAllUsersCharactersAsync(request); getCharsTask.Wait(); UUnitAssert.Null(getCharsTask.Result.Error, "Failed to GetChars"); UUnitAssert.NotNull(getCharsTask.Result.Result, "Failed to GetChars"); UUnitAssert.NotNull(getCharsTask.Result.Result.Characters, "Failed to GetChars"); foreach (var eachCharacter in getCharsTask.Result.Result.Characters) if (eachCharacter.CharacterName == CHAR_NAME) targetCharacter = eachCharacter; } // Save the requested character UUnitAssert.NotNull(targetCharacter, "The test character did not exist, and was not successfully created"); }
/// <summary> /// Grants the specified character type to the user. /// </summary> public static async Task<PlayFabResult<GrantCharacterToUserResult>> GrantCharacterToUserAsync(GrantCharacterToUserRequest request) { if (PlayFabSettings.DeveloperSecretKey == null) throw new Exception ("Must have PlayFabSettings.DeveloperSecretKey set to call this method"); object httpResult = await PlayFabHTTP.DoPost(PlayFabSettings.GetURL() + "/Server/GrantCharacterToUser", request, "X-SecretKey", PlayFabSettings.DeveloperSecretKey); if(httpResult is PlayFabError) { PlayFabError error = (PlayFabError)httpResult; if (PlayFabSettings.GlobalErrorHandler != null) PlayFabSettings.GlobalErrorHandler(error); return new PlayFabResult<GrantCharacterToUserResult> { Error = error, }; } string resultRawJson = (string)httpResult; var serializer = JsonSerializer.Create(PlayFabSettings.JsonSettings); var resultData = serializer.Deserialize<PlayFabJsonSuccess<GrantCharacterToUserResult>>(new JsonTextReader(new StringReader(resultRawJson))); GrantCharacterToUserResult result = resultData.data; return new PlayFabResult<GrantCharacterToUserResult> { Result = result }; }