diff --git a/AutoTrackR2/HomePage.xaml.cs b/AutoTrackR2/HomePage.xaml.cs index 2227fd0..e3eb36a 100644 --- a/AutoTrackR2/HomePage.xaml.cs +++ b/AutoTrackR2/HomePage.xaml.cs @@ -28,9 +28,9 @@ public partial class HomePage : UserControl // Set the TextBlock text KillTallyTitle.Text = $"Kill Tally - {_killHistoryManager.GetKillsInCurrentMonth().Count}"; AddKillHistoryKillsToUI(); + } - - // Update Start/Stop button states based on the isRunning flag + // public void UpdateButtonState(bool isRunning) { var accentColor = (Color)Application.Current.Resources["AccentColor"]; diff --git a/AutoTrackR2/KillHistoryManager.cs b/AutoTrackR2/KillHistoryManager.cs index 831a6ae..5c87a17 100644 --- a/AutoTrackR2/KillHistoryManager.cs +++ b/AutoTrackR2/KillHistoryManager.cs @@ -34,7 +34,19 @@ public class KillHistoryManager // Append the new kill data to the CSV file var csv = new StringBuilder(); csv.AppendLine($"\"{killData.KillTime}\",\"{killData.EnemyPilot}\",\"{killData.EnemyShip}\",\"{killData.Enlisted}\",\"{killData.RecordNumber}\",\"{killData.OrgAffiliation}\",\"{killData.Player}\",\"{killData.Weapon}\",\"{killData.Ship}\",\"{killData.Method}\",\"{killData.Mode}\",\"{killData.GameVersion}\",\"{killData.TrackRver}\",\"{killData.Logged}\",\"{killData.PFP}\""); - File.AppendAllText(_killHistoryPath, csv.ToString()); + + // Check file can be written to + try + { + using var fileStream = new FileStream(_killHistoryPath, FileMode.Append, FileAccess.Write, FileShare.None); + using var writer = new StreamWriter(fileStream); + writer.Write(csv.ToString()); + } + catch (IOException ex) + { + // Handle the exception (e.g., log it) + Console.WriteLine($"Error writing to file: {ex.Message}"); + } } public List<KillData> GetKills() diff --git a/AutoTrackR2/LogHandler.cs b/AutoTrackR2/LogHandler.cs index 3c7bef5..38ddd6e 100644 --- a/AutoTrackR2/LogHandler.cs +++ b/AutoTrackR2/LogHandler.cs @@ -78,7 +78,7 @@ public class LogHandler(string logPath) // Parse a single line of the log file and run matching handlers private void HandleLogEntry(string line) { - Console.WriteLine(line); + // Console.WriteLine(line); foreach (var handler in _eventHandlers) { var match = handler.Pattern.Match(line); @@ -98,6 +98,11 @@ public class LogHandler(string logPath) { while (!token.IsCancellationRequested) { + if (_reader == null || _fileStream == null) + { + break; + } + CheckGameProcessState(); List<string> lines = new List<string>(); diff --git a/AutoTrackR2/WebHandler.cs b/AutoTrackR2/WebHandler.cs index af2ebc7..2d8ad59 100644 --- a/AutoTrackR2/WebHandler.cs +++ b/AutoTrackR2/WebHandler.cs @@ -80,6 +80,8 @@ public static class WebHandler public static async Task SubmitKill(KillData killData) { + // int secondSpaceIndex = killData.Enlisted.IndexOf(" ", killData.Enlisted.IndexOf(" ") + 1); + // killData.Enlisted = killData.Enlisted.Insert(secondSpaceIndex, ","); var apiKillData = new APIKillData { victim_ship = killData.EnemyShip, @@ -88,6 +90,7 @@ public static class WebHandler rsi = killData.RecordNumber, weapon = killData.Weapon, method = killData.Method, + gamemode = killData.Mode, // loadout_ship = LocalPlayerData.PlayerShip ?? "Unknown", loadout_ship = killData.Ship, game_version = killData.GameVersion, @@ -95,11 +98,25 @@ public static class WebHandler location = "Unknown" }; + if (string.IsNullOrEmpty(apiKillData.rsi)) + { + apiKillData.rsi = "-1"; + } + var httpClient = new HttpClient(); - string jsonData = JsonSerializer.Serialize(killData); + string jsonData = JsonSerializer.Serialize(apiKillData); httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + ConfigManager.ApiKey); httpClient.DefaultRequestHeaders.Add("User-Agent", "AutoTrackR2"); httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); - await httpClient.PostAsync(ConfigManager.ApiUrl + "register-kill", new StringContent(jsonData, Encoding.UTF8, "application/json")); + var response = await httpClient.PostAsync(ConfigManager.ApiUrl + "register-kill", new StringContent(jsonData, Encoding.UTF8, "application/json")); + if (response.StatusCode != HttpStatusCode.OK) + { + Console.WriteLine("Failed to submit kill data: "); + Console.WriteLine(jsonData); + } + else if (response.StatusCode == HttpStatusCode.OK) + { + Console.WriteLine("Successfully submitted kill data"); + } } } \ No newline at end of file