Fix stale story-unlock and post-evolve weapon state

This commit is contained in:
Ilya Groshev
2026-05-01 16:22:24 +03:00
parent 3fe564cb1d
commit 20d8e4d3df
4 changed files with 145 additions and 11 deletions
+6 -7
View File
@@ -294,17 +294,16 @@ func ComputeDelta(before, after *store.UserState, changedTables []string) map[st
diff := make(map[string]*pb.DiffData, len(changedTables))
for _, table := range changedTables {
afterJSON := projectTable(table, *after)
updates := afterJSON
deleteKeys := "[]"
if kf := keyFieldsForTable(table); len(kf) > 0 {
beforeJSON := projectTable(table, *before)
deleteKeys = ComputeDeleteKeys(
parseJSONRecords(beforeJSON),
parseJSONRecords(afterJSON),
kf,
)
beforeRecs := parseJSONRecords(projectTable(table, *before))
afterRecs := parseJSONRecords(afterJSON)
updates = ComputeUpdateRecords(beforeRecs, afterRecs, kf)
deleteKeys = ComputeDeleteKeys(beforeRecs, afterRecs, kf)
}
diff[table] = &pb.DiffData{
UpdateRecordsJson: afterJSON,
UpdateRecordsJson: updates,
DeleteKeysJson: deleteKeys,
}
}
+29
View File
@@ -3,6 +3,7 @@ package userdata
import (
"encoding/json"
"fmt"
"reflect"
"strings"
pb "lunar-tear/server/gen/proto"
@@ -120,6 +121,34 @@ func ComputeDeleteKeys(oldRecords, newRecords []map[string]any, keyFields []stri
return string(b)
}
func ComputeUpdateRecords(oldRecords, newRecords []map[string]any, keyFields []string) string {
if len(newRecords) == 0 {
return "[]"
}
oldByKey := make(map[string]map[string]any, len(oldRecords))
for _, r := range oldRecords {
oldByKey[compositeKey(r, keyFields)] = r
}
var changed []map[string]any
for _, r := range newRecords {
prev, exists := oldByKey[compositeKey(r, keyFields)]
if !exists || !reflect.DeepEqual(prev, r) {
changed = append(changed, r)
}
}
if len(changed) == 0 {
return "[]"
}
b, err := json.Marshal(changed)
if err != nil {
return "[]"
}
return string(b)
}
func compositeKey(record map[string]any, fields []string) string {
var sb strings.Builder
for i, f := range fields {