Skip to content

Commit

Permalink
Updated memory-backend for leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Duyguoe committed Jun 4, 2024
1 parent ec0e5fc commit e11c4a1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ public class GameResult {
@CreationTimestamp
private Date playedDay = new Date();

public GameResult(final Boolean isFinished, final UUID configurationAsUUID, final String playerId) {
private int rewards;

public GameResult(
final Boolean isFinished,
final UUID configurationAsUUID,
final String playerId,
final int rewards
) {
this.isFinished = isFinished;
this.configurationAsUUID = configurationAsUUID;
this.playerId = playerId;
this.rewards = rewards;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class OverworldResultDTO {
String userId;

/**
* the rewards gained by the player in this round
* the rewards gained by the player in this round of memory
*/
int rewards;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public class GameResultService {
@Autowired
GameResultRepository gameResultRepository;

int flagFirstTimeFinished = 0;

/**
* Casts a GameResultDTO to GameResult and saves it in the Database
*
* @param gameResultDTO extern gameResultDTO
* @param userId id of the user
* @param accessToken accessToken of the user
* @param userId id of the user
* @param accessToken accessToken of the user
* @throws IllegalArgumentException if at least one of the arguments is null
*/
public void saveGameResult(
Expand All @@ -46,7 +48,7 @@ public void saveGameResult(
throw new IllegalArgumentException("gameResultDTO or userId is null");
}
final int resultScore = calculateResultScore(gameResultDTO.getIsFinished());
final int rewards = (int) Math.ceil(resultScore / 10);
final int rewards = calculateRewards(resultScore);

final OverworldResultDTO resultDTO = new OverworldResultDTO(
gameResultDTO.getConfigurationAsUUID(),
Expand All @@ -59,7 +61,8 @@ public void saveGameResult(
final GameResult result = new @Valid GameResult(
gameResultDTO.getIsFinished(),
gameResultDTO.getConfigurationAsUUID(),
userId
userId,
rewards
);
gameResultRepository.save(result);
} catch (final FeignException.BadGateway badGateway) {
Expand All @@ -77,4 +80,27 @@ public void saveGameResult(
private int calculateResultScore(final Boolean isCompleted) {
return (Boolean.TRUE.equals(isCompleted) ? 100 : 0);
}

/**
* This method calculates the rewards for one memory round based on the gained scores in the
* current round
*
* first round: 10 rewards, second round: 5 rewards, after that: 2 rounds per finished round
*
* @param resultScore
* @return gained rewards
*/
private int calculateRewards(final int resultScore) {
if (resultScore == 100 && flagFirstTimeFinished == 0) {
flagFirstTimeFinished += 1;
return 10;
} else if (resultScore == 100 && flagFirstTimeFinished == 1) {
flagFirstTimeFinished += 1;
return 5;
} else if (resultScore == 100 && flagFirstTimeFinished == 2) {
return 2;
}

return 0;
}
}

0 comments on commit e11c4a1

Please sign in to comment.