Skip to content

Commit

Permalink
Fix #112 : Loop on championships for statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
axel3rd committed Sep 4, 2019
1 parent fcf7fff commit b2f430b
Show file tree
Hide file tree
Showing 13 changed files with 30,106 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/blondin/mpg/ChampionshipTypeWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static ChampionshipStatsType toStats(ChampionshipType championship) {
return ChampionshipStatsType.LIGA;
case SERIE_A:
return ChampionshipStatsType.SERIE_A;
case CHAMPIONS_LEAGUE:
return ChampionshipStatsType.CHAMPIONS_LEAGUE;
default:
throw new UnsupportedOperationException(String.format("Championship type not supported: %s", championship));
}
Expand Down
42 changes: 27 additions & 15 deletions src/main/java/org/blondin/mpg/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,22 +350,14 @@ private static void writeTeamOptimized(List<Player> players) {

private static List<Player> calculateEfficiency(List<Player> players, MpgStatsClient stats, ChampionshipStatsType championship, Config config,
boolean failIfPlayerNotFound, boolean logWarnIfPlayerNotFound) {
int daysPeriod = getCurrentDay(stats, championship);
int days4efficiency = 0;
if (config.isEfficiencyRecentFocus()) {
days4efficiency = config.getEfficiencyRecentDays();
// If season start (=> daysPeriod < 8 when days4efficiency = 8 by default), the focus is on the started days
if (daysPeriod < days4efficiency) {
days4efficiency = daysPeriod;
} else {
daysPeriod = days4efficiency;
}
// Calculate efficiency for given championship or all (in champions league case)
List<ChampionshipStatsType> list = Arrays.asList(championship);
if (ChampionshipStatsType.CHAMPIONS_LEAGUE.equals(championship)) {
list = Arrays.asList(ChampionshipStatsType.LIGUE_1, ChampionshipStatsType.LIGUE_2, ChampionshipStatsType.PREMIER_LEAGUE,
ChampionshipStatsType.LIGA, ChampionshipStatsType.SERIE_A);
}
for (org.blondin.mpg.stats.model.Player p : stats.getStats(championship).getPlayers()) {
double efficiency = p.getStats().getMatchs(days4efficiency) / (double) daysPeriod * p.getStats().getAverage(days4efficiency)
* (1 + p.getStats().getGoals(days4efficiency) * config.getEfficiencyCoefficient(PositionWrapper.fromStats(p.getPosition())));
// round efficiency to 2 decimals
p.setEfficiency(efficiency);
for (ChampionshipStatsType type : list) {
calculateEfficiencyForChampionship(stats, type, config);
}

// Fill MPG model
Expand All @@ -385,6 +377,26 @@ private static List<Player> calculateEfficiency(List<Player> players, MpgStatsCl
return players;
}

private static void calculateEfficiencyForChampionship(MpgStatsClient stats, ChampionshipStatsType championship, Config config) {
int daysPeriod = getCurrentDay(stats, championship);
int days4efficiency = 0;
if (config.isEfficiencyRecentFocus()) {
days4efficiency = config.getEfficiencyRecentDays();
// If season start (=> daysPeriod < 8 when days4efficiency = 8 by default), the focus is on the started days
if (daysPeriod < days4efficiency) {
days4efficiency = daysPeriod;
} else {
daysPeriod = days4efficiency;
}
}
for (org.blondin.mpg.stats.model.Player p : stats.getStats(championship).getPlayers()) {
double efficiency = p.getStats().getMatchs(days4efficiency) / (double) daysPeriod * p.getStats().getAverage(days4efficiency)
* (1 + p.getStats().getGoals(days4efficiency) * config.getEfficiencyCoefficient(PositionWrapper.fromStats(p.getPosition())));
// round efficiency to 2 decimals
p.setEfficiency(efficiency);
}
}

private static int getCurrentDay(MpgStatsClient stats, ChampionshipStatsType championship) {
int daysPeriod = stats.getStats(championship).getInfos().getAnnualStats().getCurrentDay().getDay();
// If league not started, we take the number of day of season, because average will be on this period
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

public enum ChampionshipStatsType {

LIGUE_1("Ligue-1"), LIGUE_2("Ligue-2"), PREMIER_LEAGUE("Premier-League"), LIGA("Liga"), SERIE_A("Serie-A");
LIGUE_1("Ligue-1"), LIGUE_2("Ligue-2"), PREMIER_LEAGUE("Premier-League"), LIGA("Liga"), SERIE_A("Serie-A"),
CHAMPIONS_LEAGUE("SpecificValueNoEndpoint");

private final String value;

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/blondin/mpg/stats/MpgStatsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static MpgStatsClient build(Config config, String urlOverride) {
}

public synchronized Championship getStats(ChampionshipStatsType type) {
if (ChampionshipStatsType.CHAMPIONS_LEAGUE.equals(type)) {
throw new UnsupportedOperationException("Champions league has not dedicated statistics, please use each championship statistics");
}
if (!cache.containsKey(type)) {
// FR : "Ligue-1" / EN : "Premier-League" / ES : "Liga"
// Call with infinite cache and verify timestamp after
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/blondin/mpg/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ public void testRealWithBadCredentials() throws Exception {
}
}

@Test
public void testChampionsLeagueMercato() throws Exception {
prepareMainLigue1Mocks("LM5ZG6KK-status-3", "20190904", "20190904", null);
stubFor(get("/league/LM5ZG6KK/mercato").withHeader("client-version", equalTo(MpgClient.MPG_CLIENT_VERSION)).atPriority(1)
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.mercato.LM5ZG6KK.20190904.json")));
stubFor(get("/customteam.json/Ligue-2")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpgstats.ligue-2.20190904.json")));
stubFor(get("/customteam.json/Premier-League")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpgstats.premier-league.20190904.json")));
stubFor(get("/customteam.json/Liga")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpgstats.liga.20190904.json")));
stubFor(get("/customteam.json/Serie-A")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpgstats.serie-a.20190904.json")));
executeMainProcess();
}

@Test
public void testLeaguesInclude2() throws Exception {
prepareMainLigue1Mocks("LJV92C9Y.LJT3FXDF-status-4", "20190818", "20190818", "20190818");
Expand Down
19 changes: 19 additions & 0 deletions src/test/resources/__files/mpg.dashboard.LM5ZG6KK-status-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"data": {
"leagues": [
{
"id": "LM5ZG6KK",
"admin_mpg_user_id": "mpg_user_306874",
"current_mpg_user": "mpg_user_306874",
"name": "The Cesc Pistols",
"url": "assets/public/img/league/1.jpg",
"leagueStatus": 3,
"championship": 6,
"mode": 1,
"teamStatus": 0,
"players": 4
} ],
"follow": [
]
}
}
Loading

0 comments on commit b2f430b

Please sign in to comment.