Add SQLite persistence, import-snapshot tool, and karma functionality

This commit is contained in:
Ilya Groshev
2026-04-20 09:57:47 +03:00
parent c9ad3fa4f4
commit c33e738fd5
70 changed files with 4151 additions and 833 deletions
+56
View File
@@ -0,0 +1,56 @@
package main
import (
"encoding/json"
"flag"
"log"
"os"
"lunar-tear/server/internal/database"
"lunar-tear/server/internal/store"
"lunar-tear/server/internal/store/sqlite"
)
func main() {
dbPath := flag.String("db", "db/game.db", "SQLite database path")
snapshotPath := flag.String("snapshot", "", "Path to JSON snapshot file (required)")
userUuid := flag.String("uuid", "", "UUID to assign to the imported user (must match the client's UUID)")
flag.Parse()
if *snapshotPath == "" {
log.Fatal("--snapshot flag is required")
}
if *userUuid == "" {
log.Fatal("--uuid flag is required")
}
data, err := os.ReadFile(*snapshotPath)
if err != nil {
log.Fatalf("read snapshot: %v", err)
}
log.Printf("read %d bytes from %s", len(data), *snapshotPath)
var u store.UserState
if err := json.Unmarshal(data, &u); err != nil {
log.Fatalf("unmarshal snapshot: %v", err)
}
u.EnsureMaps()
u.Uuid = *userUuid
log.Printf("parsed user %d (uuid=%s, costumes=%d, weapons=%d, characters=%d, quests=%d)",
u.UserId, u.Uuid, len(u.Costumes), len(u.Weapons), len(u.Characters), len(u.Quests))
db, err := database.Open(*dbPath)
if err != nil {
log.Fatalf("open database: %v", err)
}
defer db.Close()
userStore := sqlite.New(db, nil)
if err := userStore.ImportUser(&u); err != nil {
log.Fatalf("import user: %v", err)
}
log.Printf("imported user %d successfully", u.UserId)
}
+14 -5
View File
@@ -12,7 +12,7 @@ import (
"lunar-tear/server/internal/masterdata"
"lunar-tear/server/internal/questflow"
"lunar-tear/server/internal/service"
"lunar-tear/server/internal/store/memory"
"lunar-tear/server/internal/store"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@@ -38,9 +38,13 @@ func (l loggingListener) Accept() (net.Conn, error) {
func startGRPC(
host string,
octoURL string,
userStore *memory.MemoryStore,
userStore interface {
store.UserRepository
store.SessionRepository
},
questEngine *questflow.QuestHandler,
gachaHandler *gacha.GachaHandler,
gachaEntries []store.GachaCatalogEntry,
cageOrnamentCatalog *masterdata.CageOrnamentCatalog,
loginBonusCatalog *masterdata.LoginBonusCatalog,
characterViewerCatalog *masterdata.CharacterViewerCatalog,
@@ -77,6 +81,7 @@ func startGRPC(
userStore,
questEngine,
gachaHandler,
gachaEntries,
cageOrnamentCatalog,
loginBonusCatalog,
characterViewerCatalog,
@@ -111,9 +116,13 @@ func registerServices(
srv *grpc.Server,
host string,
octoURL string,
userStore *memory.MemoryStore,
userStore interface {
store.UserRepository
store.SessionRepository
},
questEngine *questflow.QuestHandler,
gachaHandler *gacha.GachaHandler,
gachaEntries []store.GachaCatalogEntry,
cageOrnamentCatalog *masterdata.CageOrnamentCatalog,
loginBonusCatalog *masterdata.LoginBonusCatalog,
characterViewerCatalog *masterdata.CharacterViewerCatalog,
@@ -133,13 +142,13 @@ func registerServices(
sideStoryCatalog *masterdata.SideStoryCatalog,
bigHuntCatalog *masterdata.BigHuntCatalog,
) {
pb.RegisterBannerServiceServer(srv, service.NewBannerServiceServer(userStore))
pb.RegisterBannerServiceServer(srv, service.NewBannerServiceServer(gachaEntries))
pb.RegisterUserServiceServer(srv, service.NewUserServiceServer(userStore, userStore))
pb.RegisterBattleServiceServer(srv, service.NewBattleServiceServer(userStore, userStore))
pb.RegisterConfigServiceServer(srv, service.NewConfigServiceServer(host, int32(443), octoURL))
pb.RegisterDataServiceServer(srv, service.NewDataServiceServer(userStore, userStore))
pb.RegisterTutorialServiceServer(srv, service.NewTutorialServiceServer(userStore, userStore, questEngine))
pb.RegisterGachaServiceServer(srv, service.NewGachaServiceServer(userStore, userStore, userStore, gachaHandler))
pb.RegisterGachaServiceServer(srv, service.NewGachaServiceServer(userStore, userStore, gachaEntries, gachaHandler))
pb.RegisterGiftServiceServer(srv, service.NewGiftServiceServer(userStore, userStore))
pb.RegisterGamePlayServiceServer(srv, service.NewGameplayServiceServer())
pb.RegisterGimmickServiceServer(srv, service.NewGimmickServiceServer(userStore, userStore, gimmickCatalog))
+10 -25
View File
@@ -3,23 +3,21 @@ package main
import (
"flag"
"log"
"os"
"strconv"
"strings"
"lunar-tear/server/internal/database"
"lunar-tear/server/internal/gacha"
"lunar-tear/server/internal/gametime"
"lunar-tear/server/internal/masterdata"
"lunar-tear/server/internal/questflow"
"lunar-tear/server/internal/store/memory"
"lunar-tear/server/internal/store/sqlite"
)
func main() {
httpPort := flag.Int("http-port", 8080, "HTTP server port (Octo API)")
host := flag.String("host", "127.0.0.1", "hostname the client will connect to")
scene := flag.Int("scene", 0, "Bootstrap to scene N (0 = fresh start)")
latestScene := flag.Bool("latest-scene", false, "Bootstrap from the most recently saved snapshot (overrides -scene)")
starterItems := flag.Bool("starter-items", false, "Grant starter items to new users")
dbPath := flag.String("db", "db/game.db", "SQLite database path")
flag.Parse()
octoURL := "http://" + *host + ":" + strconv.Itoa(*httpPort)
@@ -34,18 +32,12 @@ func main() {
go startHTTP(*httpPort, resourcesBaseURL)
snapshotDir := "snapshots"
if err := os.MkdirAll(snapshotDir, 0755); err != nil {
log.Fatalf("create snapshot dir: %v", err)
}
if *latestScene {
if id, ok := memory.LatestSnapshotSceneId(snapshotDir); ok {
*scene = int(id)
log.Printf("[latest-scene] auto-selected most recent snapshot: scene=%d", id)
} else {
log.Printf("[latest-scene] no snapshots found in %q; starting fresh", snapshotDir)
}
db, err := database.Open(*dbPath)
if err != nil {
log.Fatalf("open database: %v", err)
}
defer db.Close()
log.Printf("database opened: %s", *dbPath)
gameConfig, err := masterdata.LoadGameConfig()
if err != nil {
@@ -65,14 +57,7 @@ func main() {
log.Fatalf("load quest catalog: %v", err)
}
questHandler := questflow.NewQuestHandler(questCatalog, gameConfig)
userStore := memory.New(gametime.Now,
memory.WithSnapshotDir(snapshotDir),
memory.WithSceneId(int32(*scene)),
memory.WithStarterItems(*starterItems),
)
if *scene != 0 {
log.Printf("bootstrap scene: %d (from snapshot)", *scene)
}
userStore := sqlite.New(db, gametime.Now)
gachaEntries, medalInfo, err := masterdata.LoadGachaCatalog()
if err != nil {
@@ -99,7 +84,6 @@ func main() {
gachaPool.BuildFeaturedMapping(gachaEntries)
gachaPool.BuildBannerPools(gachaEntries)
masterdata.EnrichCatalogPromotions(gachaEntries, gachaPool)
userStore.ReplaceCatalog(gachaEntries)
dupExchange, err := masterdata.LoadDupExchange()
if err != nil {
@@ -185,6 +169,7 @@ func main() {
userStore,
questHandler,
gachaHandler,
gachaEntries,
cageOrnamentCatalog,
loginBonusCatalog,
characterViewerCatalog,