Skip to content

Commit

Permalink
Fix #187 : Transactions crash and budget according current proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
axel3rd committed Apr 6, 2021
1 parent 7a8bab5 commit a46ff49
Show file tree
Hide file tree
Showing 9 changed files with 3,310 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/org/blondin/mpg/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.blondin.mpg.root.model.LeagueStatus;
import org.blondin.mpg.root.model.Mode;
import org.blondin.mpg.root.model.Player;
import org.blondin.mpg.root.model.PlayerStatus;
import org.blondin.mpg.root.model.Position;
import org.blondin.mpg.root.model.TacticalSubstitute;
import org.blondin.mpg.root.model.Team;
Expand Down Expand Up @@ -205,7 +206,9 @@ static void processGames(League league, ApiClients apiClients, Config config) {
removeOutPlayers(playersAvailable, apiClients.getOutPlayers(), ChampionshipTypeWrapper.toOut(league.getChampionship()), false);
calculateEfficiency(playersAvailable, apiClients.getStats(), ChampionshipTypeWrapper.toStats(league.getChampionship()), config,
false, false);
writeTransactionsProposal(playersTeam, playersAvailable, transferBuy.getBudget(), apiClients.getOutPlayers(),
Integer currentPlayersBuy = transferBuy.getUserPlayers().stream().filter(p -> p.getStatus().equals(PlayerStatus.PROPOSAL))
.map(Player::getPricePaid).collect(Collectors.summingInt(Integer::intValue));
writeTransactionsProposal(playersTeam, playersAvailable, transferBuy.getBudget() - currentPlayersBuy, apiClients.getOutPlayers(),
ChampionshipTypeWrapper.toOut(league.getChampionship()), config);
}
}
Expand Down Expand Up @@ -257,10 +260,13 @@ private static void writeTransactionsProposal(List<Player> playersTeam, List<Pla
List<Player> players2Sell = playersTeam.stream().filter(p -> p.getEfficiency() <= config.getEfficiencySell(p.getPosition()))
.collect(Collectors.toList());

// Remove goals if same team as the first
Player goalFirst = playersTeam.stream().filter(p -> p.getPosition().equals(Position.G))
.sorted(Comparator.comparing(Player::getEfficiency).reversed()).collect(Collectors.toList()).get(0);
players2Sell.removeIf(p -> p.getPosition().equals(Position.G) && p.getTeamId() == goalFirst.getTeamId());
// Remove goalkeeper(s) (if exist) and same team as the first
List<Player> goalkeepers = playersTeam.stream().filter(p -> p.getPosition().equals(Position.G))
.sorted(Comparator.comparing(Player::getEfficiency).reversed()).collect(Collectors.toList());
final Player goalFirst = goalkeepers.isEmpty() ? new Player() : goalkeepers.get(0);
if (!goalkeepers.isEmpty()) {
players2Sell.removeIf(p -> p.getPosition().equals(Position.G) && p.getTeamId() == goalkeepers.get(0).getTeamId());
}

int cash = budget;
if (!config.isEfficiencyRecentFocus() && !players2Sell.isEmpty()) {
Expand All @@ -285,8 +291,8 @@ private static void writeTransactionsProposal(List<Player> playersTeam, List<Pla
.sorted(Comparator.comparing(Player::getEfficiency).thenComparing(Player::getQuotation)).collect(Collectors.toList()).get(0);
Player attackerLast = playersTeam.stream().filter(p -> p.getPosition().equals(Position.A))
.sorted(Comparator.comparing(Player::getEfficiency).thenComparing(Player::getQuotation)).collect(Collectors.toList()).get(0);
cash += goalFirst.getQuotation() + defenderLast.getQuotation() + midfielderLast.getQuotation() + attackerLast.getQuotation();
LOG.info("Budget if last players by line sold: {}", cash);
cash += defenderLast.getQuotation() + midfielderLast.getQuotation() + attackerLast.getQuotation();
LOG.info("Budget if last field players by line sold: {}", cash);

final int budgetPotential = cash;
List<Player> players2buy = new ArrayList<>();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/blondin/mpg/root/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public class Player {
private String lastName;
private Position position;
private int quotation;
private int pricePaid;
@JsonProperty("teamid")
private int teamId;
@JsonProperty("club")
private String club;
private PlayerStatus status;

private double efficiency;

Expand Down Expand Up @@ -66,4 +68,12 @@ public String getClub() {
public void setClub(String club) {
this.club = club;
}

public PlayerStatus getStatus() {
return status;
}

public int getPricePaid() {
return pricePaid;
}
}
26 changes: 26 additions & 0 deletions src/main/java/org/blondin/mpg/root/model/PlayerStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

package org.blondin.mpg.root.model;

import com.fasterxml.jackson.annotation.JsonCreator;

public enum PlayerStatus {

BUY(2), PROPOSAL(1);

private final int value;

private PlayerStatus(final int value) {
this.value = value;
}

@JsonCreator
public static PlayerStatus getNameByValue(final int value) {
for (final PlayerStatus s : PlayerStatus.values()) {
if (s.value == value) {
return s;
}
}
throw new UnsupportedOperationException(String.format("Player status not supported: %s", value));
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/blondin/mpg/root/model/TransferBuy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class TransferBuy {

private int budget;
private List<Player> availablePlayers;
private List<Player> userPlayers;

public int getBudget() {
return budget;
Expand All @@ -18,4 +19,8 @@ public int getBudget() {
public List<Player> getAvailablePlayers() {
return availablePlayers;
}

public List<Player> getUserPlayers() {
return userPlayers;
}
}
33 changes: 33 additions & 0 deletions src/test/java/org/blondin/mpg/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ public void testRealWithBadCredentials() throws Exception {
}
}

@Test
public void testTransactionsProposalIndexOutOfBoundsException() throws Exception {
stubFor(post("/user/signIn")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.user-signIn.fake.json")));
stubFor(get("/builds").willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.builds.20200106.json")));
stubFor(get("/user/dashboard").willReturn(
aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.dashboard.MLAX7HMK-MLEFEX6G-MN7VSYBM-MLMHBPCB.json")));
stubFor(get("/league/MN7VSYBM/coach")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.coach.MN7VSYBM.20210406.json")));
stubFor(get("/league/MN7VSYBM/transfer/buy")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mpg.transfer.buy.MN7VSYBM.20210406.json")));
stubFor(get("/leagues/Serie-A")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("mlnstats.serie-a.20210406.json")));
stubFor(get("/injuries/football/italy-serie-a/")
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("sportsgambler.serie-a.20210406.html")));

// Only SerieA
Config config = spy(getConfig());
doReturn(Arrays.asList("MN7VSYBM")).when(config).getLeaguesInclude();
doReturn(true).when(config).isTransactionsProposal();

executeMainProcess(config);

// 3 goalkeepers proposed
Assert.assertTrue(getLogOut(), getLogOut().contains("| G | Montipò Lorenzo | 4.97 | 13 |"));
Assert.assertTrue(getLogOut(), getLogOut().contains("| G | Sepe Luigi | 4.78 | 11 |"));
Assert.assertTrue(getLogOut(), getLogOut().contains("| G | Provedel Ivan | 3.93 | 13 |"));

// Current players under buy should not be part of budget
Assert.assertTrue(getLogOut(), getLogOut().contains("Budget: 2"));
Assert.assertTrue(getLogOut(), getLogOut().contains("Budget if last field players by line sold: 36"));
}

@Test
public void testInjuredLigue2ServiceUnavailable() throws Exception {
prepareMainFrenchLigue2Mocks("MLAX7HMK-MLEFEX6G", "20201006", "20201006", null);
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/__files/mlnstats.serie-a.20210406.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit a46ff49

Please sign in to comment.