-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent.go
145 lines (116 loc) · 3.87 KB
/
event.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
package main
import (
"sync"
)
// EventsDB struct
type EventsDB struct {
db *DBHandler
querylocker sync.RWMutex
}
// Event struct
type Event struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"` // 60 characters or less
Type string `json:"type"`
TypeFlags []string `json:"typeflags"`
PrivateResponse bool `json:"privateresponse"` // Whether or not to return a response in a private message
FinalizeOutput bool `json:"finalizeoutput"` // If set to true, we want to notify the keyvalue that our output is finalized
LoadOnBoot bool `json:"-"` // Whether or not to load the event at boot
// Cycles int `json:"cycles"` // Number of times to run the event, a setting of 0 or less will be parsed as "infinite"
Data []string `json:"data"` // Different types can contain multiple data fields
DefaultData string `json:"defaultdata"` // Default data to fallback on for events that need this
// Set when event is registered
CreatorID string `json:"creatorid"` // The userID of the creator
// These are not set by "events add", these must be set with the script manager
Rooms []string `json:"rooms"`
ParentID string `json:"-"` // The id of the parent event if one exists
ChildIDs []string `json:"-"` // The ids of the various childs (there can exist multiple children, ie for a multiple choice question)
RunCount int `json:"-"` // The total number of runs the event has had during this cycle
TriggeredEvent bool `json:"-"` // Used to denote whether or not an event is a copy created by a trigger
// Used for scripting
LinkedEvent bool `json:"-"` // If we are linked, we want to read data from the keyvalue and not passthrough data
IsScriptEvent bool `json:"isscriptevent"` // If set to true, this event belongs to a script and should not be manually modified
OriginalID string `json:"originalid"`
}
// SaveEventToDB function
func (h *EventsDB) SaveEventToDB(event Event) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Events")
err = db.Save(&event)
return err
}
// RemoveEventFromDB function
func (h *EventsDB) RemoveEventFromDB(event Event) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Events")
err = db.DeleteStruct(&event)
return err
}
// UpdateEventInDB function
func (h *EventsDB) UpdateEventInDB(event Event) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
err = h.db.rawdb.Update(&event)
return err
}
// RemoveEventByID function
func (h *EventsDB) RemoveEventByID(eventID string) (err error) {
event, err := h.GetEventByID(eventID)
if err != nil {
return err
}
err = h.RemoveEventFromDB(event)
if err != nil {
return err
}
return nil
}
// GetEventByID function
func (h *EventsDB) GetEventByID(eventID string) (event Event, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Events")
err = db.One("ID", eventID, &event)
if err != nil {
return event, err
}
return event, nil
}
// GetEventByName function
func (h *EventsDB) GetEventByName(eventName string) (event Event, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Events")
err = db.One("Name", eventName, &event)
if err != nil {
return event, err
}
return event, nil
}
// ValidateEventByID function
func (h *EventsDB) ValidateEventByID(eventID string) (validated bool) {
events, err := h.GetAllEvents()
if err != nil {
return false
}
for _, record := range events {
if eventID == record.ID {
return true
}
}
return false
}
// GetAllEvents function
func (h *EventsDB) GetAllEvents() (eventlist []Event, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Events")
err = db.All(&eventlist)
if err != nil {
return eventlist, err
}
return eventlist, nil
}