Skip to content

Commit

Permalink
feat: 직업 스킬 사용을 웹소켓으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
thdwoqor committed Nov 2, 2024
1 parent a221af4 commit d0b2b23
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package mafia.mafiatogether.common.interceptor;

import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import lombok.RequiredArgsConstructor;
import mafia.mafiatogether.common.util.AuthExtractor;
import org.springframework.context.annotation.Configuration;
Expand All @@ -11,16 +14,12 @@
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

@Component
@Configuration
@RequiredArgsConstructor
public class StompChannelInterceptor implements ChannelInterceptor {

private static final String SUBSCRIBE_FORMAT = "/sub/chat/%s";
private static final String SUBSCRIBE_FORMAT = "%s/%s";
private static final String PUBLISHING_FORMAT = "%s/%s/%s";

private final Map<StompCommand, Consumer<StompHeaderAccessor>> actionByCommand = Map.of(
Expand All @@ -44,7 +43,9 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {

private void consumeWhenSubscribe(StompHeaderAccessor headerAccessor) {
String[] information = getInformation(headerAccessor);
headerAccessor.setDestination(SUBSCRIBE_FORMAT.formatted(information[0]));
String prefixUrl = headerAccessor.getDestination()
.substring(0, headerAccessor.getDestination().lastIndexOf('/'));
headerAccessor.setDestination(SUBSCRIBE_FORMAT.formatted(prefixUrl, information[0]));
}

private String[] getInformation(StompHeaderAccessor headerAccessor) {
Expand Down Expand Up @@ -76,4 +77,4 @@ private void consumeWhenPublish(StompHeaderAccessor headerAccessor) {
headerAccessor.setDestination(PUBLISHING_FORMAT.formatted(prefixUrl, information[0], information[1]));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import lombok.RequiredArgsConstructor;
import mafia.mafiatogether.common.exception.ExceptionCode;
import mafia.mafiatogether.common.exception.PlayerException;
import mafia.mafiatogether.common.exception.GameException;
import mafia.mafiatogether.common.exception.PlayerException;
import mafia.mafiatogether.job.application.dto.request.JobExecuteAbilityRequest;
import mafia.mafiatogether.job.application.dto.response.JobResponse;
import mafia.mafiatogether.job.application.dto.response.MafiaTargetResponse;
import mafia.mafiatogether.job.application.dto.response.JobExecuteAbilityResponse;
import mafia.mafiatogether.job.application.dto.response.JobResponse;
import mafia.mafiatogether.job.application.dto.response.JobResultResponse;
import mafia.mafiatogether.job.application.dto.response.MafiaTargetResponse;
import mafia.mafiatogether.job.domain.JobTarget;
import mafia.mafiatogether.job.domain.JobTargetRepository;
import mafia.mafiatogether.job.domain.PlayerJob;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/mafia/mafiatogether/job/ui/JobV2Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package mafia.mafiatogether.job.ui;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import mafia.mafiatogether.job.application.JobService;
import mafia.mafiatogether.job.application.dto.request.JobExecuteAbilityRequest;
import mafia.mafiatogether.job.application.dto.response.JobExecuteAbilityResponse;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Controller;

@Slf4j
@Controller
@RequiredArgsConstructor
public class JobV2Controller {

private final StringRedisTemplate stringRedisTemplate;
private final JobService jobService;
private final ObjectMapper objectMapper;

@MessageMapping("/jab/skill/{code}/{name}")
public void executeSkill(
@DestinationVariable("code") String code,
@DestinationVariable("name") String name,
@Payload JobExecuteAbilityRequest request
) throws JsonProcessingException {
JobExecuteAbilityResponse response = jobService.executeSkill(code, name, request);
String message = objectMapper.writeValueAsString(response);
stringRedisTemplate.convertAndSend("/sub/jab/skill/" + response.job().toLowerCase() + "/" + code, message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package mafia.mafiatogether.job.application;

import static org.mockito.BDDMockito.given;

import java.util.HashMap;
import java.util.Optional;
import mafia.mafiatogether.job.application.dto.request.JobExecuteAbilityRequest;
import mafia.mafiatogether.job.application.dto.response.JobExecuteAbilityResponse;
import mafia.mafiatogether.job.domain.JobTarget;
import mafia.mafiatogether.job.domain.JobTargetRepository;
import mafia.mafiatogether.job.domain.PlayerJob;
import mafia.mafiatogether.job.domain.PlayerJobRepository;
import mafia.mafiatogether.job.domain.jobtype.Citizen;
import mafia.mafiatogether.job.domain.jobtype.Doctor;
import mafia.mafiatogether.job.domain.jobtype.Mafia;
import mafia.mafiatogether.job.domain.jobtype.Police;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

@SpringBootTest
class JobServiceTest {

@Autowired
private JobService jobService;

@MockBean
private JobTargetRepository jobTargetRepository;
@MockBean
private PlayerJobRepository playerJobRepository;

@Test
void 마피아_능력을_사용할_수_있다() {
//given
final String code = "test";
given(jobTargetRepository.findById(code)).willReturn(Optional.of(new JobTarget(code, new HashMap<>())));
given(playerJobRepository.findById(code)).willReturn(Optional.of(new PlayerJob(code, new HashMap<>() {{
put("파워", new Mafia());
put("달리", new Citizen());
put("매튜", new Doctor());
put("지윤", new Police());
}})));

//when
JobExecuteAbilityResponse response = jobService.executeSkill(
code,
"파워",
new JobExecuteAbilityRequest("매튜")
);

//then
Assertions.assertThat(response.job()).isEqualTo("MAFIA");
Assertions.assertThat(response.result()).isEqualTo("매튜");
}

@Test
void 경찰_능력을_사용할_수_있다() {
//given
final String code = "test";
given(jobTargetRepository.findById(code)).willReturn(Optional.of(new JobTarget(code, new HashMap<>())));
given(playerJobRepository.findById(code)).willReturn(Optional.of(new PlayerJob(code, new HashMap<>() {{
put("파워", new Mafia());
put("달리", new Citizen());
put("매튜", new Doctor());
put("지윤", new Police());
}})));

//when
JobExecuteAbilityResponse response = jobService.executeSkill(
code,
"지윤",
new JobExecuteAbilityRequest("파워")
);

//then
Assertions.assertThat(response.job()).isEqualTo("POLICE");
Assertions.assertThat(response.result()).isEqualTo("MAFIA");

}
}

0 comments on commit d0b2b23

Please sign in to comment.