<# ED-Limit-Sphere Modify the Cmdr's Log system_data.txt file, hiding all stations further away than a given radius from a reference system, and unhiding all stations within the radius. #> [CmdletBinding()] Param ( [Parameter(Position=0,Mandatory)] [ValidateNotNullOrEmpty()] [string]$CmdrPath, [Parameter(Position=1,Mandatory)] [ValidateNotNullOrEmpty()] [string]$CenterSystem, [Parameter(Position=2,Mandatory)] [int]$Radius ) function Limit-Sphere { Param ( $CenterSystem, $Radius ) $Center = $SystemsMap | ? Name -eq $CenterSystem if ($Center) { $Systems | % { $xd = $_.coord[0] - $center.coord[0] $yd = $_.coord[1] - $center.coord[1] $zd = $_.coord[2] - $center.coord[2] $Distance = [math]::Sqrt(($xd * $xd) + ($yd * $yd) + ($zd * $zd)) if ($Distance -le $Radius) { $_.Name } # 'Distance from {0} to {1} is {2:N2}' -f $center.Name,$_.Name,$Distance } } } function Get-Systems { Param ( [string]$CmdrPath ) $SystemsPath = Join-Path -Path $CmdrPath -ChildPath 'Systems.xml' $Pull = @{ ver = 2 test = 'false' outputmode = 2 filter = @{ knownstatus = 1 date = "2011-01-01 00:00:01" cr = 2 } } $Systems = @() if (Test-Path -Path $SystemsPath) { $Pull.filter.date = '{0:yyyy-MM-dd hh:mm:ss}' -f ((Get-Item $SystemsPath).LastWriteTime) $Systems = Import-Clixml -Path $SystemsPath } $x = Invoke-RestMethod -ContentType 'application/json; charset=utf-8' -Method Post ` -Uri 'http://edstarcoordinator.com/api.asmx/GetSystems' -Body (ConvertTo-Json @{ data = $Pull }) $NewSystems = $Null if ($x -is [string]) { [void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions") $jsonserial = New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer $jsonserial.MaxJsonLength = 64Mb $Obj = $jsonserial.DeserializeObject($x) if ($Obj) { $NewSystems = $Obj.d.systems.GetEnumerator() | % { $f = @{} $_.GetEnumerator() | % { $f[$_.Key] = $_.Value } [PSCustomObject]$f } } } else { $NewSystems = $x.d.systems } if ($NewSystems) { $NewSystemNames = $NewSystems.Name $Systems = ($Systems | ? Name -notin $NewSystemNames) + $NewSystems $Systems | Export-Clixml -Path $SystemsPath } $Systems } function Get-CmdrSystemData { Param ( [string]$CmdrPath ) $SystemData = Get-Content (Join-Path -Path $CmdrPath -ChildPath 'system_data.txt') | ? { $_ -ne ''} | % { $_.Trim() } $ProgramProperties = @{} $i = 2 while (($SystemData[$i] -replace '"') -match "(.*?) = (.*)") { $ProgramProperties[$Matches[1]] = $Matches[2] $i += 1 } $Systems = @{} for (; ($i -lt $SystemData.Count) -and ($SystemData[$i] -ne '}');$i += 1) { for (; $SystemData[$i] -ne '}';$i += 1) { $SystemName = $SystemData[$i] if (-not ($Systems.ContainsKey($SystemName))) { $Systems[$SystemName] = @{} } for ($i += 2; $SystemData[$i] -ne '}';$i += 1) { $StationName = $SystemData[$i] $i += 2 $StationProperties = @{} while (($SystemData[$i] -replace '"') -match "(.*?) = (.*)") { $StationProperties[$Matches[1]] = $Matches[2] $i += 1 } $Commodities = @{} $Notes = @() if ($SystemData[$i] -eq 'Commodities') { for ($i += 2; $SystemData[$i] -ne '}';$i += 1) { $CommodityName = $SystemData[$i] $i += 2 $CommodityProperties = @{} while (($SystemData[$i] -replace '"') -match "(.*?) = (.*)") { $CommodityProperties[$Matches[1]] = $Matches[2] $i += 1 } $Commodities[$CommodityName] = $CommodityProperties } } if ($SystemData[$i+1] -eq 'Notes') { for ($i += 3; $SystemData[$i] -ne '}';$i += 1) { $Notes += $SystemData[$i] -replace '"' } $i += 1 } $Systems[$SystemName][$StationName] = @{ Commodities = $Commodities Notes = $Notes Properties = $StationProperties } } } } $ProgramProperties,($Systems) } function Out-CmdrSystemData { Param ( [hashtable]$ProgramProperties, $Systems ) @([string]::Empty,'Save Data','{') $ProgramProperties.Keys | % { ' {0} = {1}' -f $_,$ProgramProperties[$_] } $Systems.Keys | Sort-Object | % { $SystemName = $_ [string]::Empty ' {0}' -f $_ ' {' $Systems[$_].Keys | Sort-Object | % { $StationName = $_ $OneStation = $Systems[$SystemName][$StationName] [string]::Empty ' {0}' -f $StationName ' {' $OneStation.Properties.Keys | % { ' {0} = "{1}"' -f $_,$OneStation.Properties[$_] } [string]::Empty ' Commodities' ' {' $OneStation.Commodities.Keys | % { $CommodityName = $_ $OneCommodity = $OneStation.Commodities[$CommodityName] [string]::Empty ' {0}' -f $CommodityName ' {' $OneCommodity.Keys | % { $Value = $OneCommodity[$_] ' {0} = {1}' -f $_,(. { switch ($_) { 'status' { $Value } default {'"{0}"' -f $Value }}}) } ' }' } ' }' [string]::Empty ' Notes' ' {' $OneStation.Notes | % { ' "{0}"' -f $_ } ' }' ' }' } ' }' } "}`n" } #Requires -Version 3.0 Set-StrictMode -Version Latest $SystemDataPath = Join-Path -Path $CmdrPath -ChildPath 'system_data.txt' if (-not (Test-Path -Path $SystemDataPath)) { Write-Warning "Cannot find $($SystemDataPath)...exiting." return } Set-Variable -Name SystemsMap -Value (Get-Systems $CmdrPath) -Scope Script $ProgramProperties,$Systems = Get-CmdrSystemData $CmdrPath $LimitSystems = Limit-Sphere -CenterSystem $CenterSystem -Radius $Radius if ($LimitSystems) { $Systems.Keys | % { $SystemName = $_ $HideStatus = if ($SystemName -in $LimitSystems) {'0'} else {'1'} $Systems[$SystemName].Keys | % { $Systems[$SystemName][$_].Properties['hidden'] = $HideStatus } } $Output = Out-CmdrSystemData $ProgramProperties $Systems Copy-Item -Path $SystemDataPath -Destination (Join-Path -Path $CmdrPath -ChildPath ('system_data_{0}.txt' -f ([guid]::NewGuid() -replace '-'))) Set-Content -Path $SystemDataPath -Value $Output } else { Write-Warning "No systems found within $Radius Light Years of $CenterSystem" }