-
Notifications
You must be signed in to change notification settings - Fork 5
/
state.go
58 lines (48 loc) · 1009 Bytes
/
state.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
package dggchat
import (
"strings"
"sync"
)
type state struct {
sync.RWMutex
users []User
}
func (s *state) removeUser(nick string) {
s.Lock()
defer s.Unlock()
for i, user := range s.users {
if strings.EqualFold(user.Nick, nick) {
s.users = append(s.users[:i], s.users[i+1:]...)
break
}
}
}
func (s *state) addUser(user User) {
s.Lock()
defer s.Unlock()
// If you are not in chat (0 instances of your user), and join,
// chat backend includes your name in the NAMES command, and ALSO
// sends a JOIN command with your name. This makes sure we do not
// include ourself 2 times. Otherwise this check would not be needed.
for _, u := range s.users {
if strings.EqualFold(user.Nick, u.Nick) {
return
}
}
s.users = append(s.users, user)
}
func (s *state) updateUser(user User) {
s.Lock()
defer s.Unlock()
for i, u := range s.users {
if user.ID == u.ID {
s.users[i] = user
}
}
}
func newState() *state {
s := &state{
users: make([]User, 0),
}
return s
}