-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzombies.go
48 lines (37 loc) · 971 Bytes
/
zombies.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
package zombies
import "errors"
var (
// ErrZombieDoesntExist is an error which says a Zombie does not exist
ErrZombieDoesntExist = errors.New("Zombie does not exist")
)
// Zombies represent a pool of zombies
type Zombies struct {
pool map[int64]*Zombie
}
// New creates a new pool of Zombies
func New() *Zombies {
return &Zombies{
pool: make(map[int64]*Zombie),
}
}
// Exists checks whether a zombie has been created
func (zs *Zombies) Exists(id int64) bool {
return zs.pool[id] != nil
}
// Revive retrieves a zombie from the pool and associates it with a new WebSocket
func (zs *Zombies) Revive(id int64) (*Zombie, error) {
if !zs.Exists(id) {
return nil, ErrZombieDoesntExist
}
z := zs.pool[id]
return z, nil
}
// New creates a new zombie and adds it to the pool
func (zs *Zombies) New(id int64, server, nick string) (*Zombie, error) {
z, err := NewZombie(server, nick)
if err != nil {
return nil, err
}
zs.pool[id] = z
return z, nil
}