Skip to content

Commit 8f2d06b

Browse files
committed
refactor(Game): calculate winner in AbstractGame
1 parent 26e3cbb commit 8f2d06b

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

plugin/src/main/kotlin/sc/plugin2022/Game.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ class Game(override val currentState: GameState = GameState()): AbstractGame(Gam
4242
return player
4343
}
4444

45-
override val winner: ITeam?
46-
get() = players.singleOrNull { !it.hasViolated() }?.team ?: checkWinCondition()?.winner
47-
4845
override val playerScores: MutableList<PlayerScore>
4946
get() = players.mapTo(ArrayList(players.size)) { getScoreFor(it) }
5047

@@ -58,7 +55,6 @@ class Game(override val currentState: GameState = GameState()): AbstractGame(Gam
5855
*/
5956
override fun checkWinCondition(): WinCondition? {
6057
if (!isGameOver) return null
61-
6258
return Team.values().toList().maxByNoEqual(currentState::getPointsForTeam)?.let {
6359
WinCondition(it, WinReason.DIFFERING_SCORES)
6460
} ?: run {
@@ -67,6 +63,7 @@ class Game(override val currentState: GameState = GameState()): AbstractGame(Gam
6763
fun dist(c: Coordinates) = abs(c.x - team.startLine)
6864
lightPieces.filterValues { it.team == team }.keys.map(::dist).sortedDescending()
6965
}
66+
logger.trace("checkWinCondition TeamPositions: {}", teamPositions)
7067
(0 until teamPositions.values.minOf { it.size }).mapNotNull { index ->
7168
teamPositions.entries.maxByNoEqual { it.value[index] }?.key
7269
}.firstOrNull().let {

sdk/src/main/server-api/sc/framework/plugins/AbstractGame.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.slf4j.LoggerFactory
44
import sc.api.plugins.IGameInstance
55
import sc.api.plugins.IGameState
66
import sc.api.plugins.IMove
7+
import sc.api.plugins.ITeam
78
import sc.api.plugins.exceptions.GameLogicException
89
import sc.api.plugins.exceptions.NotYourTurnException
910
import sc.api.plugins.host.IGameListener
@@ -26,6 +27,10 @@ abstract class AbstractGame(override val pluginUUID: String) : IGameInstance, Pa
2627

2728
private var moveRequestTimeout: ActionTimeout? = null
2829

30+
override val winner: ITeam?
31+
get() = players.singleOrNull { !it.hasViolated() && !it.hasLeft() }?.team ?:
32+
checkWinCondition()?.also { logger.trace("No Winner via violation, WinCondition: {}", it) }?.winner
33+
2934
/** Pause the game after current turn has finished or continue playing. */
3035
override var isPaused = false
3136
set(value) {

server/src/test/java/sc/server/gaming/GameRoomTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ val minimalReplay = """
5050
</score>
5151
</entry>
5252
</scores>
53-
<winner team="ONE"/>
5453
</data>
5554
</room>
5655
</protocol>""".trimIndent()
@@ -74,6 +73,7 @@ class GameRoomTest: WordSpec({
7473
room.onGameOver(playersScores)
7574
room.result.isRegular shouldBe true
7675
room.result.scores shouldContainExactly playersScores
76+
room.result.winner shouldBe null
7777
room.isOver shouldBe true
7878
}
7979
"save a correct replay" {

server/src/test/java/sc/server/plugins/TestGame.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ data class TestGame(
1616
override val playerScores: List<PlayerScore>
1717
get() = players.map { getScoreFor(it) }
1818

19-
override val winner: ITeam?
20-
get() = players.firstOrNull { !it.hasViolated() && !it.hasLeft() }?.team
21-
2219
override fun onRoundBasedAction(move: IMove) {
2320
if (move !is TestMove)
2421
throw InvalidMoveException(object: IMoveMistake {

0 commit comments

Comments
 (0)