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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
package admin
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"time"
)
type ExtraFile struct {
FilePath string `json:"file_path"`
OldContent []byte `json:"-"`
NewContent []byte `json:"-"`
NewFilePath string `json:"new_file_path,omitempty"`
IsDelete bool `json:"is_delete,omitempty"`
IsCreate bool `json:"is_create,omitempty"`
}
type ChangeDesc struct {
Time string `json:"time"`
Description string `json:"description"`
FilePath string `json:"file_path"`
NewFilePath string `json:"new_file_path,omitempty"`
OldContent []byte `json:"old_content"`
NewContent []byte `json:"new_content"`
IsDelete bool `json:"is_delete"`
IsCreate bool `json:"is_create"`
ExtraFiles []ExtraFile `json:"extra_files,omitempty"`
// IDMap records a room-ID change (oldID -> newID) included in the
// change. Undo applies its inverse, redo the forward map, through the
// wired runtime migration so players, Hub membership, and casino room
// keys move back and forth with the file images.
IDMap map[int]int `json:"id_map,omitempty"`
}
func (c ChangeDesc) ShortDesc() string {
switch {
case c.IsCreate:
return fmt.Sprintf("Created %s", filepath.Base(c.FilePath))
case c.IsDelete:
return fmt.Sprintf("Deleted %s", filepath.Base(c.FilePath))
case c.NewFilePath != "":
if filepath.Dir(c.FilePath) == filepath.Dir(c.NewFilePath) {
return fmt.Sprintf("Renamed %s", filepath.Base(c.NewFilePath))
}
return fmt.Sprintf("Moved %s", filepath.Base(c.NewFilePath))
default:
return fmt.Sprintf("Edited %s", filepath.Base(c.FilePath))
}
}
type UndoInfo struct {
CanUndo bool `json:"can_undo"`
CanRedo bool `json:"can_redo"`
UndoDesc string `json:"undo_desc"`
RedoDesc string `json:"redo_desc"`
StackSize int `json:"stack_size"`
RedoSize int `json:"redo_size"`
}
// UndoStack protects history with a mutex, not live game state. Production
// callers must execute Push/Undo/Redo/Clear within the editor's owner task.
// Undo/Redo and the subsequent rebuildAfterUndo are one task, not separate
// dispatches; migrateRoomIDs is an owner-only callback.
type UndoStack struct {
mu sync.Mutex
dataDir string
history []ChangeDesc
redo []ChangeDesc
maxSize int
filePath string
// migrateRoomIDs applies a room-ID map to runtime state (players, Hub
// membership, casino room keys) during undo/redo of room-ID changes.
migrateRoomIDs func(map[int]int)
}
func NewUndoStack(dataDir string) *UndoStack {
return &UndoStack{
dataDir: dataDir,
maxSize: 100,
filePath: filepath.Join(dataDir, ".admin_history.json"),
}
}
// SetMigrateRoomIDs wires the runtime room-ID migration used by undo/redo
// of room-ID changes. Called by the server after construction.
func (us *UndoStack) SetMigrateRoomIDs(fn func(map[int]int)) {
us.mu.Lock()
defer us.mu.Unlock()
us.migrateRoomIDs = fn
}
func (us *UndoStack) Push(desc ChangeDesc) {
us.mu.Lock()
defer us.mu.Unlock()
desc.Time = time.Now().Format(time.RFC3339)
us.history = append(us.history, desc)
us.redo = nil
if len(us.history) > us.maxSize {
us.history = us.history[len(us.history)-us.maxSize:]
}
us.save()
log.Printf("undo: push %s", desc.ShortDesc())
}
func (us *UndoStack) Undo() *ChangeDesc {
us.mu.Lock()
defer us.mu.Unlock()
if len(us.history) == 0 {
return nil
}
last := us.history[len(us.history)-1]
if err := us.applyUndo(last); err != nil {
log.Printf("undo: failed %s: %v", last.ShortDesc(), err)
return nil
}
// Runtime migration moves with the file images: undo applies the
// inverse room-ID map so players, Hub membership, and casino room keys
// return to the old location together.
if len(last.IDMap) > 0 && us.migrateRoomIDs != nil {
inverse := make(map[int]int, len(last.IDMap))
for from, to := range last.IDMap {
inverse[to] = from
}
us.migrateRoomIDs(inverse)
}
us.history = us.history[:len(us.history)-1]
us.redo = append(us.redo, last)
us.save()
log.Printf("undo: undid %s", last.ShortDesc())
return &last
}
func (us *UndoStack) Redo() *ChangeDesc {
us.mu.Lock()
defer us.mu.Unlock()
if len(us.redo) == 0 {
return nil
}
last := us.redo[len(us.redo)-1]
if err := us.applyRedo(last); err != nil {
log.Printf("redo: failed %s: %v", last.ShortDesc(), err)
return nil
}
// Redo applies the forward room-ID map through the runtime migration.
if len(last.IDMap) > 0 && us.migrateRoomIDs != nil {
forward := make(map[int]int, len(last.IDMap))
for from, to := range last.IDMap {
forward[from] = to
}
us.migrateRoomIDs(forward)
}
us.redo = us.redo[:len(us.redo)-1]
us.history = append(us.history, last)
us.save()
log.Printf("redo: redid %s", last.ShortDesc())
return &last
}
// applyUndo reverts a change: the primary file, then every extra file.
// Extra-file failures propagate: a multi-file undo that restored only part
// of a change must be reported as failed, never as success.
func (us *UndoStack) applyUndo(c ChangeDesc) error {
if c.NewFilePath != "" {
if err := os.Remove(c.NewFilePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("remove %s: %w", c.NewFilePath, err)
}
}
if c.IsDelete {
if c.OldContent == nil {
if err := os.MkdirAll(c.FilePath, 0755); err != nil {
return fmt.Errorf("recreate dir %s: %w", c.FilePath, err)
}
} else {
if err := os.MkdirAll(filepath.Dir(c.FilePath), 0755); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
if err := os.WriteFile(c.FilePath, c.OldContent, 0644); err != nil {
return fmt.Errorf("restore deleted %s: %w", c.FilePath, err)
}
}
} else if c.IsCreate {
if err := os.Remove(c.FilePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("remove created %s: %w", c.FilePath, err)
}
} else {
if err := os.MkdirAll(filepath.Dir(c.FilePath), 0755); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
if err := os.WriteFile(c.FilePath, c.OldContent, 0644); err != nil {
return fmt.Errorf("revert %s: %w", c.FilePath, err)
}
}
for _, ef := range c.ExtraFiles {
if err := applyUndoExtraFile(ef); err != nil {
return err
}
}
return nil
}
// applyRedo re-applies a change: the primary file, then every extra file.
// Extra-file failures propagate like undo.
func (us *UndoStack) applyRedo(c ChangeDesc) error {
if c.NewFilePath != "" {
if err := os.Remove(c.FilePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("remove %s: %w", c.FilePath, err)
}
}
if c.IsCreate {
target := c.FilePath
if c.NewFilePath != "" {
target = c.NewFilePath
}
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
if err := os.WriteFile(target, c.NewContent, 0644); err != nil {
return fmt.Errorf("recreate %s: %w", target, err)
}
for _, ef := range c.ExtraFiles {
if err := applyRedoExtraFile(ef); err != nil {
return err
}
}
} else if c.IsDelete {
if err := os.Remove(c.FilePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("re-delete %s: %w", c.FilePath, err)
}
for _, ef := range c.ExtraFiles {
if err := applyRedoExtraFile(ef); err != nil {
return err
}
}
} else {
target := c.FilePath
content := c.NewContent
if c.NewFilePath != "" {
target = c.NewFilePath
}
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
if err := os.WriteFile(target, content, 0644); err != nil {
return fmt.Errorf("reapply %s: %w", target, err)
}
for _, ef := range c.ExtraFiles {
if err := applyRedoExtraFile(ef); err != nil {
return err
}
}
}
return nil
}
func applyUndoExtraFile(ef ExtraFile) error {
switch {
case ef.IsCreate:
if err := os.Remove(ef.FilePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("undo remove %s: %w", ef.FilePath, err)
}
case ef.IsDelete:
if err := os.MkdirAll(filepath.Dir(ef.FilePath), 0755); err != nil {
return fmt.Errorf("undo mkdir: %w", err)
}
if err := os.WriteFile(ef.FilePath, ef.OldContent, 0644); err != nil {
return fmt.Errorf("undo restore %s: %w", ef.FilePath, err)
}
case ef.NewFilePath != "":
if err := os.Remove(ef.NewFilePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("undo remove %s: %w", ef.NewFilePath, err)
}
if err := os.MkdirAll(filepath.Dir(ef.FilePath), 0755); err != nil {
return fmt.Errorf("undo mkdir: %w", err)
}
if err := os.WriteFile(ef.FilePath, ef.OldContent, 0644); err != nil {
return fmt.Errorf("undo restore %s: %w", ef.FilePath, err)
}
default:
if err := os.MkdirAll(filepath.Dir(ef.FilePath), 0755); err != nil {
return fmt.Errorf("undo mkdir: %w", err)
}
if err := os.WriteFile(ef.FilePath, ef.OldContent, 0644); err != nil {
return fmt.Errorf("undo revert %s: %w", ef.FilePath, err)
}
}
return nil
}
func applyRedoExtraFile(ef ExtraFile) error {
switch {
case ef.IsDelete:
if err := os.Remove(ef.FilePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("redo remove %s: %w", ef.FilePath, err)
}
case ef.IsCreate:
if err := os.MkdirAll(filepath.Dir(ef.FilePath), 0755); err != nil {
return fmt.Errorf("redo mkdir: %w", err)
}
if err := os.WriteFile(ef.FilePath, ef.NewContent, 0644); err != nil {
return fmt.Errorf("redo create %s: %w", ef.FilePath, err)
}
case ef.NewFilePath != "":
if err := os.Remove(ef.FilePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("redo remove %s: %w", ef.FilePath, err)
}
if err := os.MkdirAll(filepath.Dir(ef.NewFilePath), 0755); err != nil {
return fmt.Errorf("redo mkdir: %w", err)
}
if err := os.WriteFile(ef.NewFilePath, ef.NewContent, 0644); err != nil {
return fmt.Errorf("redo create %s: %w", ef.NewFilePath, err)
}
default:
if err := os.MkdirAll(filepath.Dir(ef.FilePath), 0755); err != nil {
return fmt.Errorf("redo mkdir: %w", err)
}
if err := os.WriteFile(ef.FilePath, ef.NewContent, 0644); err != nil {
return fmt.Errorf("redo reapply %s: %w", ef.FilePath, err)
}
}
return nil
}
func (us *UndoStack) Info() UndoInfo {
us.mu.Lock()
defer us.mu.Unlock()
info := UndoInfo{
StackSize: len(us.history),
RedoSize: len(us.redo),
}
if len(us.history) > 0 {
info.CanUndo = true
info.UndoDesc = us.history[len(us.history)-1].ShortDesc()
}
if len(us.redo) > 0 {
info.CanRedo = true
info.RedoDesc = us.redo[len(us.redo)-1].ShortDesc()
}
return info
}
func (us *UndoStack) save() {
type extraEntry struct {
FilePath string `json:"file_path"`
NewFilePath string `json:"new_file_path,omitempty"`
OldContent string `json:"old_content"`
NewContent string `json:"new_content"`
IsDelete bool `json:"is_delete,omitempty"`
IsCreate bool `json:"is_create,omitempty"`
}
type entry struct {
Time string `json:"time"`
Description string `json:"description"`
FilePath string `json:"file_path"`
NewFilePath string `json:"new_file_path,omitempty"`
OldContent string `json:"old_content"`
NewContent string `json:"new_content"`
IsDelete bool `json:"is_delete"`
IsCreate bool `json:"is_create"`
ExtraFiles []extraEntry `json:"extra_files,omitempty"`
IDMap map[int]int `json:"id_map,omitempty"`
}
type saveData struct {
History []entry `json:"history"`
Redo []entry `json:"redo"`
}
toEntries := func(changes []ChangeDesc) []entry {
var entries []entry
for _, c := range changes {
var extra []extraEntry
for _, ef := range c.ExtraFiles {
extra = append(extra, extraEntry{
FilePath: ef.FilePath,
NewFilePath: ef.NewFilePath,
OldContent: string(ef.OldContent),
NewContent: string(ef.NewContent),
IsDelete: ef.IsDelete,
IsCreate: ef.IsCreate,
})
}
entries = append(entries, entry{
Time: c.Time,
Description: c.Description,
FilePath: c.FilePath,
NewFilePath: c.NewFilePath,
OldContent: string(c.OldContent),
NewContent: string(c.NewContent),
IsDelete: c.IsDelete,
IsCreate: c.IsCreate,
ExtraFiles: extra,
IDMap: c.IDMap,
})
}
return entries
}
data := saveData{
History: toEntries(us.history),
Redo: toEntries(us.redo),
}
b, err := json.Marshal(data)
if err != nil {
log.Printf("undo: failed to marshal history: %v", err)
return
}
if err := os.WriteFile(us.filePath, b, 0644); err != nil {
log.Printf("undo: failed to write history file: %v", err)
}
}
func (us *UndoStack) Clear() {
us.mu.Lock()
defer us.mu.Unlock()
us.history = nil
us.redo = nil
os.Remove(us.filePath)
log.Printf("undo: cleared history")
}
|