Skip to content

Commit

Permalink
Merge pull request #21 from TTRPG-Dev/qmalcolm--add-cli-show-function…
Browse files Browse the repository at this point in the history
…ality

Add CLI `show` command and subcommands
  • Loading branch information
QMalcolm authored Oct 27, 2022
2 parents bdb1e0c + 780f63c commit f831e6f
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 18 deletions.
119 changes: 102 additions & 17 deletions lib/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
defmodule ExTTRPGDev.CLI do
alias ExTTRPGDev.Dice
alias ExTTRPGDev.RuleSystems
alias ExTTRPGDev.RuleSystems.Abilities
alias ExTTRPGDev.RuleSystems.Languages
alias ExTTRPGDev.RuleSystems.Skills

@moduledoc """
The CLI for the project
Expand All @@ -12,7 +15,7 @@ defmodule ExTTRPGDev.CLI do
Optimus.new!(
name: "ex_ttrpg_dev",
description: "CLI for all things RPG",
version: "0.2.0",
version: "0.3.0",
author: "Quigley Malcolm [email protected]",
about: "Utility for playing tabletop role-playing games.",
allow_unknown_args: false,
Expand All @@ -39,18 +42,6 @@ defmodule ExTTRPGDev.CLI do
name: "system",
about: "Top level command fo systems",
subcommands: [
metadata: [
name: "metadata",
about: "Show system metadata",
args: [
system: [
value_name: "SYSTEM",
help: "A supported system, e.g. dnd5e",
required: true,
parser: :string
]
]
],
gen: [
name: "gen",
about: "Used for generating things for the system",
Expand All @@ -68,6 +59,60 @@ defmodule ExTTRPGDev.CLI do
]
]
]
],
show: [
name: "show",
about: "Used for showing information about the rule system",
subcommands: [
abilities: [
name: "abilities",
about: "Show the rule systems character abilities",
args: [
system: [
value_name: "SYSTEM",
help: "A supported system, e.g. dnd5e",
required: true,
parser: :string
]
]
],
languages: [
name: "languages",
about: "Show the rule systems languages",
args: [
system: [
value_name: "SYSTEM",
help: "A supported system, e.g. dnd5e",
required: true,
parser: :string
]
]
],
metadata: [
name: "metadata",
about: "Show system metadata",
args: [
system: [
value_name: "SYSTEM",
help: "A supported system, e.g. dnd5e",
required: true,
parser: :string
]
]
],
skills: [
name: "skills",
about: "Show rule system skills",
args: [
system: [
value_name: "SYSTEM",
help: "A supported system, e.g. dnd5e",
required: true,
parser: :string
]
]
]
]
]
]
]
Expand Down Expand Up @@ -113,12 +158,11 @@ defmodule ExTTRPGDev.CLI do
|> RuleSystems.load_system!()

case command do
:metadata ->
Map.get(loaded_system, :metadata)
|> IO.inspect()

:gen ->
handle_system_generation_subcommands(subcommands, loaded_system)

:show ->
handle_system_show_subcommands(subcommands, loaded_system)
end
end

Expand All @@ -132,4 +176,45 @@ defmodule ExTTRPGDev.CLI do
|> IO.inspect()
end
end

def handle_system_show_subcommands(
[command | _subcommands],
%RuleSystems.RuleSystem{} = system
) do
case command do
:abilities ->
show_abilities(system)

:languages ->
show_languages(system)

:metadata ->
Map.get(system, :metadata)
|> IO.inspect()

:skills ->
show_skills(system)
end
end

def show_abilities(%RuleSystems.RuleSystem{abilities: %Abilities{specs: specs}}) do
Enum.each(specs, fn %Abilities.Spec{name: name, abbreviation: abbr} ->
IO.puts("(#{abbr}) #{name}")
end)
end

def show_languages(%RuleSystems.RuleSystem{languages: languages}) do
Enum.each(languages, fn %Languages.Language{name: name, script: script} ->
IO.puts("Name: #{name}, Script: #{script}")
end)
end

def show_skills(%RuleSystems.RuleSystem{skills: skills} = system) do
Enum.each(skills, fn %Skills.Skill{name: name, modifying_stat: mod_stat} ->
%Abilities.Spec{abbreviation: abbr} =
RuleSystems.RuleSystem.get_spec_by_name(system, mod_stat)

IO.puts("(#{abbr}) #{name}")
end)
end
end
12 changes: 12 additions & 0 deletions lib/rule_systems/abilities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,16 @@ defmodule ExTTRPGDev.RuleSystems.Abilities do
[Assignment.roll_via_method!(default_rolling_method) | acc]
end)
end

@doc """
Returns the spec for the given spec name
## Examples
iex> ExTTRPGDev.RuleSystems.Abilities.get_spec_by_name(abilities, spec_name)
%Spec{}
"""
def get_spec_by_name(%Abilities{specs: specs}, spec_name) when is_bitstring(spec_name) do
Spec.get_spec_by_name(specs, spec_name)
end
end
14 changes: 14 additions & 0 deletions lib/rule_systems/abilities/spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@ defmodule ExTTRPGDev.RuleSystems.Abilities.Spec do
def get_names([%Spec{} | _tail] = specs) do
Enum.reduce(specs, [], fn spec, acc -> [spec.name | acc] end)
end

@doc """
Returns the spec for the given spec name
## Examples
iex> ExTTRPGDev.RuleSystems.Abilities.Spec.get_spec_by_name(list_of_specs, spec_name)
%Spec{}
"""
def get_spec_by_name([%Spec{} | _tail] = specs, spec_name) when is_bitstring(spec_name) do
Enum.find(specs, fn %Spec{name: name} ->
name == spec_name
end)
end
end
13 changes: 13 additions & 0 deletions lib/rule_systems/rule_system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,17 @@ defmodule ExTTRPGDev.RuleSystems.RuleSystem do
def gen_ability_scores_unassigned(%RuleSystem{abilities: %Abilities{} = abilities}) do
Abilities.gen_scores_unassigned(abilities)
end

@doc """
Returns the spec for the given spec name
## Examples
iex> ExTTRPGDev.RuleSystems.get_spec_by_name(rule_system, spec_name)
%Spec{}
"""
def get_spec_by_name(%RuleSystem{abilities: abilities}, spec_name)
when is_bitstring(spec_name) do
Abilities.get_spec_by_name(abilities, spec_name)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ExTTRPGDev.MixProject do
def project do
[
app: :ex_ttrpg_dev,
version: "0.2.1",
version: "0.3.0",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
9 changes: 9 additions & 0 deletions test/abilities/spec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ defmodule ExTTRPGDevTest.RuleSystems.Abilities.Spec do
"charisma"
])
end

test "get spec from specs by name" do
%RuleSystems.RuleSystem{abilities: %RuleSystems.Abilities{specs: specs}} =
RuleSystems.load_system!("dnd_5e_srd")

%Spec{name: name} = spec = Enum.random(specs)
found_spec = Spec.get_spec_by_name(specs, name)
assert spec == found_spec
end
end

0 comments on commit f831e6f

Please sign in to comment.