From e52166ccbf8311f2658f437ea1a55971c68ab8b6 Mon Sep 17 00:00:00 2001
From: BubbaGumpShrump <joshua.p.farrell@gmail.com>
Date: Sun, 8 Dec 2024 20:22:01 -0500
Subject: [PATCH] Fixed issue where powershell script may stay running even
 after closing app by making it stop itself if gui app is no longer running.

---
 AutoTrackR2/ConfigPage.xaml.cs        |  2 +-
 AutoTrackR2/KillTrackR_MainScript.ps1 | 12 +++++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/AutoTrackR2/ConfigPage.xaml.cs b/AutoTrackR2/ConfigPage.xaml.cs
index 49e3a67..5ff2f69 100644
--- a/AutoTrackR2/ConfigPage.xaml.cs
+++ b/AutoTrackR2/ConfigPage.xaml.cs
@@ -259,7 +259,7 @@ namespace AutoTrackR2
                         (Color)ColorConverter.ConvertFromString("#FFFFFF"), // Text
                         (Color)ColorConverter.ConvertFromString("#CFFF04")  // AltText
                     );
-                    ChangeLogo("/Assets/AVSQN.png", (Color)ColorConverter.ConvertFromString("#3fbcff"));
+                    ChangeLogo("/Assets/HEX.png", (Color)ColorConverter.ConvertFromString("#39FF14"));
                     break;
             }
         }
diff --git a/AutoTrackR2/KillTrackR_MainScript.ps1 b/AutoTrackR2/KillTrackR_MainScript.ps1
index e7a7422..f69c74a 100644
--- a/AutoTrackR2/KillTrackR_MainScript.ps1
+++ b/AutoTrackR2/KillTrackR_MainScript.ps1
@@ -20,6 +20,8 @@ if (Test-Path $configFile) {
     exit
 }
 
+$parentApp = (Get-Process -Name AutoTrackR2).ID
+
 # Access config values
 $logFilePath = $config.Logfile
 $apiUrl = $config.ApiUrl
@@ -102,7 +104,7 @@ $global:killTally = 0
 
 # Load historic kills from csv
 if (Test-Path "$scriptFolder\Kill-Log.csv") {
-	$historicKills = Import-CSV "$scriptFolder\Kill-log.csv" | Sort-Object Descending
+	$historicKills = Import-CSV "$scriptFolder\Kill-log.csv" | Sort-Object -Property KillTime
 	foreach ($kill in $historicKills) {
 		Write-Output "NewKill=throwaway,$($kill.EnemyPilot),$($kill.EnemyShip),$($kill.OrgAffiliation),$($kill.Enlisted),$($kill.RecordNumber),$($kill.KillTime),$($kill.PFP)"
 		$global:killTally++
@@ -186,7 +188,11 @@ function Read-LogEntry {
 		}
 
 		If ($null -ne $page1){
-	
+			# Check if the Autotrackr2 process is running
+			if ($null -eq (Get-Process -ID $parentApp -ErrorAction SilentlyContinue)) {
+				Stop-Process -Id $PID -Force
+			}
+			
 			If ($enemyShip -eq $global:lastKill){
 				$enemyShip = "Passenger"
 			} Else {
@@ -299,7 +305,7 @@ function Read-LogEntry {
 
 					try {
 						# Send the POST request with JSON data
-						Invoke-RestMethod -Uri $apiURL -Method Post -Body ($data | ConvertTo-Json -Depth 5) -Headers $headers
+						$null = Invoke-RestMethod -Uri $apiURL -Method Post -Body ($data | ConvertTo-Json -Depth 5) -Headers $headers
 						$logMode = "API"
 					} catch {
 						# Catch and display errors