Sync push
This commit is contained in:
@ -1,19 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
struct CardInfo: ClientMessage {
|
||||
|
||||
static let type: ClientMessageType = .cardInfo
|
||||
|
||||
struct HandCard: Codable {
|
||||
|
||||
let card: CardId
|
||||
|
||||
let playable: Bool
|
||||
}
|
||||
|
||||
/// The cards for a player
|
||||
let cards: [HandCard]
|
||||
|
||||
// The cards on the table, as seen from the players perspective
|
||||
let tableCards: [CardId]
|
||||
}
|
30
Sources/App/Infos/PlayerInfo.swift
Normal file
30
Sources/App/Infos/PlayerInfo.swift
Normal file
@ -0,0 +1,30 @@
|
||||
import Foundation
|
||||
|
||||
struct PlayerInfo: Codable, Equatable {
|
||||
|
||||
let name: PlayerName
|
||||
|
||||
let connected: Bool
|
||||
|
||||
/// The player is the next one to perform an action
|
||||
let active: Bool
|
||||
|
||||
/// The cards in the hand of the player
|
||||
let cards: [CardInfo]
|
||||
|
||||
/// The action the player can perform
|
||||
let actions: [String]
|
||||
|
||||
init(player: Player, isMasked: Bool) {
|
||||
self.name = player.name
|
||||
self.connected = player.isConnected
|
||||
self.active = player.isNextActor
|
||||
if isMasked {
|
||||
self.cards = []
|
||||
self.actions = []
|
||||
} else {
|
||||
self.actions = player.actions.map { $0.path }
|
||||
self.cards = player.handCards.map { $0.cardInfo }
|
||||
}
|
||||
}
|
||||
}
|
27
Sources/App/Infos/PublicTableInfo.swift
Normal file
27
Sources/App/Infos/PublicTableInfo.swift
Normal file
@ -0,0 +1,27 @@
|
||||
import Foundation
|
||||
|
||||
struct PublicTableInfo: Codable {
|
||||
|
||||
let id: TableId
|
||||
|
||||
let name: TableName
|
||||
|
||||
let players: [PlayerName]
|
||||
|
||||
let tableIsFull: Bool
|
||||
|
||||
init(id: TableId, name: String, players: [PlayerName]) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.players = players
|
||||
self.tableIsFull = players.count == maximumPlayersPerTable
|
||||
}
|
||||
}
|
||||
|
||||
extension PublicTableInfo: Comparable {
|
||||
|
||||
static func < (lhs: PublicTableInfo, rhs: PublicTableInfo) -> Bool {
|
||||
lhs.name < rhs.name
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +1,34 @@
|
||||
import Foundation
|
||||
|
||||
struct TableInfo: ClientMessage {
|
||||
struct TableInfo: Codable {
|
||||
|
||||
static let type: ClientMessageType = .tableInfo
|
||||
|
||||
let id: String
|
||||
|
||||
let name: String
|
||||
|
||||
let players: [PlayerState]
|
||||
|
||||
let tableIsFull: Bool
|
||||
let player: PlayerInfo
|
||||
|
||||
struct PlayerState: Codable, Equatable {
|
||||
|
||||
let name: PlayerName
|
||||
|
||||
let connected: Bool
|
||||
|
||||
init(name: PlayerName, connected: Bool) {
|
||||
self.name = name
|
||||
self.connected = connected
|
||||
}
|
||||
let playerLeft: PlayerInfo?
|
||||
|
||||
let playerAcross: PlayerInfo?
|
||||
|
||||
let playerRight: PlayerInfo?
|
||||
|
||||
init(_ table: Table, forPlayerAt playerIndex: Int) {
|
||||
self.id = table.id
|
||||
self.name = table.name
|
||||
self.player = table.player(at: playerIndex)!.info(masked: false)
|
||||
self.playerLeft = table.player(leftOf: playerIndex)?.info(masked: true)
|
||||
self.playerAcross = table.player(acrossOf: playerIndex)?.info(masked: true)
|
||||
self.playerRight = table.player(rightOf: playerIndex)?.info(masked: true)
|
||||
}
|
||||
}
|
||||
|
||||
extension TableInfo {
|
||||
|
||||
|
||||
}
|
||||
|
||||
extension TableInfo: Comparable {
|
||||
|
||||
static func < (lhs: TableInfo, rhs: TableInfo) -> Bool {
|
||||
|
@ -1,32 +0,0 @@
|
||||
import Foundation
|
||||
import WebSocketKit
|
||||
|
||||
private let encoder = JSONEncoder()
|
||||
|
||||
enum ClientMessageType: String {
|
||||
|
||||
/// The names and connection states of th player, plus table name and id
|
||||
case tableInfo = "t"
|
||||
|
||||
/// The hand cards of the player and the cards on the table
|
||||
case cardInfo = "c"
|
||||
|
||||
/// The game is in the bidding phase
|
||||
case biddingInfo = "b"
|
||||
|
||||
///
|
||||
}
|
||||
|
||||
protocol ClientMessage: Encodable {
|
||||
|
||||
static var type: ClientMessageType { get }
|
||||
}
|
||||
|
||||
extension WebSocket {
|
||||
|
||||
func send<T>(_ data: T) where T: ClientMessage {
|
||||
let json = try! encoder.encode(data)
|
||||
let string = String(data: json, encoding: .utf8)!
|
||||
self.send(T.type.rawValue + string)
|
||||
}
|
||||
}
|
@ -24,7 +24,7 @@ final class Database {
|
||||
|
||||
func deletePlayer(named name: PlayerName) {
|
||||
_ = players.deletePlayer(named: name)
|
||||
tables.remove(player: name)
|
||||
tables.leaveTable(player: name)
|
||||
}
|
||||
|
||||
func isValid(sessionToken token: SessionToken) -> Bool {
|
||||
@ -63,8 +63,8 @@ final class Database {
|
||||
players.registeredPlayerExists(withSessionToken: token)
|
||||
}
|
||||
|
||||
func currentTableOfPlayer(named player: PlayerName) -> TableId {
|
||||
tables.currentTableOfPlayer(named: player) ?? ""
|
||||
func currentTableOfPlayer(named player: PlayerName) -> TableInfo? {
|
||||
tables.tableInfo(player: player)
|
||||
}
|
||||
|
||||
// MARK: Tables
|
||||
@ -73,20 +73,20 @@ final class Database {
|
||||
Create a new table with optional players.
|
||||
- Parameter name: The name of the table
|
||||
- Parameter players: The player creating the table
|
||||
- Parameter visible: Indicates that this is a game joinable by everyone
|
||||
- Parameter isPublic: Indicates that this is a game joinable by everyone
|
||||
- Returns: The table id
|
||||
*/
|
||||
func createTable(named name: TableName, player: PlayerName, visible: Bool) -> TableId {
|
||||
tables.createTable(named: name, player: player, visible: visible)
|
||||
func createTable(named name: TableName, player: PlayerName, isPublic: Bool) -> TableId {
|
||||
tables.createTable(named: name, player: player, isPublic: isPublic)
|
||||
}
|
||||
|
||||
func getPublicTableInfos() -> [TableInfo] {
|
||||
tables.getPublicTableInfos()
|
||||
func getPublicTableInfos() -> [PublicTableInfo] {
|
||||
tables.publicTableList
|
||||
}
|
||||
|
||||
func join(tableId: TableId, playerToken: SessionToken) -> JoinTableResult {
|
||||
func join(tableId: TableId, playerToken: SessionToken) -> Result<TableInfo,JoinTableResult> {
|
||||
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
|
||||
return .invalidToken
|
||||
return .failure(.invalidToken)
|
||||
}
|
||||
return tables.join(tableId: tableId, player: player)
|
||||
}
|
||||
@ -95,14 +95,14 @@ final class Database {
|
||||
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
|
||||
return false
|
||||
}
|
||||
tables.remove(player: player)
|
||||
tables.leaveTable(player: player)
|
||||
return true
|
||||
}
|
||||
|
||||
func dealCards(playerToken: SessionToken) -> DealCardResult {
|
||||
func performAction(playerToken: SessionToken, action: Player.Action) -> PlayerActionResult {
|
||||
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
|
||||
return .invalidToken
|
||||
}
|
||||
return tables.dealCards(player: player)
|
||||
return tables.performAction(player: player, action: action)
|
||||
}
|
||||
}
|
||||
|
@ -40,19 +40,23 @@ extension DiskWriter {
|
||||
}
|
||||
}
|
||||
|
||||
func readLinesFromDisk() throws -> [String] {
|
||||
func readDataFromDisk() throws -> Data {
|
||||
if #available(macOS 10.15.4, *) {
|
||||
guard let data = try storageFile.readToEnd() else {
|
||||
try storageFile.seekToEnd()
|
||||
return []
|
||||
return Data()
|
||||
}
|
||||
return parseLines(data: data)
|
||||
return data
|
||||
} else {
|
||||
let data = storageFile.readDataToEndOfFile()
|
||||
return parseLines(data: data)
|
||||
return storageFile.readDataToEndOfFile()
|
||||
}
|
||||
}
|
||||
|
||||
func readLinesFromDisk() throws -> [String] {
|
||||
let data = try readDataFromDisk()
|
||||
return parseLines(data: data)
|
||||
}
|
||||
|
||||
private func parseLines(data: Data) -> [String] {
|
||||
String(data: data, encoding: .utf8)!
|
||||
.components(separatedBy: "\n")
|
||||
|
@ -9,34 +9,27 @@ typealias TableName = String
|
||||
|
||||
final class TableManagement: DiskWriter {
|
||||
|
||||
/// A list of table ids for public games
|
||||
private var publicTables = Set<TableId>()
|
||||
|
||||
/// A mapping from table id to table name (for all tables)
|
||||
private var tableNames = [TableId: TableName]()
|
||||
|
||||
/// A mapping from table id to participating players
|
||||
private var tablePlayers = [TableId: [PlayerName]]()
|
||||
|
||||
/// A reverse list of players and their table id
|
||||
private var playerTables = [PlayerName: TableId]()
|
||||
|
||||
private var playerConnections = [PlayerName : WebSocket]()
|
||||
|
||||
private var tablePhase = [TableId: GamePhase]()
|
||||
|
||||
/// All tables indexed by their id
|
||||
private var tables = [TableId : Table]()
|
||||
|
||||
/// The handle to the file where the tables are persisted
|
||||
let storageFile: FileHandle
|
||||
|
||||
/// The url to the file where the tables are persisted
|
||||
let storageFileUrl: URL
|
||||
|
||||
/**
|
||||
Load the tables from a file in the storage folder
|
||||
- Parameter storageFolder: The url to the folder where the table file is stored
|
||||
- Throws: Errors when the file could not be read
|
||||
*/
|
||||
init(storageFolder: URL) throws {
|
||||
let url = storageFolder.appendingPathComponent("tables.txt")
|
||||
|
||||
storageFileUrl = url
|
||||
storageFile = try Self.prepareFile(at: url)
|
||||
|
||||
var entries = [TableId : (name: TableName, public: Bool, players: [PlayerName])]()
|
||||
var entries = [TableId : (name: TableName, isPublic: Bool, players: [PlayerName])]()
|
||||
try readLinesFromDisk().forEach { line in
|
||||
// Each line has parts: ID | NAME | PLAYER, PLAYER, ...
|
||||
let parts = line.components(separatedBy: ":")
|
||||
@ -54,31 +47,38 @@ final class TableManagement: DiskWriter {
|
||||
entries[id] = (name, isPublic, players)
|
||||
}
|
||||
}
|
||||
entries.forEach { id, table in
|
||||
tableNames[id] = table.name
|
||||
if table.public {
|
||||
publicTables.insert(id)
|
||||
}
|
||||
tablePlayers[id] = table.players
|
||||
tablePhase[id] = .waitingForPlayers
|
||||
for player in table.players {
|
||||
playerTables[player] = id
|
||||
}
|
||||
entries.forEach { id, tableData in
|
||||
let table = Table(id: id, name: tableData.name, isPublic: tableData.isPublic)
|
||||
tableData.players.forEach { _ = table.add(player: $0) }
|
||||
tables[id] = table
|
||||
}
|
||||
print("Loaded \(tableNames.count) tables")
|
||||
print("Loaded \(tables.count) tables")
|
||||
}
|
||||
|
||||
/**
|
||||
Writes the table info to disk.
|
||||
|
||||
Currently only the id, name, visibility and players are stored, all other information is lost.
|
||||
- Parameter table: The changed table information to persist
|
||||
- Returns: `true`, if the entry was written, `false` on error
|
||||
*/
|
||||
@discardableResult
|
||||
private func save(table tableId: TableId) -> Bool {
|
||||
let name = tableNames[tableId]!
|
||||
let visible = publicTables.contains(tableId) ? "public" : "private"
|
||||
let players = tablePlayers[tableId]!
|
||||
let entry = [tableId, name, visible, players.joined(separator: ",")].joined(separator: ":")
|
||||
private func writeTableToDisk(table: Table) -> Bool {
|
||||
let visible = table.isPublic ? "public" : "private"
|
||||
let players = table.playerNames.joined(separator: ",")
|
||||
let entry = [table.id, table.name, visible, players].joined(separator: ":")
|
||||
return writeToDisk(line: entry)
|
||||
}
|
||||
|
||||
/**
|
||||
Writes the deletion of a table to disk.
|
||||
|
||||
The deletion is written as a separate entry and appended to the file, in order to reduce disk I/O.
|
||||
- Parameter tableId: The id of the deleted table
|
||||
- Returns: `true`, if the entry was written, `false` on error
|
||||
*/
|
||||
@discardableResult
|
||||
private func deleteTable(tableId: TableId) -> Bool {
|
||||
private func writeTableDeletionEntry(tableId: TableId) -> Bool {
|
||||
let entry = [tableId, "", "", ""].joined(separator: ":")
|
||||
return writeToDisk(line: entry)
|
||||
}
|
||||
@ -87,166 +87,88 @@ final class TableManagement: DiskWriter {
|
||||
Create a new table with optional players.
|
||||
- Parameter name: The name of the table
|
||||
- Parameter players: The player creating the table
|
||||
- Parameter visible: Indicates that this is a game joinable by everyone
|
||||
- Parameter isPublic: Indicates that this is a game joinable by everyone
|
||||
- Returns: The table id
|
||||
*/
|
||||
func createTable(named name: TableName, player: PlayerName, visible: Bool) -> TableId {
|
||||
let tableId = TableId.newToken()
|
||||
|
||||
tableNames[tableId] = name
|
||||
tablePlayers[tableId] = [player]
|
||||
playerTables[player] = tableId
|
||||
|
||||
if visible {
|
||||
publicTables.insert(tableId)
|
||||
}
|
||||
save(table: tableId)
|
||||
return tableId
|
||||
func createTable(named name: TableName, player: PlayerName, isPublic: Bool) -> TableId {
|
||||
let table = Table(newTable: name, isPublic: isPublic)
|
||||
_ = table.add(player: name)
|
||||
tables[table.id] = table
|
||||
writeTableToDisk(table: table)
|
||||
return table.id
|
||||
}
|
||||
|
||||
func getPublicTableInfos() -> [TableInfo] {
|
||||
publicTables.map(tableInfo).sorted()
|
||||
/// A list of all public tables
|
||||
var publicTableList: [PublicTableInfo] {
|
||||
tables.values.filter { $0.isPublic }.map { $0.publicInfo }
|
||||
}
|
||||
|
||||
private func tableInfo(id tableId: TableId) -> TableInfo {
|
||||
let players = tablePlayers[tableId]!.map(playerState)
|
||||
return TableInfo(
|
||||
id: tableId,
|
||||
name: tableNames[tableId]!,
|
||||
players: players,
|
||||
tableIsFull: players.count == maximumPlayersPerTable)
|
||||
/**
|
||||
Get the table info for a player
|
||||
- Parameter player: The name of the player
|
||||
- Returns: The table info, if the player has joined a table
|
||||
*/
|
||||
func tableInfo(player: PlayerName) -> TableInfo? {
|
||||
currentTable(for: player)?.compileInfo(for: player)
|
||||
}
|
||||
|
||||
private func playerState(_ player: PlayerName) -> TableInfo.PlayerState {
|
||||
.init(name: player, connected: playerIsConnected(player))
|
||||
}
|
||||
|
||||
private func playerIsConnected(_ player: PlayerName) -> Bool {
|
||||
playerConnections[player] != nil
|
||||
}
|
||||
|
||||
func currentTableOfPlayer(named player: PlayerName) -> TableId? {
|
||||
playerTables[player]
|
||||
private func currentTable(for player: PlayerName) -> Table? {
|
||||
tables.values.first(where: { $0.contains(player: player) })
|
||||
}
|
||||
|
||||
/**
|
||||
Join a table.
|
||||
- Parameter tableId: The table to join
|
||||
- Parameter player: The name of the player who wants to join.
|
||||
- Returns: The result of the join operation
|
||||
*/
|
||||
func join(tableId: TableId, player: PlayerName) -> JoinTableResult {
|
||||
guard var players = tablePlayers[tableId] else {
|
||||
return .tableNotFound
|
||||
func join(tableId: TableId, player: PlayerName) -> Result<TableInfo, JoinTableResult> {
|
||||
if let existing = currentTable(for: player) {
|
||||
guard existing.id == tableId else {
|
||||
return .failure(.alreadyJoinedOtherTable)
|
||||
}
|
||||
return .success(existing.compileInfo(for: player)!)
|
||||
}
|
||||
guard !players.contains(player) else {
|
||||
return .success
|
||||
guard let table = tables[tableId] else {
|
||||
return .failure(.tableNotFound)
|
||||
}
|
||||
guard players.count < maximumPlayersPerTable else {
|
||||
return .tableIsFull
|
||||
guard table.add(player: player) else {
|
||||
return .failure(.tableIsFull)
|
||||
}
|
||||
players.append(player)
|
||||
if let oldTable = playerTables[tableId] {
|
||||
remove(player: player, fromTable: oldTable)
|
||||
}
|
||||
tablePlayers[tableId] = players
|
||||
playerTables[player] = tableId
|
||||
save(table: tableId)
|
||||
return .success
|
||||
writeTableToDisk(table: table)
|
||||
return .success(table.compileInfo(for: player)!)
|
||||
}
|
||||
|
||||
func remove(player: PlayerName, fromTable tableId: TableId) {
|
||||
tablePlayers[tableId] = tablePlayers[tableId]?.filter { $0 != player }
|
||||
disconnect(player: player)
|
||||
playerTables[player] = nil
|
||||
// TODO: End game if needed
|
||||
// TODO: Remove table if empty
|
||||
save(table: tableId)
|
||||
}
|
||||
|
||||
func remove(player: PlayerName) {
|
||||
guard let tableId = playerTables[player] else {
|
||||
/**
|
||||
A player leaves the table it previously joined
|
||||
- Parameter player: The name of the player
|
||||
*/
|
||||
func leaveTable(player: PlayerName) {
|
||||
guard let table = currentTable(for: player) else {
|
||||
return
|
||||
}
|
||||
// Already saves table to disk
|
||||
remove(player: player, fromTable: tableId)
|
||||
table.remove(player: player)
|
||||
writeTableToDisk(table: table)
|
||||
}
|
||||
|
||||
func connect(player: PlayerName, using socket: WebSocket) -> Bool {
|
||||
guard let tableId = playerTables[player] else {
|
||||
guard let table = currentTable(for: player) else {
|
||||
return false
|
||||
}
|
||||
guard let players = tablePlayers[tableId] else {
|
||||
print("Player \(player) was assigned to missing table \(tableId.prefix(5))")
|
||||
playerTables[player] = nil
|
||||
return false
|
||||
}
|
||||
guard players.contains(player) else {
|
||||
print("Player \(player) wants updates for table \(tableId.prefix(5)) it didn't join")
|
||||
return false
|
||||
}
|
||||
playerConnections[player] = socket
|
||||
sendTableInfo(toTable: tableId)
|
||||
// TODO: Send cards to player
|
||||
return true
|
||||
return table.connect(player: player, using: socket)
|
||||
}
|
||||
|
||||
func disconnect(player: PlayerName) {
|
||||
if let socket = playerConnections.removeValue(forKey: player) {
|
||||
if !socket.isClosed {
|
||||
_ = socket.close()
|
||||
}
|
||||
}
|
||||
guard let tableId = playerTables[player] else {
|
||||
guard let table = currentTable(for: player) else {
|
||||
return
|
||||
}
|
||||
sendTableInfo(toTable: tableId)
|
||||
// Change table phase to waiting
|
||||
table.disconnect(player: player)
|
||||
}
|
||||
|
||||
private func sendTableInfo(toTable tableId: TableId) {
|
||||
let name = tableNames[tableId]!
|
||||
var players = tablePlayers[tableId]!
|
||||
let isFull = players.count == maximumPlayersPerTable
|
||||
for _ in players.count..<maximumPlayersPerTable {
|
||||
players.append("")
|
||||
}
|
||||
let states = players.map(playerState)
|
||||
players.enumerated().forEach { index, player in
|
||||
guard let socket = playerConnections[player] else {
|
||||
return
|
||||
}
|
||||
let info = TableInfo(
|
||||
id: tableId,
|
||||
name: name,
|
||||
players: states.rotated(toStartAt: index),
|
||||
tableIsFull: isFull)
|
||||
socket.send(info)
|
||||
}
|
||||
}
|
||||
|
||||
func dealCards(player: PlayerName) -> DealCardResult {
|
||||
guard let tableId = playerTables[player] else {
|
||||
func performAction(player: PlayerName, action: Player.Action) -> PlayerActionResult {
|
||||
guard let table = currentTable(for: player) else {
|
||||
return .noTableJoined
|
||||
}
|
||||
guard let players = tablePlayers[tableId] else {
|
||||
playerTables[player] = nil
|
||||
print("Player \(player) assigned to missing table \(tableId.prefix(5))")
|
||||
return .noTableJoined
|
||||
}
|
||||
guard players.count == maximumPlayersPerTable else {
|
||||
return .tableNotFull
|
||||
}
|
||||
|
||||
let cards = Dealer.deal()
|
||||
let handCards = ["", "", "", ""]
|
||||
players.enumerated().forEach { index, player in
|
||||
guard let socket = playerConnections[player] else {
|
||||
return
|
||||
}
|
||||
let info = CardInfo(
|
||||
cards: cards[index].map { .init(card: $0.id, playable: false) },
|
||||
tableCards: handCards.rotated(toStartAt: index))
|
||||
socket.send(info)
|
||||
}
|
||||
return .success
|
||||
return table.perform(action: action, forPlayer: player)
|
||||
}
|
||||
}
|
||||
|
@ -73,6 +73,15 @@ struct Card: Codable {
|
||||
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: CustomStringConvertible {
|
||||
@ -86,3 +95,20 @@ extension Card: Hashable {
|
||||
|
||||
}
|
||||
|
||||
struct PlayableCard {
|
||||
|
||||
let card: Card
|
||||
|
||||
let isPlayable: Bool
|
||||
|
||||
var cardInfo: CardInfo {
|
||||
.init(card: card.id, playable: isPlayable)
|
||||
}
|
||||
}
|
||||
|
||||
struct CardInfo: Codable, Equatable {
|
||||
|
||||
let card: CardId
|
||||
|
||||
let playable: Bool
|
||||
}
|
||||
|
49
Sources/App/Model/CardOrders/CardOrder.swift
Normal file
49
Sources/App/Model/CardOrders/CardOrder.swift
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by iMac on 03.12.21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum CardOrderType {
|
||||
|
||||
/// The sorting for most games, where heart is trump
|
||||
case normal
|
||||
|
||||
case wenz
|
||||
|
||||
case geier
|
||||
|
||||
case soloEichel
|
||||
|
||||
case soloBlatt
|
||||
|
||||
case soloSchelln
|
||||
}
|
||||
|
||||
protocol CardOrder {
|
||||
|
||||
static var trumpOrder: [Card] { get }
|
||||
|
||||
static var sortIndex: [Card : Int] { get }
|
||||
}
|
||||
|
||||
extension CardOrder {
|
||||
|
||||
static func consecutiveTrumps(_ cards: [Card]) -> Int {
|
||||
var count = 0
|
||||
while cards.contains(trumpOrder[count]) {
|
||||
count += 1
|
||||
}
|
||||
guard count >= 3 else {
|
||||
return 0
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
static func sort(_ cards: [Card]) -> [Card] {
|
||||
cards.sorted { sortIndex[$0]! < sortIndex[$1]! }
|
||||
}
|
||||
}
|
58
Sources/App/Model/CardOrders/NormalCardOrder.swift
Normal file
58
Sources/App/Model/CardOrders/NormalCardOrder.swift
Normal file
@ -0,0 +1,58 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by iMac on 03.12.21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct NormalCardOrder: CardOrder {
|
||||
|
||||
private static let cardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private static let sortIndex: [Card : Int] = {
|
||||
cardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
static let trumpOrder: [Card] = Array(cardOrder[0..<8])
|
||||
|
||||
private static let trumps: Set<Card> = Set(trumpOrder)
|
||||
|
||||
static func trumpCount(_ cards: [Card]) -> Int {
|
||||
cards.filter { trumps.contains(card) }.count
|
||||
}
|
||||
}
|
312
Sources/App/Model/Dealer.swift
Normal file
312
Sources/App/Model/Dealer.swift
Normal file
@ -0,0 +1,312 @@
|
||||
import Foundation
|
||||
|
||||
private let wenzCardOder = [
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .ober),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .ober),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .ober),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .ober),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private let geierCardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .unter),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .unter),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .unter),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .unter),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private let eichelCardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private let blattCardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private let schellnCardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
]
|
||||
|
||||
private let wenzSortIndex: [Card : Int] = {
|
||||
wenzCardOder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private let geierSortIndex: [Card : Int] = {
|
||||
geierCardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private let eichelSortIndex: [Card : Int] = {
|
||||
eichelCardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private let blattSortIndex: [Card : Int] = {
|
||||
blattCardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private let schellnSortIndex: [Card : Int] = {
|
||||
schellnCardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private let wenzTrumps: [Card] = wenzCardOder[0..<4]
|
||||
|
||||
private let geierTrumps: [Card] = geierCardOrder[0..<4]
|
||||
|
||||
private let eichelTrumps: [Card] = eichelCardOrder[0..<8]
|
||||
|
||||
private let blattTrumps: [Card] = blattCardOrder[0..<8]
|
||||
|
||||
private let schellnTrumps: [Card] = schellnCardOrder[0..<8]
|
||||
|
||||
extension Card {
|
||||
|
||||
func isTrump(in game: GameType) -> Bool {
|
||||
switch game.sortingType {
|
||||
case .normal:
|
||||
trumpsInOrder = normalCardOrder[0..<8]
|
||||
case .wenz:
|
||||
trumpsInOrder = wenzCardOder[0..<4]
|
||||
case .geier:
|
||||
trumpsInOrder = geierCardOrder[0..<4]
|
||||
case .soloEichel:
|
||||
trumpsInOrder = eichelCardOrder[0..<8]
|
||||
case .soloBlatt:
|
||||
trumpsInOrder = blattCardOrder[0..<8]
|
||||
case .soloSchelln:
|
||||
trumpsInOrder = schellnCardOrder[0..<8]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Array where Element == Card {
|
||||
|
||||
func sorted(cardOrder order: CardOrder) -> [Card] {
|
||||
switch order {
|
||||
case .normal:
|
||||
return sorted { normalSortIndex[$0]! < normalSortIndex[$1]! }
|
||||
case .wenz:
|
||||
return sorted { wenzSortIndex[$0]! < wenzSortIndex[$1]! }
|
||||
case .geier:
|
||||
return sorted { geierSortIndex[$0]! < geierSortIndex[$1]! }
|
||||
case .soloEichel:
|
||||
return sorted { eichelSortIndex[$0]! < eichelSortIndex[$1]! }
|
||||
case .soloBlatt:
|
||||
return sorted { blattSortIndex[$0]! < blattSortIndex[$1]! }
|
||||
case .soloSchelln:
|
||||
return sorted { schellnSortIndex[$0]! < schellnSortIndex[$1]! }
|
||||
}
|
||||
}
|
||||
|
||||
func consecutiveTrumps(for game: GameType) -> Int {
|
||||
var count = 0
|
||||
let trumpsInOrder: Array<Card>.SubSequence
|
||||
switch game.sortingType {
|
||||
case .normal:
|
||||
trumpsInOrder = normalCardOrder[0..<8]
|
||||
case .wenz:
|
||||
trumpsInOrder = wenzCardOder[0..<4]
|
||||
case .geier:
|
||||
trumpsInOrder = geierCardOrder[0..<4]
|
||||
case .soloEichel:
|
||||
trumpsInOrder = eichelCardOrder[0..<8]
|
||||
case .soloBlatt:
|
||||
trumpsInOrder = blattCardOrder[0..<8]
|
||||
case .soloSchelln:
|
||||
trumpsInOrder = schellnCardOrder[0..<8]
|
||||
}
|
||||
while contains(trumpsInOrder[count]) {
|
||||
count += 1
|
||||
}
|
||||
guard count >= 3 else {
|
||||
return 0
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func trumpCount(for game: GameType) -> Int {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
Split cards into chunks to assign them to players.
|
||||
- Note: The array must contain a multiple of the `size` parameter
|
||||
*/
|
||||
func split(intoChunksOf size: Int) -> [Hand] {
|
||||
stride(from: 0, to: count, by: size).map { i in
|
||||
Array(self[i..<i+4])
|
||||
}
|
||||
}
|
||||
|
||||
var canOfferWedding: Bool {
|
||||
NormalCardOrder.trumpCount(self) == 1
|
||||
}
|
||||
}
|
||||
|
||||
struct Dealer {
|
||||
|
||||
/**
|
||||
Creates a random assignment of 4 cards per 4 players for the initial round of doubling.
|
||||
*/
|
||||
static func dealFirstCards() -> [Hand] {
|
||||
// Select 16 random cards for the first hands
|
||||
Array(Card.allCards.shuffled()[0..<16])
|
||||
.split(intoChunksOf: 4)
|
||||
.map { $0.sorted(cardOrder: .normal) }
|
||||
}
|
||||
|
||||
|
||||
static func dealRemainingCards(of initial: [Hand]) -> [Hand] {
|
||||
Card.allCards
|
||||
.subtracting(initial.reduce([], +))
|
||||
.shuffled()
|
||||
.split(intoChunksOf: 4)
|
||||
}
|
||||
|
||||
}
|
@ -32,9 +32,8 @@ struct Game: Codable {
|
||||
self.numberOfDoubles = doubles
|
||||
self.cards = cards
|
||||
self.leaders = leaders
|
||||
self.consecutiveTrumps = Dealer.consecutiveTrumps(
|
||||
in: leaders.map { cards[$0] }.joined(),
|
||||
for: type)
|
||||
self.consecutiveTrumps = Array(leaders.map { cards[$0] }.joined())
|
||||
.consecutiveTrumps(for: type)
|
||||
self.currentActor = starter
|
||||
self.lastTrickWinner = starter
|
||||
self.completedTricks = []
|
||||
|
@ -2,6 +2,23 @@ import Foundation
|
||||
|
||||
enum GameType: Codable {
|
||||
|
||||
enum GameClass: Int {
|
||||
case ruf = 1
|
||||
case hochzeit = 2
|
||||
case bettel = 3
|
||||
case wenzGeier = 4
|
||||
case solo = 5
|
||||
|
||||
var cost: Int {
|
||||
switch self {
|
||||
case .ruf: return 5
|
||||
case .hochzeit: return 10
|
||||
case .bettel: return 15
|
||||
case .wenzGeier, .solo: return 20
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case rufEichel
|
||||
case rufBlatt
|
||||
case rufSchelln
|
||||
@ -14,18 +31,18 @@ enum GameType: Codable {
|
||||
case soloHerz
|
||||
case soloSchelln
|
||||
|
||||
var gameClass: Int {
|
||||
var gameClass: GameClass {
|
||||
switch self {
|
||||
case .rufEichel, .rufBlatt, .rufSchelln:
|
||||
return 1
|
||||
return .ruf
|
||||
case .hochzeit:
|
||||
return 2
|
||||
return .hochzeit
|
||||
case .bettel:
|
||||
return 3
|
||||
return .bettel
|
||||
case .wenz, .geier:
|
||||
return 4
|
||||
return .wenzGeier
|
||||
case .soloEichel, .soloBlatt, .soloHerz, .soloSchelln:
|
||||
return 5
|
||||
return .solo
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,21 +56,10 @@ enum GameType: Codable {
|
||||
}
|
||||
|
||||
var basicCost: Int {
|
||||
switch self {
|
||||
case .rufEichel, .rufBlatt, .rufSchelln:
|
||||
return 5
|
||||
case .hochzeit:
|
||||
return 10
|
||||
case .bettel:
|
||||
return 15
|
||||
case .wenz, .geier:
|
||||
return 20
|
||||
case .soloEichel, .soloBlatt, .soloHerz, .soloSchelln:
|
||||
return 20
|
||||
}
|
||||
gameClass.cost
|
||||
}
|
||||
|
||||
var sortingType: CardSortingStrategy {
|
||||
var sortingType: CardOrderType {
|
||||
switch self {
|
||||
case .wenz:
|
||||
return .wenz
|
||||
@ -70,19 +76,3 @@ enum GameType: Codable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum CardSortingStrategy {
|
||||
|
||||
/// The sorting for most games, where heart is trump
|
||||
case normal
|
||||
|
||||
case wenz
|
||||
|
||||
case geier
|
||||
|
||||
case soloEichel
|
||||
|
||||
case soloBlatt
|
||||
|
||||
case soloSchelln
|
||||
}
|
195
Sources/App/Model/Player.swift
Normal file
195
Sources/App/Model/Player.swift
Normal file
@ -0,0 +1,195 @@
|
||||
import Foundation
|
||||
import WebSocketKit
|
||||
import CloudKit
|
||||
|
||||
private let encoder = JSONEncoder()
|
||||
|
||||
final class Player {
|
||||
|
||||
let name: PlayerName
|
||||
|
||||
/// The player is the first to play a card in a new game
|
||||
var playsFirstCard = false
|
||||
|
||||
/// The player is the next to perform an action (e.g. play a card)
|
||||
var isNextActor = false
|
||||
|
||||
/// The player must select the game to play after winning the auction
|
||||
var selectsGame = false
|
||||
|
||||
/// The players plays/played the first card for the current trick
|
||||
var startedCurrentTrick = false
|
||||
|
||||
/// The action available to the player
|
||||
var actions: [Action] = []
|
||||
|
||||
/// Indicates if the player doubled ("legen")
|
||||
var didDoubleAfterFourCards: Bool? = nil
|
||||
|
||||
/// Indicates if the player is still involved in the bidding process
|
||||
var isStillBidding = true
|
||||
|
||||
/// Indicates that the player leads the game ("Spieler")
|
||||
var isGameLeader = false
|
||||
|
||||
/// Indicates the number of raises ("Schuss") of the player
|
||||
var numberOfRaises = 0
|
||||
|
||||
/// The remaining cards of the player
|
||||
var handCards: [PlayableCard] = []
|
||||
|
||||
/// The card played for the current trick
|
||||
var playedCard: Card? = nil
|
||||
|
||||
/// All tricks won by the player in this game
|
||||
var wonTricks: [Trick] = []
|
||||
|
||||
var socket: WebSocket? = nil
|
||||
|
||||
init(name: PlayerName) {
|
||||
self.name = name
|
||||
}
|
||||
|
||||
var rawCards: [Card] {
|
||||
handCards.map { $0.card }
|
||||
}
|
||||
|
||||
func connect(using socket: WebSocket) {
|
||||
_ = self.socket?.close()
|
||||
self.socket = socket
|
||||
}
|
||||
|
||||
func send(_ info: TableInfo) {
|
||||
try? socket?.send(encodeJSON(info))
|
||||
}
|
||||
|
||||
func disconnect() -> Bool {
|
||||
guard let socket = socket else {
|
||||
return false
|
||||
}
|
||||
do {
|
||||
try socket.close().wait()
|
||||
} catch {
|
||||
print("Failed to close socket for player: \(name): \(error)")
|
||||
}
|
||||
self.socket = nil
|
||||
return true
|
||||
}
|
||||
|
||||
func prepareForFirstGame(isFirstPlayer: Bool) {
|
||||
playsFirstCard = isFirstPlayer
|
||||
isNextActor = isFirstPlayer
|
||||
selectsGame = false // Not relevant in this phase
|
||||
startedCurrentTrick = isFirstPlayer
|
||||
actions = [.deal]
|
||||
didDoubleAfterFourCards = nil // Not relevant in this phase
|
||||
isStillBidding = false // Not relevant in this phase
|
||||
isGameLeader = false // Not relevant in this phase
|
||||
numberOfRaises = 0 // Not relevant in this phase
|
||||
handCards = []
|
||||
playedCard = nil
|
||||
wonTricks = []
|
||||
}
|
||||
|
||||
func assignFirstCards(_ cards: Hand) {
|
||||
selectsGame = false // Not relevant in this phase
|
||||
actions = [.initialDoubleCost, .noDoubleCost]
|
||||
didDoubleAfterFourCards = nil
|
||||
isStillBidding = false // Not relevant in this phase
|
||||
isGameLeader = false // Not relevant in this phase
|
||||
numberOfRaises = 0 // Not relevant in this phase
|
||||
handCards = cards.map { .init(card: $0, isPlayable: false) }
|
||||
playedCard = nil
|
||||
wonTricks = []
|
||||
}
|
||||
|
||||
func didDouble(_ double: Bool) {
|
||||
selectsGame = false // Not relevant in this phase
|
||||
actions = []
|
||||
didDoubleAfterFourCards = double
|
||||
isStillBidding = false // Not relevant in this phase
|
||||
isGameLeader = false // Not relevant in this phase
|
||||
numberOfRaises = 0 // Not relevant in this phase
|
||||
playedCard = nil
|
||||
wonTricks = []
|
||||
}
|
||||
|
||||
func assignRemainingCards(_ cards: Hand) {
|
||||
isStillBidding = true
|
||||
isGameLeader = false
|
||||
numberOfRaises = 0
|
||||
handCards = (rawCards + cards)
|
||||
.sorted(CardOrderType: .normal)
|
||||
.map { .init(card: $0, isPlayable: false) }
|
||||
playedCard = nil
|
||||
wonTricks = []
|
||||
}
|
||||
|
||||
func startAuction() {
|
||||
selectsGame = false // Not relevant in this phase
|
||||
if playsFirstCard {
|
||||
actions = [.withdrawFromAuction, .increaseOrMatchGame]
|
||||
} else {
|
||||
|
||||
}
|
||||
actions = []
|
||||
isStillBidding = true
|
||||
isGameLeader = false // Not relevant in this phase
|
||||
numberOfRaises = 0 // Not relevant in this phase
|
||||
playedCard = nil
|
||||
wonTricks = []
|
||||
}
|
||||
|
||||
func info(masked: Bool) -> PlayerInfo {
|
||||
.init(player: self, isMasked: masked)
|
||||
}
|
||||
}
|
||||
|
||||
extension Player {
|
||||
|
||||
/// Indicate that the player is connected when at a table
|
||||
var isConnected: Bool {
|
||||
guard let socket = socket else {
|
||||
return false
|
||||
}
|
||||
guard !socket.isClosed else {
|
||||
self.socket = nil
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
extension Player {
|
||||
|
||||
enum Action: String, Codable {
|
||||
/// The player can request cards to be dealt
|
||||
case deal = "deal"
|
||||
|
||||
/// The player doubles on the initial four cards
|
||||
case initialDoubleCost = "double"
|
||||
|
||||
/// The player does not double on the initial four cards
|
||||
case noDoubleCost = "skip"
|
||||
|
||||
/// The player offers a wedding (one trump card)
|
||||
case offerWedding = "wedding"
|
||||
|
||||
/// The player can choose to accept the wedding
|
||||
case acceptWedding = "accept"
|
||||
|
||||
/// The player matches or increases the game during auction
|
||||
case increaseOrMatchGame = "bid"
|
||||
|
||||
/// The player does not want to play
|
||||
case withdrawFromAuction = "out"
|
||||
|
||||
/// The player claims to win and doubles the game cost ("schießen")
|
||||
case doubleDuringGame = "raise"
|
||||
|
||||
/// The url path for the client to call (e.g. /player/deal)
|
||||
var path: String {
|
||||
rawValue
|
||||
}
|
||||
}
|
||||
}
|
233
Sources/App/Model/Table.swift
Normal file
233
Sources/App/Model/Table.swift
Normal file
@ -0,0 +1,233 @@
|
||||
import Foundation
|
||||
import WebSocketKit
|
||||
|
||||
private extension Int {
|
||||
|
||||
mutating func advanceInTable() {
|
||||
self = (self + 1) % maximumPlayersPerTable
|
||||
}
|
||||
}
|
||||
|
||||
final class Table {
|
||||
|
||||
let id: TableId
|
||||
|
||||
let name: TableName
|
||||
|
||||
let isPublic: Bool
|
||||
|
||||
var players: [Player] = []
|
||||
|
||||
var phase: GamePhase = .waitingForPlayers
|
||||
|
||||
var gameType: GameType? = nil
|
||||
|
||||
var minimumPlayableGame: GameType.GameClass = .ruf
|
||||
|
||||
/// Indicates if doubles are still allowed
|
||||
var canDoubleDuringGame = false
|
||||
|
||||
/// Indicates if any player doubled during the current round, extending it to the next round
|
||||
var didDoubleInCurrentRound = false
|
||||
|
||||
/// Indicates that all players acted after the first four cards
|
||||
var allPlayersFinishedDoubling: Bool {
|
||||
!players.contains { $0.didDoubleAfterFourCards == nil }
|
||||
}
|
||||
|
||||
init(id: TableId, name: TableName, isPublic: Bool) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.isPublic = isPublic
|
||||
}
|
||||
|
||||
init(newTable name: TableName, isPublic: Bool) {
|
||||
self.id = .newToken()
|
||||
self.name = name
|
||||
self.isPublic = isPublic
|
||||
}
|
||||
|
||||
func add(player: PlayerName) -> Bool {
|
||||
guard !isFull else {
|
||||
return false
|
||||
}
|
||||
let player = Player(name: player)
|
||||
players.append(player)
|
||||
if isFull {
|
||||
prepareTableForFirstGame()
|
||||
}
|
||||
sendUpdateToAllPlayers()
|
||||
return true
|
||||
}
|
||||
|
||||
func contains(player: PlayerName) -> Bool {
|
||||
players.contains { $0.name == player }
|
||||
}
|
||||
|
||||
func select(player: PlayerName) -> Player? {
|
||||
players.first { $0.name == player }
|
||||
}
|
||||
|
||||
func player(leftOf index: Int) -> Player? {
|
||||
player(at: (index + 1) % 4)
|
||||
}
|
||||
|
||||
func player(acrossOf index: Int) -> Player? {
|
||||
player(at: (index + 2) % 4)
|
||||
}
|
||||
|
||||
func player(rightOf index: Int) -> Player? {
|
||||
player(at: (index + 3) % 4)
|
||||
}
|
||||
|
||||
func player(at index: Int) -> Player? {
|
||||
guard index < players.count else {
|
||||
return nil
|
||||
}
|
||||
return players[index]
|
||||
}
|
||||
|
||||
func remove(player: PlayerName) {
|
||||
guard contains(player: player) else {
|
||||
return
|
||||
}
|
||||
players = players.filter { $0.name != player }
|
||||
reset()
|
||||
}
|
||||
|
||||
func connect(player name: PlayerName, using socket: WebSocket) -> Bool {
|
||||
guard let player = select(player: name) else {
|
||||
return false
|
||||
}
|
||||
player.connect(using: socket)
|
||||
sendUpdateToAllPlayers()
|
||||
return true
|
||||
}
|
||||
|
||||
func disconnect(player name: PlayerName) {
|
||||
guard let player = select(player: name) else {
|
||||
return
|
||||
}
|
||||
guard player.disconnect() else {
|
||||
return
|
||||
}
|
||||
sendUpdateToAllPlayers()
|
||||
return
|
||||
}
|
||||
|
||||
private func prepareTableForFirstGame() {
|
||||
self.phase = .waitingForPlayers
|
||||
self.gameType = nil
|
||||
self.minimumPlayableGame = .ruf // Not relevant in this phase
|
||||
self.canDoubleDuringGame = true // Not relevant in this phase
|
||||
self.didDoubleInCurrentRound = false // Not relevant in this phase
|
||||
let index = players.firstIndex { $0.playsFirstCard } ?? 0
|
||||
for i in 0..<maximumPlayersPerTable {
|
||||
players[i].prepareForFirstGame(isFirstPlayer: i == index)
|
||||
}
|
||||
}
|
||||
|
||||
private func sendUpdateToAllPlayers() {
|
||||
players.enumerated().forEach { playerIndex, player in
|
||||
guard player.isConnected else {
|
||||
return
|
||||
}
|
||||
let info = TableInfo(self, forPlayerAt: playerIndex)
|
||||
player.send(info)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Player actions
|
||||
|
||||
func perform(action: Player.Action, forPlayer player: PlayerName) -> PlayerActionResult {
|
||||
defer { sendUpdateToAllPlayers() }
|
||||
switch action {
|
||||
case .deal:
|
||||
return dealInitialCards()
|
||||
case .initialDoubleCost:
|
||||
return perform(double: true, forPlayer: player)
|
||||
case .noDoubleCost:
|
||||
return perform(double: false, forPlayer: player)
|
||||
case .offerWedding:
|
||||
fatalError()
|
||||
case .acceptWedding:
|
||||
fatalError()
|
||||
case .increaseOrMatchGame:
|
||||
fatalError()
|
||||
case .withdrawFromAuction:
|
||||
fatalError()
|
||||
case .doubleDuringGame:
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
private func dealInitialCards() -> PlayerActionResult {
|
||||
guard isFull else {
|
||||
return .tableNotFull
|
||||
}
|
||||
guard phase == .waitingForPlayers else {
|
||||
return .tableStateInvalid
|
||||
}
|
||||
|
||||
phase = .collectingDoubles
|
||||
gameType = nil
|
||||
minimumPlayableGame = .ruf
|
||||
|
||||
let cards = Dealer.dealFirstCards()
|
||||
for (index, player) in players.enumerated() {
|
||||
player.assignFirstCards(cards[index])
|
||||
}
|
||||
return .success
|
||||
}
|
||||
|
||||
func perform(double: Bool, forPlayer name: PlayerName) -> PlayerActionResult {
|
||||
let player = select(player: player)!
|
||||
player.didDouble(double)
|
||||
if allPlayersFinishedDoubling {
|
||||
dealAdditionalCards()
|
||||
}
|
||||
return .success
|
||||
}
|
||||
|
||||
private func dealAdditionalCards() {
|
||||
let cards = Dealer.dealRemainingCards(of: players.map { $0.rawCards })
|
||||
for (index, player) in players.enumerated() {
|
||||
player.assignRemainingCards(cards[index])
|
||||
}
|
||||
return .success
|
||||
}
|
||||
|
||||
private func startAuction() {
|
||||
players.forEach { $0.startAuction() }
|
||||
minimumPlayableGame = .ruf
|
||||
}
|
||||
|
||||
private func reset() {
|
||||
phase = .waitingForPlayers
|
||||
gameType = nil
|
||||
minimumPlayableGame = .ruf
|
||||
}
|
||||
}
|
||||
|
||||
extension Table {
|
||||
|
||||
var isFull: Bool {
|
||||
players.count == maximumPlayersPerTable
|
||||
}
|
||||
|
||||
var publicInfo: PublicTableInfo {
|
||||
.init(id: id, name: name, players: playerNames)
|
||||
}
|
||||
|
||||
var playerNames: [PlayerName] {
|
||||
players.map { $0.name }
|
||||
}
|
||||
|
||||
func compileInfo(for player: PlayerName) -> TableInfo? {
|
||||
guard let index = players.firstIndex(where: { $0.name == player }) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return TableInfo(self, forPlayerAt: index)
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ typealias Trick = [Card]
|
||||
extension Trick {
|
||||
|
||||
func winnerIndex(forGameType type: GameType) -> Int {
|
||||
let highCard = Dealer.sort(cards: self, using: type.sortingType).first!
|
||||
let highCard = sorted(cardOrder: type.sortingType).first!
|
||||
return firstIndex(of: highCard)!
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import Foundation
|
||||
|
||||
|
||||
enum DealCardResult {
|
||||
enum PlayerActionResult {
|
||||
|
||||
case success
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
import Foundation
|
||||
|
||||
|
||||
enum JoinTableResult {
|
||||
enum JoinTableResult: Error {
|
||||
|
||||
case invalidToken
|
||||
case alreadyJoinedOtherTable
|
||||
case tableNotFound
|
||||
case tableIsFull
|
||||
case success
|
||||
}
|
||||
|
@ -1,298 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
struct Dealer {
|
||||
|
||||
private static let normalCardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private static let wenzCardOder = [
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .ober),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .ober),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .ober),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .ober),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private static let geierCardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .unter),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .unter),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .unter),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .unter),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private static let eichelCardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private static let blattCardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private static let schellnCardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
]
|
||||
|
||||
private static let normalSortIndex: [Card : Int] = {
|
||||
normalCardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private static let wenzSortIndex: [Card : Int] = {
|
||||
wenzCardOder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private static let geierSortIndex: [Card : Int] = {
|
||||
geierCardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private static let eichelSortIndex: [Card : Int] = {
|
||||
eichelCardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private static let blattSortIndex: [Card : Int] = {
|
||||
blattCardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
private static let schellnSortIndex: [Card : Int] = {
|
||||
schellnCardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
static func sort<T>(cards: T, using strategy: CardSortingStrategy = .normal) -> [Card] where T: Sequence, T.Element == Card {
|
||||
switch strategy {
|
||||
case .normal:
|
||||
return cards.sorted { normalSortIndex[$0]! < normalSortIndex[$1]! }
|
||||
case .wenz:
|
||||
return cards.sorted { wenzSortIndex[$0]! < wenzSortIndex[$1]! }
|
||||
case .geier:
|
||||
return cards.sorted { geierSortIndex[$0]! < geierSortIndex[$1]! }
|
||||
case .soloEichel:
|
||||
return cards.sorted { eichelSortIndex[$0]! < eichelSortIndex[$1]! }
|
||||
case .soloBlatt:
|
||||
return cards.sorted { blattSortIndex[$0]! < blattSortIndex[$1]! }
|
||||
case .soloSchelln:
|
||||
return cards.sorted { schellnSortIndex[$0]! < schellnSortIndex[$1]! }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a random assignment of 8 cards per 4 players.
|
||||
*/
|
||||
static func deal() -> [[Card]] {
|
||||
let deck = Card.Suit.allCases.map { suit in
|
||||
Card.Symbol.allCases.map { symbol in
|
||||
Card(suit: suit, symbol: symbol)
|
||||
}
|
||||
}.joined()
|
||||
let random = Array(deck).shuffled()
|
||||
return (0..<4).map { part -> Array<Card>.SubSequence in
|
||||
let start = part * 8
|
||||
let end = start + 8
|
||||
return random[start..<end]
|
||||
}.map { sort(cards: $0) }
|
||||
}
|
||||
|
||||
static func consecutiveTrumps<T>(in cards: T, for game: GameType) -> Int where T: Sequence, T.Element == Card {
|
||||
var count = 0
|
||||
let trumpsInOrder: Array<Card>.SubSequence
|
||||
switch game.sortingType {
|
||||
case .normal:
|
||||
trumpsInOrder = normalCardOrder[0..<8]
|
||||
case .wenz:
|
||||
trumpsInOrder = wenzCardOder[0..<4]
|
||||
case .geier:
|
||||
trumpsInOrder = geierCardOrder[0..<4]
|
||||
case .soloEichel:
|
||||
trumpsInOrder = eichelCardOrder[0..<8]
|
||||
case .soloBlatt:
|
||||
trumpsInOrder = blattCardOrder[0..<8]
|
||||
case .soloSchelln:
|
||||
trumpsInOrder = schellnCardOrder[0..<8]
|
||||
}
|
||||
while cards.contains(trumpsInOrder[count]) {
|
||||
count += 1
|
||||
}
|
||||
guard count >= 3 else {
|
||||
return 0
|
||||
}
|
||||
return count
|
||||
}
|
||||
}
|
@ -9,10 +9,15 @@ private let maximumPlayerNameLength = 40
|
||||
/// The maximum length of a valid password
|
||||
private let maximumPasswordLength = 40
|
||||
|
||||
func encodeJSON<T>(_ response: T) throws -> String where T: Encodable {
|
||||
let data = try encoder.encode(response)
|
||||
return String(data: data, encoding: .utf8)!
|
||||
}
|
||||
|
||||
func routes(_ app: Application) throws {
|
||||
|
||||
|
||||
// MARK: Players & Sessions
|
||||
|
||||
|
||||
/**
|
||||
Create a new player
|
||||
- Parameter name: The name of the player, included in the url
|
||||
@ -33,7 +38,7 @@ func routes(_ app: Application) throws {
|
||||
password.count < maximumPasswordLength else {
|
||||
throw Abort(.notAcceptable) // 406
|
||||
}
|
||||
|
||||
|
||||
guard let hash = try? req.password.hash(password) else {
|
||||
throw Abort(.failedDependency) // 424
|
||||
}
|
||||
@ -42,7 +47,7 @@ func routes(_ app: Application) throws {
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Delete a player.
|
||||
- Parameter name: The name of the player, included in the url
|
||||
@ -70,7 +75,7 @@ func routes(_ app: Application) throws {
|
||||
database.deletePlayer(named: name)
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Log in as an existing player.
|
||||
- Parameter name: The name of the player, included in the url
|
||||
@ -98,7 +103,7 @@ func routes(_ app: Application) throws {
|
||||
let token = database.startNewSessionForRegisteredPlayer(named: name)
|
||||
return token
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Log in using a session token.
|
||||
- Parameter token: The session token of the player, as a string in the request body
|
||||
@ -116,7 +121,7 @@ func routes(_ app: Application) throws {
|
||||
}
|
||||
return player
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Log out.
|
||||
- Parameter name: The name of the player, included in the url
|
||||
@ -133,14 +138,14 @@ func routes(_ app: Application) throws {
|
||||
database.endSession(forSessionToken: token)
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Get the current table of the player, if one exists.
|
||||
- Parameter token: The session token of the player, as a string in the request body
|
||||
- Throws:
|
||||
- 400: Missing token
|
||||
- 401: Invalid token
|
||||
- Returns: The table id, or an empty string
|
||||
- Returns: The table info, or an empty string
|
||||
*/
|
||||
app.post("player", "table") { req -> String in
|
||||
guard let token = req.body.string else {
|
||||
@ -149,9 +154,12 @@ func routes(_ app: Application) throws {
|
||||
guard let player = database.registeredPlayerExists(withSessionToken: token) else {
|
||||
throw Abort(.unauthorized) // 401
|
||||
}
|
||||
return database.currentTableOfPlayer(named: player)
|
||||
guard let info = database.currentTableOfPlayer(named: player) else {
|
||||
return ""
|
||||
}
|
||||
return try encodeJSON(info)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Start a new websocket connection for the client to receive table updates from the server
|
||||
- Returns: Nothing
|
||||
@ -165,9 +173,9 @@ func routes(_ app: Application) throws {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Tables
|
||||
|
||||
|
||||
/**
|
||||
Create a new table.
|
||||
- Parameter visibility: Indicate a `"public"` or `"private"` table
|
||||
@ -183,22 +191,22 @@ func routes(_ app: Application) throws {
|
||||
let token = req.body.string else {
|
||||
throw Abort(.badRequest) // 400
|
||||
}
|
||||
let isVisible: Bool
|
||||
let isPublic: Bool
|
||||
if visibility == "private" {
|
||||
isVisible = false
|
||||
isPublic = false
|
||||
} else if visibility == "public" {
|
||||
isVisible = true
|
||||
isPublic = true
|
||||
} else {
|
||||
throw Abort(.badRequest) // 400
|
||||
}
|
||||
|
||||
|
||||
guard let player = database.registeredPlayerExists(withSessionToken: token) else {
|
||||
throw Abort(.unauthorized) // 401
|
||||
}
|
||||
let tableId = database.createTable(named: tableName, player: player, visible: isVisible)
|
||||
let tableId = database.createTable(named: tableName, player: player, isPublic: isPublic)
|
||||
return tableId
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
List the public tables.
|
||||
- Parameter token: The session token of the player, as a string in the request body
|
||||
@ -215,10 +223,9 @@ func routes(_ app: Application) throws {
|
||||
throw Abort(.forbidden) // 403
|
||||
}
|
||||
let list = database.getPublicTableInfos()
|
||||
let data = try encoder.encode(list)
|
||||
return String(data: data, encoding: .utf8)!
|
||||
return try encodeJSON(list)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Join a table.
|
||||
- Parameter table: The table id
|
||||
@ -226,6 +233,7 @@ func routes(_ app: Application) throws {
|
||||
- Throws:
|
||||
- 400: Missing token
|
||||
- 401: The session token is invalid
|
||||
- 403: The player already sits at another table
|
||||
- 410: The table id doesn't exist
|
||||
- 417: The table is already full and can't be joined
|
||||
- Returns: Nothing
|
||||
@ -236,14 +244,19 @@ func routes(_ app: Application) throws {
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
switch database.join(tableId: table, playerToken: token) {
|
||||
case .invalidToken:
|
||||
throw Abort(.unauthorized) // 401
|
||||
case .tableNotFound:
|
||||
throw Abort(.gone) // 410
|
||||
case .tableIsFull:
|
||||
throw Abort(.expectationFailed) // 417
|
||||
case .success:
|
||||
return ""
|
||||
case .success(let table):
|
||||
return try encodeJSON(table)
|
||||
case .failure(let result):
|
||||
switch result {
|
||||
case .invalidToken:
|
||||
throw Abort(.unauthorized) // 401
|
||||
case .alreadyJoinedOtherTable:
|
||||
throw Abort(.forbidden) // 403
|
||||
case .tableNotFound:
|
||||
throw Abort(.gone) // 410
|
||||
case .tableIsFull:
|
||||
throw Abort(.expectationFailed) // 417
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -265,11 +278,13 @@ func routes(_ app: Application) throws {
|
||||
return ""
|
||||
}
|
||||
|
||||
app.post("deal") { req -> String in
|
||||
guard let token = req.body.string else {
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
switch database.dealCards(playerToken: token) {
|
||||
app.post("player", "action", ":action") { req -> String in
|
||||
guard let token = req.body.string,
|
||||
let actionString = req.parameters.get("action"),
|
||||
let action = Player.Action(rawValue: actionString) else {
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
switch database.performAction(playerToken: token, action: action) {
|
||||
case .success:
|
||||
return ""
|
||||
case .invalidToken:
|
||||
|
Reference in New Issue
Block a user