Skip to content

Commit e59c399

Browse files
committed
[UI] Show game result at the end of the game
fix #108
1 parent 949f06d commit e59c399

File tree

6 files changed

+155
-71
lines changed

6 files changed

+155
-71
lines changed

composeApp/src/commonMain/kotlin/org/dots/game/App.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fun App(gameSettings: GameSettings = loadClassSettings(GameSettings.Default), on
307307
}
308308
}
309309
Row(Modifier.padding(bottom = 10.dp)) {
310-
GameInfo(currentGame, player1Score, player2Score, strings, uiSettings)
310+
GameInfo(currentGame, player1Score, player2Score, getField().gameResult, strings, uiSettings)
311311
}
312312
Row {
313313
val gameAndMoveInfo = buildString {

composeApp/src/commonMain/kotlin/org/dots/game/localization/EnglishStrings.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,13 @@ object EnglishStrings : Strings {
146146
Values closer to zero mean that games were played almost until all legal moves were exhausted.
147147
Typically, such games are unrealistic and "garbage" because humans don't play until the very end (they usually resign much earlier)."""
148148
override val draws = "Draws"
149+
150+
override val result = "Result"
151+
override val reason = "Reason"
152+
override val draw = "Draw"
153+
override val win = "win"
154+
override val interrupt = "interrupt"
155+
override val resignation = "resignation"
156+
override val time = "time"
157+
override val unknown = "unknown"
149158
}

composeApp/src/commonMain/kotlin/org/dots/game/localization/RussianStrings.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,14 @@ object RussianStrings : Strings {
151151
Обычно это означает, что игры не соответствуют реальному игровому поведению, так как люди не играют до самого конца (они заземляются намного раньше).
152152
""".trimIndent()
153153
override val draws = "Ничьи"
154+
155+
// Game result
156+
override val result = "Результат"
157+
override val reason = "Причина"
158+
override val draw = "Ничья"
159+
override val win = "победил"
160+
override val interrupt = "прерывание"
161+
override val resignation = "сдача"
162+
override val time = "время"
163+
override val unknown = "неизвестно"
154164
}

composeApp/src/commonMain/kotlin/org/dots/game/localization/Strings.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,14 @@ interface Strings {
115115
val avgRemainingMoves: String
116116
val avgRemainingMovesComment: String
117117
val draws: String
118+
119+
// Game result
120+
val result: String
121+
val draw: String
122+
val win: String
123+
val reason: String
124+
val interrupt: String
125+
val resignation: String
126+
val time: String
127+
val unknown: String
118128
}

composeApp/src/commonMain/kotlin/org/dots/game/views/GameInfo.kt

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package org.dots.game.views
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.Row
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.material.Text
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.ui.Alignment
9+
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.graphics.Color
11+
import androidx.compose.ui.text.font.FontWeight
12+
import androidx.compose.ui.unit.dp
13+
import org.dots.game.Tooltip
14+
import org.dots.game.UiSettings
15+
import org.dots.game.core.Game
16+
import org.dots.game.core.GameResult
17+
import org.dots.game.isAlmostEqual
18+
import org.dots.game.localization.Strings
19+
import org.dots.game.toNeatNumber
20+
21+
@Composable
22+
fun GameInfo(
23+
currentGame: Game,
24+
player1Score: Double,
25+
player2Score: Double,
26+
gameResult: GameResult?,
27+
strings: Strings,
28+
uiSettings: UiSettings
29+
) {
30+
val rules = currentGame.rules
31+
val rulesInfo = buildString {
32+
appendLine("${strings.initPosType}: ${strings.initPosTypeLabel(rules.initPosType)}")
33+
appendLine("${strings.initPosGenType}: ${strings.initPosGenTypeLabel(rules.initPosGenType)}")
34+
appendLine("${strings.baseMode}: ${strings.baseModeLabel(rules.baseMode)}")
35+
appendLine("${strings.suicideAllowed}: ${strings.boolToString(rules.suicideAllowed)}")
36+
appendLine("${strings.komi}: ${rules.komi.toNeatNumber()}")
37+
if (rules.captureByBorder) {
38+
appendLine("${strings.captureByBorder}: ${strings.boolToString(rules.captureByBorder)}")
39+
}
40+
if (lastOrNull() == '\n') {
41+
deleteAt(lastIndex)
42+
}
43+
}
44+
45+
Tooltip(rulesInfo) {
46+
val player1Name = currentGame.player1Name ?: strings.firstPlayerDefaultName
47+
val player2Name = currentGame.player2Name ?: strings.secondPlayerDefaultName
48+
val diff = player2Score - player1Score
49+
val winnerColor: Color = when {
50+
diff.isAlmostEqual(0.0) -> Color.Black
51+
diff > 0.0 -> uiSettings.playerSecondColor
52+
else -> uiSettings.playerFirstColor
53+
}
54+
55+
Column(horizontalAlignment = Alignment.CenterHorizontally) {
56+
Row(verticalAlignment = Alignment.CenterVertically) {
57+
Text("$player1Name ", color = uiSettings.playerFirstColor)
58+
Text(
59+
player1Score.toNeatNumber().toString(),
60+
color = uiSettings.playerFirstColor,
61+
fontWeight = FontWeight.Bold
62+
)
63+
64+
Text(" : ")
65+
66+
Text(
67+
player2Score.toNeatNumber().toString(),
68+
color = uiSettings.playerSecondColor,
69+
fontWeight = FontWeight.Bold
70+
)
71+
Text(" $player2Name", color = uiSettings.playerSecondColor)
72+
73+
if (uiSettings.developerMode) {
74+
Text(" (${diff.toNeatNumber()})", color = winnerColor)
75+
}
76+
}
77+
if (gameResult != null) {
78+
Row(
79+
Modifier.padding(top = 10.dp),
80+
verticalAlignment = Alignment.CenterVertically
81+
) {
82+
val winner = when (gameResult) {
83+
is GameResult.WinGameResult -> gameResult.winner
84+
is GameResult.Draw -> null
85+
}
86+
val reason = when (gameResult) {
87+
is GameResult.Draw -> null
88+
is GameResult.InterruptWin -> strings.interrupt
89+
is GameResult.ResignWin -> strings.resign
90+
is GameResult.ScoreWin -> strings.score
91+
is GameResult.TimeWin -> strings.time
92+
is GameResult.UnknownWin -> strings.unknown
93+
}
94+
val resultText = buildString {
95+
append(strings.result)
96+
append(": ")
97+
if (winner != null) {
98+
append(player1Name)
99+
append(' ')
100+
append(strings.win)
101+
} else {
102+
append(strings.draw)
103+
}
104+
if (reason != null) {
105+
append(" (")
106+
append(strings.reason)
107+
append(": ")
108+
append(reason)
109+
if (gameResult is GameResult.ScoreWin) {
110+
append(" +")
111+
append(gameResult.score.toNeatNumber())
112+
}
113+
append(')')
114+
}
115+
}
116+
Text(
117+
resultText,
118+
color = winnerColor,
119+
fontWeight = FontWeight.Bold
120+
)
121+
}
122+
}
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)