/
mapper.go
199 lines (186 loc) · 3.53 KB
/
mapper.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package mapper
import (
"sync"
"github.com/herb-go/uniqueid"
)
type Path struct {
Command string
Delay int
From string
To string
Tags map[string]bool
ExcludeTags map[string]bool
}
func NewPath() *Path {
return &Path{
Tags: map[string]bool{},
ExcludeTags: map[string]bool{},
}
}
type Room struct {
ID string
Name string
Exits []*Path
}
func NewRoom() *Room {
return &Room{
Exits: []*Path{},
}
}
type Mapper struct {
Locker sync.RWMutex
rooms map[string]*Room
tags map[string]bool
fly []*Path
}
func (m *Mapper) FlyList() []*Path {
m.Locker.Lock()
defer m.Locker.Unlock()
return append(m.fly)
}
func (m *Mapper) SetFlyList(fly []*Path) {
m.Locker.Lock()
defer m.Locker.Unlock()
m.fly = fly
}
func (m *Mapper) FlashTags() {
m.Locker.Lock()
defer m.Locker.Unlock()
m.tags = map[string]bool{}
}
func (m *Mapper) AddTags(tagnames []string) {
m.Locker.Lock()
defer m.Locker.Unlock()
for _, v := range tagnames {
m.tags[v] = true
}
}
func (m *Mapper) SetTag(tagname string, enabled bool) {
m.Locker.Lock()
defer m.Locker.Unlock()
if enabled {
m.tags[tagname] = true
} else {
delete(m.tags, tagname)
}
}
func (m *Mapper) Tags() []string {
m.Locker.Lock()
defer m.Locker.Unlock()
var result = make([]string, 0, len(m.tags))
for k := range m.tags {
result = append(result, k)
}
return result
}
func (m *Mapper) GetPath(from string, fly bool, to []string) []*Step {
m.Locker.Lock()
defer m.Locker.Unlock()
w := m.newWalking()
w.from = from
w.to = to
if !fly {
w.fly = nil
}
return w.Walk()
}
func (m *Mapper) newWalking() *Walking {
walking := NewWalking()
walking.rooms = &m.rooms
walking.tags = m.tags
walking.fly = m.fly
return walking
}
func (m *Mapper) GetRoomID(name string) []string {
m.Locker.Lock()
defer m.Locker.Unlock()
result := []string{}
for _, v := range m.rooms {
if v.Name == name {
result = append(result, v.ID)
}
}
return result
}
func (m *Mapper) GetRoomName(id string) string {
m.Locker.Lock()
defer m.Locker.Unlock()
result := m.rooms[id]
if result == nil {
return ""
}
return result.Name
}
func (m *Mapper) SetRoomName(id string, name string) {
m.Locker.Lock()
defer m.Locker.Unlock()
result := m.rooms[id]
if result == nil {
result = NewRoom()
m.rooms[id] = result
}
result.Name = name
}
func (m *Mapper) AddPath(id string, p *Path) bool {
if p.Command == "" {
return false
}
m.Locker.Lock()
defer m.Locker.Unlock()
room := m.rooms[id]
if room == nil {
return false
}
room.Exits = append(room.Exits, p)
return true
}
func (m *Mapper) ClearRoom(id string) {
m.Locker.Lock()
defer m.Locker.Unlock()
m.clearRoom(id)
}
func (m *Mapper) clearRoom(id string) {
room := NewRoom()
room.ID = id
m.rooms[id] = room
}
func (m *Mapper) NewArea(size int) []string {
m.Locker.Lock()
defer m.Locker.Unlock()
result := []string{}
for i := 0; i < size; i++ {
id := uniqueid.MustGenerateID()
result = append(result, id)
m.clearRoom(id)
}
return result
}
func (m *Mapper) GetExits(id string, all bool) []*Path {
m.Locker.Lock()
defer m.Locker.Unlock()
result := []*Path{}
room := m.rooms[id]
if room == nil {
return result
}
for _, v := range room.Exits {
if all || ValidateTags(m.tags, v) {
result = append(result, v)
}
}
return result
}
func (m *Mapper) Reset() {
m.Locker.Lock()
defer m.Locker.Unlock()
m.rooms = map[string]*Room{}
m.tags = map[string]bool{}
m.fly = []*Path{}
}
func New() *Mapper {
return &Mapper{
rooms: map[string]*Room{},
tags: map[string]bool{},
fly: []*Path{},
}
}