From 7716b37fb344ebd6c7e0241fdea3ad18960dd6ca Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Tue, 30 Nov 2021 20:56:04 +0100 Subject: [PATCH] Socket connect on server --- Sources/App/Infos/BiddingInfo.swift | 4 +-- Sources/App/Model/ClientConnection.swift | 18 ++++++++++ Sources/App/Model/TableManagement.swift | 42 ++++++++++++++++++------ 3 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 Sources/App/Model/ClientConnection.swift diff --git a/Sources/App/Infos/BiddingInfo.swift b/Sources/App/Infos/BiddingInfo.swift index dfde24a..8d95109 100644 --- a/Sources/App/Infos/BiddingInfo.swift +++ b/Sources/App/Infos/BiddingInfo.swift @@ -1,6 +1,6 @@ import Foundation -struct BiddingInfo: Cobable { +struct BiddingInfo: Codable { /// The bidding class (0-5) specifying the minimum game that must be played let highestBidClass: Int @@ -12,5 +12,5 @@ struct BiddingInfo: Cobable { let indexOfNextBidder: Int /// Indicates which players are remaining in the bidding process - let remaingingBidders: [Bool] + let remainingBidders: [Bool] } diff --git a/Sources/App/Model/ClientConnection.swift b/Sources/App/Model/ClientConnection.swift new file mode 100644 index 0000000..2e438fe --- /dev/null +++ b/Sources/App/Model/ClientConnection.swift @@ -0,0 +1,18 @@ +import Foundation +import WebSocketKit + +private let encoder = JSONEncoder() + +enum ClientMessageType: String { + + case tableInfo = "t" +} + +extension WebSocket { + + func send(_ type: ClientMessageType, data: T) where T: Encodable { + let json = try! encoder.encode(data) + let string = String(data: json, encoding: .utf8)! + self.send(type.rawValue + string) + } +} diff --git a/Sources/App/Model/TableManagement.swift b/Sources/App/Model/TableManagement.swift index 3953dc2..0bbc40e 100644 --- a/Sources/App/Model/TableManagement.swift +++ b/Sources/App/Model/TableManagement.swift @@ -1,5 +1,6 @@ import Foundation import WebSocketKit +import Vapor let maximumPlayersPerTable = 4 @@ -47,15 +48,17 @@ final class TableManagement { } func getPublicTableInfos() -> [TableInfo] { - publicTables.map { tableId in - let players = tablePlayers[tableId]! - let connected = players.map { playerConnections[$0] != nil } - return TableInfo( - id: tableId, - name: tableNames[tableId]!, - players: players, - connected: connected) - }.sorted() + publicTables.map(tableInfo).sorted() + } + + private func tableInfo(id tableId: TableId) -> TableInfo { + let players = tablePlayers[tableId]! + let connected = players.map { playerConnections[$0] != nil } + return TableInfo( + id: tableId, + name: tableNames[tableId]!, + players: players, + connected: connected) } func currentTableOfPlayer(named player: PlayerName) -> TableId? { @@ -99,7 +102,26 @@ final class TableManagement { } func connect(player: PlayerName, using socket: WebSocket) -> Bool { - fatalError() + guard let tableId = playerTables[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) was assigned to table \(tableId.prefix(5)) where it wasn't listed") + return false + } + playerConnections[player] = socket + + let tableInfo = self.tableInfo(id: tableId) + // Notify other players at table about changes + players + .compactMap { playerConnections[$0] } + .forEach { $0.send(.tableInfo, data: tableInfo) } + return true } func disconnect(player: PlayerName) {