aboutsummaryrefslogtreecommitdiff
path: root/internal/player/store.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/player/store.go')
-rw-r--r--internal/player/store.go39
1 files changed, 18 insertions, 21 deletions
diff --git a/internal/player/store.go b/internal/player/store.go
index 9bfdf40..d0dbd0f 100644
--- a/internal/player/store.go
+++ b/internal/player/store.go
@@ -8,6 +8,7 @@ import (
"gopkg.in/yaml.v3"
"thehouseoficarus/internal/item"
+ "thehouseoficarus/internal/persistence"
)
type AccountStore struct {
@@ -41,7 +42,7 @@ func (s *AccountStore) SaveAccount(acc *Account) error {
if err != nil {
return err
}
- return os.WriteFile(path, data, 0644)
+ return persistence.WriteFile(path, data, 0600)
}
func (s *AccountStore) AccountExists(name string) bool {
@@ -61,7 +62,7 @@ func (s *AccountStore) FindAccount(name string) (string, error) {
}
lower := strings.ToLower(name)
for _, e := range entries {
- if e.IsDir() {
+ if e.IsDir() || !strings.HasSuffix(e.Name(), ".yaml") {
continue
}
base := strings.TrimSuffix(e.Name(), ".yaml")
@@ -103,23 +104,20 @@ func (s *AccountStore) LoadCharacter(name string) (*Player, error) {
func (s *AccountStore) SaveCharacter(p *Player) error {
path := s.CharPath(p.Name)
- if p.GodMode && p.GodBackup != nil {
- buffedSkills := p.Skills
- buffedHP := p.HP
- p.Skills = p.GodBackup
- if p.HP > p.MaxHP() {
- p.HP = p.MaxHP()
+ // Serialize a value copy so saving god-mode stats never temporarily
+ // changes the live player. The caller still owns all referenced maps.
+ snapshot := *p
+ if snapshot.GodMode && snapshot.GodBackup != nil {
+ snapshot.Skills = snapshot.GodBackup
+ if snapshot.HP > snapshot.MaxHP() {
+ snapshot.HP = snapshot.MaxHP()
}
- defer func() {
- p.Skills = buffedSkills
- p.HP = buffedHP
- }()
}
- data, err := yaml.Marshal(p)
+ data, err := yaml.Marshal(&snapshot)
if err != nil {
return err
}
- return os.WriteFile(path, data, 0644)
+ return persistence.WriteFile(path, data, 0600)
}
func (s *AccountStore) CharacterExists(name string) bool {
@@ -139,7 +137,7 @@ func (s *AccountStore) FindCharacter(name string) (string, error) {
}
lower := strings.ToLower(name)
for _, e := range entries {
- if e.IsDir() {
+ if e.IsDir() || !strings.HasSuffix(e.Name(), ".yaml") {
continue
}
base := strings.TrimSuffix(e.Name(), ".yaml")
@@ -150,10 +148,7 @@ func (s *AccountStore) FindCharacter(name string) (string, error) {
return "", fmt.Errorf("character not found: %s", name)
}
-// ListCharacterNames returns the stem (character name) of every
-// data/players/characters/*.yaml file. Used by room-ID migration to walk and
-// rewrite offline characters. Online characters are excluded by the caller
-// (their in-memory state is updated directly).
+// ListAccountNames returns the stem of each account YAML file.
func (s *AccountStore) ListAccountNames() ([]string, error) {
dir := filepath.Join(s.dataDir, "players", "accounts")
entries, err := os.ReadDir(dir)
@@ -162,7 +157,7 @@ func (s *AccountStore) ListAccountNames() ([]string, error) {
}
var names []string
for _, e := range entries {
- if e.IsDir() {
+ if e.IsDir() || !strings.HasSuffix(e.Name(), ".yaml") {
continue
}
name := strings.TrimSuffix(e.Name(), ".yaml")
@@ -174,6 +169,8 @@ func (s *AccountStore) ListAccountNames() ([]string, error) {
return names, nil
}
+// ListCharacterNames returns character YAML stems for offline migrations.
+// Callers exclude online characters and update their in-memory state instead.
func (s *AccountStore) ListCharacterNames() ([]string, error) {
dir := filepath.Join(s.dataDir, "players", "characters")
entries, err := os.ReadDir(dir)
@@ -182,7 +179,7 @@ func (s *AccountStore) ListCharacterNames() ([]string, error) {
}
var names []string
for _, e := range entries {
- if e.IsDir() {
+ if e.IsDir() || !strings.HasSuffix(e.Name(), ".yaml") {
continue
}
name := strings.TrimSuffix(e.Name(), ".yaml")