From 941b25346dfda069e66fbd1248216910589ae94a Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Tue, 21 Dec 2021 09:53:42 +0100 Subject: [PATCH] Add final statistics when game ends --- Sources/App/Infos/GameStatistics.swift | 32 +++++++++++++++++++ Sources/App/Infos/PlayerInfo.swift | 3 -- Sources/App/Infos/TableInfo.swift | 2 ++ .../App/Model/Players/FinishedPlayer.swift | 4 ++- Sources/App/Model/Tables/FinishedTable.swift | 6 ++++ 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 Sources/App/Infos/GameStatistics.swift diff --git a/Sources/App/Infos/GameStatistics.swift b/Sources/App/Infos/GameStatistics.swift new file mode 100644 index 0000000..7df4a01 --- /dev/null +++ b/Sources/App/Infos/GameStatistics.swift @@ -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 + } + +} diff --git a/Sources/App/Infos/PlayerInfo.swift b/Sources/App/Infos/PlayerInfo.swift index bb5cff8..c010839 100644 --- a/Sources/App/Infos/PlayerInfo.swift +++ b/Sources/App/Infos/PlayerInfo.swift @@ -22,8 +22,6 @@ struct PlayerInfo: Codable, Equatable { var leadsGame = false - var points: Int? = nil - var state: [PlayerStateId] = [] init(name: PlayerName) { @@ -39,6 +37,5 @@ struct PlayerInfo: Codable, Equatable { case positionInTrick = "position" case numberOfDoubles = "doubles" case leadsGame = "leads" - case points = "points" } } diff --git a/Sources/App/Infos/TableInfo.swift b/Sources/App/Infos/TableInfo.swift index e2db578..2191841 100644 --- a/Sources/App/Infos/TableInfo.swift +++ b/Sources/App/Infos/TableInfo.swift @@ -25,6 +25,8 @@ struct TableInfo: Codable { var playerSelectsGame = false var game: GameId? = nil + + var gameStats: GameStatistics? init(id: TableId, name: TableName) { self.id = id diff --git a/Sources/App/Model/Players/FinishedPlayer.swift b/Sources/App/Model/Players/FinishedPlayer.swift index 24cfeef..6839e3d 100644 --- a/Sources/App/Model/Players/FinishedPlayer.swift +++ b/Sources/App/Model/Players/FinishedPlayer.swift @@ -8,10 +8,13 @@ final class FinishedPlayer: Player { let playedCard: Card + let selectedGame: Bool + init(player: PlayingPlayer) { self.points = player.wonTricks.map { $0.points }.reduce(0, +) self.leadsGame = player.leadsGame self.playedCard = player.playedCard! + self.selectedGame = player.selectsGame super.init(player: player) } @@ -25,7 +28,6 @@ final class FinishedPlayer: Player { override var info: PlayerInfo { var result = super.info - result.points = points result.playedCard = playedCard.id return result } diff --git a/Sources/App/Model/Tables/FinishedTable.swift b/Sources/App/Model/Tables/FinishedTable.swift index 741f0ce..6ec4e64 100644 --- a/Sources/App/Model/Tables/FinishedTable.swift +++ b/Sources/App/Model/Tables/FinishedTable.swift @@ -84,4 +84,10 @@ final class FinishedTable: AbstractTable { 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 + } }