Created
January 3, 2024 21:51
-
-
Save FriedrichWeinmann/8501c826eaa17f921aa03d20d30ef4aa to your computer and use it in GitHub Desktop.
Integrate dice rolling into your commandline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-Dice { | |
[Alias('roll')] | |
[CmdletBinding()] | |
param ( | |
[int] | |
$Size = 20, | |
[int] | |
$Count = 1 | |
) | |
if ($global:__DiceRoll) { | |
if ($PSBoundParameters.Keys -notcontains 'Size') { $Size = $global:__DiceRoll.Size } | |
if ($PSBoundParameters.Keys -notcontains 'Count') { $Count = $global:__DiceRoll.Count } | |
} | |
$numbers = foreach ($n in 1..$Count) { | |
1..$Size | Get-Random | |
} | |
$sum = ($numbers | Measure-Object -Sum).Sum | |
'{0}: {1}' -f $sum, ($numbers -join ' + ') | |
} | |
$ExecutionContext.InvokeCommand.CommandNotFoundAction = { | |
Param ($Name, $EventArgs) | |
if ($Name.Trim('.\') -notmatch '^(?<count>\d+)D(?<size>\d+)$') { return } | |
$global:__DiceRoll = @{ | |
Size = $matches.Size | |
Count = $matches.Count | |
} | |
$EventArgs.Command = Get-Command Invoke-Dice | |
$EventArgs.StopSearch = $true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment