-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobileroommap.go
65 lines (56 loc) · 1.25 KB
/
mobileroommap.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
package spaces
import (
"github.com/trasa/syncmap"
"github.com/trasa/watchmud/mobile"
"sync"
)
type mobileToRoom map[*mobile.Instance]*Room
type MobileRoomMap struct {
sync.RWMutex
mobileToRoom mobileToRoom
roomToMobiles syncmap.MapList
}
func NewMobileRoomMap() *MobileRoomMap {
return &MobileRoomMap{
mobileToRoom: make(mobileToRoom),
roomToMobiles: syncmap.NewMapList(),
}
}
func (m *MobileRoomMap) GetRoomForMobile(mob *mobile.Instance) *Room {
m.RLock()
defer m.RUnlock()
return m.mobileToRoom[mob]
}
func (m *MobileRoomMap) GetAllMobiles() (mobs []*mobile.Instance) {
m.RLock()
defer m.RUnlock()
for m := range m.mobileToRoom {
mobs = append(mobs, m)
}
return
}
func (m *MobileRoomMap) Add(mob *mobile.Instance, r *Room) {
m.Lock()
defer m.Unlock()
m.mobileToRoom[mob] = r
m.roomToMobiles.Add(r, mob)
r.AddMobile(mob)
}
func (m *MobileRoomMap) Remove(mob *mobile.Instance) {
m.Lock()
defer m.Unlock()
r := m.mobileToRoom[mob]
delete(m.mobileToRoom, mob)
if r != nil {
m.roomToMobiles.RemoveItem(r, mob)
r.RemoveMobile(mob)
}
}
func (m *MobileRoomMap) GetMobileDefinitionCount(mobileDefinitionId string) (count int) {
for mob := range m.mobileToRoom {
if mob.Definition.Id == mobileDefinitionId {
count++
}
}
return
}