From 0a4a1615a9b86b2e60ee3d32ae813363fdbcac04 Mon Sep 17 00:00:00 2001 From: Alyssa Date: Thu, 7 Nov 2024 20:59:51 +0000 Subject: [PATCH] room copying --- .../src/main/java/server/EntropyServer.java | 52 +++---------------- .../src/main/java/util/XmlBuilderServer.java | 3 +- server/src/main/kotlin/room/Room.kt | 31 ++++++----- 3 files changed, 27 insertions(+), 59 deletions(-) diff --git a/server/src/main/java/server/EntropyServer.java b/server/src/main/java/server/EntropyServer.java index 1cd6bf1..9c2dc41 100644 --- a/server/src/main/java/server/EntropyServer.java +++ b/server/src/main/java/server/EntropyServer.java @@ -216,61 +216,25 @@ public List getChatHistory(String id) { return room.getChatHistory(); } - public Room registerNewRoom(String roomName, int capacity, GameSettings settings) { - return registerNewRoom(roomName, settings.getMode(), capacity, settings.getJokerQuantity(), settings.getJokerValue(), - settings.getIncludeMoons(), settings.getIncludeStars(), settings.getIllegalAllowed(), settings.getNegativeJacks(), - settings.getCardReveal()); - } - - private void registerNewRoom(String roomName, GameMode mode, int players, int jokerQuantity, int jokerValue) { - registerNewRoom(roomName, mode, players, jokerQuantity, jokerValue, false, false, false, false, false); - } - - private Room registerNewRoom(String roomName, GameMode mode, int players, int jokerQuantity, int jokerValue, - boolean includeMoons, boolean includeStars, boolean illegalAllowed, boolean negativeJacks, - boolean cardReveal) { + public Room registerNewRoom(Room room) { Iterator it = hmRoomByName.keySet().iterator(); for (; it.hasNext(); ) { String id = it.next(); - Room room = hmRoomByName.get(id); - - String nameToCheck = room.getName(); - if (nameToCheck.equals(roomName)) { - Debug.append("Not creating room " + nameToCheck + " as a room with that name already exists."); + Room existingRoom = hmRoomByName.get(id); + String nameToCheck = existingRoom.getName(); + if (nameToCheck.equals(room.getName())) { + logger.warn("duplicateRoom", "Not creating room " + nameToCheck + " as a room with that name already exists."); return null; } } - GameSettings settings = new GameSettings(mode, jokerQuantity, jokerValue, includeMoons, includeStars, - negativeJacks, cardReveal, illegalAllowed); + hmRoomByName.put(room.getName(), room); - Room room = new Room(roomName, settings, players, this); - room.initialiseGame(); - hmRoomByName.put(roomName, room); - - Debug.append("Room created: " + roomName); - return room; - } - - public Room registerCopy(Room room) { - String roomName = room.getName(); - int capacity = room.getCapacity(); - GameSettings settings = room.getSettings(); - - int index = roomName.indexOf(' ') + 1; - String roomNumberStr = roomName.substring(index); - int roomNumber = Integer.parseInt(roomNumberStr); - - String newRoomName = roomName.substring(0, index) + (roomNumber + 1); - - Room newRoom = registerNewRoom(newRoomName, capacity, settings); - - if (newRoom != null) { - newRoom.setCopy(true); + if (room.isCopy()) { ServerGlobals.lobbyService.lobbyChanged(); } - return newRoom; + return room; } public ArrayList getRooms() { diff --git a/server/src/main/java/util/XmlBuilderServer.java b/server/src/main/java/util/XmlBuilderServer.java index 7a501e5..f32022f 100644 --- a/server/src/main/java/util/XmlBuilderServer.java +++ b/server/src/main/java/util/XmlBuilderServer.java @@ -211,7 +211,8 @@ public static Document getRoomJoinResponse(Room room, String username, String ob if (room.isFull()) { - server.registerCopy(room); + var copy = room.makeCopy(); + server.registerNewRoom(copy); } } else diff --git a/server/src/main/kotlin/room/Room.kt b/server/src/main/kotlin/room/Room.kt index cf7e4b9..940f8b7 100644 --- a/server/src/main/kotlin/room/Room.kt +++ b/server/src/main/kotlin/room/Room.kt @@ -26,17 +26,16 @@ class Room( val name: String, val settings: GameSettings, val capacity: Int, + val isCopy: Boolean, private val server: EntropyServer ) { - var isCopy: Boolean = false - private val hmPlayerByPlayerNumber = ExtendedConcurrentHashMap() private val hmFormerPlayerByPlayerNumber: ConcurrentHashMap = ConcurrentHashMap() val chatHistory: MutableList = mutableListOf() private val currentPlayers: MutableList = mutableListOf() private val observers: MutableList = mutableListOf() private var previousGame: GameWrapper? = null - private var currentGame: GameWrapper? = null + private var currentGame: GameWrapper = initialiseGame() val isFull: Boolean get() = currentPlayers.size == capacity @@ -153,17 +152,18 @@ class Room( } private fun finishCurrentGame(winningPlayer: Int) { - val roundNumber: Int = currentGame!!.roundNumber + val roundNumber: Int = currentGame.roundNumber val winningUsername: String? = hmPlayerByPlayerNumber[winningPlayer] resetCurrentPlayers() - currentGame!!.winningPlayer = winningPlayer + currentGame.winningPlayer = winningPlayer if (roundNumber > 1) { val om = OnlineMessage("black", "$winningUsername won!", "Game") addToChatHistoryAndNotifyUsers(om) } - initialiseGame() + previousGame = currentGame.factoryCopy() + currentGame = initialiseGame() // Notify everyone that the game is over now we've finished setting up val notification: String = XmlBuilderServer.factoryGameOverNotification(this, winningPlayer) @@ -205,13 +205,10 @@ class Room( clearChatIfEmpty() } - fun initialiseGame() { + private fun initialiseGame(): GameWrapper { val gameId: String = "G" + System.currentTimeMillis() - if (currentGame != null) { - previousGame = currentGame!!.factoryCopy() - } - currentGame = GameWrapper(gameId) + val newGame = GameWrapper(gameId) val details = HandDetails() val hmHandSizeByPlayerNumber = ExtendedConcurrentHashMap() @@ -222,13 +219,15 @@ class Room( val hmHandByPlayerNumber = dealHandsHashMap(hmHandSizeByPlayerNumber) details.hands = hmHandByPlayerNumber details.handSizes = hmHandSizeByPlayerNumber - currentGame!!.setDetailsForRound(1, details) + newGame.setDetailsForRound(1, details) - val personToStart: Int = Random().nextInt(capacity) + val personToStart = Random().nextInt(capacity) val history = BidHistory() history.personToStart = personToStart - currentGame!!.setBidHistoryForRound(1, history) + newGame.setBidHistoryForRound(1, history) + + return newGame } fun handleChallenge( @@ -469,5 +468,9 @@ class Room( server.sendViaNotificationSocket(uscs, chatMessage, XmlConstants.SOCKET_NAME_CHAT, false) } + fun makeCopy(): Room { + return this + } + override fun toString() = name }