Refactor tables and players for clarity

This commit is contained in:
Christoph Hagen
2021-12-09 11:11:17 +01:00
parent 33f72c43cf
commit 289458bfd8
17 changed files with 718 additions and 64 deletions

View File

@ -1,40 +1,36 @@
import Foundation
struct PlayerInfo: Codable, Equatable {
/// The name of the player
let name: PlayerName
let connected: Bool
/// Indicates that the player is active, i.e. a session is established
let isConnected: Bool
/// The player is the next one to perform an action
let active: Bool
let selectsGame: Bool
/// The cards in the hand of the player
let cards: [CardInfo]
/// The action the player can perform
let actions: [String]
let isNextActor: Bool
/// The card which the player added to the current trick
let playedCard: CardId?
/// The height of the player card on the table stack
let position: Int
init(player: Player, isMasked: Bool, trickPosition: Int) {
let positionInTrick: Int
init(player: Player, isNextActor: Bool, position: Int) {
self.name = player.name
self.connected = player.isConnected
self.active = player.isNextActor
self.selectsGame = player.selectsGame
self.isConnected = player.isConnected
self.isNextActor = isNextActor
self.positionInTrick = position
self.playedCard = player.playedCard?.id
self.position = trickPosition
if isMasked {
self.cards = []
self.actions = []
} else {
self.actions = player.actions.map { $0.path }
self.cards = player.handCards.map { $0.cardInfo }
}
}
/// Convert the property names into shorter strings for JSON encoding
enum CodingKeys: String, CodingKey {
case name = "name"
case isConnected = "connected"
case isNextActor = "active"
case playedCard = "card"
case positionInTrick = "position"
}
}

View File

@ -15,22 +15,30 @@ struct TableInfo: Codable {
let playerRight: PlayerInfo?
let playableGames: [GameId]
init(_ table: Table, forPlayerAt playerIndex: Int) {
let player = table.player(at: playerIndex)!
self.id = table.id
self.name = table.name
self.player = table.playerInfo(at: playerIndex, masked: false)!
self.playerLeft = table.playerInfo(leftOf: playerIndex, masked: true)
self.playerAcross = table.playerInfo(acrossOf: playerIndex, masked: true)
self.playerRight = table.playerInfo(rightOf: playerIndex, masked: true)
if table.phase == .bidding || table.phase == .selectGame {
let games = table.minimumPlayableGame?.availableGames ?? GameType.allCases
self.playableGames = games.filter(player.canPlay).map { $0.id }
} else {
self.playableGames = []
}
/// The cards in the hand of the player
let cards: [CardInfo]
/// The action the player can perform
let actions: [String]
let playerSelectsGame: Bool
init(id: String, name: String,
own: PlayerInfo, left: PlayerInfo?,
across: PlayerInfo?, right: PlayerInfo?,
games: [GameId] = [], actions: [PlayerAction],
cards: [PlayableCard], selectGame: Bool = false) {
self.id = id
self.name = name
self.player = own
self.playerLeft = left
self.playerAcross = across
self.playerRight = right
self.playableGames = games
self.actions = actions.map { $0.path }
self.cards = cards.map { $0.cardInfo }
self.playerSelectsGame = selectGame
}
}