Code-Ersetzungen nach Liga sortiert

- Teams haben eine Liga; beim Import ist das der Ordner der TXT (Codes/1. Bundesliga/…, 2. Bundesliga/…)
- „Hinzufügen …“ nimmt auch ganze Ordner: jede TXT darin wird ein Team in ihrer Liga
- Code-Fenster: Teams in Liga-Abschnitten, ganze Liga per Haken an/aus, einklappen, Suche,
  „In Liga verschieben“, Liga-Feld beim Team, Reihenfolge innerhalb der Liga per Ziehen
- vorhandene Teams bekommen ihre Liga automatisch aus dem Ordner ihrer TXT
- Spieltag-Assistent und Trikot-Erkennung zeigen die Code-Teams nach Liga gegliedert
- Priorität bei doppelten Codes folgt der angezeigten Reihenfolge; Löschen ohne Index-Absturz

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-05 10:39:25 +00:00
1 parent c9d43aba9c
commit 239e3de0aa
7 files changed
+223 -44

No files matched your search

@@ -21,17 +21,20 @@ public struct CodeEntry: Codable, Hashable, Identifiable, Sendable {
public struct CodeSet: Codable, Hashable, Identifiable, Sendable {
public var id = UUID()
public var name: String
/// League / group the team is sorted under ("1. Bundesliga", "2. Bundesliga" …) – empty = no league.
/// Imported from the folder the TXT lies in.
public var league: String = ""
public var enabled = true
public var entries: [CodeEntry] = []
/// TXT file the team was imported from (for "Reload" / auto reload).
public var sourcePath: String?
public var sourceModified: Date?
public init(name: String, entries: [CodeEntry] = [], sourcePath: String? = nil) {
self.name = name; self.entries = entries; self.sourcePath = sourcePath
public init(name: String, league: String = "", entries: [CodeEntry] = [], sourcePath: String? = nil) {
self.name = name; self.league = league; self.entries = entries; self.sourcePath = sourcePath
}
enum CodingKeys: String, CodingKey { case id, name, enabled, entries, sourcePath, sourceModified }
enum CodingKeys: String, CodingKey { case id, name, league, enabled, entries, sourcePath, sourceModified }
/// Tolerant: a missing/new field must never cost the players.
public init(from decoder: Decoder) throws {
@@ -42,6 +45,13 @@ public struct CodeSet: Codable, Hashable, Identifiable, Sendable {
entries = (try? c.decode([CodeEntry].self, forKey: .entries)) ?? []
sourcePath = try? c.decode(String.self, forKey: .sourcePath)
sourceModified = try? c.decode(Date.self, forKey: .sourceModified)
// teams from before leagues existed: sorted under the folder their TXT lies in
league = (try? c.decode(String.self, forKey: .league)) ?? sourcePath.map(CodeSet.league(forFile:)) ?? ""
}
/// League of a TXT = name of its folder ("…/Codes/1. Bundesliga/VfL Wolfsburg.txt" → "1. Bundesliga").
public static func league(forFile path: String) -> String {
URL(fileURLWithPath: path).deletingLastPathComponent().lastPathComponent
}
/// Adds only codes that are not in the team yet (existing players stay untouched).
@@ -84,7 +94,8 @@ public struct CodeLibrary: Codable, Sendable {
/// Flat lookup table for the enabled teams – first team in the list wins on duplicate codes.
public var replacements: CodeReplacements {
var table: [String: [String]] = [:]
for set in sets where set.enabled {
// same order as shown (league by league) – the upper team wins on duplicate codes
for set in grouped.flatMap(\.sets) where set.enabled {
for e in set.entries where table[e.code.lowercased()] == nil { table[e.code.lowercased()] = e.columns }
}
var r = CodeReplacements(table: table)
@@ -122,22 +133,49 @@ public struct CodeLibrary: Codable, Sendable {
return String(decoding: data, as: UTF8.self)
}
/// Imports a TXT: merges into the team with the same source file (or name), otherwise creates a new team.
/// Leagues in list order (first appearance), teams without league last.
public var leagues: [String] {
var seen: [String] = []
for s in sets where !seen.contains(s.league) { seen.append(s.league) }
return seen.filter { !$0.isEmpty } + (seen.contains("") ? [""] : [])
}
/// Teams grouped by league, in list order (for pickers and the code window).
public var grouped: [(league: String, sets: [CodeSet])] {
leagues.map { l in (l, sets.filter { $0.league == l }) }
}
/// Imports a folder of TXT files: every TXT becomes a team, its league is the folder it lies in –
/// choose the parent folder ("Codes" with "1. Bundesliga", "2. Bundesliga" …) or one league folder.
@discardableResult
public mutating func importFile(_ url: URL, into setID: UUID? = nil) throws -> (team: String, added: Int, existing: Int) {
public mutating func importFolder(_ folder: URL) throws -> [(team: String, added: Int, existing: Int)] {
let fm = FileManager.default
guard let e = fm.enumerator(at: folder, includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles, .skipsPackageDescendants], errorHandler: { _, _ in true }) else { return [] }
let txts = e.compactMap { $0 as? URL }.filter { $0.pathExtension.lowercased() == "txt" }
.sorted { $0.path.localizedStandardCompare($1.path) == .orderedAscending }
return try txts.map { try importFile($0) }
}
/// Imports a TXT: merges into the team with the same source file (or name), otherwise creates a new team.
/// The league comes from the folder of the file (unless given).
@discardableResult
public mutating func importFile(_ url: URL, into setID: UUID? = nil, league: String? = nil) throws -> (team: String, added: Int, existing: Int) {
let entries = CodeLibrary.parse(try CodeLibrary.readText(url))
let mod = (try? FileManager.default.attributesOfItem(atPath: url.path)[.modificationDate]) as? Date
let name = url.deletingPathExtension().lastPathComponent.replacingOccurrences(of: "_", with: " ")
let idx = setID.flatMap { id in sets.firstIndex { $0.id == id } }
?? sets.firstIndex { $0.sourcePath == url.path }
?? sets.firstIndex { $0.name.caseInsensitiveCompare(name) == .orderedSame }
?? sets.firstIndex { $0.name.caseInsensitiveCompare(name) == .orderedSame
&& (league ?? CodeSet.league(forFile: url.path)).caseInsensitiveCompare($0.league) == .orderedSame }
if let i = idx {
let r = sets[i].merge(entries)
if sets[i].sourcePath == nil || setID == nil { sets[i].sourcePath = url.path }
sets[i].sourceModified = mod
if sets[i].league.isEmpty { sets[i].league = league ?? CodeSet.league(forFile: url.path) }
return (sets[i].name, r.added, r.existing)
}
var set = CodeSet(name: name, sourcePath: url.path)
var set = CodeSet(name: name, league: league ?? CodeSet.league(forFile: url.path), sourcePath: url.path)
let r = set.merge(entries)
set.sourceModified = mod
sets.append(set)
@@ -159,6 +197,17 @@ public struct CodeLibrary: Codable, Sendable {
return added
}
/// Moves a team within its league (code window drag & drop) – order = priority on duplicate codes.
public mutating func move(inLeague league: String, from source: IndexSet, to destination: Int) {
let positions = sets.indices.filter { sets[$0].league == league }
let group = positions.map { sets[$0] }
// same semantics as SwiftUI's move(fromOffsets:toOffset:) – Core stays Foundation-only
let moving = source.sorted().map { group[$0] }
var rest = group.enumerated().filter { !source.contains($0.offset) }.map(\.element)
rest.insert(contentsOf: moving, at: max(0, min(rest.count, destination - source.filter { $0 < destination }.count)))
for (p, s) in zip(positions, rest) { sets[p] = s }
}
/// Migration from the old flat code table.
public static func migrate(_ old: CodeReplacements) -> CodeLibrary {
var lib = CodeLibrary()