Skip to content

Commit

Permalink
room copying
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Nov 7, 2024
1 parent cc69fd1 commit 0a4a161
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 59 deletions.
52 changes: 8 additions & 44 deletions server/src/main/java/server/EntropyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,61 +216,25 @@ public List<OnlineMessage> 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<String> 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<Room> getRooms() {
Expand Down
3 changes: 2 additions & 1 deletion server/src/main/java/util/XmlBuilderServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 17 additions & 14 deletions server/src/main/kotlin/room/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, String>()
private val hmFormerPlayerByPlayerNumber: ConcurrentHashMap<Int, String> = ConcurrentHashMap()
val chatHistory: MutableList<OnlineMessage> = mutableListOf()
private val currentPlayers: MutableList<String> = mutableListOf()
private val observers: MutableList<String> = mutableListOf()
private var previousGame: GameWrapper? = null
private var currentGame: GameWrapper? = null
private var currentGame: GameWrapper = initialiseGame()

val isFull: Boolean
get() = currentPlayers.size == capacity
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<Int, Int>()
Expand All @@ -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(
Expand Down Expand Up @@ -469,5 +468,9 @@ class Room(
server.sendViaNotificationSocket(uscs, chatMessage, XmlConstants.SOCKET_NAME_CHAT, false)
}

fun makeCopy(): Room {
return this
}

override fun toString() = name
}

0 comments on commit 0a4a161

Please sign in to comment.