internal static IEnumerator InsertGame(Game game, Player player, int score, int difficulty) { GameSerializable newGame = new GameSerializable(); newGame.Id = player.Id; newGame.Nickname = player.Nickname; newGame.Score = score.ToString(); newGame.Difficulty = difficulty.ToString(); newGame.DateStarted = game.DateStarted.ToString(); newGame.DateFinished = game.DateFinished.ToString(); using (UnityWebRequest httpClient = new UnityWebRequest(player.HttpServerAddress + "/api/Game/RegisterGame", "POST")) { string playerData = JsonUtility.ToJson(newGame); byte[] bodyRaw = Encoding.UTF8.GetBytes(playerData); httpClient.uploadHandler = new UploadHandlerRaw(bodyRaw); httpClient.SetRequestHeader("Content-type", "application/json"); httpClient.SetRequestHeader("Authorization", "bearer " + player.Token); httpClient.certificateHandler = new ByPassCertificate(); yield return(httpClient.SendWebRequest()); if (httpClient.isNetworkError || httpClient.isHttpError) { throw new Exception("RegisterNewGame>" + httpClient.error); } Debug.Log("\nRegisterGame: " + httpClient.responseCode); } }
private IEnumerator InsertGame() { GameSerializable game = new GameSerializable(); game.PlayerId = player.Id; game.Difficulty = difficultyText[difficulty - 1]; using (UnityWebRequest httpClient = new UnityWebRequest(player.HttpServerAddress + "/api/Game/InsertNewGame", "POST")) { string playerData = JsonUtility.ToJson(game); byte[] bodyRaw = Encoding.UTF8.GetBytes(playerData); httpClient.uploadHandler = new UploadHandlerRaw(bodyRaw); httpClient.downloadHandler = new DownloadHandlerBuffer(); httpClient.SetRequestHeader("Content-type", "application/json"); httpClient.SetRequestHeader("Authorization", "bearer " + player.Token); yield return(httpClient.SendWebRequest()); if (httpClient.isNetworkError || httpClient.isHttpError) { throw new System.Exception("InsertNewGame > Error: " + httpClient.responseCode + ", Info: " + httpClient.error); } else { gameManager.StartGame(difficulty); } } }
private IEnumerator StopGame(int score) { GameSerializable game = new GameSerializable(); game.IdUser = player.Id; game.Score = score.ToString(); using (UnityWebRequest httpClient = new UnityWebRequest(player.HttpServer + "/api/Game/UpdateGame", "POST")) { string playerData = JsonUtility.ToJson(game); byte[] bodyRaw = Encoding.UTF8.GetBytes(playerData); httpClient.uploadHandler = new UploadHandlerRaw(bodyRaw); httpClient.downloadHandler = new DownloadHandlerBuffer(); httpClient.SetRequestHeader("Content-type", "application/json"); httpClient.SetRequestHeader("Authorization", "bearer " + player.Token); yield return(httpClient.SendWebRequest()); if (httpClient.isNetworkError || httpClient.isHttpError) { throw new System.Exception("StopGame > Error: " + httpClient.responseCode + ", Info: " + httpClient.error); } else { Debug.Log("StopGame > Info: " + httpClient.responseCode); } } }
public IEnumerator GetTopScore() { UnityWebRequest httpClient = new UnityWebRequest(player.HttpServerAddress + "/api/Game/TopScore", "GET"); httpClient.SetRequestHeader("Authorization", "bearer " + player.Token); httpClient.SetRequestHeader("Accept", "application/json"); httpClient.downloadHandler = new DownloadHandlerBuffer(); httpClient.certificateHandler = new ByPassCertificate(); yield return(httpClient.SendWebRequest()); if (httpClient.isNetworkError || httpClient.isHttpError) { throw new Exception("GetLastGames: " + httpClient.error); } else { string stringToRecieve = httpClient.downloadHandler.text; GameSerializable gameJ = JsonUtility.FromJson <GameSerializable>(stringToRecieve); topScore.text = "Your top score ever is <color=red>" + gameJ.Score + "</color>!"; } httpClient.Dispose(); }