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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;
import socket_server.common.exception.ErrorType;
Expand Down Expand Up @@ -34,7 +35,7 @@
@Service
@RequiredArgsConstructor
public class GameStartService {

@Qualifier("taskScheduler")
private final TaskScheduler taskScheduler;
private final RoomUserService roomUserService;
private final GamePlayerRepository gamePlayerRepository;
Expand Down Expand Up @@ -91,6 +92,16 @@ public void startGame(String roomId, String userUuid, ErrorType errorType) {

// 8. 5초 후 게임 시작(EntryPoint)
taskScheduler.schedule(() -> roundStartService.startNextRound(roomId), Instant.now().plusSeconds(5));

List<RoomUserInfo> users = new ArrayList<>(roomUserRepository.findUsersByRoomId(roomId, errorType));
String ownerUuid = roomMetadata.getOwnerUuid();

for (RoomUserInfo user : users) {
if (!user.getUserUuid().equals(ownerUuid)) {
user.setReady(false);
roomUserRepository.saveUserToRoom(user, roomId, errorType);
}
}
}

private void saveGame(Game game) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ private void broadcastUpdatedRoomInfoToListAndRoom(String roomId, RoomMetadata m
RoomSummaryRes summary = RoomSummaryRes.of(metadata, currentUser);
lobbyBroadCaster.broadcastToRoomList("SYSTEM", RoomEventType.UPDATE, summary);

List<RoomUserInfo> userList = roomUserRepository.findUsersByRoomId(roomId, ROOM_ERROR);
List<RoomUserInfo> userList = new ArrayList<>(roomUserRepository.findUsersByRoomId(roomId, ROOM_ERROR));
sortUserListWithOwnerFirst(userList, metadata.getOwnerUuid());
RoomDetailRes roomDetailRes = new RoomDetailRes(RoomInfoRes.from(metadata), userList);
roomBroadcaster.broadcastToRoom(roomId, "SYSTEM", RoomEventType.UPDATE, roomDetailRes);

Expand Down Expand Up @@ -224,8 +225,21 @@ public RoomDetailRes getRoomDetails(String roomId, String userUuid) {
}

RoomMetadata metadata = RoomMetadata.fromRedisMap(roomId, roomData);
List<RoomUserInfo> userList = roomUserRepository.findUsersByRoomId(roomId, ROOM_ERROR);
List<RoomUserInfo> userList = new ArrayList<>(roomUserRepository.findUsersByRoomId(roomId, ROOM_ERROR));
sortUserListWithOwnerFirst(userList, metadata.getOwnerUuid());

return new RoomDetailRes(RoomInfoRes.from(metadata), userList);
}

private void sortUserListWithOwnerFirst(List<RoomUserInfo> userList, String ownerUuid) {
userList.sort((u1, u2) -> {
if (u1.getUserUuid().equals(ownerUuid)) {
return -1;
}
if (u2.getUserUuid().equals(ownerUuid)) {
return 1;
}
return 0;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import socket_server.domain.room.repository.RoomRepository;
import socket_server.domain.room.repository.RoomUserRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -104,6 +105,10 @@ public void passRoomOwner(String roomId, String oldOwnerId, String newOwnerId) {
throw new SocketCustomException(ROOM_ERROR, RoomExceptionCode.NOT_ROOM_OWNER);
}

RoomUserInfo oldOwner = roomUserRepository.findUserInfoInRoom(roomId, oldOwnerId, ROOM_ERROR);
oldOwner.setReady(false);
roomUserRepository.saveUserToRoom(oldOwner, roomId, ROOM_ERROR);

RoomUserInfo newOwner = roomUserRepository.findUserInfoInRoom(roomId, newOwnerId, ROOM_ERROR);

changeRoomOwner(roomId, newOwner);
Expand Down Expand Up @@ -147,14 +152,18 @@ private void processUserExit(String roomId, String userUuid, boolean isKicked) {
}

public void changeRoomOwner(String roomId, RoomUserInfo newOwner) {
newOwner.setReady(true);
roomUserRepository.saveUserToRoom(newOwner, roomId, ROOM_ERROR);

roomRepository.updateAllFields(roomId, Map.of(
RoomField.OWNER_UUID.getRedisField(), newOwner.getUserUuid(),
RoomField.OWNER.getRedisField(), newOwner.getNickname()
));

RoomMetadata updatedMetadata = RoomMetadata.fromRedisMap(roomId, roomRepository.getRoomData(roomId));
RoomInfoRes roomInfoRes = RoomInfoRes.from(updatedMetadata);
List<RoomUserInfo> userList = roomUserRepository.findUsersByRoomId(roomId, ROOM_ERROR);
List<RoomUserInfo> userList = new ArrayList<>(roomUserRepository.findUsersByRoomId(roomId, ROOM_ERROR));
sortUserListWithOwnerFirst(userList, updatedMetadata.getOwnerUuid());
RoomDetailRes detailRes = new RoomDetailRes(roomInfoRes, userList);
roomBroadcaster.broadcastToRoom(roomId, newOwner.getUserUuid(), RoomEventType.UPDATE, detailRes);

Expand All @@ -164,6 +173,18 @@ public void changeRoomOwner(String roomId, RoomUserInfo newOwner) {
log.info("방장 권한이 {}에게 위임되었습니다. (roomId: {})", newOwner.getUserUuid(), roomId);
}

private void sortUserListWithOwnerFirst(List<RoomUserInfo> userList, String ownerUuid) {
userList.sort((u1, u2) -> {
if (u1.getUserUuid().equals(ownerUuid)) {
return -1;
}
if (u2.getUserUuid().equals(ownerUuid)) {
return 1;
}
return 0;
});
}

public RoomMetadata validateRoomOwnerAndGetRoomMetadata(String roomId, String userUuid) {
if(!validateRoomOwner(roomId, userUuid)){
throw new SocketCustomException(ROOM_ERROR, RoomExceptionCode.NOT_ROOM_OWNER);
Expand Down
Loading