From 1a03950b4ad079dd0788ab939b30fd01b4c4a7fa Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Mon, 10 Aug 2020 21:14:54 -0400 Subject: [PATCH] Improve web request reliability and readability --- Leadercraft/LeadercraftPlugin.cs | 12 +++--- Leadercraft/Scoring/State.cs | 22 ++++++++--- Leadercraft/Scoring/UploadJob.cs | 5 ++- Leadercraft/Server/LeadercraftApi.cs | 56 ++++++++++++++++------------ 4 files changed, 58 insertions(+), 37 deletions(-) diff --git a/Leadercraft/LeadercraftPlugin.cs b/Leadercraft/LeadercraftPlugin.cs index 54160bb..2a90fc5 100644 --- a/Leadercraft/LeadercraftPlugin.cs +++ b/Leadercraft/LeadercraftPlugin.cs @@ -26,9 +26,9 @@ namespace Leadercraft private static string criteriaUrl = #if DEBUG - "http://192.168.122.229:7048/c/criteria"; + "http://192.168.122.229:7048/criteria"; #else - "https://leadercraft.exmods.org/criteria"; + "https://leadercraft.exmods.org/c/criteria"; #endif public static void BuildApi() { @@ -36,11 +36,11 @@ namespace Leadercraft { if (!Tools.IsSteamAvailable) { - Logging.MetaDebugLog("Steam is unavailable :("); + Logging.MetaLog("Steam is unavailable :("); return; } Api = new LeadercraftApi(Tools.UserId, tokenUrl, criteriaUrl); - GamecraftModdingAPI.Utility.Logging.MetaDebugLog("Leadercraft API initialized"); + GamecraftModdingAPI.Utility.Logging.MetaLog("Leadercraft API initialized"); } } @@ -66,10 +66,10 @@ namespace Leadercraft GamecraftModdingAPI.App.Game.Edit += (_, __) => State.StopPlayingGame(); GamecraftModdingAPI.App.Game.Enter += (_, __) => State.EnterGame(); GamecraftModdingAPI.App.Game.Exit += (_, __) => State.ExitGame(); - GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has started up"); + GamecraftModdingAPI.Utility.Logging.MetaLog($"{Name} has started up"); // Debug mode - Debug(); + //Debug(); } // unused methods diff --git a/Leadercraft/Scoring/State.cs b/Leadercraft/Scoring/State.cs index 67ec1e7..14539fe 100644 --- a/Leadercraft/Scoring/State.cs +++ b/Leadercraft/Scoring/State.cs @@ -30,30 +30,32 @@ namespace Leadercraft.Scoring public static bool IsGameSynced { get; private set; } + private static bool isBadGame = false; + private static Player localPlayer = null; public static void EnterGame() { if (IsInGame) return; - Logging.MetaDebugLog("Entering game"); IsInGame = true; IsGameSynced = false; IsGameComplete = false; GameId = Server.Tools.GameId; GameEnterTime = DateTime.UtcNow; + Logging.MetaLog($"Entering game {GameId} at {GameEnterTime}"); } public static void ExitGame() { if (!IsInGame) return; - Logging.MetaDebugLog("Exiting game"); + Logging.MetaLog($"Exiting game {GameId}"); IsInGame = false; } public static void StartPlayingGame() { if (IsPlayingGame) return; - Logging.MetaDebugLog("Starting to play game"); + Logging.MetaLog($"Starting to play game {GameId}"); Score = 0; IsPlayingGame = true; GameStartTime = DateTime.UtcNow; @@ -67,7 +69,7 @@ namespace Leadercraft.Scoring { if (!IsPlayingGame) return; GamePlayTime = DateTime.UtcNow - GameStartTime; - Logging.MetaDebugLog("Stopping game"); + Logging.MetaLog($"Stopping game {GameId} after {GamePlayTime.TotalSeconds}s"); IsPlayingGame = false; } @@ -98,9 +100,17 @@ namespace Leadercraft.Scoring private static void loopPass() { if (!State.IsPlayingGame) return; - if (localPlayer == null && Player.Exists(PlayerType.Local)) localPlayer = new Player(PlayerType.Local); + if (localPlayer == null && Player.Exists(PlayerType.Local)) + { + localPlayer = new Player(PlayerType.Local); + isBadGame = localPlayer.GameOver; + if (isBadGame) + { + GamecraftModdingAPI.Utility.Logging.MetaLog($"Ignoring game {GameId} since it does not seem to have a GameOver state."); + } + } if (localPlayer == null) return; - if (localPlayer.GameOver && !localPlayer.Dead) + if (localPlayer.GameOver && !localPlayer.Dead && !isBadGame) { State.StopPlayingGame(); //State.GamePlayTime.TotalSeconds diff --git a/Leadercraft/Scoring/UploadJob.cs b/Leadercraft/Scoring/UploadJob.cs index 3fe4d05..887f6a5 100644 --- a/Leadercraft/Scoring/UploadJob.cs +++ b/Leadercraft/Scoring/UploadJob.cs @@ -1,6 +1,6 @@ using System; using System.Threading; - +using GamecraftModdingAPI.App; using Unity.Collections; using Unity.Jobs; using Unity.Mathematics; @@ -32,7 +32,7 @@ namespace Leadercraft.Scoring this.location = new float[3] { position.x, position.y, position.z}; this.player = playerId; this.playerName = playerName; - this.game = 2;//gameId; + this.game = gameId; LeadercraftPlugin.BuildApi(); } @@ -44,6 +44,7 @@ namespace Leadercraft.Scoring private void Execute() { + if (Game.CurrentGame().Name == "Leadercraft's sucky game") game = 2; if (player < 1000 || game == 0) return; // offline game LeadercraftResult tokenResult = LeadercraftPlugin.Api.RequestPOSTToken(playerName); if (tokenResult.IsError) diff --git a/Leadercraft/Server/LeadercraftApi.cs b/Leadercraft/Server/LeadercraftApi.cs index 9d4874d..b4cbe15 100644 --- a/Leadercraft/Server/LeadercraftApi.cs +++ b/Leadercraft/Server/LeadercraftApi.cs @@ -23,22 +23,25 @@ namespace Leadercraft.Server public LeadercraftResult RequestPOSTToken(string playerName = "???") { NewKeyStruct reqBodyObj = new NewKeyStruct{ PlayerID = _userId, PlayerName = playerName }; - byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(reqBodyObj)); - // Request + string reqBodyStr = JsonConvert.SerializeObject(reqBodyObj); + // Request WebRequest request = WebRequest.Create(_tokenUrl); request.Method = "POST"; - request.ContentLength = reqBodyBytes.Length; + request.ContentLength = reqBodyStr.Length; request.ContentType = "application/json"; - Stream body; + StreamWriter body; try { - body = request.GetRequestStream(); - body.Write(reqBodyBytes, 0, reqBodyBytes.Length); + body = new StreamWriter(request.GetRequestStream()); + body.Write(reqBodyStr); body.Close(); } catch (WebException e) { - return new LeadercraftResult(new byte[] { }, (int)e.Status); + int status = (int) e.Status; + if (e.Response != null && e.Response is HttpWebResponse webResponse) + status = (int)webResponse.StatusCode; + return new LeadercraftResult(new byte[] { }, status); } // Response HttpWebResponse response = null; @@ -48,35 +51,40 @@ namespace Leadercraft.Server } catch (WebException e) { - return new LeadercraftResult(new byte[] { }, (int)e.Status); + int status = (int) e.Status; + if (e.Response != null && e.Response is HttpWebResponse webResponse) + status = (int)webResponse.StatusCode; + return new LeadercraftResult(new byte[] { }, status); } - body = response.GetResponseStream(); - byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))]; - body.Read(respBodyBytes, 0, respBodyBytes.Length); + StreamReader respBody = new StreamReader(response.GetResponseStream()); + string respBodyStr = respBody.ReadToEnd(); response.Close(); - return new LeadercraftResult(respBodyBytes, (int)response.StatusCode); + return new LeadercraftResult(respBodyStr, (int)response.StatusCode); } public LeadercraftResult RequestPOSTCriteria(CriteriaStruct criteria, string token) { criteria.PlayerID = _userId; - byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(criteria)); + string reqBodyStr = JsonConvert.SerializeObject(criteria); // Request WebRequest request = WebRequest.Create(_criteriaUrl); request.Method = "POST"; - request.ContentLength = reqBodyBytes.Length; + request.ContentLength = reqBodyStr.Length; request.ContentType = "application/json"; request.Headers.Add(HttpRequestHeader.Authorization, "leadercraft "+token); - Stream body; + StreamWriter body; try { - body = request.GetRequestStream(); - body.Write(reqBodyBytes, 0, reqBodyBytes.Length); + body = new StreamWriter(request.GetRequestStream()); + body.Write(reqBodyStr); body.Close(); } catch (WebException e) { - return new LeadercraftResult(new byte[] { }, (int)e.Status); + int status = (int) e.Status; + if (e.Response != null && e.Response is HttpWebResponse webResponse) + status = (int)webResponse.StatusCode; + return new LeadercraftResult(new byte[] { }, status); } // Response HttpWebResponse response = null; @@ -86,13 +94,15 @@ namespace Leadercraft.Server } catch (WebException e) { - return new LeadercraftResult(new byte[] { }, (int)e.Status); + int status = (int) e.Status; + if (e.Response != null && e.Response is HttpWebResponse webResponse) + status = (int)webResponse.StatusCode; + return new LeadercraftResult(new byte[] { }, status); } - body = response.GetResponseStream(); - byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))]; - body.Read(respBodyBytes, 0, respBodyBytes.Length); + StreamReader respBody = new StreamReader(response.GetResponseStream()); + string respBodyStr = respBody.ReadToEnd(); response.Close(); - return new LeadercraftResult(respBodyBytes, (int)response.StatusCode); + return new LeadercraftResult(respBodyStr, (int)response.StatusCode); } } }