/
socials.ex
149 lines (115 loc) · 3.66 KB
/
socials.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
defmodule Game.Command.Socials do
@moduledoc """
The "mail" command
"""
use Game.Command
import Game.Room.Helpers, only: [find_character: 2]
alias Game.Format.Socials, as: FormatSocials
alias Game.Socials
commands(["socials"], parse: false)
@impl Game.Command
def help(:topic), do: "Socials"
def help(:short), do: "Say prewritten emotes to a room"
def help(:full) do
"""
Say prewritten emotes to the room.
View a list of available socials:
[ ] > {command}socials{/command}
Look at what a social will send:
[ ] > {command}social smile{/command}
You can then use that social by name:
[ ] > {command}smile{/command}
And add a target:
[ ] > {command}smile guard{/command}
"""
end
@impl true
def parse(command, _context), do: parse(command)
@impl Game.Command
@doc """
Parse the command into arguments
iex> Game.Command.Socials.parse("socials")
{:list}
iex> Game.Command.Socials.parse("socials smile")
{:help, "smile"}
iex> Game.Command.Socials.parse("unknown")
{:error, :bad_parse, "unknown"}
"""
@spec parse(String.t()) :: {any()}
def parse(command)
def parse("socials"), do: {:list}
def parse("socials " <> social), do: {:help, social}
def parse("social " <> social), do: {:help, social}
def parse(command) when is_binary(command) do
Socials.all()
|> Enum.find(fn social ->
Regex.match?(~r(^#{social.command}), command)
end)
|> parse_social(command)
end
defp parse_social(nil, command), do: {:error, :bad_parse, command}
defp parse_social(social, command) do
command =
command
|> String.replace(~r/^#{social.command}/i, "")
|> String.trim()
case command do
"" -> {social.command}
command -> {social.command, command}
end
end
@impl Game.Command
@doc """
Send mail to players
"""
def run(command, state)
def run({:list}, state) do
socials = Socials.all()
{:paginate, FormatSocials.socials(socials), state}
end
def run({:help, social}, state) do
case Socials.social(social) do
nil ->
state |> social_not_found(social)
social ->
state.socket |> @socket.echo(FormatSocials.social(social))
end
:ok
end
def run({social}, state = %{save: save}) do
case Socials.social(social) do
nil ->
state |> social_not_found(social)
social ->
emote = FormatSocials.social_without_target(social, state.character)
save.room_id |> @environment.emote({:player, state.character}, Message.social(state.character, emote))
state.socket |> @socket.echo(emote)
end
:ok
end
def run({social, character_name}, state = %{save: save}) do
case Socials.social(social) do
nil ->
state |> social_not_found(social)
social ->
{:ok, room} = @environment.look(save.room_id)
case find_character(room, character_name) do
{:error, :not_found} ->
message = gettext("\"%{name}\" could not be found.", name: character_name)
state.socket |> @socket.echo(message)
character ->
emote = FormatSocials.social_with_target(social, state.character, character)
save.room_id |> @environment.emote({:player, character}, Message.social(state.character, emote))
state.socket |> @socket.echo(emote)
end
end
:ok
end
defp social_not_found(state, social) do
lines = [
gettext("\"%{name}\" could not be found.", name: social),
gettext("Please make sure to enter the social command. See {command}socials{/command} for the list.")
]
state.socket |> @socket.echo(Enum.join(lines, " "))
end
end