summaryrefslogtreecommitdiff
path: root/events.go
diff options
context:
space:
mode:
Diffstat (limited to 'events.go')
-rw-r--r--events.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/events.go b/events.go
new file mode 100644
index 0000000..9800e39
--- /dev/null
+++ b/events.go
@@ -0,0 +1,49 @@
+// events.go
+package main
+
+type Event struct {
+ ID int64 `json:"id"`
+ Type string `json:"type"` // "chat" or "location"
+ Timestamp int64 `json:"timestamp"`
+ FromNick string `json:"fromNick,omitempty"`
+ FromInternal string `json:"fromInternal,omitempty"`
+ Message string `json:"message,omitempty"`
+ ServerID string `json:"serverId,omitempty"`
+ X float64 `json:"x,omitempty"`
+ Y float64 `json:"y,omitempty"`
+ Z float64 `json:"z,omitempty"`
+ Dimension string `json:"dimension,omitempty"`
+}
+
+func (h *Hub) addEvent(e Event) {
+ h.mu.Lock()
+ defer h.mu.Unlock()
+
+ e.ID = h.nextEventID
+ h.nextEventID++
+
+ if len(h.events) >= h.maxEvents {
+ copy(h.events, h.events[1:])
+ h.events[len(h.events)-1] = e
+ } else {
+ h.events = append(h.events, e)
+ }
+}
+
+func (h *Hub) getEventsSince(since int64) []Event {
+ h.mu.RLock()
+ defer h.mu.RUnlock()
+
+ if since <= 0 {
+ out := make([]Event, len(h.events))
+ copy(out, h.events)
+ return out
+ }
+ var res []Event
+ for _, e := range h.events {
+ if e.ID > since {
+ res = append(res, e)
+ }
+ }
+ return res
+} \ No newline at end of file