Skip to content

Commit

Permalink
Merge pull request #221 from 42organization/GGBE4-44-feat-rank-game-r…
Browse files Browse the repository at this point in the history
…esult

[Ggbe4-44-feat-rank-game-result]랭크게임결과 코인증가량 추가
  • Loading branch information
kylew1004 authored Aug 23, 2023
2 parents b1772e3 + 5fa0794 commit 1ba5c9c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ public void addNormalCoin(User user){
}

@Transactional
public void addRankWinCoin(User user){
public int addRankWinCoin(User user){
int amount = coinPolicyRepository.findTopByOrderByCreatedAtDesc().getRankWin();
addCoinHistory(new CoinHistory(user, HistoryType.RANKWIN.getHistory(), amount));
return amount;
}

@Transactional
public void addRankLoseCoin(User user){
public int addRankLoseCoin(User user){
int amount = coinPolicyRepository.findTopByOrderByCreatedAtDesc().getRankLose();
addCoinHistory(new CoinHistory(user, HistoryType.RANKLOSE.getHistory(), amount));
return amount;
}

private void addCoinHistory(CoinHistory coinHistory){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

import com.gg.server.domain.coin.data.CoinPolicyRepository;
import com.gg.server.domain.coin.dto.UserGameCoinResultDto;
import com.gg.server.domain.game.service.GameFindService;
import com.gg.server.domain.team.data.Team;
import com.gg.server.domain.team.data.TeamUser;
import com.gg.server.domain.user.data.User;
import com.gg.server.domain.user.data.UserRepository;
import com.gg.server.domain.user.exception.UserNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserCoinChangeService {
private final CoinPolicyRepository coinPolicyRepository;
private final CoinHistoryService coinHistoryService;
private final UserRepository userRepository;
private final GameFindService gameFindService;

@Transactional
public UserGameCoinResultDto addNormalGameCoin(Long userId) {
Expand All @@ -25,4 +31,33 @@ public UserGameCoinResultDto addNormalGameCoin(Long userId) {
coinHistoryService.addNormalCoin(user);
return new UserGameCoinResultDto(user.getGgCoin(), coinIncrement);
}

@Transactional
public UserGameCoinResultDto addRankGameCoin(Long gameId, Long userId) {
User user = userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException());
int coinIncrement;

if (userIsWinner(gameId, user))
coinIncrement = coinHistoryService.addRankWinCoin(user);
else
coinIncrement = coinHistoryService.addRankLoseCoin(user);

user.addGgCoin(coinIncrement);
return new UserGameCoinResultDto(user.getGgCoin(), coinIncrement);
}

private boolean userIsWinner(Long gameId, User user) {
List<Team> teams = gameFindService.findByGameId(gameId).getTeams();

for(Team team: teams) {
for (TeamUser teamUser : team.getTeamUsers()){
if (teamUser.getUser().getId() == user.getId() && team.getWin() == true)
return true;
else if (teamUser.getUser().getId() == user.getId() && team.getWin() == false)
return false;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ public ExpChangeResultResDto(Integer beforeExp, Integer currentExp, UserGameCoin
this.coinIncrement = userGameCoinResultDto.getCoinIncrement();
}

//랭크 게임 수정 후 사라질 생성자
public ExpChangeResultResDto(Integer beforeExp, Integer currentExp) {
this.beforeExp = ExpLevelCalculator.getCurrentLevelMyExp(beforeExp);
this.beforeLevel = ExpLevelCalculator.getLevel(beforeExp);
this.beforeMaxExp = ExpLevelCalculator.getLevelMaxExp(beforeLevel);
this.increasedExp = currentExp - beforeExp;
this.increasedLevel = ExpLevelCalculator.getLevel(currentExp) - this.beforeLevel;
this.afterMaxExp = ExpLevelCalculator.getLevelMaxExp(this.beforeLevel + this.increasedLevel);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gg.server.domain.game.dto;

import com.gg.server.domain.coin.dto.UserGameCoinResultDto;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -10,8 +11,8 @@ public class PPPChangeResultResDto extends ExpChangeResultResDto {
private Integer changedPpp;
private Integer beforePpp;

public PPPChangeResultResDto(Integer beforeExp, Integer currentExp, Integer beforePpp, Integer afterPpp) {
super(beforeExp, currentExp);
public PPPChangeResultResDto(Integer beforeExp, Integer currentExp, Integer beforePpp, Integer afterPpp, UserGameCoinResultDto userGameCoinResultDto) {
super(beforeExp, currentExp, userGameCoinResultDto);
this.changedPpp = afterPpp - beforePpp;
this.beforePpp = beforePpp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.gg.server.global.exception.custom.InvalidParameterException;
import com.gg.server.global.utils.ExpLevelCalculator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
Expand All @@ -33,7 +32,6 @@

@Service
@RequiredArgsConstructor
@Slf4j
public class GameService {
private final GameRepository gameRepository;
private final TeamUserRepository teamUserRepository;
Expand Down Expand Up @@ -123,15 +121,16 @@ public ExpChangeResultResDto expChangeResult(Long gameId, Long userId) {
}
}

@Transactional(readOnly = true)
@Transactional
public PPPChangeResultResDto pppChangeResult(Long gameId, Long userId) throws PChangeNotExistException {
Season season = gameFindService.findByGameId(gameId).getSeason();
List<PChange> pppHistory = pChangeService.findPPPChangeHistory(gameId, userId, season.getId());
List<PChange> expHistory = pChangeService.findExpChangeHistory(gameId, userId);
UserGameCoinResultDto userGameCoinResultDto = userCoinChangeService.addRankGameCoin(gameId, userId);
return new PPPChangeResultResDto(expHistory.size() <= 1 ? 0 : expHistory.get(1).getExp(),
pppHistory.get(0).getExp(),
pppHistory.size() <= 1 ? season.getStartPpp() : pppHistory.get(1).getPppResult(),
pppHistory.get(0).getPppResult());
pppHistory.get(0).getPppResult(), userGameCoinResultDto);
}

public void expUpdates(Game game, List<TeamUser> teamUsers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,28 @@ void addNormalGameService() {
assertThat(coinPolicyRepository.findTopByOrderByCreatedAtDesc().getNormal()).isEqualTo(userGameCoinResultDto.getCoinIncrement());
System.out.println(coinHistoryRepository.findFirstByOrderByIdDesc().getHistory());
}

@Test
@DisplayName("랭크 게임 승리 재화 증가 서비스 테스트")
void addRankWinGameService() {
User user = userRepository.getUserByIntraId("cheolee");

UserGameCoinResultDto userGameCoinResultDto = userCoinChangeService.addRankGameCoin(3606L, user.getId());//본인의 게임Id와 id 값

assertThat(user.getGgCoin()).isEqualTo(userGameCoinResultDto.getAfterCoin());
assertThat(coinPolicyRepository.findTopByOrderByCreatedAtDesc().getRankWin()).isEqualTo(userGameCoinResultDto.getCoinIncrement());
System.out.println(coinHistoryRepository.findFirstByOrderByIdDesc().getHistory());
}

@Test
@DisplayName("랭크 게임 패배 재화 증가 서비스 테스트")
void addRankLoseGameService() {
User user = userRepository.getUserByIntraId("cheolee");

UserGameCoinResultDto userGameCoinResultDto = userCoinChangeService.addRankGameCoin(3689L, user.getId());

assertThat(user.getGgCoin()).isEqualTo(userGameCoinResultDto.getAfterCoin());
assertThat(coinPolicyRepository.findTopByOrderByCreatedAtDesc().getRankLose()).isEqualTo(userGameCoinResultDto.getCoinIncrement());
System.out.println(coinHistoryRepository.findFirstByOrderByIdDesc().getHistory());
}
}

0 comments on commit 1ba5c9c

Please sign in to comment.