Schafkopf-Server/Sources/App/Model/Tables/FinishedTable.swift
2021-12-21 09:53:42 +01:00

94 lines
2.5 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, +)
// TODO: Set isNextActor for winners
// TODO: Check for bettel
// TODO: Set schneider, schwarz, cost
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)
}
override func tableInfo(forPlayerAt index: Int) -> TableInfo {
var info = super.tableInfo(forPlayerAt: index)
info.gameStats = GameStatistics(table: self, language: language)
return info
}
}