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