|
|
@ -53,49 +53,70 @@ public class PlayerService extends CrudService<Player, Integer> { |
|
|
|
} |
|
|
|
|
|
|
|
private List<PlayerForTable> getPlayersForTableSorted(boolean calcDiffToLastMatchday) { |
|
|
|
List<PlayerForTable> playerForTableList = players.stream() |
|
|
|
.map(this::getPlayerForTable) |
|
|
|
.sorted(Comparator.comparingDouble(PlayerForTable::getGamePointsForSelf).reversed()) |
|
|
|
.sorted(Comparator.comparingDouble(PlayerForTable::getGamePointDiff).reversed()) |
|
|
|
.sorted(Comparator.comparingInt(PlayerForTable::getMatchPoints).reversed()) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
List<PlayerForTable> playerForTableList = calcSortedPlayerForTableList(); |
|
|
|
int offset = 0; |
|
|
|
PlayerForTable lastPlayer; |
|
|
|
PlayerForTable currentPlayer; |
|
|
|
for (int i = 0; i < playerForTableList.size(); i++) { |
|
|
|
currentPlayer = playerForTableList.get(i); |
|
|
|
if (i > 0) { |
|
|
|
lastPlayer = playerForTableList.get(i - 1); |
|
|
|
// TODO: add direct comparison below |
|
|
|
if (Objects.equals(currentPlayer.getMatchPoints(), lastPlayer.getMatchPoints()) |
|
|
|
&& Objects.equals(currentPlayer.getGamePointsForSelf(), lastPlayer.getGamePointsForSelf()) |
|
|
|
&& Objects.equals(currentPlayer.getGamePointsForOpponents(), lastPlayer.getGamePointsForOpponents())) { |
|
|
|
offset += 1; |
|
|
|
} else { |
|
|
|
offset = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
offset = getOffset(playerForTableList, offset, currentPlayer, i); |
|
|
|
currentPlayer.setPlace(i + 1 - offset); |
|
|
|
currentPlayer.setPlaceString(offset == 0 ? String.valueOf(i + 1) : ""); |
|
|
|
} |
|
|
|
if (calcDiffToLastMatchday) { |
|
|
|
if (matchdays.size() < 2) { |
|
|
|
playerForTableList.forEach(playerForTable -> playerForTable.setPlaceDiffToLastMatchday(Integer.MAX_VALUE)); |
|
|
|
} else { |
|
|
|
List<PlayerForTable> lastMatchdayList = new PlayerForTableProvider(matchdays.get(matchdays.size() - 2)).getPlayersForTableSorted(false); |
|
|
|
for (PlayerForTable playerForTable : playerForTableList) { |
|
|
|
int placeLastMatchday = lastMatchdayList.stream() |
|
|
|
.filter(playerForTableLastMatchday -> playerForTable.getPlayer().equals(playerForTableLastMatchday.getPlayer())) |
|
|
|
.findFirst() |
|
|
|
.map(PlayerForTable::getPlace) |
|
|
|
.orElseThrow(); |
|
|
|
playerForTable.setPlaceDiffToLastMatchday(placeLastMatchday - playerForTable.getPlace()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (calcDiffToLastMatchday) calcAndSetPlaceDiffToLastMatchday(playerForTableList); |
|
|
|
return playerForTableList; |
|
|
|
} |
|
|
|
|
|
|
|
private int getOffset(List<PlayerForTable> playerForTableList, int offset, PlayerForTable currentPlayer, int i) { |
|
|
|
PlayerForTable lastPlayer; |
|
|
|
if (i > 0) { |
|
|
|
lastPlayer = playerForTableList.get(i - 1); |
|
|
|
// TODO: add direct comparison below |
|
|
|
if (playersHaveCompletelyEqualScores(lastPlayer, currentPlayer)) |
|
|
|
return offset + 1; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
return offset; |
|
|
|
} |
|
|
|
|
|
|
|
private boolean playersHaveCompletelyEqualScores(PlayerForTable lastPlayer, PlayerForTable currentPlayer) { |
|
|
|
return Objects.equals(currentPlayer.getMatchPoints(), lastPlayer.getMatchPoints()) |
|
|
|
&& Objects.equals(currentPlayer.getGamePointsForSelf(), lastPlayer.getGamePointsForSelf()) |
|
|
|
&& Objects.equals(currentPlayer.getGamePointsForOpponents(), lastPlayer.getGamePointsForOpponents()); |
|
|
|
} |
|
|
|
|
|
|
|
private List<PlayerForTable> calcSortedPlayerForTableList() { |
|
|
|
return players.stream() |
|
|
|
.map(this::getPlayerForTable) |
|
|
|
.sorted(Comparator.comparingDouble(PlayerForTable::getGamePointsForSelf).reversed()) |
|
|
|
.sorted(Comparator.comparingDouble(PlayerForTable::getGamePointDiff).reversed()) |
|
|
|
.sorted(Comparator.comparingInt(PlayerForTable::getMatchPoints).reversed()) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
} |
|
|
|
|
|
|
|
private void calcAndSetPlaceDiffToLastMatchday(List<PlayerForTable> playerForTableList) { |
|
|
|
if (matchdays.size() < 2) { |
|
|
|
playerForTableList.forEach(playerForTable -> playerForTable.setPlaceDiffToLastMatchday(Integer.MAX_VALUE)); |
|
|
|
return; |
|
|
|
} |
|
|
|
List<PlayerForTable> listForLastMatchday = getListForLastMatchday(); |
|
|
|
playerForTableList.forEach(playerForTable -> calcAndSetPlaceDiffToLastMatchday(playerForTable, listForLastMatchday)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private void calcAndSetPlaceDiffToLastMatchday(PlayerForTable playerForTable, List<PlayerForTable> listForLastMatchday) { |
|
|
|
int placeLastMatchday = listForLastMatchday.stream() |
|
|
|
.filter(playerForTableLastMatchday -> playerForTable.getPlayer().equals(playerForTableLastMatchday.getPlayer())) |
|
|
|
.findFirst() |
|
|
|
.map(PlayerForTable::getPlace) |
|
|
|
.orElseThrow(); |
|
|
|
playerForTable.setPlaceDiffToLastMatchday(placeLastMatchday - playerForTable.getPlace()); |
|
|
|
} |
|
|
|
|
|
|
|
private List<PlayerForTable> getListForLastMatchday() { |
|
|
|
return new PlayerForTableProvider(matchdays.get(matchdays.size() - 2)).getPlayersForTableSorted(false); |
|
|
|
} |
|
|
|
|
|
|
|
private PlayerForTable getPlayerForTable(Player player) { |
|
|
|
List<CalculatedMatch> matchesAsPlayer1 = getMatchesAsPlayer1(player); |
|
|
|
List<CalculatedMatch> matchesAsPlayer2 = getMatchesAsPlayer2(player); |
|
|
@ -107,17 +128,10 @@ public class PlayerService extends CrudService<Player, Integer> { |
|
|
|
int amountOfMatchesLost = map.get(WinState.LOST).size(); |
|
|
|
|
|
|
|
int amountOfMatches = amountOfMatchesWon + amountOfMatchesDrawn + amountOfMatchesLost; |
|
|
|
|
|
|
|
int matchPoints = amountOfMatchesWon * 2 + amountOfMatchesDrawn; |
|
|
|
|
|
|
|
double gamePointsForSelf = 0; |
|
|
|
gamePointsForSelf += matchesAsPlayer1.stream().mapToDouble(CalculatedMatch::getScore1).sum(); |
|
|
|
gamePointsForSelf += matchesAsPlayer2.stream().mapToDouble(CalculatedMatch::getScore2).sum(); |
|
|
|
|
|
|
|
double gamePointsForOpponents = 0; |
|
|
|
gamePointsForOpponents += matchesAsPlayer1.stream().mapToDouble(CalculatedMatch::getScore2).sum(); |
|
|
|
gamePointsForOpponents += matchesAsPlayer2.stream().mapToDouble(CalculatedMatch::getScore1).sum(); |
|
|
|
|
|
|
|
double gamePointsForSelf = getGamePointsForSelf(matchesAsPlayer1, matchesAsPlayer2); |
|
|
|
double gamePointsForOpponents = getGamePointsForOpponents(matchesAsPlayer1, matchesAsPlayer2); |
|
|
|
double gamePointDiff = gamePointsForSelf - gamePointsForOpponents; |
|
|
|
|
|
|
|
return new PlayerForTable(player, |
|
|
@ -143,6 +157,20 @@ public class PlayerService extends CrudService<Player, Integer> { |
|
|
|
.collect(Collectors.toList()); |
|
|
|
} |
|
|
|
|
|
|
|
private double getGamePointsForSelf(List<CalculatedMatch> matchesAsPlayer1, List<CalculatedMatch> matchesAsPlayer2) { |
|
|
|
double gamePointsForSelf = 0; |
|
|
|
gamePointsForSelf += matchesAsPlayer1.stream().mapToDouble(CalculatedMatch::getScore1).sum(); |
|
|
|
gamePointsForSelf += matchesAsPlayer2.stream().mapToDouble(CalculatedMatch::getScore2).sum(); |
|
|
|
return gamePointsForSelf; |
|
|
|
} |
|
|
|
|
|
|
|
private double getGamePointsForOpponents(List<CalculatedMatch> matchesAsPlayer1, List<CalculatedMatch> matchesAsPlayer2) { |
|
|
|
double gamePointsForOpponents = 0; |
|
|
|
gamePointsForOpponents += matchesAsPlayer1.stream().mapToDouble(CalculatedMatch::getScore2).sum(); |
|
|
|
gamePointsForOpponents += matchesAsPlayer2.stream().mapToDouble(CalculatedMatch::getScore1).sum(); |
|
|
|
return gamePointsForOpponents; |
|
|
|
} |
|
|
|
|
|
|
|
private Map<WinState, List<CalculatedMatch>> getWinStateLists(List<CalculatedMatch> matchesAsPlayer1, List<CalculatedMatch> matchesAsPlayer2) { |
|
|
|
Map<WinState, List<CalculatedMatch>> map = new HashMap<>(); |
|
|
|
map.put(WinState.WON, new ArrayList<>()); |
|
|
|