/
channels.ex
175 lines (140 loc) · 6.04 KB
/
channels.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
defmodule Game.Format.Channels do
@moduledoc """
Format functions for channels, including local room communication
"""
import Game.Format.Context
alias Game.Format
@doc """
Format a channel message
Example:
iex> Channels.channel_say(%{name: "global", color: "red"}, {:npc, %{name: "NPC"}}, %{message: "Hello"})
~s(\\\\[{red}global{/red}\\\\] {npc}NPC{/npc} says, {say}"Hello"{/say})
"""
@spec channel_say(String.t(), Character.t(), map()) :: String.t()
def channel_say(channel, sender, parsed_message) do
context()
|> assign(:channel_name, channel_name(channel))
|> assign(:say, say(sender, parsed_message))
|> Format.template("\\[[channel_name]\\] [say]")
end
@doc """
Color the channel's name
"""
@spec channel_name(Channel.t()) :: String.t()
def channel_name(channel) do
"{#{channel.color}}#{channel.name}{/#{channel.color}}"
end
@doc """
Format a say message
Example:
iex> Channels.say(:you, %{message: "Hello"})
~s[You say, {say}"Hello"{/say}]
iex> Channels.say({:npc, %{name: "NPC"}}, %{message: "Hello"})
~s[{npc}NPC{/npc} says, {say}"Hello"{/say}]
iex> Channels.say({:player, %{name: "Player"}}, %{message: "Hello"})
~s[{player}Player{/player} says, {say}"Hello"{/say}]
iex> Channels.say({:player, %{name: "Player"}}, %{adverb_phrase: "softly", message: "Hello"})
~s[{player}Player{/player} says softly, {say}"Hello"{/say}]
"""
@spec say(Character.t(), map()) :: String.t()
def say(:you, message) do
context()
|> assign(:message, message.message)
|> assign(:adverb_phrase, Map.get(message, :adverb_phrase, nil))
|> Format.template(~s(You say[ adverb_phrase], {say}"[message]"{/say}))
end
def say(character, message) do
context()
|> assign(:name, Format.name(character))
|> assign(:message, message.message)
|> assign(:adverb_phrase, Map.get(message, :adverb_phrase, nil))
|> Format.template(~s([name] says[ adverb_phrase], {say}"[message]"{/say}))
end
@doc """
Format a say to message
Example:
iex> Channels.say_to(:you, {:player, %{name: "Player"}}, %{message: "Hello"})
~s[You say to {player}Player{/player}, {say}"Hello"{/say}]
iex> Channels.say_to(:you, {:player, %{name: "Player"}}, %{message: "Hello", adverb_phrase: "softly"})
~s[You say softly to {player}Player{/player}, {say}"Hello"{/say}]
iex> Channels.say_to({:npc, %{name: "NPC"}}, {:player, %{name: "Player"}}, %{message: "Hello"})
~s[{npc}NPC{/npc} says to {player}Player{/player}, {say}"Hello"{/say}]
iex> Channels.say_to({:player, %{name: "Player"}}, {:npc, %{name: "Guard"}}, %{message: "Hello"})
~s[{player}Player{/player} says to {npc}Guard{/npc}, {say}"Hello"{/say}]
iex> Channels.say_to({:player, %{name: "Player"}}, {:npc, %{name: "Guard"}}, %{message: "Hello", adverb_phrase: "softly"})
~s[{player}Player{/player} says softly to {npc}Guard{/npc}, {say}"Hello"{/say}]
"""
@spec say_to(Character.t(), Character.t(), map()) :: String.t()
def say_to(:you, sayee, parsed_message) do
context()
|> assign(:sayee, Format.name(sayee))
|> assign(:message, parsed_message.message)
|> assign(:adverb_phrase, Map.get(parsed_message, :adverb_phrase, nil))
|> Format.template(~s(You say[ adverb_phrase] to [sayee], {say}"[message]"{/say}))
end
def say_to(sayer, sayee, parsed_message) do
context()
|> assign(:sayer, Format.name(sayer))
|> assign(:sayee, Format.name(sayee))
|> assign(:message, parsed_message.message)
|> assign(:adverb_phrase, Map.get(parsed_message, :adverb_phrase, nil))
|> Format.template(~s([sayer] says[ adverb_phrase] to [sayee], {say}"[message]"{/say}))
end
@doc """
Format an emote message
Example:
iex> Channels.emote({:npc, %{name: "NPC"}}, "does something")
~s[{npc}NPC{/npc} {say}does something{/say}]
iex> Channels.emote({:player, %{name: "Player"}}, "does something")
~s[{player}Player{/player} {say}does something{/say}]
"""
@spec emote(Character.t(), String.t()) :: String.t()
def emote(character, emote) do
~s[#{Format.name(character)} {say}#{emote}{/say}]
end
@doc """
Format a whisper message
iex> Channels.whisper({:player, %{name: "Player"}}, "secret message")
~s[{player}Player{/player} whispers to you, {say}"secret message"{/say}]
"""
@spec whisper(Character.t(), String.t()) :: String.t()
def whisper(sender, message) do
~s[#{Format.name(sender)} whispers to you, {say}"#{message}"{/say}]
end
@doc """
Format a whisper message from the player
iex> Channels.send_whisper({:player, %{name: "Player"}}, "secret message")
~s[You whisper to {player}Player{/player}, {say}"secret message"{/say}]
"""
@spec send_whisper(Character.t(), String.t()) :: String.t()
def send_whisper(receiver, message) do
~s[You whisper to #{Format.name(receiver)}, {say}"#{message}"{/say}]
end
@doc """
Format a whisper overheard message for others in the room
iex> Channels.whisper_overheard({:player, %{name: "Player"}}, {:npc, %{name: "Guard"}})
~s[You overhear {player}Player{/player} whispering to {npc}Guard{/npc}.]
"""
@spec whisper_overheard(Character.t(), String.t()) :: String.t()
def whisper_overheard(sender, receiver) do
~s[You overhear #{Format.name(sender)} whispering to #{Format.name(receiver)}.]
end
@doc """
Format a tell message
iex> Channels.tell({:player, %{name: "Player"}}, "secret message")
~s[{player}Player{/player} tells you, {say}"secret message"{/say}]
"""
@spec tell(Character.t(), String.t()) :: String.t()
def tell(sender, message) do
~s[#{Format.name(sender)} tells you, {say}"#{message}"{/say}]
end
@doc """
Format a tell message, for display of the sender
iex> Channels.send_tell({:player, %{name: "Player"}}, "secret message")
~s[You tell {player}Player{/player}, {say}"secret message"{/say}]
"""
@spec send_tell(Character.t(), String.t()) :: String.t()
def send_tell(character, message) do
~s[You tell #{Format.name(character)}, {say}"#{message}"{/say}]
end
end