-
Notifications
You must be signed in to change notification settings - Fork 2
/
rooms.go
222 lines (181 loc) · 4.09 KB
/
rooms.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
package main
import (
"errors"
"sync"
)
// Rooms struct
type Rooms struct {
db *DBHandler
querylocker sync.RWMutex
}
// Room struct
type Room struct {
ID string `storm:"id"` // primary key
Name string
GuildID string
Type string
Topic string // For recording the discord room topic
ParentID string
ParentName string
TravelRoleID string
AdditionalRoleIDs []string
UserIDs []string
/*
These are likely to change, but here's generally what is needed in the roleID slice
0 - Default Travel ID
1 - Region Role ID
2 - Quest Role ID
3 - Spell Role ID
4 - Blessing Role ID
5 - Override Role ID
*/
GuildTransferInvite string
TransferRoomID string
// Connecting Room ID's
UpID string
UpItemID []string
UpScriptName string
UpDescription string
DownID string
DownItemID []string
DownScriptName string
DownDescription string
NorthID string
NorthItemID []string
NorthScriptName string
NorthDescription string
NorthEastID string
NorthEastItemID []string
NorthEastScriptName string
NorthEastDescription string
EastID string
EastItemID []string
EastScriptName string
EastDescription string
SouthEastID string
SouthEastItemID []string
SouthEastScriptName string
SouthEastDescription string
SouthID string
SouthItemID []string
SouthScriptName string
SouthDescription string
SouthWestID string
SouthWestItemID []string
SouthWestScriptName string
SouthWestDescription string
WestID string
WestItemID []string
WestScriptName string
WestDescription string
NorthWestID string
NorthWestItemID []string
NorthWestScriptName string
NorthWestDescription string
Items []string
NPC []string
Description string
}
// SaveRoomToDB function
func (h *Rooms) SaveRoomToDB(room Room) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Rooms")
err = db.Save(&room)
return err
}
// RemoveRoomFromDB function
func (h *Rooms) RemoveRoomFromDB(room Room) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Rooms")
err = db.DeleteStruct(&room)
return err
}
// RemoveRoomByID function
func (h *Rooms) RemoveRoomByID(roomID string) (err error) {
room, err := h.GetRoomByID(roomID)
if err != nil {
return err
}
err = h.RemoveRoomFromDB(room)
if err != nil {
return err
}
return nil
}
// GetRoomByID function
func (h *Rooms) GetRoomByID(roomID string) (room Room, err error) {
rooms, err := h.GetAllRooms()
if err != nil {
return room, err
}
for _, record := range rooms {
if roomID == record.ID {
return record, nil
}
}
return room, errors.New("No record found")
}
// GetRoomByName function
func (h *Rooms) GetRoomByName(roomname string, guildID string) (room Room, err error) {
rooms, err := h.GetAllRooms()
if err != nil {
return room, err
}
for _, i := range rooms {
if i.Name == roomname && i.GuildID == guildID {
return i, nil
}
}
return room, errors.New("No record found")
}
// GetAllRooms function
func (h *Rooms) GetAllRooms() (roomlist []Room, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Rooms")
err = db.All(&roomlist)
if err != nil {
return roomlist, err
}
return roomlist, nil
}
// IsRoomLinkedTo function
func (h *Rooms) IsRoomLinkedTo(roomID string, checklink string) (linked bool, err error) {
room, err := h.GetRoomByID(roomID)
if err != nil {
return false, err
}
if room.NorthID == checklink {
return true, nil
}
if room.NorthEastID == checklink {
return true, nil
}
if room.EastID == checklink {
return true, nil
}
if room.SouthEastID == checklink {
return true, nil
}
if room.SouthID == checklink {
return true, nil
}
if room.SouthWestID == checklink {
return true, nil
}
if room.WestID == checklink {
return true, nil
}
if room.NorthWestID == checklink {
return true, nil
}
if room.UpID == checklink {
return true, nil
}
if room.DownID == checklink {
return true, nil
}
return false, nil
}