Add basic language support for final text

This commit is contained in:
Christoph Hagen 2021-12-21 09:53:20 +01:00
parent 5d6ad89b56
commit fa1be9485e
5 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,39 @@
import Foundation
struct EnglishGameSummarizer: GameSummarizer {
let game: GameStatistics
private var winText: String {
game.didWin ? "won" : "lost"
}
private var gameText: String {
"game"
}
private var coPlayerNames: String {
switch game.coPlayers.count {
case 0:
return ""
case 1:
return " with \(game.coPlayers[0])"
case 2:
return " with \(game.coPlayers[0]) and \(game.coPlayers[1])"
default:
return ""
}
}
private var costText: String {
guard game.cost >= 100 else {
return "\(game.cost) cents"
}
return String(format: "%d.%02d €", game.cost / 100, game.cost % 100)
}
var text: String {
"\(game.leader) \(winText) a \(gameText)\(coPlayerNames) collecting \(game.leaderPoints) points. " +
"The game cost \(costText)."
}
}

View File

@ -0,0 +1,8 @@
import Foundation
protocol GameSummarizer {
init(game: GameStatistics)
var text: String { get }
}

View File

@ -0,0 +1,62 @@
import Foundation
struct GermanGameSummarizer: GameSummarizer {
let game: GameStatistics
var winText: String {
game.didWin ? "gewinnt" : "verliert"
}
var gameText: String {
switch GameType(id: game.game)! {
case .rufBlatt:
return "den Ruf Blatt"
case .rufEichel:
return "den Ruf Eichel"
case .rufSchelln:
return "den Ruf Schelln"
case .bettel:
return "den Bettel"
case .geier:
return "den Geier"
case .wenz:
return "den Wenz"
case .hochzeit:
return "die Hochzeit"
case .soloBlatt:
return "das Solo Blatt"
case .soloEichel:
return "das Solo Eichel"
case .soloHerz:
return "das Solo Herz"
case .soloSchelln:
return "das Solo Schelln"
}
}
var coPlayerNames: String {
switch game.coPlayers.count {
case 0:
return ""
case 1:
return " mit \(game.coPlayers[0])"
case 2:
return " mit \(game.coPlayers[0]) und \(game.coPlayers[1])"
default:
return ""
}
}
var costText: String {
guard game.cost >= 100 else {
return "\(game.cost) Cent"
}
return String(format: "%d.%02d €", game.cost / 100, game.cost % 100)
}
var text: String {
"\(game.leader) \(winText) \(gameText)\(coPlayerNames) mit \(game.leaderPoints) points. " +
"Das Spiel kostet \(costText)."
}
}

View File

@ -0,0 +1,15 @@
import Foundation
enum SupportedLanguage {
case german
case english
var gameSummarizer: GameSummarizer.Type {
switch self {
case .german:
return GermanGameSummarizer.self
case .english:
return EnglishGameSummarizer.self
}
}
}

View File

@ -12,6 +12,8 @@ class AbstractTable<TablePlayer> where TablePlayer: Player {
/// Indicates that the table is visible to all players, and can be joined by anyone
let isPublic: Bool
let language: SupportedLanguage = .german
/**
The players sitting at the table.