From 400f3607dd7ba8b11f0369fce1826ec228a70633 Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Thu, 21 Nov 2024 23:52:30 -0600 Subject: [PATCH 1/2] Create custom parser `dice_parser` to parse a multiple dice specs --- lib/custom_parsers.ex | 20 ++++++++++++++++++++ test/custom_parsers_test.exs | 5 +++++ 2 files changed, 25 insertions(+) create mode 100644 lib/custom_parsers.ex create mode 100644 test/custom_parsers_test.exs diff --git a/lib/custom_parsers.ex b/lib/custom_parsers.ex new file mode 100644 index 0000000..08b5e0b --- /dev/null +++ b/lib/custom_parsers.ex @@ -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 diff --git a/test/custom_parsers_test.exs b/test/custom_parsers_test.exs new file mode 100644 index 0000000..d16849f --- /dev/null +++ b/test/custom_parsers_test.exs @@ -0,0 +1,5 @@ +defmodule ExTTRPGDevTest.CustomParsers do + use ExUnit.Case + + doctest ExTTRPGDev.CustomParsers +end From 87d7b3492966457766293a6701d847c256916f21 Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Fri, 22 Nov 2024 00:34:48 -0600 Subject: [PATCH 2/2] Update `ex_ttrpg_dev roll` to handle multiple dice specs Previously, only one dice spec could be given, i.e. `ex_ttrpg_dev roll 3d4`. However, now multiple dice specs can be given in a single invocation. That is, one can now do something like ``` $ ./ex_ttrpg_dev roll 10d10,10d12,10d20 10d10: [5, 3, 8, 4, 10, 3, 9, 2, 7, 4] 10d12: [9, 3, 3, 4, 11, 12, 12, 3, 6, 12] 10d20: [7, 3, 7, 4, 6, 1, 16, 3, 16, 6] ``` --- lib/cli.ex | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/cli.ex b/lib/cli.ex index f575006..790a4fb 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -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 @@ -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) ] ] ], @@ -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{