-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtravel_handler.go
358 lines (299 loc) · 9.64 KB
/
travel_handler.go
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
package main
import (
"errors"
"fmt"
"github.com/bwmarrin/discordgo"
"strings"
"time"
)
// TravelHandler struct
type TravelHandler struct {
conf *Config
registry *CommandRegistry
callback *CallbackHandler
db *DBHandler
perms *PermissionsHandler
room *RoomsHandler
user *UserHandler
transfer *TransferHandler
scripts *ScriptHandler
}
// Init function
func (h *TravelHandler) Init() {
h.RegisterCommands()
}
// RegisterCommands function
func (h *TravelHandler) RegisterCommands() (err error) {
h.registry.Register("travel", "Travel in a direction", "up|down|north|northeast|etc")
h.registry.AddGroup("travel", "player")
return nil
}
// Read function
func (h *TravelHandler) Read(s *discordgo.Session, m *discordgo.MessageCreate) {
cp := h.conf.MainConfig.CP
if !SafeInput(s, m, h.conf) {
return
}
user, err := h.db.GetUser(m.Author.ID)
if err != nil {
//fmt.Println("Error finding usermanager")
return
}
if strings.HasPrefix(m.Content, cp+"travel") {
if h.registry.CheckPermission("travel", user, s, m) {
command := strings.Fields(m.Content)
// Grab our sender ID to verify if this usermanager has permission to use this command
db := h.db.rawdb.From("Users")
var user User
err := db.One("ID", m.Author.ID, &user)
if err != nil {
fmt.Println("error retrieving usermanager:" + m.Author.ID)
}
if user.CheckRole("player") {
h.ParseCommand(command, s, m)
}
}
}
}
// ParseCommand function
func (h *TravelHandler) ParseCommand(command []string, s *discordgo.Session, m *discordgo.MessageCreate) {
if len(command) < 2 {
s.ChannelMessageSend(m.ChannelID, "Expected flag for 'travel' command, see command usage for more info")
return
}
err := h.Travel(command[1], s, m)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
user, err := h.user.GetUser(m.Author.ID, s, m.ChannelID)
if err != nil {
s.ChannelMessageSend(user.RoomID, "Error retrieving usermanager: "+err.Error())
return
}
fromroom, err := h.room.rooms.GetRoomByID(user.RoomID)
if err != nil {
s.ChannelMessageSend(user.RoomID, "Error retrieving room: "+err.Error())
return
}
travelfrom := ""
if command[1] == "north" {
travelfrom = "south"
} else if command[1] == "northeast" {
travelfrom = "southwest"
} else if command[1] == "east" {
travelfrom = "west"
} else if command[1] == "southeast" {
travelfrom = "northwest"
} else if command[1] == "south" {
travelfrom = "north"
} else if command[1] == "southwest" {
travelfrom = "northeast"
} else if command[1] == "west" {
travelfrom = "east"
} else if command[1] == "northwest" {
travelfrom = "southeast"
} else if command[1] == "up" {
travelfrom = "below"
} else if command[1] == "down" {
travelfrom = "above"
}
transferroom := Room{}
if fromroom.GuildTransferInvite != "" {
transferroom, err = h.room.rooms.GetRoomByID(fromroom.TransferRoomID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error retrieving transfer room: "+err.Error())
return
}
}
// Notify channel that usermanager has left
discorduser, err := s.User(user.ID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error retrieving usermanager: "+err.Error())
return
}
leaveoutout := discorduser.Username + " has left traveling " + command[1]
s.ChannelMessageSend(m.ChannelID, leaveoutout)
// If we're not leaving the server, we want to notify the channel that the usermanager has arrived
if travelfrom == "below" || travelfrom == "above" {
s.ChannelMessageSend(user.RoomID, discorduser.Mention()+" has arrived from "+travelfrom+".")
} else {
s.ChannelMessageSend(user.RoomID, discorduser.Mention()+" has arrived from the "+travelfrom+".")
}
time.Sleep(3000)
// If we're leaving this server, we want to avoid sending an arrival message to the holding channel
if fromroom.GuildTransferInvite != "" {
// m.ChannelID because this is the channel we are leaving from
h.HandleServerTransfer(user, fromroom.ID, fromroom.TransferRoomID, transferroom.GuildID, fromroom, travelfrom, s, m)
return
}
return
}
// HandleServerTransfer function
func (h *TravelHandler) HandleServerTransfer(user User, travelfromID string, transerToID string, targetGuildID string, fromroom Room, fromDirection string,
s *discordgo.Session, m *discordgo.MessageCreate) {
// We create a private message to send to the usermanager
privateInviteMessage := ":satellite: You are now traveling through The Aether, please " +
"click the invite link below to complete your journey. The materialization process may take a few " +
"minutes to complete depending on the *Materialization Backlog*: "
privateInviteMessage = privateInviteMessage + fromroom.GuildTransferInvite
userprivatechannel, err := s.UserChannelCreate(user.ID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error creating Aether Link: "+err.Error())
return
}
s.ChannelMessageSend(userprivatechannel.ID, privateInviteMessage)
channel, err := s.Channel(transerToID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error creating Aether Link: "+err.Error())
return
}
if channel.GuildID != targetGuildID {
s.ChannelMessageSend(m.ChannelID, "Error creating Aether Link: "+err.Error())
return
}
// We create an notification for the transfer_handler
err = h.transfer.AddTransfer(user.ID, travelfromID, transerToID, targetGuildID, fromDirection)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error creating Aether Link: "+err.Error())
return
}
return
}
// Travel function
func (h *TravelHandler) Travel(direction string, s *discordgo.Session, m *discordgo.MessageCreate) (err error) {
user, err := h.user.GetUser(m.Author.ID, s, m.ChannelID)
if err != nil {
return err
}
fromroom, err := h.room.rooms.GetRoomByID(m.ChannelID)
if err != nil {
return err
}
toroom := ""
travelscriptName := ""
if direction == "up" {
toroom = fromroom.UpID
travelscriptName = fromroom.UpScriptName
} else if direction == "down" {
toroom = fromroom.DownID
travelscriptName = fromroom.DownScriptName
} else if direction == "north" {
toroom = fromroom.NorthID
travelscriptName = fromroom.NorthScriptName
} else if direction == "northeast" {
toroom = fromroom.NorthEastID
travelscriptName = fromroom.NorthEastScriptName
} else if direction == "east" {
toroom = fromroom.EastID
travelscriptName = fromroom.EastScriptName
} else if direction == "southeast" {
toroom = fromroom.SouthEastID
travelscriptName = fromroom.SouthEastScriptName
} else if direction == "south" {
toroom = fromroom.SouthID
travelscriptName = fromroom.SouthScriptName
} else if direction == "southwest" {
toroom = fromroom.SouthWestID
travelscriptName = fromroom.SouthWestScriptName
} else if direction == "west" {
toroom = fromroom.WestID
travelscriptName = fromroom.WestScriptName
} else if direction == "northwest" {
toroom = fromroom.NorthWestID
travelscriptName = fromroom.NorthWestScriptName
} else {
return errors.New("Unrecognized direction: " + direction)
}
if toroom == "" {
return errors.New("There is nowhere to travel in that direction")
}
// Here we run our associated travel script, if one exists for the direction we are traveling
if travelscriptName != "" {
err = h.ExecTravelScript(travelscriptName, user, s, m)
if err != nil {
return errors.New("Cannot travel " + direction + " - " + err.Error())
}
}
targetroom, err := h.room.rooms.GetRoomByID(toroom)
if err != nil {
return err
}
if len(targetroom.AdditionalRoleIDs) < 1 {
return errors.New("Target room is not configured properly: " + toroom)
}
if len(fromroom.AdditionalRoleIDs) < 1 {
return errors.New("From room is not configured properly: " + toroom)
}
if len(targetroom.Items) > 0 {
// This is where the logic for items validation will go
}
guildID, err := getGuildID(s, m.ChannelID)
if err != nil {
return err
}
err = h.perms.AddRoleToUser(targetroom.TravelRoleID, user.ID, s, m, true)
if err != nil {
return err
}
err = h.perms.RemoveRoleFromUser(fromroom.TravelRoleID, user.ID, s, m, true)
if err != nil {
return err
}
user, err = h.user.GetUser(m.Author.ID, s, m.ChannelID)
if err != nil {
return err
}
user.RoomID = targetroom.ID
user.GuildID = guildID
db := h.db.rawdb.From("Users")
err = db.Update(&user)
if err != nil {
return errors.New("Error updating user record into database")
}
err = h.room.AddUserIDToRoomRecord(user.ID, targetroom.ID, guildID, s)
if err != nil {
return errors.New("Error updating user record into room: " + err.Error())
}
err = h.room.RemoveUserIDFromRoomRecord(user.ID, fromroom.ID)
if err != nil {
return errors.New("Error removing user record from room: " + err.Error())
}
return nil
}
// ExecTravelScript function
func (h *TravelHandler) ExecTravelScript(scriptName string, user User, s *discordgo.Session, m *discordgo.MessageCreate) (err error) {
status, err := h.scripts.ExecuteScript(scriptName, s, m)
if err != nil {
return err
}
if status == false {
fmt.Println(err.Error())
if err == nil {
return errors.New("")
}
return err
}
return nil
// Proof of concept, to be updated later when scripts system is in place!
/*
if user.Intelligence < 4 {
return errors.New("You are not smart enough to travel here yet")
}
if user.Strength < 3 {
return errors.New("You are not strong enough to travel here yet")
}
if user.Dexterity < 4 {
return errors.New("You are not dexterious enough to travel here yet")
}
if user.Constitution < 3 {
return errors.New("You do not have enough constitution to travel here yet")
}
if user.Wisdom < 3 {
return errors.New("You are not wise enough to travel here yet")
}
if user.Charisma < 2 {
return errors.New("You are not charismatic enough to travel here yet")
}
*/
}