84 lines
2.2 KiB
Swift
84 lines
2.2 KiB
Swift
|
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) {
|
||
|
let players = table.players.map(FinishedPlayer.init)
|
||
|
self.game = table.game
|
||
|
leadingPoints = players
|
||
|
.filter { $0.leadsGame }
|
||
|
.map { $0.points! }
|
||
|
.reduce(0, +)
|
||
|
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)
|
||
|
}
|
||
|
}
|