Schafkopf-Server/Sources/App/Model/Card/Card.swift
2021-12-06 11:40:53 +01:00

76 lines
1.4 KiB
Swift

import Foundation
import Vapor
typealias CardId = String
struct Card: Codable {
enum Suit: Character, CaseIterable, Codable {
case eichel = "E"
case blatt = "B"
case herz = "H"
case schelln = "S"
}
let symbol: Symbol
let suit: Suit
init(suit: Suit, symbol: Symbol) {
self.suit = suit
self.symbol = symbol
}
init(_ suit: Suit, _ symbol: Symbol) {
self.suit = suit
self.symbol = symbol
}
init?(id: CardId) {
guard id.count == 2 else {
return nil
}
guard let suit = Suit(rawValue: id.first!),
let symbol = Symbol(id: id.last!) else {
return nil
}
self.suit = suit
self.symbol = symbol
}
var id: CardId {
"\(suit.rawValue)\(symbol.id)"
}
var points: Int {
symbol.points
}
static let allCards: Set<Card> = {
let all = Card.Suit.allCases.map { suit in
Card.Symbol.allCases.map { symbol in
Card(suit: suit, symbol: symbol)
}
}.joined()
return Set(all)
}()
}
extension Card {
func isTrump(in game: GameType) -> Bool {
game.sortingType.isTrump(self)
}
}
extension Card: CustomStringConvertible {
var description: String {
"\(suit) \(symbol)"
}
}
extension Card: Hashable {
}