Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feat
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-go-round committed Jun 24, 2024
2 parents 83dd1e3 + 0ff9836 commit a7995d6
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public class GameSession {
private String hostGameCode;
@Setter
private String otherGameCode;
@Setter
private String hostCodeLanguage;
@Setter
private String guestCodeLanguage;
@Setter
private String winnerId;

private Algorithmproblem algorithmProblem;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void submitCode(String playerId, GameCodeRequest request) {
.toEntity(SubmitResultResponse.class)
.subscribe(response -> {
String message = response.getBody().getMessage();
messagingTemplate.convertAndSendToUser(playerId, "/queue/game/result", message);
messagingTemplate.convertAndSendToUser(playerId, "/queue/game/result", new SubmitResultResponse(message));
if(message.equals("맞았습니다.")) {
closeGame(session, playerId);
} else {
Expand All @@ -184,6 +184,7 @@ public void closeGame(GameSession session, String winnerId) {
if(winnerId != null) {
GameSessionOverResponse winnerResponse = new GameSessionOverResponse(GameOverType.WIN, runningTime);
GameSessionOverResponse loserResponse = new GameSessionOverResponse(GameOverType.LOSE, runningTime);
session.setWinnerId(winnerId);
messagingTemplate.convertAndSendToUser(
winnerId, "/queue/game/over", winnerResponse
);
Expand All @@ -207,8 +208,10 @@ public void savePlayerCode(String playerId, GameCodeRequest request) {
}
if (playerId.equals(session.getHostId())) {
session.setHostGameCode(request.getCode());
session.setHostCodeLanguage(request.getLanguage());
} else {
session.setOtherGameCode(request.getCode());
session.setGuestCodeLanguage(request.getLanguage());
}
//양 측의 코드가 저장됐으면 DB에 저장
if (session.getHostGameCode() != null && session.getOtherGameCode() != null) {
Expand All @@ -223,7 +226,11 @@ private void saveResult(GameSession session) {
session.getHostGameCode(),
session.getOtherGameCode(),
session.getAlgorithmProblem(),
session.getRunningTime());
session.getRunningTime(),
session.getHostId(),
session.getHostCodeLanguage(),
session.getGuestCodeLanguage(),
session.getWinnerId());

removeSession(session);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Gameresult {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long GameresultId;
private Long gameresultId;

private int runningTime;

Expand All @@ -30,6 +30,19 @@ public class Gameresult {
@NotNull
private String guestCodeContent;

//DB table에 더 들어가야함
@Setter
private String hostId;

@Setter
private String guestId;

private String hostCodeLanguage;

private String guestCodeLanguage;

private String gameOverType;

@CreatedDate
@NotNull
@Setter
Expand All @@ -43,10 +56,15 @@ public class Gameresult {
@JoinColumn(name = "chatroom_id", nullable = false)
private Chatroom chatroomId;

public Gameresult(int runningTime, String hostCodeContent, String guestCodeContent, Algorithmproblem algorithmproblemId, Chatroom chatroomId) {
public Gameresult(int runningTime, String hostCodeContent, String guestCodeContent, String hostId, String guestId, String hostCodeLanguage, String guestCodeLanguage, String gameOverType, Algorithmproblem algorithmproblemId, Chatroom chatroomId) {
this.runningTime = runningTime;
this.hostCodeContent = hostCodeContent;
this.guestCodeContent = guestCodeContent;
this.hostId = hostId;
this.guestId = guestId;
this.hostCodeLanguage = hostCodeLanguage;
this.guestCodeLanguage = guestCodeLanguage;
this.gameOverType = gameOverType;
this.algorithmproblemId = algorithmproblemId;
this.chatroomId = chatroomId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package gooroommoon.algofi_core.gameresult;

import gooroommoon.algofi_core.gameresult.dto.GameresultResponse;
import gooroommoon.algofi_core.gameresult.dto.StateResponse;
import gooroommoon.algofi_core.gameresult.dto.GameresultsResponse;
import gooroommoon.algofi_core.gameresult.membergameresult.MemberGameresultRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
Expand All @@ -18,27 +21,39 @@
public class GameresultController {

private final GameresultService gameresultService;
private final MemberGameresultRepository memberGameresultRepository;

/**
* 멤버의 특정 게임결과 조회
*/
//TODO uri 정해야함
@GetMapping("/api/game/member/{GameresultId}")
public ResponseEntity findGameresult(@PathVariable Long GameresultId, Authentication auth) {
GameresultResponse gameresult = gameresultService.findGameresult(auth.getName(), GameresultId);

//TODO 상태코드랑, 메세지같이 보내야함
return ResponseEntity.ok().body(gameresult);
@GetMapping("/app/game/result/{gameresultId}")
public ResponseEntity<StateResponse<GameresultResponse>> findGameresult(@PathVariable("gameresultId") Long gameresultId, Authentication auth) {
GameresultResponse gameresult = gameresultService.findGameresult(auth.getName(), gameresultId);
String memberGameOverType = memberGameresultRepository.findMemberGameOverType(auth.getName(), gameresultId);
gameresult.setGameOverType(memberGameOverType);

StateResponse<GameresultResponse> stateResponse = StateResponse.<GameresultResponse>builder()
.statusCode(HttpStatus.OK.value())
.message("success")
.data(gameresult)
.build();

return ResponseEntity.ok(stateResponse);
}

/**
* 멤버의 모든 게임결과 조회
*/
//TODO uri 정해야함
@GetMapping("/api/game/member/gameresults")
public ResponseEntity<List<GameresultsResponse>> findAllGameresults(Authentication auth) {
@GetMapping("/app/game/results")
public ResponseEntity<StateResponse<List<GameresultsResponse>>> findAllGameresults(Authentication auth) {
List<GameresultsResponse> gameresultList = gameresultService.findGameresultList(auth.getName());

return ResponseEntity.ok().body(gameresultList);
StateResponse<List<GameresultsResponse>> stateResponse = StateResponse.<List<GameresultsResponse>>builder()
.statusCode(HttpStatus.OK.value())
.message("Success")
.data(gameresultList)
.build();

return ResponseEntity.ok(stateResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ public interface GameresultRepository extends JpaRepository<Gameresult, Long> {
/*
member의 모든 게임 결과 조회
*/
@Query("select gr from Gameresult gr inner join fetch MemberGameresult mg on gr.GameresultId = mg.gameresult.GameresultId " +
"inner join fetch Member m on mg.member.loginId = m.loginId where m.loginId = :loginId")
List<Gameresult> findGameresultsByMemberId(@Param("loginId")String loginId);
@Query("select mg.gameresult from MemberGameresult mg " +
"join fetch mg.gameresult gr " +
"join fetch mg.member m " +
"where m.loginId = :loginId")
List<Gameresult> findGameresultsByMemberId(@Param("loginId") String loginId);

/*
member의 특정한 게임 결과 조회
*/
@Query("select gr from Gameresult gr inner join fetch MemberGameresult mg on gr.GameresultId = mg.gameresult.GameresultId " +
"inner join fetch Member m on mg.member.loginId = m.loginId where m.loginId = :loginId and gr.GameresultId = :gameresultId")
Gameresult findGameresultByMemberIdAndGameresultId(@Param("loginId")String loginId, @Param("gameresultId")Long gameresultId);
@Query("select gr from MemberGameresult mg " +
"join mg.gameresult gr " +
"join mg.member m " +
"where m.loginId = :loginId and gr.gameresultId = :gameresultId")
Gameresult findGameresultByMemberIdAndGameresultId(@Param("loginId") String loginId, @Param("gameresultId") Long gameresultId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class GameresultService {
/**
* 멤버의 게임결과 저장하기
*/
//TODO gameSession에서 문제정보와 chatroom정보, 코드 가져오기
public Gameresult save(String chatroomId, Set<String> players ,String hostCode, String guestCode, Algorithmproblem algorithmproblem,int runningTime) {
//TODO hostId,guestId,game_over_type,language
public void save(String chatroomId, Set<String> players ,String hostCode, String guestCode, Algorithmproblem algorithmproblem,
int runningTime,String hostId,String hostCodeLanguage, String guestCodeLanguage,String winnerId) {

Chatroom chatroom = chatRoomRepository.findByChatroomId(chatroomId).orElseThrow(()->
new IllegalStateException("채팅방을 찾을 수 없습니다."));
Expand All @@ -43,23 +44,48 @@ public Gameresult save(String chatroomId, Set<String> players ,String hostCode,
.algorithmproblemId(algorithmproblem)
.chatroomId(chatroom)
.runningTime(runningTime)
.hostCodeLanguage(hostCodeLanguage)
.guestCodeLanguage(guestCodeLanguage)
.build();

Gameresult saveGameresult = gameresultRepository.save(gameresult);
Gameresult saveGameresult = null;

//각 멤버별 gameresult 저장
for (String playerId : players) {
Member member = memberRepository.findByLoginId(playerId).orElseThrow(() -> new UsernameNotFoundException("유저를 찾을 수 없습니다."));

for (String player : players) {
Member member = memberRepository.findByLoginId(player)
.orElseThrow(() -> new UsernameNotFoundException("유저를 찾을 수 없습니다."));

// Host와 Guest ID 설정
if (member.getLoginId().equals(hostId)) {
gameresult.setHostId(member.getLoginId());
} else {
gameresult.setGuestId(member.getLoginId());
}

// gameresult를 한 번만 저장
if (saveGameresult == null) {
saveGameresult = gameresultRepository.save(gameresult);
}

// Game over type 결정
String gameOverType;
if (winnerId == null) {
gameOverType = "TimeOver";
} else if (winnerId.equals(member.getLoginId())) {
gameOverType = "WIN";
} else {
gameOverType = "LOSE";
}

// MemberGameresult 생성 및 저장
MemberGameresult memberGameresult = MemberGameresult.builder()
.member(member)
.gameresult(gameresult)
.gameresult(saveGameresult) // saveGameresult 사용
.gameOverType(gameOverType)
.build();

memberGameresultService.save(memberGameresult);
}

return saveGameresult;
}

/**
Expand All @@ -68,7 +94,7 @@ public Gameresult save(String chatroomId, Set<String> players ,String hostCode,
*/
public List<GameresultsResponse> findGameresultList(String loginId) {
List<Gameresult> GameresultsByMemberId = gameresultRepository.findGameresultsByMemberId(loginId);
return fromGameresults(GameresultsByMemberId);
return toGameresults(GameresultsByMemberId);
}

/**
Expand All @@ -77,29 +103,35 @@ public List<GameresultsResponse> findGameresultList(String loginId) {
*/
public GameresultResponse findGameresult(String loginId, Long GameresultId) {
Gameresult GameresultByMemberIdAndGameresultId = gameresultRepository.findGameresultByMemberIdAndGameresultId(loginId, GameresultId);
return fromGameresult(GameresultByMemberIdAndGameresultId);
return toGameresult(GameresultByMemberIdAndGameresultId);
}

public List<Gameresult> findAllGameresult() {
return gameresultRepository.findAll();
}

private GameresultResponse fromGameresult(Gameresult Gameresult) {
//hostId,guestId,hostCodeLanguage,guestCodeLanguage,game_over_type
private GameresultResponse toGameresult(Gameresult Gameresult) {
return GameresultResponse.builder()
.runningTime(Gameresult.getRunningTime())
.guestCodeContent(Gameresult.getGuestCodeContent())
.hostCodeContent(Gameresult.getHostCodeContent())
.algorithmproblemId(Gameresult.getAlgorithmproblemId().getAlgorithmproblemId())
.chatroomId(Gameresult.getChatroomId().getChatroomId())
.hostId(Gameresult.getHostId())
.guestId(Gameresult.getGuestId())
.hostCodeLanguage(Gameresult.getHostCodeLanguage())
.guestCodeLanguage(Gameresult.getGuestCodeLanguage())
.title(Gameresult.getAlgorithmproblemId().getTitle())
.gameOverType(Gameresult.getGameOverType())
.build();
}

private List<GameresultsResponse> fromGameresults(List<Gameresult> Gameresults) {
private List<GameresultsResponse> toGameresults(List<Gameresult> Gameresults) {
List<GameresultsResponse> results = new CopyOnWriteArrayList<>();
for (Gameresult Gameresult : Gameresults) {
GameresultsResponse gameresultsResponse = GameresultsResponse.builder()
.title(Gameresult.getAlgorithmproblemId().getTitle())
.runningTime(Gameresult.getRunningTime())
.gameresultId(Gameresult.getGameresultId())
.build();

results.add(gameresultsResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import gooroommoon.algofi_core.algorithmproblem.Algorithmproblem;
import gooroommoon.algofi_core.game.session.GameOverType;
import lombok.*;

@Getter
Expand All @@ -16,7 +18,16 @@ public class GameresultResponse {

private String guestCodeContent;

private Long algorithmproblemId;
private String title;

private String hostId;

private String guestId;

private String chatroomId;
@Setter
private String gameOverType;

private String hostCodeLanguage;

private String guestCodeLanguage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public class GameresultsResponse {
private String title;

private int runningTime;

private Long gameresultId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gooroommoon.algofi_core.gameresult.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.*;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class StateResponse<T> {

private int statusCode;
private String message;
private T data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ public class MemberGameresult {
@JoinColumn(name = "game_result_id")
private Gameresult gameresult;

public MemberGameresult(Member member, Gameresult gameresult) {
private String gameOverType;

public MemberGameresult(Member member, Gameresult gameresult,String gameOverType) {
this.member = member;
this.gameresult = gameresult;
this.gameOverType = gameOverType;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package gooroommoon.algofi_core.gameresult.membergameresult;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface MemberGameresultRepository extends JpaRepository<MemberGameresult, Long> {

@Query("select mg.gameOverType from MemberGameresult mg " +
"inner join mg.member m " +
"inner join mg.gameresult g " +
"where m.loginId = :loginId and g.GameresultId = :gameresultId")
String findMemberGameOverType(@Param("loginId") String loginId, @Param("gameresultId") Long gameresultId);
}
Loading

0 comments on commit a7995d6

Please sign in to comment.