Schafkopf-Server/Sources/App/Model/Tables/FinishedTable.swift

88 lines
2.3 KiB
Swift
Raw Normal View History

2021-12-18 15:08:43 +01:00
import Foundation
final class FinishedTable: AbstractTable<FinishedPlayer> {
let game: GameType
var winners: [FinishedPlayer] {
leadersHaveWon ? leaders : opponents
}
var loosers: [FinishedPlayer] {
leadersHaveWon ? opponents : leaders
}
var leaders: [FinishedPlayer] {
players.filter { $0.leadsGame }
}
var opponents: [FinishedPlayer] {
players.filter { !$0.leadsGame }
}
var winningPoints: Int {
leadersHaveWon ? leadingPoints : 120 - leadingPoints
}
var loosingPoints: Int {
leadersHaveWon ? 120 - leadingPoints : leadingPoints
}
let leadingPoints: Int
var leadersHaveWon: Bool {
leadingPoints > 60
}
var isSchwarz: Bool {
loosingPoints == 0
}
var isSchneider: Bool {
loosingPoints < (leadersHaveWon ? 30 : 31)
}
override var playedGame: GameType? {
game
}
init(table: PlayingTable) {
2021-12-18 15:08:43 +01:00
let players = table.players.map(FinishedPlayer.init)
self.game = table.game
leadingPoints = players
.filter { $0.leadsGame }
.map { $0.points }
2021-12-18 15:08:43 +01:00
.reduce(0, +)
// TODO: Set isNextActor for winners
// TODO: Check for bettel
// TODO: Set schneider, schwarz, cost
2021-12-18 15:08:43 +01:00
super.init(table: table, players: players)
}
/**
Perform a deal action on the finished table.
- Parameter action: The action to perform
- Parameter player: The name of the player
*/
override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
// Only dealing is allowed...
guard action == .deal else {
return (.tableStateInvalid, nil)
}
guard let player = players.player(named: name) else {
print("Unexpectedly missing player \(name) for deal action at finished table \(self.name)")
return (.tableStateInvalid, nil)
}
guard player.canPerform(.deal) else {
print("Finished table: Player \(name) can't perform deal")
return (.tableStateInvalid, nil)
}
let waiting = WaitingTable(oldTableAdvancedByOne: self)
let table = DealingTable(table: waiting)
return (.success, table)
}
}