Skip to content

Instantly share code, notes, and snippets.

@FriedrichWeinmann
Created January 3, 2024 21:51
Show Gist options
  • Save FriedrichWeinmann/8501c826eaa17f921aa03d20d30ef4aa to your computer and use it in GitHub Desktop.
Save FriedrichWeinmann/8501c826eaa17f921aa03d20d30ef4aa to your computer and use it in GitHub Desktop.
Integrate dice rolling into your commandline
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