-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
525 additions
and
19 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/fr/zelytra/poolpoint/LeaderboardEndpoint.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,33 @@ | ||
package fr.zelytra.poolpoint; | ||
|
||
import fr.zelytra.logger.LogEndpoint; | ||
import fr.zelytra.user.UserService; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
@Path("/leaderboard") | ||
public class LeaderboardEndpoint { | ||
|
||
@Inject | ||
UserService userService; | ||
|
||
@Inject | ||
SecurityIdentity securityIdentity; | ||
|
||
@GET | ||
@LogEndpoint | ||
@Path("/all") | ||
public Response getTopLeaderboard() { | ||
return Response.ok(userService.getUsersOrderByPoolPoint()).build(); | ||
} | ||
|
||
@GET | ||
@LogEndpoint | ||
@Path("/self") | ||
public Response getUserLeaderboardPosition() { | ||
return Response.ok(userService.getUsersLeaderboardPosition(securityIdentity.getPrincipal().getName())).build(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
backend/src/main/java/fr/zelytra/poolpoint/PoolPointCalculator.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,55 @@ | ||
package fr.zelytra.poolpoint; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
public class PoolPointCalculator { | ||
|
||
private final int poolpoint; | ||
private final int k; // K-factor | ||
private final int totalGamePlayed; | ||
|
||
public PoolPointCalculator(int poolpoint, int totalGamePlayed) { | ||
this.poolpoint = poolpoint; | ||
this.totalGamePlayed = totalGamePlayed; | ||
this.k = getFactorK(); | ||
} | ||
|
||
/** | ||
* @param partyStatus Win = 1 | Loose = 0 | Draw = 0.5 | ||
* @param opponentElo Poolpoint of opponent | ||
* @return En+1 = En + K * (W - p(D)) The new elo of the player | ||
*/ | ||
public int computeNewElo(double partyStatus, int opponentElo) { | ||
return (int) Math.round(poolpoint + k * (partyStatus - getWinProbability(opponentElo))); | ||
} | ||
|
||
public int getFactorK() { | ||
// New player with less than 30 games and less than 2300 of score | ||
if (totalGamePlayed < 30 && poolpoint < 2300) { | ||
return 40; | ||
} | ||
// Old player with less than 2400 of score | ||
else if (poolpoint < 2400) { | ||
return 20; | ||
} | ||
// Old player with a superior or equal score of 2400 | ||
return 10; | ||
} | ||
|
||
/** | ||
* The function p(D) Which represent win probability | ||
* | ||
* @param opponentElo Poolpoint of opponent | ||
* @return p(D) Gain probability | ||
*/ | ||
public double getWinProbability(int opponentElo) { | ||
double eloDelta = poolpoint - opponentElo; // D | ||
if (Math.abs(eloDelta) > 400 && eloDelta != 0) { | ||
eloDelta = (400 * eloDelta) / Math.abs(eloDelta); | ||
} | ||
return BigDecimal.valueOf(1 / (1 + Math.pow(10, -eloDelta / 400))).setScale(3, RoundingMode.HALF_UP).doubleValue(); | ||
} | ||
|
||
|
||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/fr/zelytra/poolpoint/UserLeaderBoardPosition.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,4 @@ | ||
package fr.zelytra.poolpoint; | ||
|
||
public record UserLeaderBoardPosition(String username, int pp, long position) { | ||
} |
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
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/fr/zelytra/user/reflections/LeaderBoardUser.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,7 @@ | ||
package fr.zelytra.user.reflections; | ||
|
||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
|
||
@RegisterForReflection | ||
public record LeaderBoardUser(String username, String icon, int pp) { | ||
} |
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
95 changes: 95 additions & 0 deletions
95
backend/src/test/java/fr/zelytra/poolpoint/LeaderboardEndpointTest.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,95 @@ | ||
package fr.zelytra.poolpoint; | ||
|
||
import fr.zelytra.user.UserEntity; | ||
import fr.zelytra.user.reflections.LeaderBoardUser; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.oidc.server.OidcWiremockTestResource; | ||
import io.restassured.common.mapper.TypeRef; | ||
import jakarta.transaction.Transactional; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static io.quarkus.test.oidc.server.OidcWiremockTestResource.getAccessToken; | ||
import static io.restassured.RestAssured.given; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@QuarkusTest | ||
@QuarkusTestResource(OidcWiremockTestResource.class) | ||
class LeaderboardEndpointTest { | ||
|
||
@BeforeAll | ||
@Transactional | ||
static void init() { | ||
for (int x = 0; x < 200; x++) { | ||
UserEntity user = new UserEntity("User" + x); | ||
user.setPp(1000 + x); | ||
} | ||
} | ||
|
||
@Test | ||
void getTopLeaderboard_maxResult100() { | ||
List<LeaderBoardUser> list = given() | ||
.auth().oauth2(getAccessToken("User1", Set.of("user"))) | ||
.when().get("/leaderboard/all") | ||
.then().statusCode(200) | ||
.extract() | ||
.body() | ||
.as(new TypeRef<>() { | ||
}); | ||
assertEquals(100, list.size(), "Should return max 100 results"); | ||
} | ||
|
||
@Test | ||
void getTopLeaderboard_orderByPP() { | ||
List<LeaderBoardUser> list = given() | ||
.auth().oauth2(getAccessToken("User1", Set.of("user"))) | ||
.when().get("/leaderboard/all") | ||
.then().statusCode(200) | ||
.extract() | ||
.body() | ||
.as(new TypeRef<>() { | ||
}); | ||
int index = 0; | ||
for (LeaderBoardUser leaderBoardUser : list) { | ||
assertEquals(1000 + index, leaderBoardUser.pp()); | ||
index++; | ||
} | ||
} | ||
|
||
@Test | ||
void getUserLeaderboardPosition_returnTheRightPosition1() { | ||
UserLeaderBoardPosition userLeaderBoardPosition = given() | ||
.auth().oauth2(getAccessToken("User0", Set.of("user"))) | ||
.when().get("/leaderboard/self") | ||
.then().statusCode(200) | ||
.extract() | ||
.body() | ||
.as(UserLeaderBoardPosition.class); | ||
assertEquals(200, userLeaderBoardPosition.position()); | ||
assertEquals(1000,userLeaderBoardPosition.pp()); | ||
} | ||
|
||
@Test | ||
void getUserLeaderboardPosition_returnTheRightPosition2() { | ||
UserLeaderBoardPosition userLeaderBoardPosition = given() | ||
.auth().oauth2(getAccessToken("User42", Set.of("user"))) | ||
.when().get("/leaderboard/self") | ||
.then().statusCode(200) | ||
.extract() | ||
.body() | ||
.as(UserLeaderBoardPosition.class); | ||
assertEquals(158, userLeaderBoardPosition.position()); | ||
assertEquals(1042,userLeaderBoardPosition.pp()); | ||
} | ||
|
||
@AfterAll | ||
@Transactional | ||
static void cleanDataBase() { | ||
UserEntity.deleteAll(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
backend/src/test/java/fr/zelytra/poolpoint/PoolPointCalculatorTest.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,91 @@ | ||
package fr.zelytra.poolpoint; | ||
|
||
import fr.zelytra.poolpoint.simulation.PoolPlayerSimulator; | ||
import io.quarkus.logging.Log; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.transaction.Transactional; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@QuarkusTest | ||
class PoolPointCalculatorTest { | ||
|
||
@Test | ||
@Transactional | ||
void simulation() { | ||
PoolPlayerSimulator poolPlayerSimulator = new PoolPlayerSimulator(101, 5000); | ||
Log.info(poolPlayerSimulator); | ||
poolPlayerSimulator.generateCSV("game_history.csv"); | ||
} | ||
|
||
@Test | ||
void getFactorK_lessThan30Games() { | ||
int user1PP = 600; | ||
int user1GamesPlayed = 25; | ||
PoolPointCalculator poolPointCalculator = new PoolPointCalculator(user1PP, user1GamesPlayed); | ||
assertEquals(40, poolPointCalculator.getFactorK()); | ||
} | ||
|
||
@Test | ||
void getFactorK_lessThan2400AndMoreThan30Games() { | ||
int user1PP = 600; | ||
int user1GamesPlayed = 80; | ||
PoolPointCalculator poolPointCalculator = new PoolPointCalculator(user1PP, user1GamesPlayed); | ||
assertEquals(20, poolPointCalculator.getFactorK()); | ||
} | ||
|
||
@Test | ||
void getFactorK_MoreThan2400() { | ||
int user1PP = 2401; | ||
int user1GamesPlayed = 80; | ||
PoolPointCalculator poolPointCalculator = new PoolPointCalculator(user1PP, user1GamesPlayed); | ||
assertEquals(10, poolPointCalculator.getFactorK()); | ||
} | ||
|
||
@Test | ||
void getWinProbability_deltaLessThan400() { | ||
int user1PP = 1800; | ||
int user1GamesPlayed = 80; | ||
int user2PP = 1600; | ||
PoolPointCalculator poolPointCalculator = new PoolPointCalculator(user1PP, user1GamesPlayed); | ||
assertEquals(0.76, poolPointCalculator.getWinProbability(user2PP)); | ||
assertEquals(20, poolPointCalculator.getFactorK()); | ||
} | ||
|
||
@Test | ||
void getWinProbability_deltaMoreThan400() { | ||
int user1PP = 2600; | ||
int user1GamesPlayed = 80; | ||
int user2PP = 1600; | ||
PoolPointCalculator poolPointCalculator = new PoolPointCalculator(user1PP, user1GamesPlayed); | ||
assertEquals(0.909, poolPointCalculator.getWinProbability(user2PP)); | ||
assertEquals(10, poolPointCalculator.getFactorK()); | ||
} | ||
|
||
@Test | ||
void computeNewElo_win() { | ||
int user1PP = 2600; | ||
int user1GamesPlayed = 80; | ||
int user2PP = 1600; | ||
PoolPointCalculator poolPointCalculator = new PoolPointCalculator(user1PP, user1GamesPlayed); | ||
assertEquals(0.909, poolPointCalculator.getWinProbability(user2PP)); | ||
assertEquals(10, poolPointCalculator.getFactorK()); | ||
assertEquals(2601, poolPointCalculator.computeNewElo(1, user2PP)); | ||
|
||
} | ||
|
||
@Test | ||
void computeNewElo_loose() { | ||
int user1PP = 2600; | ||
int user1GamesPlayed = 80; | ||
int user2PP = 1600; | ||
PoolPointCalculator poolPointCalculator = new PoolPointCalculator(user1PP, user1GamesPlayed); | ||
assertEquals(0.909, poolPointCalculator.getWinProbability(user2PP)); | ||
assertEquals(10, poolPointCalculator.getFactorK()); | ||
assertEquals(2591, poolPointCalculator.computeNewElo(0, user2PP)); | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.