| 1 |
|
defmodule Tools.Sys.Become.Schema do |
| 2 |
|
import Enum, only: [drop: 2, map: 2, join: 2] |
| 3 |
|
|
| 4 |
|
def get(name, state) do |
| 5 |
49 |
%{name: name, description: desc(state), parameters: params()} |
| 6 |
|
end |
| 7 |
|
|
| 8 |
|
defp desc(state) do |
| 9 |
49 |
"Switch to role. Available: #{roles(state.config)}. Use only once per turn." |
| 10 |
|
end |
| 11 |
|
|
| 12 |
|
defp params do |
| 13 |
49 |
%{type: "object", properties: properties(), required: ["role"]} |
| 14 |
|
end |
| 15 |
|
|
| 16 |
|
defp properties do |
| 17 |
49 |
%{role: %{type: "string", description: "Role name to switch to"}} |
| 18 |
|
end |
| 19 |
|
|
| 20 |
|
defp roles(config) do |
| 21 |
|
config |
| 22 |
|
|> drop(1) |
| 23 |
193 |
|> map(& &1.name) |
| 24 |
49 |
|> join(", ") |
| 25 |
|
end |
| 26 |
|
end |
| 27 |
|
|
| 28 |
|
defmodule Tools.Sys.Become do |
| 29 |
|
import Enum, only: [map: 2] |
| 30 |
|
import Log, only: [log: 5, agent: 5] |
| 31 |
|
import Map, only: [put: 3] |
| 32 |
|
|
| 33 |
|
@icon "ðŸŽ" |
| 34 |
|
|
| 35 |
|
defdelegate spec(name, state), to: Tools.Sys.Become.Schema, as: :get |
| 36 |
|
|
| 37 |
|
def icon, do: @icon |
| 38 |
|
|
| 39 |
|
def exec(_, %{"role" => role}, %{name: name} = state) do |
| 40 |
|
log(@icon, name, " as ", role, :magenta) |
| 41 |
|
agent(@icon, name, " as ", role, %{name: name}) |
| 42 |
|
{"switched to #{role}", switch(state, role)} |
| 43 |
|
end |
| 44 |
|
|
| 45 |
|
def exec(_, _args, state) do |
| 46 |
|
{"become needs role", state} |
| 47 |
|
end |
| 48 |
|
|
| 49 |
|
defp switch(state, role) do |
| 50 |
|
%{state | config: map(state.config, &activate(&1, role))} |
| 51 |
|
end |
| 52 |
|
|
| 53 |
|
defp activate(config, target) do |
| 54 |
|
put(config, :active, config.name == target) |
| 55 |
|
end |
| 56 |
|
end |