feat: beim ersten Abgleich die Serverfassung übernehmen

- Datei auf beiden Seiten ohne gemeinsamen Stand: Serverfassung behält
  den Namen, lokale Fassung bleibt als "(Lokal …)"-Kopie erhalten
- echte Konflikte (beide seit dem letzten Sync geändert) bleiben wie
  bisher: lokale Fassung ist Hauptdatei, Server als "(Server …)"-Kopie
- Zusammenfassung nennt übernommene Dateien samt lokaler Sicherung
- Tests und docs/sync.md entsprechend erweitert
This commit is contained in:
torsten 2026-10-30 11:25:26 +01:00
parent f553785710
commit 12ead7b6ab
6 changed files with 111 additions and 14 deletions

View file

@ -243,8 +243,10 @@ ggf. eines nach `touchend` (300-ms-Guard).
- **Kontosync:** manueller FTP-/FTPS-Abgleich der `.tres`-Dateien über
`FtpClient`, `SyncPlan` und `RoutineSync`; Details in `docs/sync.md`.
Zugangsdaten liegen in `user://sync.cfg`, der letzte gemeinsame Stand in
`user://sync_state.cfg`. Konflikte werden als Kopie mit Server-Zeitstempel
behalten.
`user://sync_state.cfg`. Beim ersten Abgleich einer beidseitig vorhandenen
Datei übernimmt die Serverfassung den Namen, die lokale bleibt als
`(Lokal …)`-Kopie; bei einem echten Konflikt bleibt die lokale Fassung
Hauptdatei und die Serverfassung kommt als `(Server …)`-Kopie dazu.
## 7. Tests

View file

@ -34,14 +34,34 @@ Danach **Datei → Jetzt synchronisieren**.
| nur auf dem Server | herunterladen |
| seit dem letzten Sync nur lokal geändert | hochladen |
| seit dem letzten Sync nur auf dem Server geändert | herunterladen |
| beidseitig geändert | **Konflikt**: beide Fassungen behalten |
| beidseitig geändert (seit dem letzten Sync) | **Konflikt**: lokale Fassung bleibt Hauptdatei, Serverfassung kommt als `(Server …)`-Kopie dazu |
| Datei auf einer Seite gelöscht | nichts tun (keine Löschweitergabe) |
| erster Sync, Datei auf beiden Seiten | **Konflikt** (kein Stand bekannt) |
| erster Sync, Datei auf beiden Seiten | **Server übernimmt**: Serverfassung wird Hauptdatei, lokale Fassung bleibt als `(Lokal …)`-Kopie |
- Bei einem Konflikt bleibt die lokale Fassung unverändert; die Serverfassung
wird als `name (Server JJJJ-MM-TT HHMM).tres` daneben gespeichert. Beim
nächsten Sync wird diese Kopie als neue Kür hochgeladen. Zusammenführen
musst du selbst.
### Wie kommt eine Kür vom Desktop aufs Tablet?
1. Auf dem Desktop die Kür speichern (Datei → Speichern) und synchronisieren.
2. Auf dem Tablet **zuerst synchronisieren**, dann Datei → Öffnen.
Die Kür wird heruntergeladen und erscheint in der Liste.
Wichtig: Der Abgleich ordnet Dateien über den **Dateinamen** zu (der aus dem
Kür-Titel entsteht). Eine Kür mit demselben Namen auf beiden Geräten ist
kein „Update“, sondern eine Kollision:
- **Erster Sync einer Datei auf beiden Seiten** (kein gemeinsamer Stand):
Die **Serverfassung übernimmt den Namen**, die lokale Fassung bleibt als
`name (Lokal JJJJ-MM-TT HHMM).tres` daneben. So bekommt ein neues Gerät die
gemeinsame Version, ohne die lokale Arbeit zu verlieren.
- **Beide Seiten seit dem letzten Sync geändert**: Die lokale Fassung bleibt
Hauptdatei, die Serverfassung kommt als
`name (Server JJJJ-MM-TT HHMM).tres` dazu. Beim nächsten Sync wird diese
Kopie als neue Kür hochgeladen. Zusammenführen musst du selbst.
Wenn du auf dem Tablet also eine leere „Testkür1“ anlegst, obwohl es die
Desktop-Version schon gibt, hast du nach dem Sync zwei Dateien: die
Desktop-Version unter dem normalen Namen und deine leere Fassung als
`testkür1 (Lokal …).tres`. Besser: erst synchronisieren, dann die vorhandene
Kür öffnen und bearbeiten.
- Vor dem Sync wird eine geänderte, bereits gespeicherte Kür automatisch
gespeichert. Eine noch nie gespeicherte Kür öffnet zuerst „Speichern unter“.
- Läuft der Sync, zeigt die Statusleiste den Fortschritt; danach erscheint

View file

@ -22,6 +22,8 @@ func run(settings: Dictionary) -> void:
"downloaded": [],
"conflicts": [],
"conflict_sources": [],
"adopted": [],
"local_backups": [],
"skipped": [],
"errors": [],
}
@ -116,6 +118,12 @@ func run(settings: Dictionary) -> void:
transferred = true
else:
result["errors"].append(conflict_error)
SyncPlan.Action.TAKE_SERVER:
var adopt_error := _take_server(client, name, used_names)
if adopt_error.is_empty():
transferred = true
else:
result["errors"].append(adopt_error)
_:
result["skipped"].append(name)
@ -155,6 +163,46 @@ func _download(client: FtpClient, name: String) -> String:
return _replace_file(temporary, target)
## First sync of a file that exists on both sides (no common state): the
## server copy keeps the normal name, the local copy is preserved next to it.
func _take_server(client: FtpClient, name: String, used_names: Dictionary) -> String:
var target := _local_path(name)
var temporary := target + TEMP_SUFFIX
var err := client.download(name, temporary)
if not err.is_empty():
return "Übernahme %s: %s" % [name, err]
var backup_name := ""
if FileAccess.file_exists(target):
backup_name = SyncPlan.unique_name(
SyncPlan.local_backup_file_name(name, SyncPlan.timestamp_label()),
used_names
)
var backup_path := _local_path(backup_name)
var rename_error := DirAccess.rename_absolute(
ProjectSettings.globalize_path(target),
ProjectSettings.globalize_path(backup_path)
)
if rename_error != OK:
DirAccess.remove_absolute(ProjectSettings.globalize_path(temporary))
return "Lokale Fassung nicht sicherbar: %s" % name
used_names[backup_name] = true
err = _replace_file(temporary, target)
if not err.is_empty():
if not backup_name.is_empty():
DirAccess.rename_absolute(
ProjectSettings.globalize_path(_local_path(backup_name)),
ProjectSettings.globalize_path(target)
)
return "Übernahme %s: %s" % [name, err]
result["adopted"].append(name)
if not backup_name.is_empty():
result["local_backups"].append(backup_name)
return ""
func _keep_both(client: FtpClient, name: String, used_names: Dictionary) -> String:
var stamp := SyncPlan.timestamp_label()
var conflict_name := SyncPlan.unique_name(
@ -210,6 +258,7 @@ func _save_state(client: FtpClient, state: Dictionary) -> void:
touched.append_array(result["uploaded"])
touched.append_array(result["downloaded"])
touched.append_array(result["conflict_sources"])
touched.append_array(result["adopted"])
for name: String in touched:
if not local_after.has(name) or not remote_after.has(name):

View file

@ -9,13 +9,18 @@ extends RefCounted
## remote: {"modify": int, "size": int}
## state: {"local_mtime": int, "remote_modify": int}
##
## Without a state entry there is no common ancestor, so a file that exists on
## both sides is treated as a conflict instead of guessing which one is newer.
## Without a state entry (first sync of a device, or a reset sync state) there
## is no common ancestor. In that case the server copy is the shared, established
## version: it takes the normal file name and the local variant is kept as a
## "(Lokal ...)" copy. Only a real conflict (both sides changed since the last
## sync) keeps the local file under its name and stores the server variant as a
## "(Server ...)" copy.
enum Action {
UPLOAD,
DOWNLOAD,
CONFLICT,
TAKE_SERVER,
SKIP,
}
@ -23,6 +28,7 @@ const ACTION_NAMES := {
Action.UPLOAD: "upload",
Action.DOWNLOAD: "download",
Action.CONFLICT: "conflict",
Action.TAKE_SERVER: "take_server",
Action.SKIP: "skip",
}
@ -60,7 +66,7 @@ static func build(
))
continue
if not known:
plan.append(_entry(name, Action.CONFLICT, "beidseitig ohne Stand"))
plan.append(_entry(name, Action.TAKE_SERVER, "beidseitig ohne Stand"))
continue
var local_changed := int(local[name].get("mtime", 0)) != int(state[name].get("local_mtime", 0))
@ -91,6 +97,12 @@ static func conflict_file_name(file_name: String, stamp: String) -> String:
return "%s (Server %s).tres" % [base, stamp]
## "kuer.tres" -> "kuer (Lokal 2026-10-30 1050).tres"
static func local_backup_file_name(file_name: String, stamp: String) -> String:
var base := file_name.get_basename()
return "%s (Lokal %s).tres" % [base, stamp]
static func timestamp_label(unix_time: int = -1) -> String:
var moment := unix_time
if moment < 0:

View file

@ -1859,6 +1859,8 @@ func _format_sync_summary(result: Dictionary) -> String:
var uploaded: Array = result.get("uploaded", [])
var downloaded: Array = result.get("downloaded", [])
var conflicts: Array = result.get("conflicts", [])
var adopted: Array = result.get("adopted", [])
var backups: Array = result.get("local_backups", [])
var skipped: Array = result.get("skipped", [])
var errors: Array = result.get("errors", [])
@ -1869,6 +1871,12 @@ func _format_sync_summary(result: Dictionary) -> String:
lines.append("Heruntergeladen: %d" % downloaded.size())
if not downloaded.is_empty():
lines.append(" " + ", ".join(downloaded))
lines.append("Vom Server übernommen: %d" % adopted.size())
for index: int in range(adopted.size()):
var backup := ""
if index < backups.size():
backup = " (lokale Fassung: %s)" % backups[index]
lines.append(" %s%s" % [adopted[index], backup])
lines.append("Konflikte (beide Fassungen behalten): %d" % conflicts.size())
if not conflicts.is_empty():
lines.append(" " + ", ".join(conflicts))
@ -1887,7 +1895,8 @@ func _reload_after_sync(result: Dictionary) -> void:
return
var current_name := _current_path.get_file()
var downloaded: Array = result.get("downloaded", [])
if not downloaded.has(current_name):
var adopted: Array = result.get("adopted", [])
if not downloaded.has(current_name) and not adopted.has(current_name):
return
if is_dirty():

View file

@ -74,8 +74,8 @@ func _test_plan() -> void:
{}
)
_check(
first_sync.size() == 1 and first_sync[0]["action"] == SyncPlan.Action.CONFLICT,
"erster Abgleich ohne Stand ist ein Konflikt"
first_sync.size() == 1 and first_sync[0]["action"] == SyncPlan.Action.TAKE_SERVER,
"erster Abgleich ohne Stand übernimmt die Serverfassung"
)
var local_deleted := SyncPlan.build(
@ -105,6 +105,11 @@ func _test_naming() -> void:
SyncPlan.unique_name("frei.tres", used) == "frei.tres",
"freier Name bleibt unverändert"
)
_check(
SyncPlan.local_backup_file_name("kuer.tres", "2026-10-30 1050")
== "kuer (Lokal 2026-10-30 1050).tres",
"lokale Sicherung trägt den Lokal-Zeitstempel"
)
var label := SyncPlan.timestamp_label(0)
_check(
label.length() == 15 and label.contains("-") and label.contains(" "),