Update KillTrackR_MainScript.ps1

-Enhance logic to correctly handle time-based expiration for last kill tracking for identifiying a Passenger

-PvE event detection and handling reimplemented
This commit is contained in:
Koda-Dog 2025-01-31 20:45:13 +01:00
parent 32935de85c
commit 799cbc21c9

View file

@ -7,7 +7,7 @@ $script:TrackRver = "2.06-koda-mod-opt"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# ================================= Configuration ================================= # ================================= Configuration =================================
$script:DebugLvl=0 $script:DebugLvl=0 # Can be set in config.ini with Debug=1
$script:AppName = "AutoTrackR2" $script:AppName = "AutoTrackR2"
$script:ScriptFolder = Join-Path -Path $env:LOCALAPPDATA -ChildPath $AppName $script:ScriptFolder = Join-Path -Path $env:LOCALAPPDATA -ChildPath $AppName
@ -26,18 +26,23 @@ $script:VideoRecordFileName = "videorecord.ahk"
$script:VideoRecordFile = "$script:ScriptFolder\$script:VideoRecordFileName" $script:VideoRecordFile = "$script:ScriptFolder\$script:VideoRecordFileName"
# Definition of script variables # Definition of script variables
$script:DateFormat = "dd MMM yyyy HH:mm UTC"
$script:KillTally = 0 $script:KillTally = 0
$script:DeathTally = 0 $script:DeathTally = 0
$script:OtherTally = 0 $script:OtherTally = 0
$script:FpsLoadout = "Person"
$script:LastKill = $null $script:LastKill = $null
$script:LastKillUpdated = Get-Date
$script:PlayerCache = @{} $script:PlayerCache = @{}
$script:UserName = $null $script:UserName = $null
$script:Loadout = $script:FpsLoadout $script:Loadout = $script:FpsLoadout
$script:GameMode = $null $script:GameMode = $null
$script:GameVersion = $null $script:GameVersion = $null
# Configurable script variables
$script:DateFormat = "dd MMM yyyy HH:mm UTC"
$script:PassengerTimeOut = 60 # How long could be a Kill in the same ship a Passenger [sec]
$script:FpsLoadout = "Person" # Shipnames for FPS
# Ship Manufacturers # Ship Manufacturers
$prefixes = @( $prefixes = @(
"ORIG", "ORIG",
@ -77,6 +82,10 @@ $script:VehiclePattern = "<(?<timestamp>[^>]+)> \[Notice\] <Vehicle Destruction>
"advanced from destroy level (?<destroy_level_from>\d+) to (?<destroy_level_to>\d+) " + "advanced from destroy level (?<destroy_level_from>\d+) to (?<destroy_level_to>\d+) " +
"caused by '(?<caused_by>[^']+)' \[\d+\] with '(?<damage_type>[^']+)'" "caused by '(?<caused_by>[^']+)' \[\d+\] with '(?<damage_type>[^']+)'"
# Lookup Patterns
$script:joinDatePattern = '<span class="label">Enlisted</span>\s*<strong class="value">([^<]+)</strong>'
$script:ueePattern = '<p class="entry citizen-record">\s*<span class="label">UEE Citizen Record<\/span>\s*<strong class="value">#?(n\/a|\d+)<\/strong>\s*<\/p>'
# ================================= Functions ================================= # ================================= Functions =================================
function Get-ConfigurationSettings { function Get-ConfigurationSettings {
@ -309,8 +318,12 @@ function Read-LogEntry {
Write-OutputData "LogInfo=KillPattern detected" Write-OutputData "LogInfo=KillPattern detected"
$eventData = New-KillEvent -data $matches -location $location -vehicle_id $vehicle_id $eventData = New-KillEvent -data $matches -location $location -vehicle_id $vehicle_id
$type = New-EventType -eventData $eventData -userName $script:UserName -killLog $config.KillLog -deathLog $config.DeathLog -otherLog $config.OtherLog $type = New-EventType -eventData $eventData -userName $script:UserName -killLog $config.KillLog -deathLog $config.DeathLog -otherLog $config.OtherLog
if ($type -ne "none") { if(Test-EventForPVE -eventData $eventData -type $type -and -ne "none"){
if($type -eq "Death"){$type = "Other"}else{$type = "none"}
}
if ($type -ne "none") {
if ($type -ne "Other") { if ($type -ne "Other") {
$playerInfo = Get-PlayerInfo $(if ($type -eq "Kill") { $eventData.VictimPilot } else { $eventData.AgressorPilot }) $playerInfo = Get-PlayerInfo $(if ($type -eq "Kill") { $eventData.VictimPilot } else { $eventData.AgressorPilot })
} }
@ -322,6 +335,7 @@ function Read-LogEntry {
if ($type -eq "Kill" -and $null -ne $config.ApiUrl -and -not $config.OfflineMode) { if ($type -eq "Kill" -and $null -ne $config.ApiUrl -and -not $config.OfflineMode) {
Write-OutputData "LogInfo=Send $type data to server" Write-OutputData "LogInfo=Send $type data to server"
Send-ApiData -csvData $csvData -location $location -apiUrl $config.ApiUrl -apiKey $config.ApiKey Send-ApiData -csvData $csvData -location $location -apiUrl $config.ApiUrl -apiKey $config.ApiKey
$csvData.Logged = "API"
} }
Write-CSVData -csvData $csvData -csvFile $script:CSVFile Write-CSVData -csvData $csvData -csvFile $script:CSVFile
@ -356,7 +370,7 @@ function New-CsvData {
Mode = $script:GameMode Mode = $script:GameMode
GameVersion = $script:GameVersion GameVersion = $script:GameVersion
TrackRver = $script:TrackRver TrackRver = $script:TrackRver
Logged = $eventData.LogMode Logged = "NONE"
PFP = $playerInfo.PFP PFP = $playerInfo.PFP
} }
@ -377,9 +391,9 @@ function New-KillEvent {
[string] $vehicle_id [string] $vehicle_id
) )
$victimShip = $data['VictimShip'] $victimShip = $data.VictimShip
$weapon = $data['Weapon'] $weapon = $data.Weapon
$damageType = $data['DamageType'] $damageType = $data.DamageType
$agressorShip = $null $agressorShip = $null
# Clean Weapon pattern # Clean Weapon pattern
@ -398,26 +412,54 @@ function New-KillEvent {
} }
# Do we have a Passenger? # Do we have a Passenger?
If ($victimShip -ne $script:FpsLoadout){ $delay = $(Get-Date) - $script:LastKillUpdated
If ($victimShip -eq $script:LastKill){ If ($victimShip -ne $script:FpsLoadout) {
# If last Kill is the same Ship and time it lowerthen PassengerTimeOut
If ($victimShip -eq $script:LastKill -and $delay.TotalSeconds -lt $script:PassengerTimeOut){
$victimShip = "Passenger" $victimShip = "Passenger"
} Else { } Else {
$script:LastKill = $victimShip $script:LastKill = $victimShip
$script:LastKillUpdated = Get-Date
} }
} }
return @{ return @{
VictimPilot = $data['VictimPilot'] VictimPilot = $data.VictimPilot
VictimShip = $victimShip VictimShip = $victimShip
AgressorPilot = $data['AgressorPilot'] AgressorPilot = $data.AgressorPilot
AgressorShip = $agressorShip AgressorShip = $agressorShip
Weapon = $weapon Weapon = $weapon
DamageType = $damageType DamageType = $damageType
#Location = if ($data['VictimShip'] -ne "vehicle_id") { $location } else { "NONE" } #I think this was an error #Location = if ($data.VictimShip -ne "vehicle_id") { $location } else { "NONE" } #I think this was an error
Location = if ($data['VictimShip'] -ne $vehicle_id) { $location } else { "NONE" } Location = if ($data.VictimShip -ne $vehicle_id) { $location } else { "NONE" }
} }
} }
function Test-EventForPVE {
param (
[hashtable]$eventData,
[string]$type
)
if ($type -eq "Kill") {
$proofPlayer = $eventData.VictimPilot
}elseif ($type -eq "Death"){
$proofPlayer = $eventData.AgressorPilot
}
# Proof if event is PvE
if ($proofPlayer) {
try {
$null = Invoke-WebRequest -Uri "https://robertsspaceindustries.com/citizens/$proofPlayer" -ErrorAction Stop
Write-OutputData "LogInfo=PVP Event detected"
return $false
} catch {
Write-OutputData "LogInfo=PVE Event detected"
return $true
}
}
}
function New-EventType { function New-EventType {
param ( param (
[hashtable]$eventData, [hashtable]$eventData,
@ -494,9 +536,9 @@ function Get-PlayerInfo {
try { try {
$page = Invoke-WebRequest -Uri "https://robertsspaceindustries.com/citizens/$playerName" $page = Invoke-WebRequest -Uri "https://robertsspaceindustries.com/citizens/$playerName"
$joinDate = if ($page.Content -match $joinDatePattern) { $matches[1] -replace ',', '' } else { "-" } $joinDate = if ($page.Content -match $script:joinDatePattern) { $matches[1] -replace ',', '' } else { "-" }
$enemyOrgs = if ($null -eq $page.Links[0].innerHTML) { $page.Links[4].innerHTML } else { $page.Links[3].innerHTML } $enemyOrgs = if ($null -eq $page.Links[0].innerHTML) { $page.Links[4].innerHTML } else { $page.Links[3].innerHTML }
$citizenRecord = if ($page.Content -match $ueePattern) { $matches[1] } else { "-" } $citizenRecord = if ($page.Content -match $script:ueePattern) { $matches[1] } else { "-" }
$enemyPFP = if ($page.Images[0].src -like "/media/*") { "https://robertsspaceindustries.com$($page.Images[0].src)" } else { "https://cdn.robertsspaceindustries.com/static/images/account/avatar_default_big.jpg" } $enemyPFP = if ($page.Images[0].src -like "/media/*") { "https://robertsspaceindustries.com$($page.Images[0].src)" } else { "https://cdn.robertsspaceindustries.com/static/images/account/avatar_default_big.jpg" }
$playerInfo = @{ $playerInfo = @{
@ -592,11 +634,12 @@ function Write-CSVData {
[string]$csvFile [string]$csvFile
) )
foreach ($property in $csvData.PSObject.Properties) { # JUMP redundant
if ($property.Value -is [string]) { #foreach ($property in $csvData.PSObject.Properties) {
$property.Value = $property.Value -replace ',', '' # if ($property.Value -is [string]) {
} # $property.Value = $property.Value -replace ',', ''
} # }
#}
if (-Not (Test-Path $csvFile)) { if (-Not (Test-Path $csvFile)) {
$csvData | Export-Csv -Path $csvFile -NoTypeInformation $csvData | Export-Csv -Path $csvFile -NoTypeInformation