Skip to content

Commit d111015

Browse files
some more docs for TrainingWatcher
1 parent fc8c031 commit d111015

File tree

1 file changed

+105
-18
lines changed

1 file changed

+105
-18
lines changed

chmengine/stream/scripts/TrainingWatcher.ps1

Lines changed: 105 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,19 @@ param(
8181
[Parameter(Mandatory = $false)]
8282
[switch]$Infinite = (& { if ($MaxGames -lt 1) { $true } else { $false } })
8383
)
84+
8485
# Set default values for script variables
85-
[bool]$script:_init_ = $true
86-
[double]$script:lastSize = 0.0
87-
[string]$script:lastLine = ''
86+
[bool]$script:_init_ = $true # Used in Write-QTableSize to determine if this is the first call of the function during the scripts runtime.
87+
[double]$script:lastSize = 0.0 # Used in Write-QTableSize to determine if the Q-table size has changed.
88+
[string]$script:lastLine = '' # Used in Watch-TrainingGames to store the last line of the game.
89+
90+
# Redundant $script: variable assignments to make the script more readable.
8891
[string]$script:TrainingDirectory = $TrainingDirectory
8992
[string]$script:QTableDirectory = $QTableDirectory
90-
[int]$script:MaxGames = $MaxGames
9193
[int]$script:PollIntervalSeconds = $PollIntervalSeconds
9294
[hashtable]$script:InitialElo = $InitialElo
9395
[double]$script:PoolAvg = $PoolAvg
96+
[int]$script:MaxGames = $MaxGames
9497
[bool]$script:Infinite = $Infinite
9598

9699
function Get-PredictedEloPerSide {
@@ -106,22 +109,25 @@ function Get-PredictedEloPerSide {
106109
$stats = @{
107110
'Phillyclause89' = @{ White = @{ Wins = 2554; Losses = 2439; Draws = 144 }; Black = @{ Wins = 2427; Losses = 2575; Draws = 143 } }
108111
}
109-
.PARAMETER PoolAvg
110-
Fallback "field average" Elo if no InitialElo is provided for a player/side.
111-
Default is `$script:PoolAvg`.
112112
.PARAMETER InitialElo
113113
OPTIONAL hashtable of player → @{ White=[int]; Black=[int] }.
114114
Default is `$script:InitialElo`.
115115
Missing entries fall back to using `-PoolAvg`.
116+
.PARAMETER PoolAvg
117+
Fallback "field average" Elo if no InitialElo is provided for a player/side.
118+
Default is `$script:PoolAvg`.
116119
.OUTPUTS
117120
Hashtable of player → @{ White=[double]; Black=[double] }.
118121
#>
119122
[CmdletBinding()]
120123
param(
121-
[Parameter(Mandatory)]
124+
[Parameter(Mandatory, Position = 0, ValueFromPipeline = $true)]
122125
[hashtable]$Stats,
123-
[int]$PoolAvg = $script:PoolAvg,
124-
[hashtable]$InitialElo = $script:InitialElo
126+
[Parameter(Mandatory = $false, Position = 1)]
127+
[hashtable]$InitialElo = $script:InitialElo,
128+
[Parameter(Mandatory = $false, Position = 0)]
129+
[double]$PoolAvg = $script:PoolAvg
130+
125131
)
126132

127133
$elos = @{}
@@ -188,9 +194,24 @@ function Get-PredictedEloPerSide {
188194
}
189195

190196
function Get-QTableColor {
197+
<#
198+
.SYNOPSIS
199+
Get the color for the Q-table size based on the remaining free space on the drive.
200+
.DESCRIPTION
201+
This function calculates the color to be used for displaying the Q-table size based on the remaining free space on the drive.
202+
It uses a gradient from green (low usage) to red (high usage).
203+
.PARAMETER sizeMB
204+
The size of the Q-table in MB. Default is the size of the Q-table in the specified directory.
205+
.PARAMETER QTableDirectory
206+
The directory where the Q-table is located. Default is `$script:QTableDirectory`.
207+
.OUTPUTS
208+
A string representing the color to be used for displaying the Q-table size.
209+
#>
191210
[CmdletBinding()]
192211
param (
212+
[Parameter(Mandatory = $false, Position = 0)]
193213
[double]$sizeMB = (Get-QTableSize -QTableDirectory $script:QTableDirectory),
214+
[Parameter(Mandatory = $false, Position = 1)]
194215
[string]$QTableDirectory = $script:QTableDirectory
195216
)
196217

@@ -230,26 +251,73 @@ function Get-QTableColor {
230251
}
231252

232253
function Get-QTableSize {
254+
<#
255+
.SYNOPSIS
256+
Get the size of the Q-table in MB.
257+
.DESCRIPTION
258+
This function calculates the size of the Q-table in MB by summing the sizes of all files in the specified directory.
259+
.PARAMETER QTableDirectory
260+
The directory where the Q-table is located. Default is `$script:QTableDirectory`.
261+
.PARAMETER SizeSpec
262+
The size specification for the calculation. Default is 1MB.
263+
.PARAMETER SizeDecimal
264+
The number of decimal places to round the size to. Default is 3.
265+
.OUTPUTS
266+
A double representing the size of the Q-table in MB.
267+
#>
233268
[CmdletBinding()]
234269
param (
270+
[Parameter(Mandatory = $false, Position = 0)]
235271
[string]$QTableDirectory = $script:QTableDirectory,
272+
[Parameter(Mandatory = $false, Position = 1)]
236273
[int]$SizeSpec = 1MB,
274+
[Parameter(Mandatory = $false, Position = 2)]
237275
[int]$SizeDecimal = 3
238276
)
239277
$fileSize = (Get-ChildItem -Path $QTableDirectory -File | Measure-Object -Property Length -Sum).Sum
240278
return [math]::Round($fileSize / $SizeSpec, $SizeDecimal)
241279
}
242280

243281
function Watch-TrainingGames {
282+
<#
283+
.SYNOPSIS
284+
Watch the training games and print statistics to the host.
285+
.DESCRIPTION
286+
This function monitors the training directory for new games and prints statistics about the players and their performance.
287+
It also monitors the Q-table size and prints it to the host.
288+
.PARAMETER TrainingDirectory
289+
The directory where the training games are located. Default is `$script:TrainingDirectory`.
290+
.PARAMETER QTableDirectory
291+
The directory where the Q-table is located. Default is `$script:QTableDirectory`.
292+
.PARAMETER PollIntervalSeconds
293+
The interval in seconds to poll the directories. Default is `$script:PollIntervalSeconds`.
294+
.PARAMETER InitialElo
295+
A hashtable of player names to their initial Elo ratings. Default is `$script:InitialElo`.
296+
.PARAMETER PoolAvg
297+
The expected average Elo of the pool of players. Default is `$script:PoolAvg`.
298+
.PARAMETER MaxGames
299+
The maximum number of games to monitor. Default is `$script:MaxGames`.
300+
.PARAMETER Infinite
301+
If this switch is set, the script will run indefinitely. Default is `$script:Infinite`.
302+
.OUTPUTS
303+
None - Prints statistics to the host.
304+
#>
244305
[CmdletBinding()]
245306
param (
307+
[Parameter(Mandatory = $false, Position = 0)]
246308
[string]$TrainingDirectory = $script:TrainingDirectory,
247-
[string]$QTableDirectory = $script:QTableDirectory,
248-
[int]$MaxGames = $script:MaxGames,
249-
[bool]$Infinite = $script:Infinite,
309+
[Parameter(Mandatory = $false, Position = 1)]
310+
[string]$QTableDirectory = $script:QTableDirectory,
311+
[Parameter(Mandatory = $false, Position = 2)]
250312
[int]$PollIntervalSeconds = $script:PollIntervalSeconds,
251-
[int]$PoolAvg = $script:PoolAvg,
252-
[hashtable]$InitialElo = $script:InitialElo
313+
[Parameter(Mandatory = $false, Position = 3)]
314+
[hashtable]$InitialElo = $script:InitialElo,
315+
[Parameter(Mandatory = $false, Position = 4)]
316+
[int]$PoolAvg = $script:PoolAvg,
317+
[Parameter(Mandatory = $false, Position = 5)]
318+
[int]$MaxGames = $script:MaxGames,
319+
[Parameter(Mandatory = $false, Position = 6)]
320+
[bool]$Infinite = $script:Infinite
253321
)
254322
$script:_init_ = $true
255323
$script:lastSize = Get-QTableSize -QTableDirectory $QTableDirectory
@@ -372,11 +440,32 @@ function Watch-TrainingGames {
372440
}
373441

374442
function Write-QTableSize {
443+
<#
444+
.SYNOPSIS
445+
Write the Q-table size to the host.
446+
.DESCRIPTION
447+
This function writes the Q-table size to the host, including the growth rate.
448+
It uses a color gradient to indicate the size of the Q-table.
449+
.PARAMETER sizeMB
450+
The size of the Q-table in MB. Default is the size of the Q-table in the specified directory.
451+
.PARAMETER lastSize
452+
The last size of the Q-table in MB. Default is `$script:lastSize`.
453+
.PARAMETER QTableDirectory
454+
The directory where the Q-table is located. Default is `$script:QTableDirectory`.
455+
.PARAMETER PollIntervalSeconds
456+
The interval in seconds to poll the directories. Default is `$script:PollIntervalSeconds`.
457+
.OUTPUTS
458+
A double representing the last size of the Q-table in MB.
459+
#>
375460
[CmdletBinding()]
376461
param (
462+
[Parameter(Mandatory = $false, Position = 0)]
377463
[double]$sizeMB = (Get-QTableSize -QTableDirectory $script:QTableDirectory),
464+
[Parameter(Mandatory = $false, Position = 1)]
378465
[double]$lastSize = $script:lastSize,
466+
[Parameter(Mandatory = $false, Position = 2)]
379467
[string]$QTableDirectory = $script:QTableDirectory,
468+
[Parameter(Mandatory = $false, Position = 3)]
380469
[int]$PollIntervalSeconds = $script:PollIntervalSeconds
381470
)
382471
if (($sizeMB -ne $lastSize) -or ($script:_init_)) {
@@ -404,6 +493,4 @@ if ($MyInvocation.InvocationName -ne '.') {
404493
}
405494
Watch-TrainingGames @WTGArgs
406495
}
407-
# P.s. I think PowerShell is a terrible scripting language and the guy who created it is certainly not Dutch.
408-
409-
496+
# P.s. I think PowerShell is a terrible scripting language and the guy who created it is certainly not Dutch.

0 commit comments

Comments
 (0)