Skip to content

Commit

Permalink
Merge pull request #24 from TTRPG-Dev/qmalcolm--multiple-die-rolls
Browse files Browse the repository at this point in the history
Enable `ex_ttrpg_dev roll` to handle multiple dice specs
  • Loading branch information
QMalcolm authored Nov 22, 2024
2 parents 20bc154 + 87d7b34 commit 811c9b3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule ExTTRPGDev.CLI do
alias ExTTRPGDev.RuleSystems.Abilities
alias ExTTRPGDev.RuleSystems.Languages
alias ExTTRPGDev.RuleSystems.Skills
alias ExTTRPGDev.CustomParsers

@moduledoc """
The CLI for the project
Expand All @@ -30,7 +31,7 @@ defmodule ExTTRPGDev.CLI do
help:
"Dice in the format of xdy wherein x is the number of dice, y is the number of sides the dice should have",
required: true,
parser: :string
parser: &CustomParsers.dice_parser(&1)
]
]
],
Expand Down Expand Up @@ -144,9 +145,10 @@ defmodule ExTTRPGDev.CLI do
end
end

def handle_roll(%Optimus.ParseResult{args: %{dice: dice_str}}) do
Dice.roll(dice_str)
|> IO.inspect(label: "Results")
def handle_roll(%Optimus.ParseResult{args: %{dice: dice}}) do
dice
|> Enum.map(fn dice_spec -> {dice_spec, Dice.roll(dice_spec)} end)
|> Enum.each(fn {dice_spec, results} -> IO.inspect(results, label: dice_spec) end)
end

def handle_system_subcommands([command | subcommands], %Optimus.ParseResult{
Expand Down
20 changes: 20 additions & 0 deletions lib/custom_parsers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule ExTTRPGDev.CustomParsers do
@moduledoc """
Custom parsers to be used with Optimus args :parse
"""

@doc """
Parses a string of dice specifications seperated by commas
## Examples
iex> ExTTRPGDev.CustomParsers.dice_parser("3d4, 1d10,2d20")
{:ok, ["3d4", "1d10", "2d20"]}
"""
def dice_parser(arg) when is_bitstring(arg) do
arg
|> String.split(",")
|> Enum.map(&String.trim(&1))
|> Kernel.then(fn result -> {:ok, result} end)
end
end
5 changes: 5 additions & 0 deletions test/custom_parsers_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule ExTTRPGDevTest.CustomParsers do
use ExUnit.Case

doctest ExTTRPGDev.CustomParsers
end

0 comments on commit 811c9b3

Please sign in to comment.