1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
package player
import (
"fmt"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
"thehouseoficarus/internal/item"
)
type AccountStore struct {
dataDir string
}
func NewAccountStore(dataDir string) *AccountStore {
return &AccountStore{dataDir: dataDir}
}
func (s *AccountStore) AccountPath(name string) string {
return filepath.Join(s.dataDir, "players", "accounts", name+".yaml")
}
func (s *AccountStore) LoadAccount(name string) (*Account, error) {
path := s.AccountPath(name)
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("account not found: %s", name)
}
var acc Account
if err := yaml.Unmarshal(data, &acc); err != nil {
return nil, fmt.Errorf("parse account: %w", err)
}
return &acc, nil
}
func (s *AccountStore) SaveAccount(acc *Account) error {
path := s.AccountPath(acc.Name)
data, err := yaml.Marshal(acc)
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
func (s *AccountStore) AccountExists(name string) bool {
_, err := s.FindAccount(name)
return err == nil
}
func (s *AccountStore) FindAccount(name string) (string, error) {
path := s.AccountPath(name)
if _, err := os.Stat(path); err == nil {
return name, nil
}
dir := filepath.Join(s.dataDir, "players", "accounts")
entries, err := os.ReadDir(dir)
if err != nil {
return "", fmt.Errorf("account not found: %s", name)
}
lower := strings.ToLower(name)
for _, e := range entries {
if e.IsDir() {
continue
}
base := strings.TrimSuffix(e.Name(), ".yaml")
if strings.ToLower(base) == lower {
return base, nil
}
}
return "", fmt.Errorf("account not found: %s", name)
}
func (s *AccountStore) CharPath(name string) string {
return filepath.Join(s.dataDir, "players", "characters", name+".yaml")
}
func (s *AccountStore) LoadCharacter(name string) (*Player, error) {
path := s.CharPath(name)
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("character not found: %s", name)
}
var p Player
if err := yaml.Unmarshal(data, &p); err != nil {
return nil, fmt.Errorf("parse character: %w", err)
}
if p.Skills == nil {
p.Skills = make(map[SkillName]int)
}
if p.Equipment == nil {
p.Equipment = make(map[item.EquipSlot]string)
}
if p.Inventory == nil {
p.Inventory = make(map[int]*InventorySlot)
}
if p.MapSymbols == nil {
p.MapSymbols = make(map[int]MapSymbolData)
}
return &p, nil
}
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()
}
defer func() {
p.Skills = buffedSkills
p.HP = buffedHP
}()
}
data, err := yaml.Marshal(p)
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
func (s *AccountStore) CharacterExists(name string) bool {
_, err := s.FindCharacter(name)
return err == nil
}
func (s *AccountStore) FindCharacter(name string) (string, error) {
path := s.CharPath(name)
if _, err := os.Stat(path); err == nil {
return name, nil
}
dir := filepath.Join(s.dataDir, "players", "characters")
entries, err := os.ReadDir(dir)
if err != nil {
return "", fmt.Errorf("character not found: %s", name)
}
lower := strings.ToLower(name)
for _, e := range entries {
if e.IsDir() {
continue
}
base := strings.TrimSuffix(e.Name(), ".yaml")
if strings.ToLower(base) == lower {
return base, nil
}
}
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).
func (s *AccountStore) ListAccountNames() ([]string, error) {
dir := filepath.Join(s.dataDir, "players", "accounts")
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var names []string
for _, e := range entries {
if e.IsDir() {
continue
}
name := strings.TrimSuffix(e.Name(), ".yaml")
if name == "" {
continue
}
names = append(names, name)
}
return names, nil
}
func (s *AccountStore) ListCharacterNames() ([]string, error) {
dir := filepath.Join(s.dataDir, "players", "characters")
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var names []string
for _, e := range entries {
if e.IsDir() {
continue
}
name := strings.TrimSuffix(e.Name(), ".yaml")
if name == "" {
continue
}
names = append(names, name)
}
return names, nil
}
|