Socket connect on server
This commit is contained in:
parent
294b124807
commit
7716b37fb3
@ -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]
|
||||
}
|
||||
|
18
Sources/App/Model/ClientConnection.swift
Normal file
18
Sources/App/Model/ClientConnection.swift
Normal file
@ -0,0 +1,18 @@
|
||||
import Foundation
|
||||
import WebSocketKit
|
||||
|
||||
private let encoder = JSONEncoder()
|
||||
|
||||
enum ClientMessageType: String {
|
||||
|
||||
case tableInfo = "t"
|
||||
}
|
||||
|
||||
extension WebSocket {
|
||||
|
||||
func send<T>(_ 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)
|
||||
}
|
||||
}
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user