From 6eb028d4dfd15955d7408567905e7a857e648b37 Mon Sep 17 00:00:00 2001 From: Zehao-Zhang Date: Mon, 20 May 2024 11:05:33 +0200 Subject: [PATCH 1/2] restrict max 10 rooms in lobby --- .../ifi/hase/soprafs24/service/GameService.java | 14 +++++++------- .../ifi/hase/soprafs24/service/RoomService.java | 5 ++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/uzh/ifi/hase/soprafs24/service/GameService.java b/src/main/java/ch/uzh/ifi/hase/soprafs24/service/GameService.java index 9551d19..1b7300a 100644 --- a/src/main/java/ch/uzh/ifi/hase/soprafs24/service/GameService.java +++ b/src/main/java/ch/uzh/ifi/hase/soprafs24/service/GameService.java @@ -178,13 +178,13 @@ public void startGame(Room room) { socketService.broadcastGameinfo(game.getRoomId(), "gameover"); socketService.broadcastPlayerInfo(game.getRoomId(), "gameover"); socketService.broadcastLobbyInfo(); - try { - Thread.sleep(30000);//20000 - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new RuntimeException(e); - } - System.out.println("End Revealingļ¼š"+ LocalDateTime.now()); +// try { +// Thread.sleep(30000);//20000 +// } catch (InterruptedException e) { +// Thread.currentThread().interrupt(); +// throw new RuntimeException(e); +// } +// System.out.println("End Revealingļ¼š"+ LocalDateTime.now()); // Display the leaderboard for 2 minutes, and dismiss the room in advance if all // players leave if (gameRepository.findByRoomId(game.getRoomId()).isPresent()){ diff --git a/src/main/java/ch/uzh/ifi/hase/soprafs24/service/RoomService.java b/src/main/java/ch/uzh/ifi/hase/soprafs24/service/RoomService.java index a85b1da..4210d80 100644 --- a/src/main/java/ch/uzh/ifi/hase/soprafs24/service/RoomService.java +++ b/src/main/java/ch/uzh/ifi/hase/soprafs24/service/RoomService.java @@ -64,6 +64,9 @@ public boolean checkRoomExists(String roomId) { // Here we create a new room, and we need to set the room property and theme // according to the input from client public Room createRoom(Room newRoom) { + if (roomRepository.findAll().size() >= 10){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Only 10 rooms are allowed to exist at the same time!"); + } Optional existingRoom = roomRepository.findByRoomId(newRoom.getRoomId()); if (existingRoom.isPresent()) { throw new ResponseStatusException(HttpStatus.CONFLICT, "Room already exists"); @@ -163,7 +166,7 @@ public List getWords(String theme) throws IOException { " \"messages\": [\n" + " {\n" + " \"role\": \"user\",\n" + - " \"content\": \"Generate a JSON list of noun words with the theme '" + theme + " \"content\": \"Generate a JSON list of noun words within the theme '" + theme + "' and ensure each word has no more than four syllables and all in lowercase in the following format: {\\\"words\\\": [\\\"word1\\\", \\\"word2\\\", \\\"word3\\\", ...]}\"\n" + " }\n" + From f91d32eba355783f8edf8624a4a489cdcf061045 Mon Sep 17 00:00:00 2001 From: Zihan Liu Date: Mon, 20 May 2024 11:13:44 +0200 Subject: [PATCH 2/2] Add test for new feature --- .../ifi/hase/soprafs24/service/RoomServiceTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/ch/uzh/ifi/hase/soprafs24/service/RoomServiceTest.java b/src/test/java/ch/uzh/ifi/hase/soprafs24/service/RoomServiceTest.java index 243f83f..84ca5fc 100644 --- a/src/test/java/ch/uzh/ifi/hase/soprafs24/service/RoomServiceTest.java +++ b/src/test/java/ch/uzh/ifi/hase/soprafs24/service/RoomServiceTest.java @@ -174,6 +174,17 @@ void createRoom_EmptyTheme_ThrowsException() { assertThrows(ResponseStatusException.class, () -> roomService.createRoom(newRoom)); } + @Test + void createRoom_tooManyRoom(){ + Room room = new Room(); + List roomList = new ArrayList<>(); + for (int i = 0; i < 100; i++) { + roomList.add(room); + } + when(roomRepository.findAll()).thenReturn(roomList); + assertThrows(ResponseStatusException.class, () -> roomService.createRoom(room)); + } + @Test void enterRoom_RoomIsFull_ThrowsException() { Room room = new Room();