Schafkopf-Server/Sources/App/Model/Players/BiddingPlayer.swift

72 lines
1.7 KiB
Swift
Raw Normal View History

import Foundation
import WebSocketKit
final class BiddingPlayer: CardHoldingPlayer {
var isStillBidding = true
2021-12-18 15:08:43 +01:00
var isAllowedToOfferWedding: Bool
2021-12-18 15:08:43 +01:00
init(player: DealingPlayer) {
isAllowedToOfferWedding = true
super.init(player: player)
}
2021-12-18 15:08:43 +01:00
init(player: WeddingPlayer) {
isStillBidding = !player.offersWedding
isAllowedToOfferWedding = false
super.init(player: player)
}
2021-12-18 15:08:43 +01:00
func canPlay(game: GameType) -> Bool {
guard let suit = game.calledSuit else {
if game == .hochzeit {
return canOfferWedding
}
return true
}
let sorter = game.sortingType
guard sorter.hasCardToCall(suit, in: cards) else {
// Player needs at least one card of the called suit
return false
}
let ace = Card(suit, .ass)
return !cards.contains(ace)
}
2021-12-18 15:08:43 +01:00
override var actions: [PlayerAction] {
guard isStillBidding else {
return []
}
2021-12-18 15:08:43 +01:00
var actions: [PlayerAction] = isNextActor ? [.increaseOrMatchGame, .withdrawFromAuction] : []
if canOfferWedding {
actions.append(.offerWedding)
}
2021-12-18 15:08:43 +01:00
return actions
}
private var biddingState: [PlayerState] {
isStillBidding ? [] : [.didFold]
}
override var states: [PlayerState] {
var states = super.states
2021-12-25 16:53:58 +01:00
if selectsGame {
return states
}
if !isStillBidding {
states.append(.didFold)
} else if isNextActor {
states.append(.mustBid)
}
return states
2021-12-18 15:08:43 +01:00
}
}
extension BiddingPlayer {
var canOfferWedding: Bool {
cards.canOfferWedding
}
}