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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
package admin
import (
"net/http"
"os"
"path/filepath"
"strconv"
"thehouseoficarus/internal/world"
)
func (s *AdminServer) handleMap(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
return
}
zStr := r.URL.Query().Get("z")
z, err := strconv.Atoi(zStr)
if err != nil {
z = 0
}
dir := r.URL.Query().Get("dir")
seed := s.cfg.StartingRoom
if dir != "" {
base := filepath.Join(s.dataDir, "rooms", dir)
if low, ok := findLowestRoom(base); ok {
seed = low
}
}
g := world.BuildGrid(seed, func(id int) (*world.Room, bool) {
room, err := s.world.LoadRoom(id)
if err != nil {
return nil, false
}
return room, true
}, nil, nil)
type RoomEntry struct {
ID int `json:"id"`
X int `json:"x"`
Y int `json:"y"`
Name string `json:"name"`
Color string `json:"color"`
Symbol string `json:"symbol"`
}
type LinkEntry struct {
From int `json:"from"`
To int `json:"to"`
Dir string `json:"dir"`
Bidirectional bool `json:"bidirectional"`
}
type UDLink struct {
From int `json:"from"`
To int `json:"to"`
}
roomData := make(map[int]*world.Room)
var rooms []RoomEntry
minX, maxX := 0, 0
minY, maxY := 0, 0
first := true
for rid, coord := range g.Coord {
if coord[2] != z {
continue
}
r, err := s.world.LoadRoom(rid)
if err != nil {
continue
}
roomData[rid] = r
rooms = append(rooms, RoomEntry{
ID: rid,
X: coord[0],
Y: coord[1],
Name: r.Name,
Color: r.Color,
})
if first {
minX, maxX = coord[0], coord[0]
minY, maxY = coord[1], coord[1]
first = false
} else {
if coord[0] < minX {
minX = coord[0]
}
if coord[0] > maxX {
maxX = coord[0]
}
if coord[1] < minY {
minY = coord[1]
}
if coord[1] > maxY {
maxY = coord[1]
}
}
}
var links []LinkEntry
var upLinks []UDLink
var downLinks []UDLink
seenLinks := make(map[string]bool)
for rid, room := range roomData {
c := g.Coord[rid]
for dir, exit := range room.Exits {
target := exit.Room
if target <= 0 {
continue
}
targetCoord, ok := g.Coord[target]
if !ok {
continue
}
if targetCoord[2] > c[2] {
upLinks = append(upLinks, UDLink{From: rid, To: target})
continue
}
if targetCoord[2] < c[2] {
downLinks = append(downLinks, UDLink{From: rid, To: target})
continue
}
if dir == world.Up || dir == world.Down {
continue
}
if _, onZ := roomData[target]; !onZ {
continue
}
bidirectional := false
if targetRoom, ok := roomData[target]; ok {
if oppExit, ok := targetRoom.Exits[world.OppositeExit[dir]]; ok && oppExit.Room == rid {
bidirectional = true
}
}
key := linkKey(rid, target)
if bidirectional && seenLinks[key] {
continue
}
seenLinks[key] = true
links = append(links, LinkEntry{
From: rid,
To: target,
Dir: string(dir),
Bidirectional: bidirectional,
})
}
}
writeJSON(w, map[string]any{
"rooms": rooms,
"links": links,
"upLinks": upLinks,
"downLinks": downLinks,
"dir": getRoomDir(s, seed),
"bounds": map[string]int{
"minX": minX,
"maxX": maxX,
"minY": minY,
"maxY": maxY,
},
})
}
func getRoomDir(s *AdminServer, roomID int) string {
path, ok := s.world.GetRoomPath(roomID)
if !ok {
return ""
}
rel, err := filepath.Rel(filepath.Join(s.dataDir, "rooms"), filepath.Dir(path))
if err != nil || rel == "." {
return ""
}
return rel
}
func linkKey(a, b int) string {
if a < b {
return strconv.Itoa(a) + "-" + strconv.Itoa(b)
}
return strconv.Itoa(b) + "-" + strconv.Itoa(a)
}
func findLowestRoom(dir string) (int, bool) {
entries, err := os.ReadDir(dir)
if err != nil {
return 0, false
}
lowest := 0
found := false
for _, e := range entries {
if e.IsDir() {
continue
}
name := e.Name()
if filepath.Ext(name) != ".yaml" {
continue
}
id, err := strconv.Atoi(name[:len(name)-5])
if err != nil || id <= 0 {
continue
}
if !found || id < lowest {
lowest = id
found = true
}
}
return lowest, found
}
func (s *AdminServer) handleNextRoomID(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
return
}
fromStr := r.URL.Query().Get("from")
fromID := 0
if fromStr != "" {
fromID, _ = strconv.Atoi(fromStr)
}
id, _, err := nextRoomIDInDir(s.dataDir, fromID)
if err != nil {
writeJSON(w, map[string]any{"error": err.Error()})
return
}
writeJSON(w, map[string]any{"id": id})
}
|