Kürzel-Änderung gilt auch für Trainer- und Staff-Codes

Codes ohne Rückennummer (z. B. „htr“ Trainer, „h-co“ Co-Trainer) werden beim Umstellen h → hs mit
umbenannt (hstr, hs-co) und auch beim Nachladen der TXT umgestellt; Codes, die schon mit dem neuen
Kürzel beginnen, bleiben unverändert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
This commit is contained in:
Lichtkasten DevandClaude Opus 5.5 committed 2026-11-17 19:51:01 +00:00
1 parent 7efaa74678
commit d559a4f182
2 files changed
+33 -5

No files matched your search

@@ -67,10 +67,24 @@ public struct CodeSet: Codable, Hashable, Identifiable, Sendable {
return (p, rest) return (p, rest)
} }
/// The code as this team uses it (own short code applied). /// The code as this team uses it (own short code applied) – players and trainer/staff codes alike.
public func mapped(_ code: String) -> String { public func mapped(_ code: String) -> String {
guard let (p, rest) = CodeSet.split(code), let to = prefixMap[p.lowercased()] else { return code } // longest matching old prefix wins ("hs" before "h")
return to + rest for (from, to) in prefixMap.sorted(by: { $0.key.count > $1.key.count }) {
if let r = CodeSet.renamed(code, from: from, to: to) { return r }
}
return code
}
/// One code with the short code changed, nil if it does not use `from`:
/// players "h10" → "hs10" (letters must be exactly the prefix), and codes without a number –
/// trainer "htr", "h-co" … → "hstr", "hs-co" (start with the prefix).
static func renamed(_ code: String, from: String, to: String) -> String? {
if let (p, rest) = split(code) { return p.lowercased() == from ? to + rest : nil }
let lc = code.lowercased()
guard lc.hasPrefix(from), lc.count > from.count else { return nil }
if to.hasPrefix(from) && lc.hasPrefix(to) { return nil } // already has the new prefix
return to + String(code.dropFirst(from.count))
} }
/// Short code of the team as it is now ("h" for h10, h7 …). /// Short code of the team as it is now ("h" for h10, h7 …).
@@ -85,8 +99,7 @@ public struct CodeSet: Codable, Hashable, Identifiable, Sendable {
let existing = Set(entries.map { $0.code.lowercased() }) let existing = Set(entries.map { $0.code.lowercased() })
var n = 0 var n = 0
for i in entries.indices { for i in entries.indices {
guard let (p, rest) = CodeSet.split(entries[i].code), p.lowercased() == from else { continue } guard let renamed = CodeSet.renamed(entries[i].code, from: from, to: to) else { continue }
let renamed = to + rest
if existing.contains(renamed.lowercased()) { continue } // never two players on one code if existing.contains(renamed.lowercased()) { continue } // never two players on one code
entries[i].code = renamed entries[i].code = renamed
n += 1 n += 1
@@ -534,3 +534,18 @@ final class CodePrefixTests: XCTestCase {
XCTAssertTrue(lib.importNewFiles().isEmpty) XCTAssertTrue(lib.importNewFiles().isEmpty)
} }
} }
final class CodePrefixStaffTests: XCTestCase {
func testTrainerCodesFollowThePrefix() {
var set = CodeSet(name: "HSV", entries: [CodeEntry(code: "h10", columns: ["Spieler"]),
CodeEntry(code: "htr", columns: ["Trainer"]),
CodeEntry(code: "h-co", columns: ["Co-Trainer"])])
XCTAssertEqual(set.renamePrefix(from: "h", to: "hs"), 3)
XCTAssertEqual(set.entries.map(\.code), ["hs10", "hstr", "hs-co"])
set.merge([CodeEntry(code: "htr", columns: ["Trainer"])]) // TXT reload: no old code back
XCTAssertEqual(set.entries.count, 3)
XCTAssertEqual(set.renamePrefix(from: "hs", to: "h"), 3) // and back
XCTAssertEqual(set.entries.map(\.code), ["h10", "htr", "h-co"])
}
}