2021-12-09 11:11:17 +01:00
|
|
|
import Foundation
|
2021-12-18 15:08:43 +01:00
|
|
|
import WebSocketKit
|
2021-12-09 11:11:17 +01:00
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
class AbstractTable<TablePlayer> where TablePlayer: Player {
|
2021-12-09 11:11:17 +01:00
|
|
|
|
|
|
|
/// The unique id of the table
|
|
|
|
let id: TableId
|
|
|
|
|
|
|
|
/// The name of the table
|
|
|
|
let name: TableName
|
|
|
|
|
|
|
|
/// Indicates that the table is visible to all players, and can be joined by anyone
|
|
|
|
let isPublic: Bool
|
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
/**
|
|
|
|
The players sitting at the table.
|
|
|
|
|
|
|
|
The players are ordered clockwise around the table, with the first player starting the game.
|
|
|
|
*/
|
|
|
|
var players: [TablePlayer]
|
|
|
|
|
|
|
|
var playedGame: GameType? {
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
|
|
|
init(table: ManageableTable, players: [TablePlayer]) {
|
2021-12-09 11:11:17 +01:00
|
|
|
self.id = table.id
|
|
|
|
self.name = table.name
|
|
|
|
self.isPublic = table.isPublic
|
2021-12-18 15:08:43 +01:00
|
|
|
self.players = players
|
2021-12-09 11:11:17 +01:00
|
|
|
}
|
|
|
|
|
2021-12-18 15:08:43 +01:00
|
|
|
init(id: TableId, name: TableName, isPublic: Bool, players: [TablePlayer]) {
|
2021-12-09 11:11:17 +01:00
|
|
|
self.id = id
|
|
|
|
self.name = name
|
|
|
|
self.isPublic = isPublic
|
2021-12-18 15:08:43 +01:00
|
|
|
self.players = players
|
|
|
|
}
|
|
|
|
|
|
|
|
func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
|
|
|
|
(.tableStateInvalid, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func play(card: Card, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
|
|
|
|
(.tableStateInvalid, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerData(at index: Int) -> (actions: [PlayerAction], games: [GameConvertible], cards: [PlayableCard], selectsGame: Bool) {
|
|
|
|
let player = players[index]
|
|
|
|
return (actions: player.actions, games: [], cards: player.cards.unplayable, selectsGame: false)
|
2021-12-09 11:11:17 +01:00
|
|
|
}
|
2021-12-18 15:08:43 +01:00
|
|
|
|
|
|
|
func cardStackPosition(ofPlayerAt index: Int) -> Int {
|
|
|
|
index
|
|
|
|
}
|
|
|
|
|
2021-12-09 11:11:17 +01:00
|
|
|
}
|
2021-12-18 15:08:43 +01:00
|
|
|
|
|
|
|
extension AbstractTable: ManageableTable {
|
|
|
|
|
|
|
|
var publicInfo: PublicTableInfo {
|
|
|
|
.init(id: id, name: name, players: playerNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
var playerNames: [PlayerName] {
|
|
|
|
players.map { $0.name }
|
|
|
|
}
|
|
|
|
|
|
|
|
var allPlayers: [Player] {
|
|
|
|
players
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: Connection
|
|
|
|
|
|
|
|
func connect(player name: PlayerName, using socket: WebSocket) -> Bool {
|
|
|
|
guard let player = players.player(named: name) else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
player.connect(using: socket)
|
|
|
|
sendUpdateToAllPlayers()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func disconnect(player name: PlayerName) {
|
|
|
|
guard let player = players.player(named: name) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard player.disconnect() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendUpdateToAllPlayers()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendUpdateToAllPlayers() {
|
|
|
|
players.enumerated().forEach { playerIndex, player in
|
|
|
|
guard player.isConnected else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let info = self.tableInfo(forPlayerAt: playerIndex)
|
|
|
|
player.send(info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Client info
|
|
|
|
|
|
|
|
func playerInfo(forIndex index: Int) -> PlayerInfo? {
|
|
|
|
guard let player = players.at(index) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let height = cardStackPosition(ofPlayerAt: index)
|
|
|
|
return PlayerInfo(player: player, position: height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableInfo(forPlayer player: PlayerName) -> TableInfo {
|
|
|
|
let index = players.index(of: player)
|
|
|
|
return tableInfo(forPlayerAt: index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableInfo(forPlayerAt index: Int) -> TableInfo {
|
|
|
|
.init(table: self, index: index)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|