Refactor table + player data, add state

This commit is contained in:
Christoph Hagen
2021-12-20 20:18:19 +01:00
parent 49787db1aa
commit 3a95e1c990
18 changed files with 261 additions and 169 deletions

View File

@ -6,33 +6,28 @@ struct PlayerInfo: Codable, Equatable {
let name: PlayerName
/// Indicates that the player is active, i.e. a session is established
let isConnected: Bool
var isConnected = false
/// The player is the next one to perform an action
let isNextActor: Bool
var isNextActor = false
/// The card which the player added to the current trick
let playedCard: CardId?
var playedCard: CardId? = nil
/// The height of the player card on the table stack
let positionInTrick: Int
var positionInTrick = 0
/// The number of times the player doubled the game cost (initial double and raises)
let numberOfDoubles: Int
var numberOfDoubles = 0
let leadsGame: Bool
var leadsGame = false
let points: Int?
var points: Int? = nil
init(player: Player, position: Int) {
self.name = player.name
self.isConnected = player.isConnected
self.isNextActor = player.isNextActor
self.positionInTrick = position
self.playedCard = player.playedCard?.id
self.numberOfDoubles = player.numberOfDoubles
self.leadsGame = player.leadsGame
self.points = player.points
var state: [PlayerStateId] = []
init(name: PlayerName) {
self.name = name
}
/// Convert the property names into shorter strings for JSON encoding

View File

@ -6,39 +6,29 @@ struct TableInfo: Codable {
let name: String
let player: PlayerInfo
var player: PlayerInfo = .init(name: "")
let playerLeft: PlayerInfo?
var playerLeft: PlayerInfo? = nil
let playerAcross: PlayerInfo?
var playerAcross: PlayerInfo? = nil
let playerRight: PlayerInfo?
var playerRight: PlayerInfo? = nil
let playableGames: [GameId]
var playableGames: [GameId] = []
/// The cards in the hand of the player
let cards: [CardInfo]
var cards: [CardInfo] = []
/// The action the player can perform
let actions: [ActionId]
var actions: [ActionId] = []
let playerSelectsGame: Bool
var playerSelectsGame = false
let game: GameId?
var game: GameId? = nil
init<T>(table: AbstractTable<T>, index: Int) {
self.id = table.id
self.name = table.name
self.player = table.playerInfo(forIndex: index)!
self.playerLeft = table.playerInfo(forIndex: (index + 1) % 4)
self.playerAcross = table.playerInfo(forIndex: (index + 2) % 4)
self.playerRight = table.playerInfo(forIndex: (index + 3) % 4)
let data = table.playerData(at: index)
self.playableGames = data.games.map { $0.id }
self.actions = data.actions.map { $0.id }
self.cards = data.cards.map { $0.cardInfo }
self.playerSelectsGame = data.selectsGame
self.game = table.playedGame?.id
init(id: TableId, name: TableName) {
self.id = id
self.name = name
}
}