diff --git a/Sources/LichtkastenCore/Metadata/CodeLibrary.swift b/Sources/LichtkastenCore/Metadata/CodeLibrary.swift index e3751d0..90b0527 100644 --- a/Sources/LichtkastenCore/Metadata/CodeLibrary.swift +++ b/Sources/LichtkastenCore/Metadata/CodeLibrary.swift @@ -67,10 +67,24 @@ public struct CodeSet: Codable, Hashable, Identifiable, Sendable { 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 { - guard let (p, rest) = CodeSet.split(code), let to = prefixMap[p.lowercased()] else { return code } - return to + rest + // longest matching old prefix wins ("hs" before "h") + 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 …). @@ -85,8 +99,7 @@ public struct CodeSet: Codable, Hashable, Identifiable, Sendable { let existing = Set(entries.map { $0.code.lowercased() }) var n = 0 for i in entries.indices { - guard let (p, rest) = CodeSet.split(entries[i].code), p.lowercased() == from else { continue } - let renamed = to + rest + guard let renamed = CodeSet.renamed(entries[i].code, from: from, to: to) else { continue } if existing.contains(renamed.lowercased()) { continue } // never two players on one code entries[i].code = renamed n += 1 diff --git a/Tests/LichtkastenCoreTests/CoreTests.swift b/Tests/LichtkastenCoreTests/CoreTests.swift index 5642dba..783ffe5 100644 --- a/Tests/LichtkastenCoreTests/CoreTests.swift +++ b/Tests/LichtkastenCoreTests/CoreTests.swift @@ -534,3 +534,18 @@ final class CodePrefixTests: XCTestCase { 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"]) + } +}