chessbaseLinux/iosApp/ChessPenguin/NotationModel.swift
2026-12-01 00:56:28 +03:00

264 lines
9 KiB
Swift

import Foundation
struct NotationMove: Equatable, Identifiable {
let id: Int
let label: String
}
struct NotationMoveRow: Equatable, Identifiable {
let id: String
let number: UInt32
let white: NotationMove?
let black: NotationMove?
let indent: Int
}
struct NotationComment: Equatable, Identifiable {
let id: String
let text: String
let indent: Int
}
enum NotationToken: Equatable, Identifiable {
case move(NotationMove)
case comment(id: String, text: String)
case punctuation(id: String, text: String)
var id: String {
switch self {
case .move(let move): return "move-\(move.id)"
case .comment(let id, _), .punctuation(let id, _): return id
}
}
}
struct NotationVariation: Equatable, Identifiable {
let id: String
let startNodeID: Int
let tokens: [NotationToken]
let indent: Int
}
struct NotationResult: Equatable, Identifiable {
let id: String
let text: String
let indent: Int
}
enum NotationEntry: Equatable, Identifiable {
case comment(NotationComment)
case moves(NotationMoveRow)
case variation(NotationVariation)
case result(NotationResult)
var id: String {
switch self {
case .comment(let entry): return entry.id
case .moves(let entry): return entry.id
case .variation(let entry): return entry.id
case .result(let entry): return entry.id
}
}
var indent: Int {
switch self {
case .comment(let entry): return entry.indent
case .moves(let entry): return entry.indent
case .variation(let entry): return entry.indent
case .result(let entry): return entry.indent
}
}
}
/// Complete PGN presentation: a two-column main line and every alternative subtree as
/// clickable tokens. IDs are MoveNode.id, never visible row offsets.
struct NotationModel {
let entries: [NotationEntry]
let entryByNode: [Int: String]
var moveIDs: [Int] {
entries.flatMap { entry -> [Int] in
switch entry {
case .moves(let row):
return [row.white?.id, row.black?.id].compactMap { $0 }
case .variation(let variation):
return variation.tokens.compactMap {
if case .move(let move) = $0 { return move.id }
return nil
}
case .comment, .result:
return []
}
}
}
init(game: GameData) {
var entries: [NotationEntry] = []
var entryByNode: [Int: String] = [:]
var visited: Set<Int> = [0]
if let root = game.nodes.first, !root.commentAfter.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
entries.append(.comment(NotationComment(
id: "comment-root-after",
text: root.commentAfter,
indent: 0
)))
}
var pendingRow: PendingRow?
func flushRow() {
guard let row = pendingRow else { return }
let entry = NotationMoveRow(
id: row.id,
number: row.number,
white: row.white.map { NotationMove(id: $0, label: game.nodes[$0].san + game.nodes[$0].nagSymbols) },
black: row.black.map { NotationMove(id: $0, label: game.nodes[$0].san + game.nodes[$0].nagSymbols) },
indent: 0
)
entries.append(.moves(entry))
if let white = row.white { entryByNode[white] = row.id }
if let black = row.black { entryByNode[black] = row.id }
pendingRow = nil
}
var parent = 0
for _ in 0..<game.nodes.count {
guard game.nodes.indices.contains(parent) else { break }
let children = game.nodes[parent].children
guard let first = children.first else { break }
let id = Int(first)
guard game.nodes.indices.contains(id), !visited.contains(id) else { break }
visited.insert(id)
let node = game.nodes[id]
if !node.commentBefore.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
flushRow()
entries.append(.comment(NotationComment(
id: "comment-\(id)-before",
text: node.commentBefore,
indent: 0
)))
}
if node.white {
flushRow()
pendingRow = PendingRow(id: "moves-\(id)", number: node.moveNumber, white: id, black: nil)
} else if pendingRow?.black == nil {
if pendingRow == nil {
pendingRow = PendingRow(id: "moves-\(id)", number: node.moveNumber, white: nil, black: id)
} else {
pendingRow?.black = id
}
} else {
flushRow()
pendingRow = PendingRow(id: "moves-\(id)", number: node.moveNumber, white: nil, black: id)
}
let alternatives = children.dropFirst()
if !node.white || !node.commentAfter.isEmpty || !alternatives.isEmpty {
flushRow()
if !node.commentAfter.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
entries.append(.comment(NotationComment(
id: "comment-\(id)-after",
text: node.commentAfter,
indent: 0
)))
}
for alternative in alternatives {
let start = Int(alternative)
let entryID = "variation-\(start)"
if let variation = NotationModel.makeVariation(
game: game,
start: start,
id: entryID,
indent: 1,
visited: &visited,
entryByNode: &entryByNode
) {
entries.append(.variation(variation))
}
}
}
parent = id
}
flushRow()
let result = L10n.resultLine(game.result)
if !result.isEmpty {
entries.append(.result(NotationResult(id: "result", text: result, indent: 0)))
}
self.entries = entries
self.entryByNode = entryByNode
}
private static func makeVariation(
game: GameData,
start: Int,
id entryID: String,
indent: Int,
visited: inout Set<Int>,
entryByNode: inout [Int: String]
) -> NotationVariation? {
var tokens: [NotationToken] = []
func appendComment(_ text: String, suffix: String) {
guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }
tokens.append(.comment(id: "\(entryID)-comment-\(suffix)-\(tokens.count)", text: text))
}
func appendPunctuation(_ text: String) {
tokens.append(.punctuation(id: "\(entryID)-punctuation-\(tokens.count)", text: text))
}
func line(first: Int) {
var id = first
var showNumber = true
for _ in 0..<game.nodes.count {
guard game.nodes.indices.contains(id), !visited.contains(id) else { break }
visited.insert(id)
entryByNode[id] = entryID
let node = game.nodes[id]
if !node.commentBefore.isEmpty {
appendComment(node.commentBefore, suffix: "\(id)-before")
showNumber = true
}
let number = node.white ? "\(node.moveNumber)." : (showNumber ? "\(node.moveNumber)…" : "")
tokens.append(.move(NotationMove(
id: id,
label: number + node.san + node.nagSymbols
)))
showNumber = false
if !node.commentAfter.isEmpty {
appendComment(node.commentAfter, suffix: "\(id)-after")
showNumber = true
}
if id != first, let parent = node.parent, game.nodes.indices.contains(Int(parent)) {
for alternative in game.nodes[Int(parent)].children.dropFirst() {
let alternativeID = Int(alternative)
guard game.nodes.indices.contains(alternativeID), !visited.contains(alternativeID) else { continue }
appendPunctuation("(")
line(first: alternativeID)
appendPunctuation(")")
showNumber = true
}
}
guard let next = node.children.first else { break }
id = Int(next)
}
}
line(first: start)
guard !tokens.isEmpty else { return nil }
return NotationVariation(id: entryID, startNodeID: start, tokens: tokens, indent: indent)
}
}
private struct PendingRow {
let id: String
let number: UInt32
var white: Int?
var black: Int?
}