Skip to content

Commit

Permalink
extract out RoomFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Nov 10, 2024
1 parent 3687837 commit 265dd49
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 106 deletions.
14 changes: 7 additions & 7 deletions core/src/main/kotlin/game/GameSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package game

data class GameSettings(
val mode: GameMode,
val jokerQuantity: Int,
val jokerValue: Int,
val includeMoons: Boolean,
val includeStars: Boolean,
val negativeJacks: Boolean,
val cardReveal: Boolean,
val illegalAllowed: Boolean
val jokerQuantity: Int = 0,
val jokerValue: Int = 0,
val includeMoons: Boolean = false,
val includeStars: Boolean = false,
val negativeJacks: Boolean = false,
val cardReveal: Boolean = false,
val illegalAllowed: Boolean = false
)
67 changes: 2 additions & 65 deletions server/src/main/java/server/EntropyServer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package server;

import auth.UserConnection;
import game.GameMode;
import game.GameSettings;
import logging.LoggerUncaughtExceptionHandler;
import object.OnlineMessage;
import object.ServerRunnable;
import object.ServerThread;
import room.Room;
import room.RoomFactory;
import util.*;

import java.util.ArrayList;
Expand Down Expand Up @@ -38,7 +37,7 @@ public static void main() {
private void onStart() {
Debug.appendBanner("Start-Up");

registerDefaultRooms();
RoomFactory.INSTANCE.registerStarterRooms();

Debug.append("Starting permanent threads");

Expand All @@ -47,68 +46,6 @@ private void onStart() {
Debug.appendBanner("Server is ready - accepting connections");
}

private void registerDefaultRooms() {
Debug.append("Creating rooms...");

//Entropy
//2 player
registerNewRoom("Potassium 1", GameMode.Entropy, 2, 0, 2);
registerNewRoom("Zinc 1", GameMode.Entropy, 2, 2, 2);
registerNewRoom("Helium 1", GameMode.Entropy, 2, 4, 3);
registerNewRoom("Magnesium 1", GameMode.Entropy, 2, 2, 2, true, true, true, false, false);
registerNewRoom("Cobalt 1", GameMode.Entropy, 2, 0, 2, true, true, false, false, false);
registerNewRoom("Chlorine 1", GameMode.Entropy, 2, 0, 2, true, true, false, true, false);
registerNewRoom("Gold 1", GameMode.Entropy, 2, 0, 2, false, false, true, false, false);
registerNewRoom("Lithium 1", GameMode.Entropy, 2, 2, 2, false, false, false, true, true);
registerNewRoom("Beryllium 1", GameMode.Entropy, 2, 0, 2, true, true, false, false, true);

//3 player
registerNewRoom("Bromine 1", GameMode.Entropy, 3, 0, 2);
registerNewRoom("Argon 1", GameMode.Entropy, 3, 2, 2);
registerNewRoom("Hydrogen 1", GameMode.Entropy, 3, 4, 3);
registerNewRoom("Zirconium 1", GameMode.Entropy, 3, 2, 2, true, true, true, false, false);
registerNewRoom("Calcium 1", GameMode.Entropy, 3, 0, 2, true, true, false, false, false);
registerNewRoom("Iron 1", GameMode.Entropy, 3, 2, 2, true, false, false, true, false);
registerNewRoom("Palladium 1", GameMode.Entropy, 3, 3, 3, true, true, true, false, true);

//4 player
registerNewRoom("Nickel 1", GameMode.Entropy, 4, 0, 2);
registerNewRoom("Sodium 1", GameMode.Entropy, 4, 2, 2);
registerNewRoom("Phosphorus 1", GameMode.Entropy, 4, 4, 3);
registerNewRoom("Titanium 1", GameMode.Entropy, 4, 0, 2, true, true, false, true, false);
registerNewRoom("Gallium 1", GameMode.Entropy, 4, 2, 2, true, true, false, false, true);

//Vectropy
//2 player
registerNewRoom("Oxygen 1", GameMode.Vectropy, 2, 0, 2);
registerNewRoom("Neon 1", GameMode.Vectropy, 2, 2, 2);
registerNewRoom("Copper 1", GameMode.Vectropy, 2, 4, 3);
registerNewRoom("Manganese 1", GameMode.Vectropy, 2, 2, 2, true, true, true, false, false);
registerNewRoom("Selenium 1", GameMode.Vectropy, 2, 0, 2, true, true, false, false, false);
registerNewRoom("Chromium 1", GameMode.Vectropy, 2, 0, 2, true, true, false, true, false);
registerNewRoom("Silver 1", GameMode.Vectropy, 2, 0, 2, false, false, true, false, false);
registerNewRoom("Antimony 1", GameMode.Vectropy, 2, 2, 2, false, false, false, true, true);
registerNewRoom("Tungsten 1", GameMode.Vectropy, 2, 0, 2, true, true, false, false, true);

//3 player
registerNewRoom("Carbon 1", GameMode.Vectropy, 3, 0, 2);
registerNewRoom("Silicon 1", GameMode.Vectropy, 3, 2, 2);
registerNewRoom("Nitrogen 1", GameMode.Vectropy, 3, 4, 3);
registerNewRoom("Sulphur 1", GameMode.Vectropy, 3, 2, 2, true, true, true, false, false);
registerNewRoom("Fluorine 1", GameMode.Vectropy, 3, 0, 2, true, true, false, false, false);
registerNewRoom("Tin 1", GameMode.Vectropy, 3, 2, 2, true, false, false, true, false);
registerNewRoom("Indium 1", GameMode.Vectropy, 3, 3, 3, true, true, true, false, true);

//4 player
registerNewRoom("Iodine 1", GameMode.Vectropy, 4, 0, 2);
registerNewRoom("Lead 1", GameMode.Vectropy, 4, 2, 2);
registerNewRoom("Uranium 1", GameMode.Vectropy, 4, 4, 3);
registerNewRoom("Vanadium 1", GameMode.Vectropy, 4, 0, 2, true, true, false, true, false);
registerNewRoom("Xenon 1", GameMode.Vectropy, 4, 2, 2, true, true, false, false, true);

Debug.append("Finished creating " + hmRoomByName.size() + " rooms");
}

private void startListenerThreads() {
try {
ServerThread listenerThread = new ServerThread(new MessageListener(this, SERVER_PORT_NUMBER));
Expand Down
66 changes: 36 additions & 30 deletions server/src/main/kotlin/room/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import `object`.HandDetails
import `object`.LeftBid
import `object`.OnlineMessage
import `object`.Player
import server.EntropyServer
import util.CardsUtil
import util.EntropyUtil
import util.ServerGlobals
Expand All @@ -23,11 +22,10 @@ import util.XmlConstants
import utils.CoreGlobals.logger

class Room(
val name: String,
private val baseName: String,
val settings: GameSettings,
val capacity: Int,
val isCopy: Boolean,
private val server: EntropyServer
private val index: Int = 1,
) {
private val hmPlayerByPlayerNumber = ExtendedConcurrentHashMap<Int, String>()
private val hmFormerPlayerByPlayerNumber: ConcurrentHashMap<Int, String> = ConcurrentHashMap()
Expand All @@ -37,6 +35,12 @@ class Room(
private var previousGame: GameWrapper? = null
private var currentGame: GameWrapper = initialiseGame()

val name: String
get() = "$baseName $index"

val isCopy: Boolean
get() = index > 1

val isFull: Boolean
get() = currentPlayers.size == capacity

Expand Down Expand Up @@ -89,28 +93,28 @@ class Room(
notifyAllPlayersOfPlayerChange(username, true)

// The game has not started
if (currentGame!!.gameStartMillis == -1L) {
if (currentGame.gameStartMillis == -1L) {
// Unset the countdown if it's going, reset current capacity and get out of this
// madness
currentGame!!.countdownStartMillis = -1
currentGame.countdownStartMillis = -1
resetCurrentPlayers(fireLobbyChanged)
return
}

// There is a game in progress
if (currentGame!!.gameEndMillis == -1L) {
if (currentGame.gameEndMillis == -1L) {
val bid = LeftBid()
val player =
Player(playerNumber, EntropyUtil.getColourForPlayerNumber(playerNumber))
player.name = username
bid.player = player

val history: BidHistory = currentGame!!.currentBidHistory
val history: BidHistory = currentGame.currentBidHistory
history.addBidForPlayer(playerNumber, bid)

// Moved this into here as otherwise we set it to 0 incorrectly and a person
// ends up with no cards!
val details: HandDetails = currentGame!!.currentRoundDetails
val details: HandDetails = currentGame.currentRoundDetails
val hmHandSizeByPlayerNumber = details.handSizes
hmHandSizeByPlayerNumber[playerNumber] = 0
}
Expand Down Expand Up @@ -143,7 +147,7 @@ class Room(
}

val uscs: List<UserConnection> = uscStore.getAllForNames(usersToNotify)
server.sendViaNotificationSocket(
ServerGlobals.server.sendViaNotificationSocket(
uscs,
notification,
XmlConstants.SOCKET_NAME_GAME,
Expand Down Expand Up @@ -274,16 +278,15 @@ class Room(
}

private fun setUpNextRound(losingPlayerNumber: Int) {
val currentRoundNumber: Int = currentGame!!.roundNumber
val currentRoundNumber: Int = currentGame.roundNumber

var nextRoundDetails: HandDetails? =
currentGame!!.getDetailsForRound(currentRoundNumber + 1)
var nextRoundDetails: HandDetails? = currentGame.getDetailsForRound(currentRoundNumber + 1)
if (nextRoundDetails != null) {
logger.error("doubleRound", "Trying to set up next round but it's not null. Room $name")
return
}

val currentRoundDetails: HandDetails = currentGame!!.getDetailsForRound(currentRoundNumber)
val currentRoundDetails: HandDetails = currentGame.getDetailsForRound(currentRoundNumber)
val hmHandSizeByPlayerNumber = currentRoundDetails.handSizes
val handSize = hmHandSizeByPlayerNumber.getValue(losingPlayerNumber)
val newHandSize: Int = max(0, handSize - 1)
Expand All @@ -302,13 +305,13 @@ class Room(
dealHandsHashMap(hmHandSizeByPlayerNumberForNextRound)

nextRoundDetails.hands = hmHandByPlayerNumber
currentGame!!.setDetailsForRound(currentRoundNumber + 1, nextRoundDetails)
currentGame.setDetailsForRound(currentRoundNumber + 1, nextRoundDetails)

val history = BidHistory()
history.personToStart = losingPlayerNumber
currentGame!!.setBidHistoryForRound(currentRoundNumber + 1, history)
currentGame.setBidHistoryForRound(currentRoundNumber + 1, history)

currentGame!!.roundNumber = currentRoundNumber + 1
currentGame.roundNumber = currentRoundNumber + 1

val newRoundNotification: String =
XmlBuilderServer.factoryNewRoundNotification(
Expand All @@ -325,7 +328,7 @@ class Room(
): ConcurrentHashMap<Int, List<String>> {
val hmHandByPlayerNumber: ConcurrentHashMap<Int, List<String>> = ConcurrentHashMap()

val seed: Long = server.generateSeed()
val seed: Long = ServerGlobals.server.generateSeed()
val deck =
CardsUtil.createAndShuffleDeck(
true,
Expand Down Expand Up @@ -390,8 +393,8 @@ class Room(
}

private fun getGameForId(gameId: String): GameWrapper {
if (gameId == currentGame?.gameId) {
return currentGame!!
if (gameId == currentGame.gameId) {
return currentGame
}

if ((gameId == previousGame?.gameId)) {
Expand All @@ -401,38 +404,38 @@ class Room(
throw RuntimeException("Got a null game for room $name and gameId $gameId")
}

fun getNextGameForId(previousGameIdFromClient: String): GameWrapper? {
fun getNextGameForId(previousGameIdFromClient: String): GameWrapper {
if (previousGameIdFromClient.isEmpty()) {
return currentGame
}

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
}

val previousGameId: String = previousGame!!.gameId
if ((previousGameId == previousGameIdFromClient)) {
if (previousGameId == previousGameIdFromClient) {
return currentGame
}

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
}

fun getLastBidForPlayer(playerNumber: Int, roundNumber: Int): Bid? {
if ((currentGame == null || playerNumber == -1)) {
if (playerNumber == -1) {
return null
}

val history: BidHistory = currentGame!!.getBidHistoryForRound(roundNumber)
val history: BidHistory = currentGame.getBidHistoryForRound(roundNumber)
return history.getLastBidForPlayer(playerNumber)
}

Expand Down Expand Up @@ -465,12 +468,15 @@ class Room(

val chatMessage = XmlBuilderServer.getChatNotification(name, message)
val uscs = uscStore.getAllForNames(allUsersInRoom)
server.sendViaNotificationSocket(uscs, chatMessage, XmlConstants.SOCKET_NAME_CHAT, false)
ServerGlobals.server.sendViaNotificationSocket(
uscs,
chatMessage,
XmlConstants.SOCKET_NAME_CHAT,
false
)
}

fun makeCopy(): Room {
return this
}
fun makeCopy() = Room(baseName, settings, capacity, index + 1)

override fun toString() = name
}
57 changes: 57 additions & 0 deletions server/src/main/kotlin/room/RoomFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package room

import game.GameMode
import game.GameSettings
import util.ServerGlobals

object RoomFactory {
fun registerStarterRooms() {
makeStarterRooms().forEach(ServerGlobals.server::registerNewRoom)
}

private fun makeStarterRooms(): List<Room> {
val elementGenerator = getElementNames().iterator()

return GameMode.entries.flatMap { gameMode ->
val configs = getConfigs(gameMode)
val capacities = listOf(2, 3, 4)

capacities.flatMap { capacity ->
configs.map { Room(elementGenerator.next(), it, capacity) }
}
}
}

private fun getElementNames(): List<String> {
val inputStream =
javaClass.getResourceAsStream("/elements.txt")
?: throw RuntimeException("Unable to read elements list")

return inputStream.bufferedReader().readLines()
}

private fun getConfigs(mode: GameMode): List<GameSettings> =
listOf(
GameSettings(mode),
GameSettings(mode, illegalAllowed = true),
GameSettings(mode, jokerQuantity = 2, jokerValue = 2),
GameSettings(mode, includeMoons = true, includeStars = true),
GameSettings(mode, includeMoons = true, includeStars = true, cardReveal = true),
GameSettings(
mode,
jokerQuantity = 2,
jokerValue = 2,
negativeJacks = true,
cardReveal = true
),
GameSettings(mode, includeMoons = true, includeStars = true, negativeJacks = true),
GameSettings(
mode,
jokerQuantity = 2,
jokerValue = 2,
includeMoons = true,
includeStars = true,
illegalAllowed = true
)
)
}
Loading

0 comments on commit 265dd49

Please sign in to comment.