Schafkopf-Server/Sources/App/Model/Card.swift
2021-12-01 22:47:19 +01:00

89 lines
1.7 KiB
Swift

import Foundation
import Vapor
typealias CardId = String
struct Card: Codable {
enum Symbol: Character, CaseIterable, Codable {
case ass = "A"
case zehn = "Z"
case könig = "K"
case ober = "O"
case unter = "U"
case neun = "9"
case acht = "8"
case sieben = "7"
var points: Int {
switch self {
case .ass:
return 11
case .zehn:
return 10
case .könig:
return 4
case .ober:
return 3
case .unter:
return 2
default:
return 0
}
}
}
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?(rawValue: String) {
guard rawValue.count == 2 else {
return nil
}
guard let suit = Suit(rawValue: rawValue.first!),
let symbol = Symbol(rawValue: rawValue.last!) else {
return nil
}
self.suit = suit
self.symbol = symbol
}
var id: CardId {
"\(suit.rawValue)\(symbol.rawValue)"
}
var points: Int {
symbol.points
}
}
extension Card: CustomStringConvertible {
var description: String {
"\(suit) \(symbol)"
}
}
extension Card: Hashable {
}