-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(Commit message generated with AI) feat: Added EloService and updated UserEntity and ParticipantEntity Introduced EloService for calculating user ratings. Updated UserEntity: added eloPoints field for storing ratings in the database. Updated ParticipantEntity: added temporary fields to track rating changes during tournaments. Frontend changes to display user ratings. feat: EloService minor updates Modified EloService to initialize repository fields via constructor. Updated UserDto: added eloPoints field. Updated ParticipantEntity: temporary fields are now stored in the database. Frontend changes for user rating display. feat: EloService minor updates Adjusted EloService logic for calculating tournament ratings, with a default rating of 1200 for new users. Updated MatchEntity: added hasEloCalculated field. Updated ParticipantEntity: made temporaryEloPoints transient. Removed dependency on EloServiceImpl from RoundService. In UserPane.tsx, users without ratings are labeled as "unrated." feat: Major updates to EloServiceImpl Frontend: Improved rating logo and description for better user experience. Backend: Extended EloService functionality: Added reverse rating calculation method to the EloService interface. Introduced EloCalculationService interface for Elo logic. Created EloUpdateResultDTO for passing calculation results. Refactored services: Moved rating initialization to UserEloInitializerService. Moved Elo calculation to DefaultEloCalculationService. Main rating logic centralized in EloServiceImpl. Changes in EloServiceImpl: Logic now depends on whether tournament Elo has been calculated. Replaced match result storage with Map<UUID, Integer> currentEloMap, removed temporaryElo from ParticipantEntity. Refactored processing: Match processing moved to finishRound method. Elo rollback handled in startTournament method. Added unit tests: Tests for match scenarios between authorized and unauthorized players. Tests for Elo rollback and match result changes within tournaments. REST API Updates: Removed unused ratings endpoint. Set default Elo to 0. Replaced explicit constructor with annotation. Moved isAuthorizedUser to SecurityUtil. Various refactoring, renaming, and cleanup. Added feature-flag for EloServiceImpl on both backend and frontend. Added finalEloPoints field to ParticipantEntity and database. Introduced AdvancedEloCalculationStrategy for standard Elo calculations. Refactoring and Enhancements: Refactored EloServiceImpl logic into smaller methods. Updated processTournamentAndUpdateElo for improved Elo initialization and match handling. Modified calculateAndApplyElo to properly apply changes and accumulate finalEloPoints. Refactored finalizeTournament to apply final Elo changes and persist updated ratings. Fixed issue in rollbackEloChanges to reset finalEloPoints correctly, preventing duplicate processing.
- Loading branch information
1 parent
c4995f2
commit 1e0493f
Showing
26 changed files
with
1,201 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
REACT_APP_auth.signupWithPasswordEnabled=false | ||
REACT_APP_eloServiceEnabled=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/chessgrinder/chessgrinder/dto/EloUpdateResultDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.chessgrinder.chessgrinder.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class EloUpdateResultDto { | ||
private final int whiteNewElo; | ||
private final int blackNewElo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/com/chessgrinder/chessgrinder/service/AdvancedEloCalculationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.chessgrinder.chessgrinder.service; | ||
|
||
import com.chessgrinder.chessgrinder.dto.EloUpdateResultDto; | ||
import com.chessgrinder.chessgrinder.enums.MatchResult; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class AdvancedEloCalculationStrategy implements EloCalculationStrategy { | ||
|
||
private static final double K_FACTOR = 32; | ||
private static final int UNRATED_WIN_POINTS = 5; | ||
private static final int UNRATED_LOSE_POINTS = -5; | ||
private static final int UNRATED_DRAW_POINTS = 1; | ||
|
||
public static double calculateExpectedScore(int whiteElo, int blackElo) { | ||
return 1.0 / (1 + Math.pow(10, (blackElo - whiteElo) / 400.0)); | ||
} | ||
|
||
public static int calculateNewElo(int whiteElo, int blackElo, double score) { | ||
double expectedScore = calculateExpectedScore(whiteElo, blackElo); | ||
return (int) Math.round(whiteElo + K_FACTOR * (score - expectedScore)); | ||
} | ||
|
||
|
||
@Override | ||
public EloUpdateResultDto calculateElo(int whiteElo, int blackElo, MatchResult result, boolean bothUsersAuthorized) { | ||
|
||
if (result == null) { | ||
return EloUpdateResultDto.builder() | ||
.whiteNewElo(whiteElo) | ||
.blackNewElo(blackElo) | ||
.build(); | ||
} | ||
|
||
int whitePoints = 0; | ||
int blackPoints = 0; | ||
|
||
if (result == MatchResult.WHITE_WIN) { | ||
whitePoints = bothUsersAuthorized ? (calculateNewElo(whiteElo, blackElo, 1.0) - whiteElo) : UNRATED_WIN_POINTS; | ||
blackPoints = bothUsersAuthorized ? (calculateNewElo(blackElo, whiteElo, 0) - blackElo) : UNRATED_LOSE_POINTS; | ||
} else if (result == MatchResult.BLACK_WIN) { | ||
whitePoints = bothUsersAuthorized ? (calculateNewElo(whiteElo, blackElo, 0) - whiteElo) : UNRATED_LOSE_POINTS; | ||
blackPoints = bothUsersAuthorized ? (calculateNewElo(blackElo, whiteElo, 1.0) - blackElo) : UNRATED_WIN_POINTS; | ||
} else if (result == MatchResult.DRAW) { | ||
whitePoints = bothUsersAuthorized ? (calculateNewElo(whiteElo, blackElo, 0.5) - whiteElo) : UNRATED_DRAW_POINTS; | ||
blackPoints = bothUsersAuthorized ? (calculateNewElo(blackElo, whiteElo, 0.5) - blackElo) : UNRATED_DRAW_POINTS; | ||
} | ||
|
||
int whiteNewElo = whiteElo + whitePoints; | ||
int blackNewElo = blackElo + blackPoints; | ||
|
||
return EloUpdateResultDto.builder() | ||
.whiteNewElo(whiteNewElo) | ||
.blackNewElo(blackNewElo) | ||
.build(); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/chessgrinder/chessgrinder/service/DefaultEloCalculationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.chessgrinder.chessgrinder.service; | ||
|
||
import com.chessgrinder.chessgrinder.dto.EloUpdateResultDto; | ||
import com.chessgrinder.chessgrinder.enums.MatchResult; | ||
import org.springframework.stereotype.Component; | ||
|
||
|
||
public class DefaultEloCalculationStrategy implements EloCalculationStrategy { | ||
|
||
private static final int WIN_POINTS = 10; | ||
private static final int LOSE_POINTS = -10; | ||
private static final int UNRATED_WIN_POINTS = 5; | ||
private static final int UNRATED_LOSE_POINTS = -5; | ||
|
||
@Override | ||
public EloUpdateResultDto calculateElo(int whiteElo, int blackElo, MatchResult result, boolean bothUsersAuthorized) { | ||
|
||
if (result == null) { | ||
return EloUpdateResultDto.builder() | ||
.whiteNewElo(whiteElo) | ||
.blackNewElo(blackElo) | ||
.build(); | ||
} | ||
int whitePoints = 0; | ||
int blackPoints = 0; | ||
|
||
int winPoints = bothUsersAuthorized ? WIN_POINTS : UNRATED_WIN_POINTS; | ||
int losePoints = bothUsersAuthorized ? LOSE_POINTS : UNRATED_LOSE_POINTS; | ||
|
||
if (result == MatchResult.WHITE_WIN) { | ||
whitePoints = winPoints; | ||
blackPoints = losePoints; | ||
} else if (result == MatchResult.BLACK_WIN) { | ||
whitePoints = losePoints; | ||
blackPoints = winPoints; | ||
} | ||
|
||
int whiteNewElo = whiteElo + whitePoints; | ||
int blackNewElo = blackElo + blackPoints; | ||
|
||
return EloUpdateResultDto.builder() | ||
.whiteNewElo(whiteNewElo) | ||
.blackNewElo(blackNewElo) | ||
.build(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/chessgrinder/chessgrinder/service/EloCalculationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.chessgrinder.chessgrinder.service; | ||
|
||
import com.chessgrinder.chessgrinder.dto.EloUpdateResultDto; | ||
import com.chessgrinder.chessgrinder.enums.MatchResult; | ||
|
||
public interface EloCalculationStrategy { | ||
EloUpdateResultDto calculateElo(int whiteElo, int blackElo, MatchResult result, boolean bothUsersAuthorized); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/chessgrinder/chessgrinder/service/EloService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.chessgrinder.chessgrinder.service; | ||
|
||
import com.chessgrinder.chessgrinder.entities.TournamentEntity; | ||
|
||
|
||
public interface EloService { | ||
|
||
void processTournamentAndUpdateElo(TournamentEntity tournament); | ||
void rollbackEloChanges(TournamentEntity tournament); | ||
} |
Oops, something went wrong.