diff options
| author | historia <[not public]> | 2026-07-07 05:50:37 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-07-07 05:50:37 -0400 |
| commit | 3f30fa3eec9c2e2acf9a984ccefb9529a25e688a (patch) | |
| tree | b6c146ec7f3dc2ea21dbf8ee7612c3889d93ca1a /internal/admin/id_alloc.go | |
| parent | a51f7a53aa2c487ebf94ba0363038574ae8bb590 (diff) | |
| download | thehouseoficarus-3f30fa3eec9c2e2acf9a984ccefb9529a25e688a.tar.gz | |
feat: add insert/remove room to context menu in admin web GUI to 'push' or 'pull' map rooms
Diffstat (limited to 'internal/admin/id_alloc.go')
| -rw-r--r-- | internal/admin/id_alloc.go | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/internal/admin/id_alloc.go b/internal/admin/id_alloc.go index d79718b..3a3c973 100644 --- a/internal/admin/id_alloc.go +++ b/internal/admin/id_alloc.go @@ -6,24 +6,43 @@ import ( "strconv" ) -func nextRoomIDInDir(dataDir string, fromRoomID int) (int, string, error) { - base := filepath.Join(dataDir, "rooms") +// nextRoomID allocates a free room ID for a new room that will live in the same +// directory as fromRoomID (so new IDs cluster near their neighbors). Unlike the +// old per-subdir scan, the candidate is checked against EVERY room ID on disk +// (via listRoomIDs, which walks the whole data/rooms tree), so it can never +// collide with an ID that lives in a different subdirectory. The starting point +// is the local subdir's minimum existing ID (clustering), then the first +// globally-free ID scanning upward. +func (s *AdminServer) nextRoomID(fromRoomID int) (int, string, error) { + base := filepath.Join(s.dataDir, "rooms") subdir, err := findRoomSubdir(base, fromRoomID) if err != nil { subdir = base } - used := map[int]bool{} - scanDir(subdir, used) - if len(used) == 0 { - return 1, subdir, nil - } - minID := 1<<31 - 1 - for id := range used { - if id < minID { - minID = id + + localUsed := map[int]bool{} + scanDir(subdir, localUsed) + start := 1 + if len(localUsed) > 0 { + minID := 1<<31 - 1 + for id := range localUsed { + if id < minID { + minID = id + } } + start = minID + } + + globalUsed, _ := listRoomIDs(s.dataDir) + used := make(map[int]bool, len(globalUsed)) + for _, id := range globalUsed { + used[id] = true + } + + id := start + if id < 1 { + id = 1 } - id := minID for used[id] { id++ } |
