From 08b8d56d07bb2b7e9c5a31baa8bb97e0efa524a1 Mon Sep 17 00:00:00 2001 From: alyssa Date: Tue, 12 Nov 2024 08:44:29 +0000 Subject: [PATCH] add tests and fix broken placeholder hands --- .../src/main/java/online/screen/GameRoom.java | 4 +- client/src/main/java/screen/HandPanelMk2.java | 34 ++++++--- client/src/test/kotlin/http/SessionApiTest.kt | 2 +- server/src/main/kotlin/room/Room.kt | 38 +++++----- server/src/test/kotlin/room/RoomTest.kt | 36 +++++++++ .../test/kotlin/store/AbstractStoreTest.kt | 3 +- server/src/test/kotlin/store/RoomStoreTest.kt | 76 +++++++++++++++++++ server/src/test/kotlin/util/TestFactory.kt | 9 +++ 8 files changed, 167 insertions(+), 35 deletions(-) create mode 100644 server/src/test/kotlin/room/RoomTest.kt create mode 100644 server/src/test/kotlin/store/RoomStoreTest.kt diff --git a/client/src/main/java/online/screen/GameRoom.java b/client/src/main/java/online/screen/GameRoom.java index 45e3786..f137a8f 100644 --- a/client/src/main/java/online/screen/GameRoom.java +++ b/client/src/main/java/online/screen/GameRoom.java @@ -495,7 +495,7 @@ public void addOrUpdatePlayer(int playerNumber, String username) newPlayer.setEnabled(true); hmPlayerByAdjustedPlayerNumber.put(adjustedNo, newPlayer); - handPanel.initialisePlayer(adjustedNo, username, colour); + handPanel.initialisePlayer(adjustedNo, username, colour, 5); handPanel.activateEmptySeats(); } else @@ -893,7 +893,7 @@ private void processEndOfGame(int winningPlayerAdjusted) return; } - handPanel.resetPlayers(); + handPanel.resetPlayers(5); clearScreenAfterGameEnd(); int currentSize = hmPlayerByAdjustedPlayerNumber.size(); diff --git a/client/src/main/java/screen/HandPanelMk2.java b/client/src/main/java/screen/HandPanelMk2.java index ba4a36b..4bbc708 100644 --- a/client/src/main/java/screen/HandPanelMk2.java +++ b/client/src/main/java/screen/HandPanelMk2.java @@ -309,6 +309,16 @@ private void initialiseCardLabelsForPlayer(List hand, CardLabel[] labels refreshIcons(labels); } + + private void makePlaceholderHand(CardLabel[] labels, int handSize) { + for (int i=0; i(), playerCards, false); + makePlaceholderHand(playerCards, startingNumberOfCards); break; case 1: opponentOneName = name; - initialiseCardLabelsForPlayer(new ArrayList<>(), opponentOneCards, false); + makePlaceholderHand(opponentOneCards, startingNumberOfCards); break; case 2: opponentTwoName = name; - initialiseCardLabelsForPlayer(new ArrayList<>(), opponentTwoCards, false); + makePlaceholderHand(opponentTwoCards, startingNumberOfCards); break; case 3: opponentThreeName = name; - initialiseCardLabelsForPlayer(new ArrayList<>(), opponentThreeCards, false); + makePlaceholderHand(opponentThreeCards, startingNumberOfCards); break; default: Debug.stackTrace("Unexpected playerNumber [" + playerNumber + "]"); @@ -677,23 +687,23 @@ private void deselectOtherPlayers(int playerNumber) } } - public void resetPlayers() + public void resetPlayers(int startingNumberOfCards) { resetPlayerCardPositions(); - resetPlayer(0, playerName); - resetPlayer(1, opponentOneName); - resetPlayer(2, opponentTwoName); - resetPlayer(3, opponentThreeName); + resetPlayer(0, playerName, startingNumberOfCards); + resetPlayer(1, opponentOneName, startingNumberOfCards); + resetPlayer(2, opponentTwoName, startingNumberOfCards); + resetPlayer(3, opponentThreeName, startingNumberOfCards); } - private void resetPlayer(int playerNumber, String name) + private void resetPlayer(int playerNumber, String name, int startingNumberOfCards) { PlayerLabel label = getPlayerLabelForPlayerNumber(playerNumber); label.setText(name); if (label.isVisible()) { - initialisePlayer(playerNumber, name, label.getColour()); + initialisePlayer(playerNumber, name, label.getColour(), startingNumberOfCards); } } diff --git a/client/src/test/kotlin/http/SessionApiTest.kt b/client/src/test/kotlin/http/SessionApiTest.kt index f0d25e9..d1c91c2 100644 --- a/client/src/test/kotlin/http/SessionApiTest.kt +++ b/client/src/test/kotlin/http/SessionApiTest.kt @@ -134,7 +134,7 @@ class SessionApiTest : AbstractClientTest() { SessionApi(httpClient).updateAchievementCount(8) verify { - httpClient.doCall( + httpClient.doCall( HttpMethod.POST, Routes.ACHIEVEMENT_COUNT, UpdateAchievementCountRequest(8), diff --git a/server/src/main/kotlin/room/Room.kt b/server/src/main/kotlin/room/Room.kt index 6a96de3..be70533 100644 --- a/server/src/main/kotlin/room/Room.kt +++ b/server/src/main/kotlin/room/Room.kt @@ -22,7 +22,7 @@ import util.XmlBuilderServer import util.XmlConstants import utils.CoreGlobals.logger -class Room( +data class Room( override val id: UUID, private val baseName: String, val settings: GameSettings, @@ -64,7 +64,7 @@ class Room( if (existingUsername != null) { logger.info( "seatTaken", - "$username tried to join $name as player $playerNumber but seat was taken by $existingUsername" + "$username tried to join $name as player $playerNumber but seat was taken by $existingUsername", ) return -1 } @@ -85,7 +85,7 @@ class Room( } fun removePlayer(username: String, fireLobbyChanged: Boolean) { - for (playerNumber in 0 ..< capacity) { + for (playerNumber in 0..() - for (i in 0 ..< capacity) { + for (i in 0.. 0) { activePlayers++ @@ -414,7 +414,7 @@ class Room( if (previousGame == null) { logger.warn( "staleGameId", - "Tried to get next game for gameId $previousGameIdFromClient but previous game was null. Current: ${currentGame.gameId}" + "Tried to get next game for gameId $previousGameIdFromClient but previous game was null. Current: ${currentGame.gameId}", ) return currentGame } @@ -426,7 +426,7 @@ class Room( logger.warn( "staleGameId", - "Tried to get next game for gameId $previousGameIdFromClient but this did not match. Previous [${previousGame?.gameId}], Current [${currentGame.gameId}]" + "Tried to get next game for gameId $previousGameIdFromClient but this did not match. Previous [${previousGame?.gameId}], Current [${currentGame.gameId}]", ) return currentGame @@ -445,7 +445,7 @@ class Room( gameId: String, playerNumber: Int, roundNumber: Int, - newBid: Bid? + newBid: Bid?, ): Boolean { val game = getGameForId(gameId) @@ -474,7 +474,7 @@ class Room( uscs, chatMessage, XmlConstants.SOCKET_NAME_CHAT, - false + false, ) } diff --git a/server/src/test/kotlin/room/RoomTest.kt b/server/src/test/kotlin/room/RoomTest.kt new file mode 100644 index 0000000..2bb3937 --- /dev/null +++ b/server/src/test/kotlin/room/RoomTest.kt @@ -0,0 +1,36 @@ +package room + +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test +import testCore.AbstractTest +import util.makeGameSettings +import util.makeRoom + +class RoomTest : AbstractTest() { + @Test + fun `should report correct name`() { + makeRoom(baseName = "Mercury", index = 1).name shouldBe "Mercury 1" + makeRoom(baseName = "Carbon", index = 5).name shouldBe "Carbon 5" + } + + @Test + fun `should report if room is a copy`() { + makeRoom(baseName = "Mercury", index = 1).isCopy shouldBe false + makeRoom(baseName = "Carbon", index = 2).isCopy shouldBe true + makeRoom(baseName = "Oxygen", index = 5).isCopy shouldBe true + } + + @Test + fun `should make a copy of a room`() { + val settings = makeGameSettings(includeMoons = true, jokerQuantity = 2) + val room = makeRoom(baseName = "Yttrium", index = 1, settings = settings, capacity = 3) + + val copy = room.makeCopy() + copy.id shouldNotBe room.id + copy.name shouldBe "Yttrium 2" + copy.settings shouldBe settings + copy.capacity shouldBe room.capacity + } +} diff --git a/server/src/test/kotlin/store/AbstractStoreTest.kt b/server/src/test/kotlin/store/AbstractStoreTest.kt index 572e0de..8597e79 100644 --- a/server/src/test/kotlin/store/AbstractStoreTest.kt +++ b/server/src/test/kotlin/store/AbstractStoreTest.kt @@ -5,8 +5,9 @@ import io.kotest.matchers.collections.shouldBeEmpty import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test +import testCore.AbstractTest -abstract class AbstractStoreTest> { +abstract class AbstractStoreTest> : AbstractTest() { abstract fun makeStore(): Store abstract fun makeIdA(): K diff --git a/server/src/test/kotlin/store/RoomStoreTest.kt b/server/src/test/kotlin/store/RoomStoreTest.kt new file mode 100644 index 0000000..893f361 --- /dev/null +++ b/server/src/test/kotlin/store/RoomStoreTest.kt @@ -0,0 +1,76 @@ +package store + +import game.GameMode +import game.GameSettings +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder +import io.kotest.matchers.collections.shouldHaveSize +import io.kotest.matchers.shouldBe +import java.util.UUID +import org.junit.jupiter.api.Test +import room.Room + +class RoomStoreTest : AbstractStoreTest() { + override fun makeStore() = RoomStore() + + override fun makeIdA(): UUID = UUID.fromString("e115f1ce-0021-4653-9888-ea330b07f3a8") + + override fun makeIdB(): UUID = UUID.fromString("7a10f6a0-03bc-4e95-a4cb-44b92582f016") + + override fun makeItemA(id: UUID) = Room(id, "Mercury", GameSettings(GameMode.Entropy), 2) + + override fun makeItemB(id: UUID) = Room(id, "Oxygen", GameSettings(GameMode.Vectropy), 3) + + @Test + fun `Should be able to find a room for name`() { + val store = makeStore() + val roomA = makeItemA(UUID.randomUUID()) + val roomB = makeItemB(UUID.randomUUID()) + store.putAll(roomA, roomB) + + store.findForName("Mercury 1") shouldBe roomA + store.findForName("Oxygen 1") shouldBe roomB + store.findForName("Mercury 2") shouldBe null + } + + @Test + fun `Reset should remove all room copies`() { + val store = makeStore() + + val room = makeItemA(UUID.randomUUID()) + val roomCopy = room.makeCopy() + val roomCopy2 = roomCopy.makeCopy() + + store.putAll(room, roomCopy, roomCopy2) + store.reset() + + store.getAll().shouldContainExactly(room) + } + + @Test + fun `Should add a copy`() { + val store = makeStore() + + val room = makeItemA(UUID.randomUUID()) + store.put(room) + store.addCopy(room) + + val rooms = store.getAll() + rooms.shouldHaveSize(2) + rooms.map { it.name }.shouldContainExactlyInAnyOrder("Mercury 1", "Mercury 2") + } + + @Test + fun `Should not add a copy if it would be redundant`() { + val store = makeStore() + + val room = makeItemA(UUID.randomUUID()) + store.put(room) + store.addCopy(room) + store.getAll().shouldHaveSize(2) + + store.addCopy(room) + store.getAll().shouldHaveSize(2) + verifyLog("roomExists") + } +} diff --git a/server/src/test/kotlin/util/TestFactory.kt b/server/src/test/kotlin/util/TestFactory.kt index 8a357e8..a5248a8 100644 --- a/server/src/test/kotlin/util/TestFactory.kt +++ b/server/src/test/kotlin/util/TestFactory.kt @@ -5,6 +5,7 @@ import auth.UserConnection import game.GameMode import game.GameSettings import java.util.* +import room.Room fun makeSession( id: UUID = UUID.randomUUID(), @@ -36,3 +37,11 @@ fun makeGameSettings( cardReveal, illegalAllowed, ) + +fun makeRoom( + id: UUID = UUID.randomUUID(), + baseName: String = "Sodium", + settings: GameSettings = GameSettings(GameMode.Entropy), + capacity: Int = 2, + index: Int = 1, +) = Room(id, baseName, settings, capacity, index)