Add game summary on client side

This commit is contained in:
Christoph Hagen 2021-12-21 11:16:54 +01:00
parent 941b25346d
commit b5cc395456
11 changed files with 46 additions and 10 deletions

View File

@ -26,6 +26,7 @@ const elementIdTableNameField = "table-name-field"
const elementIdPublicTableCheckbox = "table-public-checkbox"
const elementIdActionBar = "action-bar"
const elementIdAvailableGamesList = "available-games-list"
const elementIdGameSummary = "game-summary"
const localStorageTokenId = "token"
@ -265,6 +266,20 @@ function updateTableInfo(table) {
} else {
setEmptyPlayerInfo("right")
}
if (table.hasOwnProperty("summary")) {
setGameSummary(table.summary)
} else {
hideGameSummary()
}
}
function setGameSummary(summary) {
document.getElementById(elementIdGameSummary).innerHTML = summary.text
setDisplayStyle(elementIdGameSummary, "inherit")
}
function hideGameSummary() {
hide(elementIdGameSummary)
}
function setInfoForPlayer(player, position, game) {

View File

@ -54,6 +54,7 @@
<div id="table-player-state-left" class="player-connection-state">Offline</div>
</div>
<div id="available-games-list"></div>
<div id="game-summary">The game has ended.</div>
</div>
<div id="action-bar-centering">
<div id="action-bar"></div>

View File

@ -386,6 +386,23 @@ body {
text-align: center;
}
#game-summary {
display: none;
width: 180px;
height: 130px;
text-align: center;
grid-column: 2 / span 5;
grid-row: 2 / span 3;
z-index: 6;
margin: auto;
padding: 10px;
background-color: var(--standard-background);
color: var(--text-color);
border: 2px solid var(--button-color);
border-radius: 5px;
font-size: medium;
}
#player-cards-center {
width: 100%;
margin-top: 10px;

View File

@ -2,7 +2,7 @@ import Foundation
struct EnglishGameSummarizer: GameSummarizer {
let game: GameStatistics
let game: GameSummary
private var winText: String {
game.didWin ? "won" : "lost"

View File

@ -2,7 +2,7 @@ import Foundation
protocol GameSummarizer {
init(game: GameStatistics)
init(game: GameSummary)
var text: String { get }
}

View File

@ -2,7 +2,7 @@ import Foundation
struct GermanGameSummarizer: GameSummarizer {
let game: GameStatistics
let game: GameSummary
var winText: String {
game.didWin ? "gewinnt" : "verliert"
@ -56,7 +56,7 @@ struct GermanGameSummarizer: GameSummarizer {
}
var text: String {
"\(game.leader) \(winText) \(gameText)\(coPlayerNames) mit \(game.leaderPoints) points. " +
"\(game.leader) \(winText) \(gameText)\(coPlayerNames) mit \(game.leaderPoints) Punkten. " +
"Das Spiel kostet \(costText)."
}
}

View File

@ -1,6 +1,6 @@
import Foundation
struct GameStatistics: Codable, Equatable {
struct GameSummary: Codable, Equatable {
let leader: PlayerName
@ -27,6 +27,7 @@ struct GameStatistics: Codable, Equatable {
self.didWin = table.winners.contains(player: leader.name)
self.cost = table.game.basicCost
// TODO: Calculate cost correctly
self.text = language.gameSummarizer.init(game: self).text
}
}

View File

@ -26,7 +26,7 @@ struct TableInfo: Codable {
var game: GameId? = nil
var gameStats: GameStatistics?
var summary: GameSummary?
init(id: TableId, name: TableName) {
self.id = id

View File

@ -42,9 +42,9 @@ final class PlayingPlayer: CardHoldingPlayer {
guard canStillRaise else {
return []
}
if isUnknownCallee && leadsGame {
guard !isUnknownCallee else {
// Player belongs to caller, but other side has raised
return [.doubleDuringGame]
return leadsGame ? [.doubleDuringGame] : []
}
guard !leadsGame else {
return []
@ -54,6 +54,7 @@ final class PlayingPlayer: CardHoldingPlayer {
func play(card: Card) {
playedCard = card
canStillRaise = false
cards = cards.filter { $0 != card }
if card == isCalledWithAce {
leadsGame.toggle()

View File

@ -19,7 +19,7 @@ final class DealingTable: AbstractTable<DealingPlayer> {
/// At least one player placed a bid
var hasDouble: Bool {
players.contains { $0.didDouble == true }
players.contains { $0.didDouble }
}
override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
@ -46,6 +46,7 @@ final class DealingTable: AbstractTable<DealingPlayer> {
return (.tableStateInvalid, nil)
}
player.didDouble = double
player.didDecide = true
guard allPlayersActed else {
return (.success, nil)
}

View File

@ -87,7 +87,7 @@ final class FinishedTable: AbstractTable<FinishedPlayer> {
override func tableInfo(forPlayerAt index: Int) -> TableInfo {
var info = super.tableInfo(forPlayerAt: index)
info.gameStats = GameStatistics(table: self, language: language)
info.summary = GameSummary(table: self, language: language)
return info
}
}