Refactor table + player data, add state
This commit is contained in:
@ -45,15 +45,36 @@ class AbstractTable<TablePlayer> where TablePlayer: Player {
|
||||
(.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)
|
||||
}
|
||||
|
||||
func cardStackPosition(ofPlayerAt index: Int) -> Int {
|
||||
index
|
||||
}
|
||||
|
||||
func cards(forPlayerAt index: Int) -> [PlayableCard] {
|
||||
[]
|
||||
}
|
||||
|
||||
func games(forPlayerAt index: Int) -> [GameConvertible] {
|
||||
[]
|
||||
}
|
||||
|
||||
func gameIsSelected(byPlayerAt index: Int) -> Bool {
|
||||
false
|
||||
}
|
||||
|
||||
func tableInfo(forPlayerAt index: Int) -> TableInfo {
|
||||
var info = TableInfo(id: id, name: name)
|
||||
info.player = playerInfo(forIndex: index)!
|
||||
info.playerLeft = playerInfo(forIndex: (index + 1) % 4)
|
||||
info.playerAcross = playerInfo(forIndex: (index + 2) % 4)
|
||||
info.playerRight = playerInfo(forIndex: (index + 3) % 4)
|
||||
info.playableGames = games(forPlayerAt: index).map { $0.id }
|
||||
info.actions = players[index].actions.map { $0.id }
|
||||
info.cards = cards(forPlayerAt: index).map { $0.cardInfo }
|
||||
info.playerSelectsGame = gameIsSelected(byPlayerAt: index)
|
||||
info.game = playedGame?.id
|
||||
return info
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension AbstractTable: ManageableTable {
|
||||
@ -111,7 +132,9 @@ extension AbstractTable: ManageableTable {
|
||||
return nil
|
||||
}
|
||||
let height = cardStackPosition(ofPlayerAt: index)
|
||||
return PlayerInfo(player: player, position: height)
|
||||
var info = player.info
|
||||
info.positionInTrick = height
|
||||
return info
|
||||
}
|
||||
|
||||
func tableInfo(forPlayer player: PlayerName) -> TableInfo {
|
||||
@ -119,9 +142,5 @@ extension AbstractTable: ManageableTable {
|
||||
return tableInfo(forPlayerAt: index)
|
||||
}
|
||||
|
||||
func tableInfo(forPlayerAt index: Int) -> TableInfo {
|
||||
.init(table: self, index: index)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -164,18 +164,22 @@ final class BiddingTable: AbstractTable<BiddingPlayer> {
|
||||
return (.success, nil)
|
||||
}
|
||||
|
||||
override func playerData(at index: Int) -> (actions: [PlayerAction], games: [GameConvertible], cards: [PlayableCard], selectsGame: Bool) {
|
||||
let player = players[index]
|
||||
|
||||
let games: [GameConvertible]
|
||||
if isWaitingForGameSelection {
|
||||
games = gameToOutbid.availableGames.filter(player.canPlay)
|
||||
} else if index <= indexOfHighestBidder {
|
||||
games = gameToOutbid.availableClasses
|
||||
} else {
|
||||
games = gameToOutbid.classesWhenOutbidding
|
||||
}
|
||||
return (player.actions, games, player.cards.unplayable, selectsGame: player.selectsGame)
|
||||
override func cards(forPlayerAt index: Int) -> [PlayableCard] {
|
||||
players[index].cards.unplayable
|
||||
}
|
||||
|
||||
override func games(forPlayerAt index: Int) -> [GameConvertible] {
|
||||
if isWaitingForGameSelection {
|
||||
let player = players[index]
|
||||
return gameToOutbid.availableGames.filter(player.canPlay)
|
||||
}
|
||||
if index <= indexOfHighestBidder {
|
||||
return gameToOutbid.availableClasses
|
||||
}
|
||||
return gameToOutbid.classesWhenOutbidding
|
||||
}
|
||||
|
||||
override func gameIsSelected(byPlayerAt index: Int) -> Bool {
|
||||
players[index].selectsGame
|
||||
}
|
||||
}
|
||||
|
@ -4,16 +4,17 @@ final class DealingTable: AbstractTable<DealingPlayer> {
|
||||
|
||||
init(table: WaitingTable) {
|
||||
let cards = Dealer.dealFirstCards()
|
||||
for (index, player) in table.players.enumerated() {
|
||||
let players = table.players.map(DealingPlayer.init)
|
||||
for (index, player) in players.enumerated() {
|
||||
player.cards = cards[index]
|
||||
}
|
||||
let players = table.players.map(DealingPlayer.init)
|
||||
super.init(table: table, players: players)
|
||||
print("\(self.players[0].cards.count) cards")
|
||||
}
|
||||
|
||||
/// All players either doubled or didn't double
|
||||
var allPlayersActed: Bool {
|
||||
!players.contains { $0.didDouble == nil }
|
||||
!players.contains { !$0.didDecide }
|
||||
}
|
||||
|
||||
/// At least one player placed a bid
|
||||
@ -41,7 +42,7 @@ final class DealingTable: AbstractTable<DealingPlayer> {
|
||||
}
|
||||
|
||||
private func perform(double: Bool, forPlayer player: DealingPlayer) -> (result: PlayerActionResult, table: ManageableTable?) {
|
||||
guard player.didDouble == nil else {
|
||||
guard !player.didDecide else {
|
||||
return (.tableStateInvalid, nil)
|
||||
}
|
||||
player.didDouble = double
|
||||
@ -58,4 +59,7 @@ final class DealingTable: AbstractTable<DealingPlayer> {
|
||||
return (.success, table)
|
||||
}
|
||||
|
||||
override func cards(forPlayerAt index: Int) -> [PlayableCard] {
|
||||
players[index].cards.unplayable
|
||||
}
|
||||
}
|
||||
|
@ -47,12 +47,16 @@ final class FinishedTable: AbstractTable<FinishedPlayer> {
|
||||
}
|
||||
|
||||
init(table: PlayingTable) {
|
||||
|
||||
let players = table.players.map(FinishedPlayer.init)
|
||||
self.game = table.game
|
||||
leadingPoints = players
|
||||
.filter { $0.leadsGame }
|
||||
.map { $0.points! }
|
||||
.map { $0.points }
|
||||
.reduce(0, +)
|
||||
// TODO: Set isNextActor for winners
|
||||
// TODO: Check for bettel
|
||||
// TODO: Set schneider, schwarz, cost
|
||||
super.init(table: table, players: players)
|
||||
}
|
||||
|
||||
|
@ -108,10 +108,8 @@ final class PlayingTable: AbstractTable<PlayingPlayer> {
|
||||
}
|
||||
}
|
||||
|
||||
override func playerData(at index: Int) -> (actions: [PlayerAction], games: [GameConvertible], cards: [PlayableCard], selectsGame: Bool) {
|
||||
let player = players[index]
|
||||
let cards = player.playableCards(for: nextTrick, in: game)
|
||||
return (actions: player.actions, games: [], cards: cards, selectsGame: false)
|
||||
override func cards(forPlayerAt index: Int) -> [PlayableCard] {
|
||||
players[index].playableCards(for: nextTrick, in: game)
|
||||
}
|
||||
|
||||
private func didFinish(trick: Trick, in game: GameType) -> (result: PlayerActionResult, table: ManageableTable?) {
|
||||
|
@ -82,15 +82,16 @@ final class WeddingTable: AbstractTable<WeddingPlayer> {
|
||||
return (.success, table)
|
||||
}
|
||||
|
||||
override func playerData(at index: Int) -> (actions: [PlayerAction], games: [GameConvertible], cards: [PlayableCard], selectsGame: Bool) {
|
||||
guard requiresCardSelection else {
|
||||
return super.playerData(at: index)
|
||||
}
|
||||
override func cards(forPlayerAt index: Int) -> [PlayableCard] {
|
||||
let player = players[index]
|
||||
guard player.selectsGame else {
|
||||
return super.playerData(at: index)
|
||||
guard requiresCardSelection, player.selectsGame else {
|
||||
return player.cards.unplayable
|
||||
}
|
||||
return (actions: player.actions, games: [], cards: player.exchangeableCards, selectsGame: false)
|
||||
return player.exchangeableCards
|
||||
}
|
||||
|
||||
override func gameIsSelected(byPlayerAt index: Int) -> Bool {
|
||||
players[index].selectsGame
|
||||
}
|
||||
|
||||
override func play(card: Card, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
|
||||
|
Reference in New Issue
Block a user