2021-12-09 11:11:17 +01:00
|
|
|
import Foundation
|
|
|
|
import WebSocketKit
|
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
class Player {
|
2021-12-09 11:11:17 +01:00
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
let name: PlayerName
|
2021-12-09 11:11:17 +01:00
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
var socket: WebSocket?
|
2021-12-09 11:11:17 +01:00
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
var playedCard: Card?
|
2021-12-09 11:11:17 +01:00
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
var isNextActor: Bool
|
2021-12-09 11:11:17 +01:00
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
var cards: [Card]
|
2021-12-09 11:11:17 +01:00
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
var numberOfDoubles: Int
|
|
|
|
|
|
|
|
var leadsGame: Bool
|
|
|
|
|
|
|
|
var points: Int?
|
|
|
|
|
|
|
|
init(name: PlayerName, socket: WebSocket? = nil) {
|
|
|
|
self.name = name
|
|
|
|
self.socket = socket
|
|
|
|
self.cards = []
|
|
|
|
self.isNextActor = false
|
|
|
|
self.playedCard = nil
|
|
|
|
self.numberOfDoubles = 0
|
|
|
|
self.leadsGame = false
|
|
|
|
self.points = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
init(player: Player) {
|
|
|
|
self.name = player.name
|
|
|
|
self.socket = player.socket
|
|
|
|
self.cards = player.cards
|
|
|
|
self.isNextActor = false
|
|
|
|
self.playedCard = player.playedCard
|
|
|
|
self.numberOfDoubles = player.numberOfDoubles
|
|
|
|
self.leadsGame = player.leadsGame
|
|
|
|
self.points = player.points
|
|
|
|
}
|
|
|
|
|
|
|
|
var actions: [PlayerAction] {
|
|
|
|
[]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Player: Equatable {
|
|
|
|
|
|
|
|
static func == (lhs: Player, rhs: Player) -> Bool {
|
|
|
|
lhs.name == rhs.name
|
|
|
|
}
|
2021-12-09 11:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Player {
|
|
|
|
|
|
|
|
// MARK: Connection
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func connect(using socket: WebSocket) {
|
|
|
|
_ = self.socket?.close()
|
|
|
|
self.socket = socket
|
|
|
|
}
|
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
@discardableResult
|
2021-12-09 11:11:17 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
@discardableResult
|
|
|
|
func send(_ info: TableInfo) -> Bool {
|
|
|
|
guard let socket = socket else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
try socket.send(encodeJSON(info))
|
|
|
|
} catch {
|
|
|
|
print("Failed to send info: \(error)")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2021-12-09 11:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
func canPerform(_ action: PlayerAction) -> Bool {
|
|
|
|
actions.contains(action)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-12-18 15:08:43 +01:00
|
|
|
|
|
|
|
extension Player: CustomStringConvertible {
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|