95 lines
2.8 KiB
Swift
95 lines
2.8 KiB
Swift
import Foundation
|
|
|
|
enum BoardMarkColor: String, Equatable {
|
|
case red = "R"
|
|
case green = "G"
|
|
case blue = "B"
|
|
case yellow = "Y"
|
|
case other
|
|
|
|
init(_ letter: String) {
|
|
self = BoardMarkColor(rawValue: letter) ?? .other
|
|
}
|
|
|
|
var rgb: UInt32 {
|
|
switch self {
|
|
case .red: return 0x882020
|
|
case .green: return 0x15781B
|
|
case .blue: return 0x003088
|
|
case .yellow: return 0xE68F00
|
|
case .other: return 0x787878
|
|
}
|
|
}
|
|
}
|
|
|
|
struct BoardArrowModel: Equatable {
|
|
let from: Int
|
|
let to: Int
|
|
let color: BoardMarkColor
|
|
}
|
|
|
|
struct BoardCircleModel: Equatable, Identifiable {
|
|
let id: Int
|
|
let square: Int
|
|
let color: BoardMarkColor
|
|
}
|
|
|
|
struct BoardBadgeModel: Equatable {
|
|
let square: Int
|
|
let text: String
|
|
let rgb: UInt32
|
|
}
|
|
|
|
enum BoardEnding: String, Equatable {
|
|
case checkmate
|
|
case stalemate
|
|
}
|
|
|
|
struct BoardAnnotationModel: Equatable {
|
|
let arrows: [BoardArrowModel]
|
|
let circles: [BoardCircleModel]
|
|
let badge: BoardBadgeModel?
|
|
let ending: BoardEnding?
|
|
|
|
init(node: MoveNode?) {
|
|
var arrows: [BoardArrowModel] = []
|
|
var circles: [BoardCircleModel] = []
|
|
for (offset, mark) in (node?.marks ?? []).enumerated() {
|
|
guard let from = squareIndex(mark.from) else { continue }
|
|
let color = BoardMarkColor(mark.color)
|
|
if let destination = mark.to, let to = squareIndex(destination) {
|
|
arrows.append(BoardArrowModel(from: from, to: to, color: color))
|
|
} else {
|
|
circles.append(BoardCircleModel(id: offset, square: from, color: color))
|
|
}
|
|
}
|
|
self.arrows = arrows
|
|
self.circles = circles
|
|
self.ending = node.flatMap { BoardEnding(rawValue: $0.end) }
|
|
|
|
if let node, let square = squareIndex(node.toSquare),
|
|
let nag = node.nags.first(where: { (1...6).contains($0) }),
|
|
let badge = BoardAnnotationModel.badge(for: nag, square: square) {
|
|
self.badge = badge
|
|
} else {
|
|
self.badge = nil
|
|
}
|
|
}
|
|
|
|
private static func badge(for nag: UInt8, square: Int) -> BoardBadgeModel? {
|
|
switch nag {
|
|
case 1: return BoardBadgeModel(square: square, text: "!", rgb: 0x81B64C)
|
|
case 2: return BoardBadgeModel(square: square, text: "?", rgb: 0xFFA459)
|
|
case 3: return BoardBadgeModel(square: square, text: "!!", rgb: 0x26C2A3)
|
|
case 4: return BoardBadgeModel(square: square, text: "??", rgb: 0xFA412D)
|
|
case 5: return BoardBadgeModel(square: square, text: "!?", rgb: 0xEA45D8)
|
|
case 6: return BoardBadgeModel(square: square, text: "?!", rgb: 0x56B4E9)
|
|
default: return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func visualSquareIndex(_ index: Int, flipped: Bool) -> Int? {
|
|
guard (0..<64).contains(index) else { return nil }
|
|
return flipped ? 63 - index : index
|
|
}
|