mirror of Walter-Sparrow / lunar-tear
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Added --latest-scene flag

Author: https://github.com/L4w1i3t

authored by

Lawliet and committed by
GitHub
57a00512 8b53df74

+58 -1
+9
server/cmd/lunar-tear/main.go
··· 18 18 httpPort := flag.Int("http-port", 8080, "HTTP server port (Octo API)") 19 19 host := flag.String("host", "127.0.0.1", "hostname the client will connect to") 20 20 scene := flag.Int("scene", 0, "Bootstrap to scene N (0 = fresh start)") 21 + latestScene := flag.Bool("latest-scene", false, "Bootstrap from the most recently saved snapshot (overrides -scene)") 21 22 starterItems := flag.Bool("starter-items", false, "Grant starter items to new users") 22 23 flag.Parse() 23 24 ··· 36 37 snapshotDir := "snapshots" 37 38 if err := os.MkdirAll(snapshotDir, 0755); err != nil { 38 39 log.Fatalf("create snapshot dir: %v", err) 40 + } 41 + if *latestScene { 42 + if id, ok := memory.LatestSnapshotSceneId(snapshotDir); ok { 43 + *scene = int(id) 44 + log.Printf("[latest-scene] auto-selected most recent snapshot: scene=%d", id) 45 + } else { 46 + log.Printf("[latest-scene] no snapshots found in %q; starting fresh", snapshotDir) 47 + } 39 48 } 40 49 41 50 gameConfig, err := masterdata.LoadGameConfig()
+1 -1
server/internal/service/listbin.go
··· 495 495 } 496 496 } 497 497 return candidates, entry.Size, true 498 - } 498 + }
+48
server/internal/store/memory/snapshot.go
··· 6 6 "log" 7 7 "os" 8 8 "path/filepath" 9 + "strconv" 10 + "strings" 9 11 10 12 "lunar-tear/server/internal/store" 11 13 ) ··· 30 32 return 31 33 } 32 34 log.Printf("[snapshot] saved scene=%d (%d bytes)", sceneId, len(data)) 35 + } 36 + 37 + // parseSceneId extracts the numeric scene ID from a filename of the form "scene_<id>.json". 38 + // Returns (0, false) if the name does not match the expected format. 39 + func parseSceneId(name string) (int32, bool) { 40 + if !strings.HasPrefix(name, "scene_") || !strings.HasSuffix(name, ".json") { 41 + return 0, false 42 + } 43 + raw := strings.TrimSuffix(strings.TrimPrefix(name, "scene_"), ".json") 44 + id, err := strconv.ParseInt(raw, 10, 32) 45 + if err != nil { 46 + return 0, false 47 + } 48 + return int32(id), true 49 + } 50 + 51 + // LatestSnapshotSceneId scans dir for scene_*.json files and returns the scene ID 52 + // of the most recently modified snapshot. Returns (0, false) if none are found. 53 + func LatestSnapshotSceneId(dir string) (int32, bool) { 54 + entries, err := os.ReadDir(dir) 55 + if err != nil { 56 + return 0, false 57 + } 58 + var latestId int32 59 + var latestMod int64 60 + for _, e := range entries { 61 + if e.IsDir() { 62 + continue 63 + } 64 + id, ok := parseSceneId(e.Name()) 65 + if !ok { 66 + continue 67 + } 68 + info, err := e.Info() 69 + if err != nil { 70 + continue 71 + } 72 + if mt := info.ModTime().UnixNano(); mt > latestMod { 73 + latestMod = mt 74 + latestId = id 75 + } 76 + } 77 + if latestId == 0 { 78 + return 0, false 79 + } 80 + return latestId, true 33 81 } 34 82 35 83 func loadSnapshot(dir string, sceneId int32) (*store.UserState, error) {