Improve web request reliability and readability
This commit is contained in:
parent
9a4cfd97bb
commit
1a03950b4a
4 changed files with 58 additions and 37 deletions
|
@ -26,9 +26,9 @@ namespace Leadercraft
|
||||||
|
|
||||||
private static string criteriaUrl =
|
private static string criteriaUrl =
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
"http://192.168.122.229:7048/c/criteria";
|
"http://192.168.122.229:7048/criteria";
|
||||||
#else
|
#else
|
||||||
"https://leadercraft.exmods.org/criteria";
|
"https://leadercraft.exmods.org/c/criteria";
|
||||||
#endif
|
#endif
|
||||||
public static void BuildApi()
|
public static void BuildApi()
|
||||||
{
|
{
|
||||||
|
@ -36,11 +36,11 @@ namespace Leadercraft
|
||||||
{
|
{
|
||||||
if (!Tools.IsSteamAvailable)
|
if (!Tools.IsSteamAvailable)
|
||||||
{
|
{
|
||||||
Logging.MetaDebugLog("Steam is unavailable :(");
|
Logging.MetaLog("Steam is unavailable :(");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Api = new LeadercraftApi(Tools.UserId, tokenUrl, criteriaUrl);
|
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.Edit += (_, __) => State.StopPlayingGame();
|
||||||
GamecraftModdingAPI.App.Game.Enter += (_, __) => State.EnterGame();
|
GamecraftModdingAPI.App.Game.Enter += (_, __) => State.EnterGame();
|
||||||
GamecraftModdingAPI.App.Game.Exit += (_, __) => State.ExitGame();
|
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 mode
|
||||||
Debug();
|
//Debug();
|
||||||
}
|
}
|
||||||
|
|
||||||
// unused methods
|
// unused methods
|
||||||
|
|
|
@ -30,30 +30,32 @@ namespace Leadercraft.Scoring
|
||||||
|
|
||||||
public static bool IsGameSynced { get; private set; }
|
public static bool IsGameSynced { get; private set; }
|
||||||
|
|
||||||
|
private static bool isBadGame = false;
|
||||||
|
|
||||||
private static Player localPlayer = null;
|
private static Player localPlayer = null;
|
||||||
|
|
||||||
public static void EnterGame()
|
public static void EnterGame()
|
||||||
{
|
{
|
||||||
if (IsInGame) return;
|
if (IsInGame) return;
|
||||||
Logging.MetaDebugLog("Entering game");
|
|
||||||
IsInGame = true;
|
IsInGame = true;
|
||||||
IsGameSynced = false;
|
IsGameSynced = false;
|
||||||
IsGameComplete = false;
|
IsGameComplete = false;
|
||||||
GameId = Server.Tools.GameId;
|
GameId = Server.Tools.GameId;
|
||||||
GameEnterTime = DateTime.UtcNow;
|
GameEnterTime = DateTime.UtcNow;
|
||||||
|
Logging.MetaLog($"Entering game {GameId} at {GameEnterTime}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ExitGame()
|
public static void ExitGame()
|
||||||
{
|
{
|
||||||
if (!IsInGame) return;
|
if (!IsInGame) return;
|
||||||
Logging.MetaDebugLog("Exiting game");
|
Logging.MetaLog($"Exiting game {GameId}");
|
||||||
IsInGame = false;
|
IsInGame = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void StartPlayingGame()
|
public static void StartPlayingGame()
|
||||||
{
|
{
|
||||||
if (IsPlayingGame) return;
|
if (IsPlayingGame) return;
|
||||||
Logging.MetaDebugLog("Starting to play game");
|
Logging.MetaLog($"Starting to play game {GameId}");
|
||||||
Score = 0;
|
Score = 0;
|
||||||
IsPlayingGame = true;
|
IsPlayingGame = true;
|
||||||
GameStartTime = DateTime.UtcNow;
|
GameStartTime = DateTime.UtcNow;
|
||||||
|
@ -67,7 +69,7 @@ namespace Leadercraft.Scoring
|
||||||
{
|
{
|
||||||
if (!IsPlayingGame) return;
|
if (!IsPlayingGame) return;
|
||||||
GamePlayTime = DateTime.UtcNow - GameStartTime;
|
GamePlayTime = DateTime.UtcNow - GameStartTime;
|
||||||
Logging.MetaDebugLog("Stopping game");
|
Logging.MetaLog($"Stopping game {GameId} after {GamePlayTime.TotalSeconds}s");
|
||||||
IsPlayingGame = false;
|
IsPlayingGame = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,9 +100,17 @@ namespace Leadercraft.Scoring
|
||||||
private static void loopPass()
|
private static void loopPass()
|
||||||
{
|
{
|
||||||
if (!State.IsPlayingGame) return;
|
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 == null) return;
|
||||||
if (localPlayer.GameOver && !localPlayer.Dead)
|
if (localPlayer.GameOver && !localPlayer.Dead && !isBadGame)
|
||||||
{
|
{
|
||||||
State.StopPlayingGame();
|
State.StopPlayingGame();
|
||||||
//State.GamePlayTime.TotalSeconds
|
//State.GamePlayTime.TotalSeconds
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using GamecraftModdingAPI.App;
|
||||||
using Unity.Collections;
|
using Unity.Collections;
|
||||||
using Unity.Jobs;
|
using Unity.Jobs;
|
||||||
using Unity.Mathematics;
|
using Unity.Mathematics;
|
||||||
|
@ -32,7 +32,7 @@ namespace Leadercraft.Scoring
|
||||||
this.location = new float[3] { position.x, position.y, position.z};
|
this.location = new float[3] { position.x, position.y, position.z};
|
||||||
this.player = playerId;
|
this.player = playerId;
|
||||||
this.playerName = playerName;
|
this.playerName = playerName;
|
||||||
this.game = 2;//gameId;
|
this.game = gameId;
|
||||||
LeadercraftPlugin.BuildApi();
|
LeadercraftPlugin.BuildApi();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,6 +44,7 @@ namespace Leadercraft.Scoring
|
||||||
|
|
||||||
private void Execute()
|
private void Execute()
|
||||||
{
|
{
|
||||||
|
if (Game.CurrentGame().Name == "Leadercraft's sucky game") game = 2;
|
||||||
if (player < 1000 || game == 0) return; // offline game
|
if (player < 1000 || game == 0) return; // offline game
|
||||||
LeadercraftResult<KeyStruct> tokenResult = LeadercraftPlugin.Api.RequestPOSTToken(playerName);
|
LeadercraftResult<KeyStruct> tokenResult = LeadercraftPlugin.Api.RequestPOSTToken(playerName);
|
||||||
if (tokenResult.IsError)
|
if (tokenResult.IsError)
|
||||||
|
|
|
@ -23,22 +23,25 @@ namespace Leadercraft.Server
|
||||||
public LeadercraftResult<KeyStruct> RequestPOSTToken(string playerName = "???")
|
public LeadercraftResult<KeyStruct> RequestPOSTToken(string playerName = "???")
|
||||||
{
|
{
|
||||||
NewKeyStruct reqBodyObj = new NewKeyStruct{ PlayerID = _userId, PlayerName = playerName };
|
NewKeyStruct reqBodyObj = new NewKeyStruct{ PlayerID = _userId, PlayerName = playerName };
|
||||||
byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(reqBodyObj));
|
string reqBodyStr = JsonConvert.SerializeObject(reqBodyObj);
|
||||||
// Request
|
// Request
|
||||||
WebRequest request = WebRequest.Create(_tokenUrl);
|
WebRequest request = WebRequest.Create(_tokenUrl);
|
||||||
request.Method = "POST";
|
request.Method = "POST";
|
||||||
request.ContentLength = reqBodyBytes.Length;
|
request.ContentLength = reqBodyStr.Length;
|
||||||
request.ContentType = "application/json";
|
request.ContentType = "application/json";
|
||||||
Stream body;
|
StreamWriter body;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
body = request.GetRequestStream();
|
body = new StreamWriter(request.GetRequestStream());
|
||||||
body.Write(reqBodyBytes, 0, reqBodyBytes.Length);
|
body.Write(reqBodyStr);
|
||||||
body.Close();
|
body.Close();
|
||||||
}
|
}
|
||||||
catch (WebException e)
|
catch (WebException e)
|
||||||
{
|
{
|
||||||
return new LeadercraftResult<KeyStruct>(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<KeyStruct>(new byte[] { }, status);
|
||||||
}
|
}
|
||||||
// Response
|
// Response
|
||||||
HttpWebResponse response = null;
|
HttpWebResponse response = null;
|
||||||
|
@ -48,35 +51,40 @@ namespace Leadercraft.Server
|
||||||
}
|
}
|
||||||
catch (WebException e)
|
catch (WebException e)
|
||||||
{
|
{
|
||||||
return new LeadercraftResult<KeyStruct>(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<KeyStruct>(new byte[] { }, status);
|
||||||
}
|
}
|
||||||
body = response.GetResponseStream();
|
StreamReader respBody = new StreamReader(response.GetResponseStream());
|
||||||
byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))];
|
string respBodyStr = respBody.ReadToEnd();
|
||||||
body.Read(respBodyBytes, 0, respBodyBytes.Length);
|
|
||||||
response.Close();
|
response.Close();
|
||||||
return new LeadercraftResult<KeyStruct>(respBodyBytes, (int)response.StatusCode);
|
return new LeadercraftResult<KeyStruct>(respBodyStr, (int)response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LeadercraftResult<string> RequestPOSTCriteria(CriteriaStruct criteria, string token)
|
public LeadercraftResult<string> RequestPOSTCriteria(CriteriaStruct criteria, string token)
|
||||||
{
|
{
|
||||||
criteria.PlayerID = _userId;
|
criteria.PlayerID = _userId;
|
||||||
byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(criteria));
|
string reqBodyStr = JsonConvert.SerializeObject(criteria);
|
||||||
// Request
|
// Request
|
||||||
WebRequest request = WebRequest.Create(_criteriaUrl);
|
WebRequest request = WebRequest.Create(_criteriaUrl);
|
||||||
request.Method = "POST";
|
request.Method = "POST";
|
||||||
request.ContentLength = reqBodyBytes.Length;
|
request.ContentLength = reqBodyStr.Length;
|
||||||
request.ContentType = "application/json";
|
request.ContentType = "application/json";
|
||||||
request.Headers.Add(HttpRequestHeader.Authorization, "leadercraft "+token);
|
request.Headers.Add(HttpRequestHeader.Authorization, "leadercraft "+token);
|
||||||
Stream body;
|
StreamWriter body;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
body = request.GetRequestStream();
|
body = new StreamWriter(request.GetRequestStream());
|
||||||
body.Write(reqBodyBytes, 0, reqBodyBytes.Length);
|
body.Write(reqBodyStr);
|
||||||
body.Close();
|
body.Close();
|
||||||
}
|
}
|
||||||
catch (WebException e)
|
catch (WebException e)
|
||||||
{
|
{
|
||||||
return new LeadercraftResult<string>(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<string>(new byte[] { }, status);
|
||||||
}
|
}
|
||||||
// Response
|
// Response
|
||||||
HttpWebResponse response = null;
|
HttpWebResponse response = null;
|
||||||
|
@ -86,13 +94,15 @@ namespace Leadercraft.Server
|
||||||
}
|
}
|
||||||
catch (WebException e)
|
catch (WebException e)
|
||||||
{
|
{
|
||||||
return new LeadercraftResult<string>(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<string>(new byte[] { }, status);
|
||||||
}
|
}
|
||||||
body = response.GetResponseStream();
|
StreamReader respBody = new StreamReader(response.GetResponseStream());
|
||||||
byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))];
|
string respBodyStr = respBody.ReadToEnd();
|
||||||
body.Read(respBodyBytes, 0, respBodyBytes.Length);
|
|
||||||
response.Close();
|
response.Close();
|
||||||
return new LeadercraftResult<string>(respBodyBytes, (int)response.StatusCode);
|
return new LeadercraftResult<string>(respBodyStr, (int)response.StatusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue