Skip to content

Commit b108b7d

Browse files
committed
refactor(plugin): move movability validation into isGameOver
1 parent b27f1e1 commit b108b7d

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

plugin/src/client/sc/plugin2021/AbstractClient.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ abstract class AbstractClient @Throws(IOException::class) constructor(
3030
private val gameType = GamePlugin.PLUGIN_UUID
3131
}
3232

33+
var isGameOver = false
34+
3335
/** The handler reacts to messages from the server received by the lobby client. */
3436
protected lateinit var handler: IGameHandler
3537

@@ -94,10 +96,12 @@ abstract class AbstractClient @Throws(IOException::class) constructor(
9496
if(id == PlayerType.OBSERVER) return
9597

9698
handler.onUpdate(gameState)
97-
if(gameState.currentTeam == team) {
98-
handler.onUpdate(gameState.currentPlayer, gameState.otherPlayer)
99-
} else {
100-
handler.onUpdate(gameState.otherPlayer, gameState.currentPlayer)
99+
if (gameState.orderedColors.isNotEmpty()) {
100+
if(gameState.currentTeam == team) {
101+
handler.onUpdate(gameState.currentPlayer, gameState.otherPlayer)
102+
} else {
103+
handler.onUpdate(gameState.otherPlayer, gameState.currentPlayer)
104+
}
101105
}
102106
}
103107

@@ -123,6 +127,7 @@ abstract class AbstractClient @Throws(IOException::class) constructor(
123127

124128
override fun onGameOver(roomId: String, data: GameResult) {
125129
logger.info("$this on Game Over with game result $data")
130+
isGameOver = true
126131
if (this::handler.isInitialized) {
127132
handler.gameEnded(data, team, error)
128133
}

plugin/src/server/sc/plugin2021/Game.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,7 @@ class Game(UUID: String = GamePlugin.PLUGIN_UUID): RoundBasedGameInstance<Player
161161
logger.debug("Current State: $gameState")
162162
logger.debug("Performing Move $data")
163163
GameRuleLogic.performMove(gameState, data)
164-
if (!isGameOver()) {
165-
GameRuleLogic.validateMovability(gameState)
166-
next(gameState.currentPlayer)
167-
} else {
168-
next(null)
169-
}
164+
next(if (isGameOver()) null else gameState.currentPlayer)
170165
logger.debug("Current Board:\n${gameState.board}")
171166
} catch(e: InvalidMoveException) {
172167
super.catchInvalidMove(e, fromPlayer)
@@ -175,5 +170,8 @@ class Game(UUID: String = GamePlugin.PLUGIN_UUID): RoundBasedGameInstance<Player
175170

176171
override fun getCurrentState(): IGameState = gameState
177172

178-
fun isGameOver(): Boolean = gameState.orderedColors.isEmpty()
173+
fun isGameOver(): Boolean {
174+
GameRuleLogic.removeInvalidColors(gameState)
175+
return gameState.orderedColors.isEmpty()
176+
}
179177
}

plugin/src/shared/sc/plugin2021/GameState.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ class GameState @JvmOverloads constructor(
5757
get() = try {
5858
orderedColors[currentColorIndex]
5959
} catch (e: IndexOutOfBoundsException) {
60-
throw GameLogicException("Trying to access the currently active color after the game has ended")
60+
logger.error("""
61+
:-- Exception on accessing currentColor --:
62+
orderedColors: $orderedColors
63+
currentColorIndex: $currentColorIndex
64+
""".trimIndent())
65+
throw GameLogicException("Trying to access the currently active color with invalid index")
6166
}
6267

6368
@XStreamAsAttribute
@@ -111,14 +116,17 @@ class GameState @JvmOverloads constructor(
111116
* Do not do anything with currentColor before the turn is done for good.
112117
*/
113118
fun removeActiveColor() {
114-
logger.info("Removed $currentColor from the game")
115-
orderedColors.remove(currentColor)
119+
logger.info("Removing $currentColor from the game")
120+
orderedColors.removeAt(currentColorIndex)
116121
if (orderedColors.isNotEmpty())
117122
currentColorIndex = (currentColorIndex + orderedColors.size - 1) % orderedColors.size
118123
logger.debug("Remaining Colors: $orderedColors")
119124
}
120125

121-
override fun toString(): String = "GameState $round/$turn -> $currentColor ${if (GameRuleLogic.isFirstMove(this)) "(Start Piece: $startPiece)" else ""}"
126+
override fun toString(): String =
127+
if (orderedColors.isNotEmpty())
128+
"GameState $round/$turn -> $currentColor ${if (GameRuleLogic.isFirstMove(this)) "(Start Piece: $startPiece)" else ""}"
129+
else "GameState finished at $round/$turn"
122130

123131
override fun equals(other: Any?): Boolean {
124132
return !(this === other) &&

plugin/src/shared/sc/plugin2021/util/GameRuleLogic.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,12 @@ object GameRuleLogic {
215215

216216
/** Ensures the currently active color of [gameState] can perform a move. */
217217
@JvmStatic
218-
fun validateMovability(gameState: GameState) {
219-
if (streamPossibleMoves(gameState).none { isValidSetMove(gameState, it) })
218+
fun removeInvalidColors(gameState: GameState) {
219+
if (gameState.orderedColors.isEmpty()) return
220+
if (streamPossibleMoves(gameState).none { isValidSetMove(gameState, it) }) {
220221
gameState.removeActiveColor()
222+
removeInvalidColors(gameState)
223+
}
221224
}
222225

223226
/** Streams all possible moves in the current turn of [gameState]. */

0 commit comments

Comments
 (0)