/// <summary> /// Creates a new record on server with data from <paramref name="record"/>.<br/> /// Note that the <see cref="PostPlayerRecord.objectId"/> of <paramref name="record"/> is ignored. /// </summary> /// <param name="record"></param> /// <returns></returns> /// <remarks>Designed to run as a <see cref="Coroutine"/></remarks> public static IEnumerator PostRecord(PostPlayerRecord record) { string uri = ConstructURL(); string data = JsonUtility.ToJson(record); //For some bizarre reason, UnityWebRequest can't send JSON through POST requests, so I have to use a Put here. using UnityWebRequest request = UnityWebRequest.Put(uri, data); request.SetRequestHeader("Content-Type", "application/json"); LogRequest(request); yield return(request.SendWebRequest()); if (request.result != UnityWebRequest.Result.Success) { Debug.LogWarning(request.error); } }
/// <summary> /// Updates the backendless record by adding the sepcified <paramref name="killsToAdd"/> and <paramref name="levelsToAdd"/> /// </summary> /// <param name="playerName">The identifying name of the client</param> /// <param name="killsToAdd">Number of kills to add</param> /// <param name="levelsToAdd">Nunber of levels to add</param> public void AddScore(string playerName, int killsToAdd, int levelsToAdd) { this.StartCoroutine(BackendlessHelper.GetRecord(BackendlessHelper.URIByName(playerName), (player, errors) => { if (player == null) { var newPlayer = new PostPlayerRecord() { Player_Name = playerName, Total_Kills = killsToAdd, Total_Levels = levelsToAdd, }; this.StartCoroutine(BackendlessHelper.PostRecord(newPlayer)); } else { player.Total_Kills += killsToAdd; player.Total_Levels += levelsToAdd; this.StartCoroutine(BackendlessHelper.PutRecord(player)); } })); }