Add final statistics when game ends

This commit is contained in:
Christoph Hagen 2021-12-21 09:53:42 +01:00
parent fa1be9485e
commit 941b25346d
5 changed files with 43 additions and 4 deletions

View File

@ -0,0 +1,32 @@
import Foundation
struct GameStatistics: Codable, Equatable {
let leader: PlayerName
let coPlayers: [PlayerName]
let didWin: Bool
let game: GameId
let leaderPoints: Int
let cost: Int
var text: String = ""
init(table: FinishedTable, language: SupportedLanguage) {
let leader = table.players.first { $0.selectedGame }!
self.coPlayers = table.players
.filter { $0 != leader && $0.leadsGame == leader.leadsGame }
.map { $0.name }
self.leader = leader.name
self.game = table.game.id
self.leaderPoints = table.leadingPoints
self.didWin = table.winners.contains(player: leader.name)
self.cost = table.game.basicCost
// TODO: Calculate cost correctly
}
}

View File

@ -22,8 +22,6 @@ struct PlayerInfo: Codable, Equatable {
var leadsGame = false var leadsGame = false
var points: Int? = nil
var state: [PlayerStateId] = [] var state: [PlayerStateId] = []
init(name: PlayerName) { init(name: PlayerName) {
@ -39,6 +37,5 @@ struct PlayerInfo: Codable, Equatable {
case positionInTrick = "position" case positionInTrick = "position"
case numberOfDoubles = "doubles" case numberOfDoubles = "doubles"
case leadsGame = "leads" case leadsGame = "leads"
case points = "points"
} }
} }

View File

@ -25,6 +25,8 @@ struct TableInfo: Codable {
var playerSelectsGame = false var playerSelectsGame = false
var game: GameId? = nil var game: GameId? = nil
var gameStats: GameStatistics?
init(id: TableId, name: TableName) { init(id: TableId, name: TableName) {
self.id = id self.id = id

View File

@ -8,10 +8,13 @@ final class FinishedPlayer: Player {
let playedCard: Card let playedCard: Card
let selectedGame: Bool
init(player: PlayingPlayer) { init(player: PlayingPlayer) {
self.points = player.wonTricks.map { $0.points }.reduce(0, +) self.points = player.wonTricks.map { $0.points }.reduce(0, +)
self.leadsGame = player.leadsGame self.leadsGame = player.leadsGame
self.playedCard = player.playedCard! self.playedCard = player.playedCard!
self.selectedGame = player.selectsGame
super.init(player: player) super.init(player: player)
} }
@ -25,7 +28,6 @@ final class FinishedPlayer: Player {
override var info: PlayerInfo { override var info: PlayerInfo {
var result = super.info var result = super.info
result.points = points
result.playedCard = playedCard.id result.playedCard = playedCard.id
return result return result
} }

View File

@ -84,4 +84,10 @@ final class FinishedTable: AbstractTable<FinishedPlayer> {
let table = DealingTable(table: waiting) let table = DealingTable(table: waiting)
return (.success, table) 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
}
} }