Schafkopf-Server/Sources/App/Model/Card.swift

89 lines
1.7 KiB
Swift
Raw Normal View History

2021-11-30 11:55:13 +01:00
import Foundation
2021-12-01 22:47:19 +01:00
import Vapor
2021-11-30 11:55:13 +01:00
typealias CardId = String
2021-12-01 22:47:19 +01:00
struct Card: Codable {
2021-11-30 11:55:13 +01:00
2021-12-01 22:47:19 +01:00
enum Symbol: Character, CaseIterable, Codable {
2021-11-30 11:55:13 +01:00
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"
2021-12-01 22:47:19 +01:00
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
}
}
2021-11-30 11:55:13 +01:00
}
2021-12-01 22:47:19 +01:00
enum Suit: Character, CaseIterable, Codable {
2021-11-30 11:55:13 +01:00
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
}
2021-12-01 22:47:19 +01:00
init(_ suit: Suit, _ symbol: Symbol) {
self.suit = suit
self.symbol = symbol
}
2021-11-30 11:55:13 +01:00
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 {
2021-12-01 22:47:19 +01:00
"\(suit.rawValue)\(symbol.rawValue)"
}
var points: Int {
symbol.points
2021-11-30 11:55:13 +01:00
}
}
extension Card: CustomStringConvertible {
var description: String {
2021-12-01 22:47:19 +01:00
"\(suit) \(symbol)"
2021-11-30 11:55:13 +01:00
}
}
2021-12-01 22:47:19 +01:00
extension Card: Hashable {
}