Skip to content

Commit

Permalink
add tests and fix broken placeholder hands
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Nov 12, 2024
1 parent 9a3926f commit 08b8d56
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 35 deletions.
4 changes: 2 additions & 2 deletions client/src/main/java/online/screen/GameRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -893,7 +893,7 @@ private void processEndOfGame(int winningPlayerAdjusted)
return;
}

handPanel.resetPlayers();
handPanel.resetPlayers(5);
clearScreenAfterGameEnd();

int currentSize = hmPlayerByAdjustedPlayerNumber.size();
Expand Down
34 changes: 22 additions & 12 deletions client/src/main/java/screen/HandPanelMk2.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,16 @@ private void initialiseCardLabelsForPlayer(List<String> hand, CardLabel[] labels

refreshIcons(labels);
}

private void makePlaceholderHand(CardLabel[] labels, int handSize) {
for (int i=0; i<handSize; i++) {
labels[i].setFaceUp(false);
labels[i].setFaded(false);
labels[i].setCard(null);
}

refreshIcons(labels);
}

private void refreshIcons()
{
Expand Down Expand Up @@ -374,7 +384,7 @@ private void displayAndHighlightHand(int suitCode, CardLabel[] cards)
}
}

public void initialisePlayer(int playerNumber, String name, String colour)
public void initialisePlayer(int playerNumber, String name, String colour, int startingNumberOfCards)
{
PlayerLabel label = getPlayerLabelForPlayerNumber(playerNumber);
label.setText(name);
Expand All @@ -385,19 +395,19 @@ public void initialisePlayer(int playerNumber, String name, String colour)
{
case 0:
playerName = name;
initialiseCardLabelsForPlayer(new ArrayList<>(), 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 + "]");
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/test/kotlin/http/SessionApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class SessionApiTest : AbstractClientTest() {
SessionApi(httpClient).updateAchievementCount(8)

verify {
httpClient.doCall<UpdateAchievementCountRequest>(
httpClient.doCall<Unit>(
HttpMethod.POST,
Routes.ACHIEVEMENT_COUNT,
UpdateAchievementCountRequest(8),
Expand Down
38 changes: 19 additions & 19 deletions server/src/main/kotlin/room/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -85,7 +85,7 @@ class Room(
}

fun removePlayer(username: String, fireLobbyChanged: Boolean) {
for (playerNumber in 0 ..< capacity) {
for (playerNumber in 0..<capacity) {
val user: String? = hmPlayerByPlayerNumber[playerNumber]
if ((user != null && (username == user))) {
hmPlayerByPlayerNumber.remove(playerNumber)
Expand Down Expand Up @@ -141,7 +141,7 @@ class Room(
private fun notifyAllUsersViaGameSocket(
notification: String,
userToExclude: String?,
blocking: Boolean
blocking: Boolean,
) {
var usersToNotify = allUsersInRoom
if (userToExclude != null) {
Expand All @@ -153,7 +153,7 @@ class Room(
uscs,
notification,
XmlConstants.SOCKET_NAME_GAME,
blocking
blocking,
)
}

Expand All @@ -179,7 +179,7 @@ class Room(
@JvmOverloads
fun resetCurrentPlayers(fireLobbyChanged: Boolean = true) {
currentPlayers.clear()
for (i in 0 ..< capacity) {
for (i in 0..<capacity) {
val username: String? = hmPlayerByPlayerNumber[i]
if (username != null) {
currentPlayers.add(username)
Expand Down Expand Up @@ -218,7 +218,7 @@ class Room(

val details = HandDetails()
val hmHandSizeByPlayerNumber = ExtendedConcurrentHashMap<Int, Int>()
for (i in 0 ..< capacity) {
for (i in 0..<capacity) {
hmHandSizeByPlayerNumber[i] = 5
}

Expand All @@ -241,7 +241,7 @@ class Room(
roundNumber: Int,
playerNumber: Int,
challengedNumber: Int,
bid: Bid
bid: Bid,
) {
val game = getGameForId(gameId)
val details: HandDetails = game.getDetailsForRound(roundNumber)
Expand All @@ -260,7 +260,7 @@ class Room(
roundNumber: Int,
playerNumber: Int,
bidderNumber: Int,
bid: Bid
bid: Bid,
) {
val game = getGameForId(gameId)
val details: HandDetails = game.getDetailsForRound(roundNumber)
Expand All @@ -270,7 +270,7 @@ class Room(
hmHandByPlayerNumber,
settings.jokerValue,
settings.includeMoons,
settings.includeStars
settings.includeStars,
)
) {
setUpNextRound(bidderNumber)
Expand Down Expand Up @@ -319,7 +319,7 @@ class Room(
XmlBuilderServer.factoryNewRoundNotification(
this,
nextRoundDetails,
losingPlayerNumber
losingPlayerNumber,
)
notifyAllUsersViaGameSocket(newRoundNotification, null, false)
}
Expand All @@ -338,12 +338,12 @@ class Room(
settings.includeMoons,
settings.includeStars,
settings.negativeJacks,
seed
seed,
)

for (i in 0 ..< capacity) {
for (i in 0..<capacity) {
val size: Int = hmHandSizeByPlayerNumber.getValue(i)
val hand = (0 ..< size).map { deck.removeAt(0) }
val hand = (0..<size).map { deck.removeAt(0) }
hmHandByPlayerNumber[i] = hand
}

Expand All @@ -354,7 +354,7 @@ class Room(
var activePlayers = 0
var potentialWinner = 0

for (i in 0 ..< capacity) {
for (i in 0..<capacity) {
val handSize: Int = hmHandSizeByPlayerNumber.getValue(i)
if (handSize > 0) {
activePlayers++
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -445,7 +445,7 @@ class Room(
gameId: String,
playerNumber: Int,
roundNumber: Int,
newBid: Bid?
newBid: Bid?,
): Boolean {
val game = getGameForId(gameId)

Expand Down Expand Up @@ -474,7 +474,7 @@ class Room(
uscs,
chatMessage,
XmlConstants.SOCKET_NAME_CHAT,
false
false,
)
}

Expand Down
36 changes: 36 additions & 0 deletions server/src/test/kotlin/room/RoomTest.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 2 additions & 1 deletion server/src/test/kotlin/store/AbstractStoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, T : IHasId<K>> {
abstract class AbstractStoreTest<K, T : IHasId<K>> : AbstractTest() {
abstract fun makeStore(): Store<K, T>

abstract fun makeIdA(): K
Expand Down
76 changes: 76 additions & 0 deletions server/src/test/kotlin/store/RoomStoreTest.kt
Original file line number Diff line number Diff line change
@@ -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<UUID, Room>() {
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")
}
}
Loading

0 comments on commit 08b8d56

Please sign in to comment.