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.go32
1 files changed, 21 insertions, 11 deletions
diff --git a/internal/player/store.go b/internal/player/store.go
index d0dbd0f..d461485 100644
--- a/internal/player/store.go
+++ b/internal/player/store.go
@@ -37,12 +37,17 @@ func (s *AccountStore) LoadAccount(name string) (*Account, error) {
}
func (s *AccountStore) SaveAccount(acc *Account) error {
- path := s.AccountPath(acc.Name)
- data, err := yaml.Marshal(acc)
+ data, err := s.MarshalAccount(acc)
if err != nil {
return err
}
- return persistence.WriteFile(path, data, 0600)
+ return persistence.WriteFile(s.AccountPath(acc.Name), data, 0600)
+}
+
+// MarshalAccount serializes an account; callers that own the account (the
+// game owner, or transaction code holding it) may use this directly.
+func (s *AccountStore) MarshalAccount(acc *Account) ([]byte, error) {
+ return yaml.Marshal(acc)
}
func (s *AccountStore) AccountExists(name string) bool {
@@ -103,9 +108,18 @@ func (s *AccountStore) LoadCharacter(name string) (*Player, error) {
}
func (s *AccountStore) SaveCharacter(p *Player) error {
- path := s.CharPath(p.Name)
- // Serialize a value copy so saving god-mode stats never temporarily
- // changes the live player. The caller still owns all referenced maps.
+ data, err := s.MarshalCharacter(p)
+ if err != nil {
+ return err
+ }
+ return persistence.WriteFile(s.CharPath(p.Name), data, 0600)
+}
+
+// MarshalCharacter serializes a character for a same-directory temporary
+// atomic write. It serializes a value snapshot so saving god-mode stats
+// never temporarily changes the live player; the caller still owns all
+// referenced maps (serialize on the owner goroutine).
+func (s *AccountStore) MarshalCharacter(p *Player) ([]byte, error) {
snapshot := *p
if snapshot.GodMode && snapshot.GodBackup != nil {
snapshot.Skills = snapshot.GodBackup
@@ -113,11 +127,7 @@ func (s *AccountStore) SaveCharacter(p *Player) error {
snapshot.HP = snapshot.MaxHP()
}
}
- data, err := yaml.Marshal(&snapshot)
- if err != nil {
- return err
- }
- return persistence.WriteFile(path, data, 0600)
+ return yaml.Marshal(&snapshot)
}
func (s *AccountStore) CharacterExists(name string) bool {