Skip to content

Commit 09cacb4

Browse files
committed
fix(plugin): ensure a game is running smoothly with random moves
1 parent 05cc38d commit 09cacb4

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Game(UUID: String = GamePlugin.PLUGIN_UUID): RoundBasedGameInstance<Player
2727

2828
override fun start() {
2929
players.forEach {it.notifyListeners(WelcomeMessage(it.color)) }
30+
// next(players.first(), true)
3031
super.start()
3132
}
3233

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class GameState @JvmOverloads constructor(
4444
override val currentTeam
4545
get() = currentColor.team
4646

47+
override val currentPlayer
48+
get() = getPlayer(currentTeam)!!
49+
4750
@XStreamAsAttribute
4851
val orderedColors: MutableList<Color> = mutableListOf()
4952

plugin/src/test/sc/plugin2021/GameTest.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package sc.plugin2021
22

33
import io.kotlintest.matchers.collections.shouldContainExactly
44
import io.kotlintest.shouldBe
5+
import io.kotlintest.shouldNotBe
56
import io.kotlintest.specs.StringSpec
67
import org.junit.jupiter.api.assertThrows
78
import sc.plugin2020.util.Constants
9+
import sc.plugin2021.util.GameRuleLogic
810
import sc.shared.InvalidMoveException
911
import sc.shared.PlayerScore
1012
import sc.shared.ScoreCause
@@ -28,12 +30,33 @@ class GameTest: StringSpec({
2830
game.onAction(state.currentPlayer, PassMove(state.currentColor))
2931
}
3032

31-
println(game.winners)
3233
game.winners shouldBe listOf(second)
3334

3435
game.playerScores shouldContainExactly listOf(
3536
PlayerScore(ScoreCause.RULE_VIOLATION, e.message, Constants.LOSE_SCORE, -178),
3637
PlayerScore(ScoreCause.REGULAR, "", Constants.WIN_SCORE, -178)
3738
)
3839
}
40+
"A few moves can be performd without issues" {
41+
val game = Game()
42+
val state = game.gameState
43+
Pair(game.onPlayerJoined(), game.onPlayerJoined())
44+
game.start()
45+
46+
while (true) {
47+
try {
48+
val condition = game.checkWinCondition()
49+
if (condition != null) {
50+
println(condition)
51+
break
52+
}
53+
val moves = GameRuleLogic.getPossibleMoves(state)
54+
moves shouldNotBe emptySet<SetMove>()
55+
game.onAction(state.currentPlayer, moves.random())
56+
} catch (e: Exception) {
57+
println(e.message)
58+
break
59+
}
60+
}
61+
}
3962
})

socha-sdk/src/server-api/sc/framework/plugins/RoundBasedGameInstance.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public final void onAction(Player fromPlayer, ProtocolMessage data)
6767
errorMsg = Optional.of("We didn't request a data from you yet.");
6868
}
6969
} else {
70-
errorMsg = Optional.of("It's not your turn yet.");
70+
errorMsg = Optional.of(String.format(
71+
"It's not your turn yet; expected: %s, got: %s (msg was %s).",
72+
this.activePlayer, fromPlayer, data));
7173
}
7274
if (errorMsg.isPresent()) {
7375
fromPlayer.notifyListeners(new ProtocolErrorMessage(data, errorMsg.get()));
@@ -90,7 +92,7 @@ protected abstract void onRoundBasedAction(Player fromPlayer, ProtocolMessage da
9092
* @return WinCondition with winner and reason or null, if no win condition is
9193
* yet met.
9294
*/
93-
protected abstract WinCondition checkWinCondition();
95+
public abstract WinCondition checkWinCondition();
9496

9597
/**
9698
* At any time this method might be invoked by the server. Any open handles

0 commit comments

Comments
 (0)