Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/src/api/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type Room = {
difficulty: Difficulty,
duration: number,
size: number,
numProblems: number,
};

export type CreateRoomParams = {
Expand All @@ -28,6 +29,7 @@ export type UpdateSettingsParams = {
difficulty?: Difficulty,
duration?: number,
size?: number,
numProblems?: number,
};

export type ChangeHostParams = {
Expand Down
55 changes: 55 additions & 0 deletions frontend/src/views/Lobby.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ function LobbyPage() {
const [difficulty, setDifficulty] = useState<Difficulty | null>(null);
const [duration, setDuration] = useState<number | undefined>(15);
const [size, setSize] = useState<number | undefined>(10);
const [numProblems, setNumProblems] = useState<number>(1);
const [mousePosition, setMousePosition] = useState<Coordinate>({ x: 0, y: 0 });
const [hoverVisible, setHoverVisible] = useState<boolean>(false);

Expand Down Expand Up @@ -174,6 +175,7 @@ function LobbyPage() {
setDifficulty(room.difficulty);
setDuration(room.duration / 60);
setSize(room.size);
setNumProblems(room.numProblems);
};

// Function to determine if the given user is the host or not
Expand Down Expand Up @@ -386,6 +388,25 @@ function LobbyPage() {
});
};

const updateNumProblems = () => {
setError('');
setLoading(true);
const prevNumProblems = numProblems;
const settings = {
initiator: currentUser!,
numProblems,
};

updateRoomSettings(currentRoomId, settings)
.then(() => setLoading(false))
.catch((err) => {
setLoading(false);
setError(err.message);
// Set numProblems back to original if REST call failed
setNumProblems(prevNumProblems);
});
};

/**
* Display the passed-in list of users on the UI, either as
* active or inactive.
Expand Down Expand Up @@ -711,6 +732,40 @@ function LobbyPage() {
/>
</SliderContainer>
</HoverContainerSlider>
<NoMarginMediumText>Number of Problems</NoMarginMediumText>
<NoMarginSubtitleText>
{`${numProblems} problem${numProblems === 1 ? '' : 's'}`}
</NoMarginSubtitleText>
<HoverContainerSlider>
<HoverElementSlider
enabled={isHost(currentUser)}
onMouseEnter={() => {
if (!isHost(currentUser)) {
setHoverVisible(true);
}
}}
onMouseLeave={() => {
if (!isHost(currentUser)) {
setHoverVisible(false);
}
}}
/>
<SliderContainer>
<Slider
min={1}
max={10}
value={numProblems}
disabled={!isHost(currentUser)}
onChange={(e) => {
const newNumProblems = Number(e.target);
if (newNumProblems >= 1 && newNumProblems <= 10) {
setNumProblems(newNumProblems);
}
}}
onMouseUp={updateNumProblems}
/>
</SliderContainer>
</HoverContainerSlider>
</BackgroundContainer>
</RoomSettingsContainer>
</FlexBareContainerLeft>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,39 @@ public void startGameSuccess() {
assertEquals(room.getDuration(), game.getGameTimer().getDuration());
}

@Test
public void startGameWithMultipleQuestionsSuccess() {
User host = new User();
host.setNickname(NICKNAME);
host.setUserId(USER_ID);

Room room = new Room();
room.setRoomId(ROOM_ID);
room.setHost(host);
room.setDifficulty(ProblemDifficulty.RANDOM);
room.setDuration(DURATION);
room.setNumProblems(6);

StartGameRequest request = new StartGameRequest();
request.setInitiator(UserMapper.toDto(host));

Mockito.doReturn(room).when(repository).findRoomByRoomId(ROOM_ID);
RoomDto response = gameService.startGame(ROOM_ID, request);

verify(socketService).sendSocketUpdate(eq(response));

assertEquals(ROOM_ID, response.getRoomId());
assertTrue(response.isActive());

// Game object is created when the room chooses to start
Game game = gameService.getGameFromRoomId(ROOM_ID);
assertNotNull(game);

assertNotNull(game.getGameTimer());
assertEquals(room.getDuration(), game.getGameTimer().getDuration());
assertEquals(room.getNumProblems(), response.getNumProblems());
}
Comment thread
alankbi marked this conversation as resolved.

@Test
public void startGameRoomNotFound() {
UserDto user = new UserDto();
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/rocketden/main/service/RoomServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,31 @@ public void manyUsersJoiningAnInfinitelySizedRoomSuccess() {
assertEquals(102, room.getUsers().size());
}

@Test
public void setInvalidNumProblemsFailure() {
/**
* Verify update settings request fails when numProblems is
* set to outside of the allowable range
*/
User host = new User();
host.setNickname(NICKNAME);

Room room = new Room();
room.setRoomId(ROOM_ID);
room.setHost(host);
room.addUser(host);

UpdateSettingsRequest request = new UpdateSettingsRequest();
request.setInitiator(UserMapper.toDto(host));
request.setNumProblems(15);

// Mock repository to return room when called
Mockito.doReturn(room).when(repository).findRoomByRoomId(eq(ROOM_ID));
ApiException exception = assertThrows(ApiException.class, () -> roomService.updateRoomSettings(ROOM_ID, request));

verify(repository).findRoomByRoomId(ROOM_ID);
assertEquals(ProblemError.INVALID_NUMBER_REQUEST, exception.getError());
}

@Test
public void getRoomSuccess() {
Expand Down