Socket connect on server
This commit is contained in:
parent
294b124807
commit
7716b37fb3
@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct BiddingInfo: Cobable {
|
struct BiddingInfo: Codable {
|
||||||
|
|
||||||
/// The bidding class (0-5) specifying the minimum game that must be played
|
/// The bidding class (0-5) specifying the minimum game that must be played
|
||||||
let highestBidClass: Int
|
let highestBidClass: Int
|
||||||
@ -12,5 +12,5 @@ struct BiddingInfo: Cobable {
|
|||||||
let indexOfNextBidder: Int
|
let indexOfNextBidder: Int
|
||||||
|
|
||||||
/// Indicates which players are remaining in the bidding process
|
/// 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 Foundation
|
||||||
import WebSocketKit
|
import WebSocketKit
|
||||||
|
import Vapor
|
||||||
|
|
||||||
let maximumPlayersPerTable = 4
|
let maximumPlayersPerTable = 4
|
||||||
|
|
||||||
@ -47,15 +48,17 @@ final class TableManagement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getPublicTableInfos() -> [TableInfo] {
|
func getPublicTableInfos() -> [TableInfo] {
|
||||||
publicTables.map { tableId in
|
publicTables.map(tableInfo).sorted()
|
||||||
let players = tablePlayers[tableId]!
|
}
|
||||||
let connected = players.map { playerConnections[$0] != nil }
|
|
||||||
return TableInfo(
|
private func tableInfo(id tableId: TableId) -> TableInfo {
|
||||||
id: tableId,
|
let players = tablePlayers[tableId]!
|
||||||
name: tableNames[tableId]!,
|
let connected = players.map { playerConnections[$0] != nil }
|
||||||
players: players,
|
return TableInfo(
|
||||||
connected: connected)
|
id: tableId,
|
||||||
}.sorted()
|
name: tableNames[tableId]!,
|
||||||
|
players: players,
|
||||||
|
connected: connected)
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentTableOfPlayer(named player: PlayerName) -> TableId? {
|
func currentTableOfPlayer(named player: PlayerName) -> TableId? {
|
||||||
@ -99,7 +102,26 @@ final class TableManagement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func connect(player: PlayerName, using socket: WebSocket) -> Bool {
|
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) {
|
func disconnect(player: PlayerName) {
|
||||||
|
Loading…
Reference in New Issue
Block a user