summaryrefslogtreecommitdiff
path: root/events.go
blob: 9800e39f84670b4ba6e8a91f77837dbd7477525d (plain)
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
// 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
}