/
gmcp.ex
415 lines (354 loc) · 9.75 KB
/
gmcp.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
defmodule Game.Session.GMCP do
@moduledoc """
Helpers for pushing GMCP data
"""
use Networking.Socket
alias Data.Exit
alias Data.Room
alias Game.Config
alias Game.Skills
alias Game.Format
@doc """
Handle a GMCP request from the client
"""
def handle_gmcp(state, module, data)
def handle_gmcp(state, "External.Discord.Hello", _data) do
data = %{
inviteurl: Config.discord_invite_url(),
applicationid: Config.discord_client_id(),
}
data =
data
|> Enum.reject(fn {_key, val} ->
is_nil(val)
end)
|> Enum.into(%{})
state.socket |> @socket.push_gmcp("External.Discord.Info", data |> Poison.encode!())
end
def handle_gmcp(state, "External.Discord.Get", _data) do
discord_status(state)
end
def handle_gmcp(state, "Character.Skills.Get", _data) do
character_skills(state)
end
def handle_gmcp(state, "Target.Set", %{"name" => name}) do
Game.Command.Target.run({:set, name}, state)
end
def handle_gmcp(state, "Target.Clear", _data) do
Game.Command.Target.run({:clear}, state)
end
def handle_gmcp(_state, _module, _data), do: :ok
@doc """
Push Character data (save stats)
"""
@spec character(map) :: :ok
def character(%{socket: socket, character: character}) do
data = %{
name: character.name,
level: character.save.level,
class: %{
name: character.class.name
}
}
socket |> @socket.push_gmcp("Character.Info", data |> Poison.encode!())
end
@doc """
Push Character.Vitals data (save stats)
"""
@spec vitals(map) :: :ok
def vitals(%{socket: socket, save: save}) do
vitals =
save.stats
|> Map.put(:experience_towards_level, rem(save.experience_points, 1000))
socket |> @socket.push_gmcp("Character.Vitals", vitals |> Poison.encode!())
end
@doc """
Push Room.Info data
"""
@spec room(map, Room.t(), [Item.t()]) :: :ok
def room(%{socket: socket}, room, items) do
socket |> @socket.push_gmcp("Room.Info", room |> room_info(items) |> Poison.encode!())
end
@doc """
A character enters a room
Does not push directly to the socket
"""
@spec character_enter(map, Character.t()) :: {String.t(), map}
def character_enter(%{socket: socket}, character) do
socket
|> @socket.push_gmcp(
"Room.Character.Enter",
character |> character_info() |> Poison.encode!()
)
end
@doc """
A character leaves a room
Does not push directly to the socket
"""
@spec character_leave(map, Character.t()) :: {String.t(), map}
def character_leave(%{socket: socket}, character) do
socket
|> @socket.push_gmcp(
"Room.Character.Leave",
character |> character_info() |> Poison.encode!()
)
end
@doc """
Send the player's target info
"""
@spec target(map, Character.t()) :: :ok
def target(%{socket: socket}, character) do
socket
|> @socket.push_gmcp("Target.Character", character |> character_info() |> Poison.encode!())
end
@doc """
A character targeted the player
"""
@spec counter_targeted(map, Character.t()) :: :ok
def counter_targeted(%{socket: socket}, character) do
socket |> @socket.push_gmcp("Target.You", character |> character_info() |> Poison.encode!())
end
@doc """
Send a target cleared message
"""
@spec clear_target(map) :: :ok
def clear_target(%{socket: socket}) do
socket |> @socket.push_gmcp("Target.Clear", "{}")
end
@doc """
Send a map message
"""
@spec map(map, String.t()) :: :ok
def map(%{socket: socket}, map) do
socket |> @socket.push_gmcp("Zone.Map", %{map: map} |> Poison.encode!())
end
@doc """
Push a new channel message
"""
@spec channel_broadcast(State.t(), String.t(), Message.t()) :: :ok
def channel_broadcast(%{socket: socket}, channel, message) do
data = %{
channel: channel,
from: character_info({message.type, message.sender}),
message: message.message,
formatted: message.formatted,
}
socket |> @socket.push_gmcp("Channels.Broadcast", Poison.encode!(data))
end
@doc """
Push a new tell
"""
@spec tell(State.t(), Character.t(), Message.t()) :: :ok
def tell(%{socket: socket}, character, message) do
data = %{
from: character_info(character),
message: message.message
}
socket |> @socket.push_gmcp("Channels.Tell", Poison.encode!(data))
end
@doc """
Push a new room heard
"""
@spec room_heard(State.t(), Message.t()) :: :ok
def room_heard(%{socket: socket}, message) do
data = %{
from: character_info({message.type, message.sender}),
message: message.message
}
socket |> @socket.push_gmcp("Room.Heard", Poison.encode!(data))
end
@doc """
Push a new room whisper
"""
@spec room_whisper(State.t(), Message.t()) :: :ok
def room_whisper(%{socket: socket}, message) do
data = %{
from: character_info({message.type, message.sender}),
message: message.message
}
socket |> @socket.push_gmcp("Room.Whisper", Poison.encode!(data))
end
@doc """
Push a new mail
"""
@spec mail_new(State.t(), Mail.t()) :: :ok
def mail_new(%{socket: socket}, mail) do
data = %{
id: mail.id,
from: player_info(mail.sender),
title: mail.title
}
socket |> @socket.push_gmcp("Mail.New", Poison.encode!(data))
end
@doc """
Push player configuration to the client
"""
@spec config(State.t(), map()) :: :ok
def config(%{socket: socket}, config) do
socket |> @socket.push_gmcp("Config.Update", Poison.encode!(config))
end
@doc """
Send the player's configured action bar
"""
@spec config_actions(State.t()) :: :ok
def config_actions(state) do
actions =
state.character.save.actions
|> Enum.map(&Map.delete(&1, :__struct__))
|> Enum.map(&config_action_transform/1)
data = %{actions: actions}
state.socket |> @socket.push_gmcp("Config.Actions", Poison.encode!(data))
end
def config_action_transform(action = %{type: "skill"}) do
case Skills.skill(action.id) do
nil ->
nil
skill ->
action
|> Map.delete(:id)
|> Map.put(:key, skill.api_id)
end
end
def config_action_transform(action), do: action
@doc """
Push Core.Heartbeat
"""
@spec heartbeat(map) :: :ok
def heartbeat(%{socket: socket}) do
socket |> @socket.push_gmcp("Core.Heartbeat", Poison.encode!(%{}))
end
@doc """
Let the player know the skill is inactive
"""
@spec skill_state(State.t(), Skill.t(), Keyword.t()) :: :ok
def skill_state(%{socket: socket}, skill, opts) do
data = %{
key: skill.api_id,
name: skill.name,
command: skill.command,
active: opts[:active]
}
socket |> @socket.push_gmcp("Character.Skill", Poison.encode!(data))
end
@doc """
Send the player's skills
"""
@spec character_skills(State.t()) :: :ok
def character_skills(state) do
skills =
state.character.save.skill_ids
|> Skills.skills()
|> Enum.map(fn skill ->
%{
key: skill.api_id,
name: skill.name,
command: skill.command,
points: skill.points,
cooldown: skill.cooldown_time,
}
end)
data = %{skills: skills}
state.socket |> @socket.push_gmcp("Character.Skills", Poison.encode!(data))
end
@doc """
Send discord status
"""
@spec discord_status(State.t()) :: :ok
def discord_status(state) do
data = %{
game: Config.game_name(),
starttime: state.session_started_at |> Timex.to_unix()
}
state.socket |> @socket.push_gmcp("External.Discord.Status", data |> Poison.encode!())
end
defp room_info(room, items) do
room
|> Map.take([:id, :name, :ecology, :x, :y, :map_layer])
|> Map.merge(%{
zone: zone_info(room),
description: VML.collapse(Format.Rooms.room_description(room)),
items: render_many(items),
players: render_many(room, :players),
npcs: render_many(room, :npcs),
shops: render_many(room, :shops),
exits: render_many(room, :exits)
})
end
@doc """
Get info for an NPC or a User
"""
@spec character_info(Character.t()) :: map()
def character_info({:player, player}), do: player_info(player)
def character_info({:npc, npc}), do: npc_info(npc)
def character_info({:gossip, player_name}), do: gossip_info(player_name)
@doc """
Gather information for a player
"""
@spec player_info(User.t()) :: map
def player_info(player) do
%{
type: :player,
id: Map.get(player, :id, nil),
name: player.name
}
end
@doc """
Gather information for a npc
"""
@spec npc_info(NPC.t()) :: map
def npc_info(npc) do
%{
type: :npc,
id: npc.id,
name: npc.name
}
end
@doc """
Gather information for a Gossip player
"""
@spec gossip_info(String.t()) :: map()
def gossip_info(player_name) do
%{
type: :gossip,
name: player_name
}
end
@doc """
Zone information
"""
def zone_info(room) do
%{
id: room.zone.id,
name: room.zone.name
}
end
defp render_many(data) when is_list(data) do
Enum.map(data, &%{id: &1.id, name: &1.name})
end
defp render_many(room, :exits) do
room
|> Room.exits()
|> Enum.map(fn direction ->
room_exit = Exit.exit_to(room, direction)
%{room_id: room_exit.finish_id, direction: direction}
end)
end
defp render_many(room, :npcs) do
Enum.map(room.npcs, fn npc ->
%{id: npc.id, name: npc.name, status_line: Format.NPCs.npc_status(npc)}
end)
end
defp render_many(room, :players) do
Enum.map(room.players, fn player ->
%{id: player.id, name: player.name, status_line: Format.Players.player_full(player)}
end)
end
defp render_many(struct, key) do
case struct do
%{^key => data} when data != nil ->
render_many(data)
_ ->
[]
end
end
end