| @ -0,0 +1,10 @@ | |||||
| .player-view .player-header-label-layout { | |||||
| width: 100%; | |||||
| justify-content: center; | |||||
| font-size: x-large; | |||||
| } | |||||
| .player-view .column-header { | |||||
| font-weight: bold; | |||||
| font-size: large; | |||||
| } | |||||
| @ -0,0 +1,191 @@ | |||||
| package app.data.service; | |||||
| import app.data.bean.CalculatedMatch; | |||||
| import app.data.bean.PlayerForTable; | |||||
| import app.data.entity.Matchday; | |||||
| import app.data.entity.Player; | |||||
| import java.util.*; | |||||
| import java.util.function.Function; | |||||
| import java.util.stream.Collectors; | |||||
| import java.util.stream.Stream; | |||||
| class PlayerForTableProvider { | |||||
| private final PlayerService playerService; | |||||
| private final MatchdayService matchdayService; | |||||
| private final List<Matchday> matchdays = new ArrayList<>(); | |||||
| private final List<CalculatedMatch> calculatedMatches = new ArrayList<>(); | |||||
| private final List<Player> players; | |||||
| public PlayerForTableProvider(PlayerService playerService, MatchdayService matchdayService, Matchday matchday) { | |||||
| this.playerService = playerService; | |||||
| this.matchdayService = matchdayService; | |||||
| this.players = playerService.getRepository().findAll(); | |||||
| matchdays.addAll(matchdayService.getMatchdaysSorted(matchday.getSeason()).stream() | |||||
| .filter(matchdayToFilter -> matchdayToFilter.getNumber() <= matchday.getNumber()) | |||||
| .collect(Collectors.toList())); | |||||
| calculatedMatches.addAll(matchdays.stream() | |||||
| .flatMap((Function<Matchday, Stream<CalculatedMatch>>) matchdayToMap -> matchdayService.getCalculatedMatches(matchdayToMap).stream()) | |||||
| .collect(Collectors.toList())); | |||||
| } | |||||
| List<PlayerForTable> getPlayersForTableSorted(boolean calcDiffToLastMatchday) { | |||||
| List<PlayerForTable> playerForTableList = calcSortedPlayerForTableList(); | |||||
| int offset = 0; | |||||
| PlayerForTable currentPlayer; | |||||
| for (int i = 0; i < playerForTableList.size(); i++) { | |||||
| currentPlayer = playerForTableList.get(i); | |||||
| offset = getOffset(playerForTableList, offset, currentPlayer, i); | |||||
| currentPlayer.setPlace(i + 1 - offset); | |||||
| currentPlayer.setPlaceString(offset == 0 ? String.valueOf(i + 1) : ""); | |||||
| } | |||||
| 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(playerService, matchdayService, matchdays.get(matchdays.size() - 2)).getPlayersForTableSorted(false); | |||||
| } | |||||
| private PlayerForTable getPlayerForTable(Player player) { | |||||
| List<CalculatedMatch> matchesAsPlayer1 = getMatchesAsPlayer1(player); | |||||
| List<CalculatedMatch> matchesAsPlayer2 = getMatchesAsPlayer2(player); | |||||
| Map<WinState, List<CalculatedMatch>> map = getWinStateLists(matchesAsPlayer1, matchesAsPlayer2); | |||||
| int amountOfMatchesWon = map.get(WinState.WON).size(); | |||||
| int amountOfMatchesDrawn = map.get(WinState.DRAWN).size(); | |||||
| int amountOfMatchesLost = map.get(WinState.LOST).size(); | |||||
| int amountOfMatches = amountOfMatchesWon + amountOfMatchesDrawn + amountOfMatchesLost; | |||||
| int matchPoints = amountOfMatchesWon * 2 + amountOfMatchesDrawn; | |||||
| double gamePointsForSelf = getGamePointsForSelf(matchesAsPlayer1, matchesAsPlayer2); | |||||
| double gamePointsForOpponents = getGamePointsForOpponents(matchesAsPlayer1, matchesAsPlayer2); | |||||
| double gamePointDiff = gamePointsForSelf - gamePointsForOpponents; | |||||
| return new PlayerForTable(player, | |||||
| matchPoints, | |||||
| amountOfMatches, | |||||
| amountOfMatchesWon, | |||||
| amountOfMatchesDrawn, | |||||
| amountOfMatchesLost, | |||||
| gamePointsForSelf, | |||||
| gamePointsForOpponents, | |||||
| gamePointDiff); | |||||
| } | |||||
| private List<CalculatedMatch> getMatchesAsPlayer1(Player player) { | |||||
| return calculatedMatches.stream() | |||||
| .filter(match -> match.getPlayer1().equals(player)) | |||||
| .collect(Collectors.toList()); | |||||
| } | |||||
| private List<CalculatedMatch> getMatchesAsPlayer2(Player player) { | |||||
| return calculatedMatches.stream() | |||||
| .filter(match -> match.getPlayer2().equals(player)) | |||||
| .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<>()); | |||||
| map.put(WinState.DRAWN, new ArrayList<>()); | |||||
| map.put(WinState.LOST, new ArrayList<>()); | |||||
| for (CalculatedMatch match : matchesAsPlayer1) { | |||||
| if (match.getScore1() > match.getScore2()) { | |||||
| map.get(WinState.WON).add(match); | |||||
| continue; | |||||
| } | |||||
| if (match.getScore1() < match.getScore2()) { | |||||
| map.get(WinState.LOST).add(match); | |||||
| continue; | |||||
| } | |||||
| if (match.getScore1() > 0) { | |||||
| map.get(WinState.DRAWN).add(match); | |||||
| } | |||||
| } | |||||
| for (CalculatedMatch match : matchesAsPlayer2) { | |||||
| if (match.getScore2() > match.getScore1()) { | |||||
| map.get(WinState.WON).add(match); | |||||
| continue; | |||||
| } | |||||
| if (match.getScore2() < match.getScore1()) { | |||||
| map.get(WinState.LOST).add(match); | |||||
| continue; | |||||
| } | |||||
| if (match.getScore2() > 0) { | |||||
| map.get(WinState.DRAWN).add(match); | |||||
| } | |||||
| } | |||||
| return map; | |||||
| } | |||||
| private enum WinState { | |||||
| WON, DRAWN, LOST | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,84 @@ | |||||
| package app.navigation.player; | |||||
| import app.data.entity.Player; | |||||
| import app.data.entity.Season; | |||||
| import app.data.service.PlayerService; | |||||
| import app.data.service.SeasonService; | |||||
| import app.navigation.Navigable; | |||||
| import app.navigation.Navigation; | |||||
| import com.vaadin.flow.component.select.Select; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.lang.NonNull; | |||||
| import org.springframework.lang.Nullable; | |||||
| import java.util.List; | |||||
| import java.util.Optional; | |||||
| public class PlayerNavigation extends Navigation { | |||||
| PlayerNavigation(@Autowired PlayerService playerService, | |||||
| @Autowired SeasonService seasonService) { | |||||
| super(playerService, seasonService); | |||||
| } | |||||
| @Override | |||||
| @SuppressWarnings("unchecked") | |||||
| protected <PARENT extends Navigable, CHILD extends Navigable> List<CHILD> getChildren(@Nullable PARENT parent, @NonNull Class<CHILD> childClass) { | |||||
| if (childClass.equals(Player.class)) { | |||||
| return (List<CHILD>) getPlayerService().getAllPlayersSorted(); | |||||
| } | |||||
| if (childClass.equals(Season.class)) { | |||||
| return (List<CHILD>) getSeasonService().getAllSeasonsForPlayerSorted((Player) parent); | |||||
| } | |||||
| throw new UnsupportedOperationException(String.format("This method is not supported for childClass %s", childClass.getSimpleName())); | |||||
| } | |||||
| @Override | |||||
| @SuppressWarnings("unchecked") | |||||
| protected <T extends Navigable> T getDefaultValue(Class<T> clazz) { | |||||
| if(clazz.equals(Player.class)) { | |||||
| assert !getPlayerList().isEmpty(); | |||||
| return (T) getPlayerList().get(0); | |||||
| } | |||||
| if (clazz.equals(Season.class)) { | |||||
| assert !getSeasonList().isEmpty(); | |||||
| return (T) getSeasonList().get(getSeasonList().size() - 1); | |||||
| } | |||||
| throw new UnsupportedOperationException(String.format("This method is not supported for clazz %s", clazz.getSimpleName())); | |||||
| } | |||||
| public PlayerService getPlayerService() { | |||||
| return (PlayerService) serviceMap.get(Player.class); | |||||
| } | |||||
| public SeasonService getSeasonService() { | |||||
| return (SeasonService) serviceMap.get(Season.class); | |||||
| } | |||||
| @SuppressWarnings("unchecked") | |||||
| public List<Season> getSeasonList() { | |||||
| return (List<Season>) listMap.get(Season.class); | |||||
| } | |||||
| @SuppressWarnings("unchecked") | |||||
| public List<Player> getPlayerList() { | |||||
| return (List<Player>) listMap.get(Player.class); | |||||
| } | |||||
| @SuppressWarnings("unchecked") | |||||
| public Select<Season> getSeasonSelect() { | |||||
| return (Select<Season>) selectMap.get(Season.class); | |||||
| } | |||||
| @SuppressWarnings("unchecked") | |||||
| public Select<Player> getPlayerSelect() { | |||||
| return (Select<Player>) selectMap.get(Player.class); | |||||
| } | |||||
| public Optional<Player> getSelectedPlayer() { | |||||
| return getPlayerSelect().getOptionalValue(); | |||||
| } | |||||
| public Optional<Season> getSelectedSeason() { | |||||
| return getSeasonSelect().getOptionalValue(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,32 @@ | |||||
| package app.navigation.player; | |||||
| import app.data.service.PlayerService; | |||||
| import app.data.service.SeasonService; | |||||
| import app.navigation.NavigationHeader; | |||||
| import app.navigation.NavigationService; | |||||
| import app.navigation.player.components.PlayerNavigationHeader; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.stereotype.Service; | |||||
| @Service | |||||
| public class PlayerNavigationService implements NavigationService<PlayerNavigation> { | |||||
| private final PlayerService playerService; | |||||
| private final SeasonService seasonService; | |||||
| public PlayerNavigationService(@Autowired PlayerService playerService, | |||||
| @Autowired SeasonService seasonService) { | |||||
| this.playerService = playerService; | |||||
| this.seasonService = seasonService; | |||||
| } | |||||
| @Override | |||||
| public PlayerNavigation getNewNavigation() { | |||||
| return new PlayerNavigation(playerService, seasonService); | |||||
| } | |||||
| @Override | |||||
| public NavigationHeader<PlayerNavigation> getNewNavigationHeader(PlayerNavigation navigation) { | |||||
| return new PlayerNavigationHeader(navigation); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,19 @@ | |||||
| package app.navigation.player.components; | |||||
| import app.navigation.NavigationHeader; | |||||
| import app.navigation.player.PlayerNavigation; | |||||
| import com.vaadin.flow.component.html.Label; | |||||
| public class PlayerNavigationHeader extends NavigationHeader<PlayerNavigation> { | |||||
| public PlayerNavigationHeader(PlayerNavigation navigation) { | |||||
| super(navigation); | |||||
| } | |||||
| @Override | |||||
| protected void defineChildren() { | |||||
| removeAll(); | |||||
| add(new Label("Player:"), navigation.getPlayerSelect()); | |||||
| add(new Label("Season:"), navigation.getSeasonSelect()); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,37 @@ | |||||
| package app.navigation.player.components.button; | |||||
| import app.data.entity.Player; | |||||
| import app.navigation.player.PlayerNavigation; | |||||
| import com.vaadin.flow.component.button.Button; | |||||
| import com.vaadin.flow.component.icon.VaadinIcon; | |||||
| import java.util.List; | |||||
| import java.util.Optional; | |||||
| public class NextPlayerButton extends Button { | |||||
| private final PlayerNavigation playerNavigation; | |||||
| public NextPlayerButton(PlayerNavigation playerNavigation) { | |||||
| this.playerNavigation = playerNavigation; | |||||
| setIcon(VaadinIcon.ARROW_RIGHT.create()); | |||||
| playerNavigation.addRunnableToBeRunAfterSelection(this::configure); | |||||
| } | |||||
| private void configure() { | |||||
| Optional<Player> nextPlayer = getNextPlayer(); | |||||
| setEnabled(nextPlayer.isPresent()); | |||||
| addClickListener(event -> nextPlayer.ifPresent(player -> playerNavigation.getPlayerSelect().setValue(player))); | |||||
| } | |||||
| private Optional<Player> getNextPlayer() { | |||||
| int index = PlayerButtonUtils.getPlayerIndex(playerNavigation); | |||||
| if (index >= 0) { | |||||
| List<Player> playerList = playerNavigation.getPlayerList(); | |||||
| return Optional.of(playerList.get(Math.floorMod(index + 1, playerList.size()))); | |||||
| } | |||||
| return Optional.empty(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,14 @@ | |||||
| package app.navigation.player.components.button; | |||||
| import app.navigation.player.PlayerNavigation; | |||||
| import java.util.concurrent.atomic.AtomicInteger; | |||||
| class PlayerButtonUtils { | |||||
| static int getPlayerIndex(PlayerNavigation playerNavigation) { | |||||
| AtomicInteger index = new AtomicInteger(-1); | |||||
| playerNavigation.getSelectedPlayer().ifPresent(player -> index.set(playerNavigation.getPlayerList().indexOf(player))); | |||||
| return index.get(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,37 @@ | |||||
| package app.navigation.player.components.button; | |||||
| import app.data.entity.Player; | |||||
| import app.navigation.player.PlayerNavigation; | |||||
| import com.vaadin.flow.component.button.Button; | |||||
| import com.vaadin.flow.component.icon.VaadinIcon; | |||||
| import java.util.List; | |||||
| import java.util.Optional; | |||||
| public class PrevPlayerButton extends Button { | |||||
| private final PlayerNavigation playerNavigation; | |||||
| public PrevPlayerButton(PlayerNavigation playerNavigation) { | |||||
| this.playerNavigation = playerNavigation; | |||||
| setIcon(VaadinIcon.ARROW_LEFT.create()); | |||||
| playerNavigation.addRunnableToBeRunAfterSelection(this::configure); | |||||
| } | |||||
| private void configure() { | |||||
| Optional<Player> prevPlayer = getPrevPlayer(); | |||||
| setEnabled(prevPlayer.isPresent()); | |||||
| addClickListener(event -> prevPlayer.ifPresent(player -> playerNavigation.getPlayerSelect().setValue(player))); | |||||
| } | |||||
| private Optional<Player> getPrevPlayer() { | |||||
| int index = PlayerButtonUtils.getPlayerIndex(playerNavigation); | |||||
| if (index >= 0) { | |||||
| List<Player> playerList = playerNavigation.getPlayerList(); | |||||
| return Optional.of(playerList.get(Math.floorMod(index - 1, playerList.size()))); | |||||
| } | |||||
| return Optional.empty(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,45 @@ | |||||
| package app.views.player; | |||||
| import app.navigation.player.PlayerNavigation; | |||||
| import app.navigation.player.PlayerNavigationService; | |||||
| import app.views.main.MainView; | |||||
| import app.views.navigation.NavigationViewBase; | |||||
| import app.views.player.components.PlayerCard; | |||||
| import com.vaadin.flow.component.dependency.CssImport; | |||||
| import com.vaadin.flow.router.PageTitle; | |||||
| import com.vaadin.flow.router.Route; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| @CssImport("app/views/player/player-view.css") | |||||
| @Route(value = "player", layout = MainView.class) | |||||
| @PageTitle("Schachliga DACH - Players") | |||||
| public class PlayerView extends NavigationViewBase<PlayerNavigation>{ | |||||
| private PlayerCard playerCard; | |||||
| protected PlayerView(@Autowired PlayerNavigationService playerNavigationService) { | |||||
| super(playerNavigationService, "player"); | |||||
| addClassName("player-view"); | |||||
| defineLayout(); | |||||
| } | |||||
| //////////// | |||||
| // LAYOUT // | |||||
| //////////// | |||||
| private void defineLayout() { | |||||
| playerCard = new PlayerCard((PlayerNavigation) navigation); | |||||
| add(playerCard); | |||||
| } | |||||
| ///////////// | |||||
| // CONTENT // | |||||
| ///////////// | |||||
| @Override | |||||
| protected void configureContent() { | |||||
| playerCard.configureContent(); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,142 @@ | |||||
| package app.views.player.components; | |||||
| import app.data.bean.CalculatedMatch; | |||||
| import app.data.entity.Player; | |||||
| import app.data.entity.Season; | |||||
| import app.navigation.player.PlayerNavigation; | |||||
| import app.navigation.player.components.button.NextPlayerButton; | |||||
| import app.navigation.player.components.button.PrevPlayerButton; | |||||
| import app.utils.ComponentUtils; | |||||
| import app.utils.EntityStringUtils; | |||||
| import app.utils.StringUtils; | |||||
| import app.views.navigation.interfaces.ContentConfigurable; | |||||
| import com.vaadin.flow.component.UI; | |||||
| import com.vaadin.flow.component.button.Button; | |||||
| import com.vaadin.flow.component.button.ButtonVariant; | |||||
| import com.vaadin.flow.component.grid.ColumnTextAlign; | |||||
| import com.vaadin.flow.component.grid.Grid; | |||||
| import com.vaadin.flow.component.grid.GridVariant; | |||||
| import com.vaadin.flow.component.html.Div; | |||||
| import com.vaadin.flow.component.html.Label; | |||||
| import com.vaadin.flow.component.icon.VaadinIcon; | |||||
| import com.vaadin.flow.component.orderedlayout.HorizontalLayout; | |||||
| import com.vaadin.flow.component.orderedlayout.VerticalLayout; | |||||
| import java.util.NoSuchElementException; | |||||
| public class PlayerCard extends Div implements ContentConfigurable { | |||||
| private final PlayerNavigation playerNavigation; | |||||
| private final HorizontalLayout header = new HorizontalLayout(); | |||||
| private final HorizontalLayout headerLabelLayout = new HorizontalLayout(); | |||||
| private final Grid<CalculatedMatch> grid = new Grid<>(); | |||||
| private Player player; | |||||
| public PlayerCard(PlayerNavigation playerNavigation) { | |||||
| this.playerNavigation = playerNavigation; | |||||
| addClassName("card"); | |||||
| add(new VerticalLayout(header, grid)); | |||||
| defineHeader(); | |||||
| defineGrid(); | |||||
| } | |||||
| private void defineHeader() { | |||||
| header.add(new PrevPlayerButton(this.playerNavigation), headerLabelLayout, new NextPlayerButton(this.playerNavigation)); | |||||
| header.setWidthFull(); | |||||
| headerLabelLayout.addClassName("player-header-label-layout"); | |||||
| } | |||||
| private void defineGrid() { | |||||
| Label headerMatchday = new Label("Matchday"); | |||||
| headerMatchday.addClassName("column-header"); | |||||
| Label headerOpponent = new Label("Opponent"); | |||||
| headerOpponent.addClassName("column-header"); | |||||
| Label headerResult = new Label("Result"); | |||||
| headerResult.addClassName("column-header"); | |||||
| grid.addColumn(calculatedMatch -> calculatedMatch.getMatch().getMatchday().getNumber()) | |||||
| .setHeader(headerMatchday) | |||||
| .setTextAlign(ColumnTextAlign.CENTER) | |||||
| .setWidth("7em"); | |||||
| grid.addComponentColumn(calculatedMatch -> ComponentUtils.getPlayerLabel(getOpponent(calculatedMatch), getSeason(calculatedMatch), true)) | |||||
| .setHeader(headerOpponent) | |||||
| .setTextAlign(ColumnTextAlign.CENTER) | |||||
| .setWidth("13em"); | |||||
| grid.addColumn(this::getResultString) | |||||
| .setHeader(headerResult) | |||||
| .setTextAlign(ColumnTextAlign.CENTER) | |||||
| .setWidth("6em"); | |||||
| grid.addComponentColumn(this::createButton) | |||||
| .setTextAlign(ColumnTextAlign.CENTER) | |||||
| .setWidth("4em"); | |||||
| grid.setWidth("31em"); // TODO: find a way to set this dynamically based on column widths | |||||
| grid.setHeightByRows(true); | |||||
| grid.addThemeVariants(GridVariant.LUMO_NO_BORDER, | |||||
| GridVariant.LUMO_NO_ROW_BORDERS, GridVariant.LUMO_ROW_STRIPES); | |||||
| } | |||||
| @SuppressWarnings("DuplicatedCode") // TODO; get rid of duplicates | |||||
| private Button createButton(CalculatedMatch match) { | |||||
| Button button = new Button(); | |||||
| button.addThemeVariants(ButtonVariant.LUMO_TERTIARY_INLINE); | |||||
| String seasonParam = EntityStringUtils.getSeasonStringForURL(playerNavigation.getSelectedSeason().orElseThrow()); | |||||
| String matchdayParam = EntityStringUtils.getMatchdayStringForURL(match.getMatch().getMatchday()); | |||||
| String matchParam = EntityStringUtils.getMatchStringForURL(match.getMatch()); | |||||
| String targetWildcardParam = String.format("match/%s/%s/%s/", seasonParam, matchdayParam, matchParam); | |||||
| if (match.getScore1() == 0 && match.getScore2() == 0) { | |||||
| button.setIcon(VaadinIcon.PENCIL.create()); | |||||
| button.addClickListener(event -> UI.getCurrent().navigate(targetWildcardParam + "edit")); | |||||
| return button; | |||||
| } | |||||
| button.setIcon(VaadinIcon.EYE.create()); | |||||
| button.addClickListener(event -> UI.getCurrent().navigate(targetWildcardParam)); | |||||
| return button; | |||||
| } | |||||
| private Player getOpponent(CalculatedMatch match) { | |||||
| return match.getPlayer1().equals(player) ? match.getPlayer2() : match.getPlayer1(); | |||||
| } | |||||
| private Season getSeason(CalculatedMatch calculatedMatch) { | |||||
| return calculatedMatch.getMatch().getMatchday().getSeason(); | |||||
| } | |||||
| private String getResultString(CalculatedMatch match) { | |||||
| boolean isPlayer1 = match.getPlayer1().equals(player); | |||||
| double ownScore = isPlayer1 ? match.getScore1() : match.getScore2(); | |||||
| double opponentScore = isPlayer1 ? match.getScore2() : match.getScore1(); | |||||
| return StringUtils.getResultString(":", ownScore, opponentScore); | |||||
| } | |||||
| @Override | |||||
| public void configureContent() { | |||||
| try { | |||||
| player = playerNavigation.getSelectedPlayer().orElseThrow(); | |||||
| Season season = playerNavigation.getSelectedSeason().orElseThrow(); | |||||
| configureHeaderLabels(player, season); | |||||
| grid.setItems(playerNavigation.getPlayerService().getCalculatedMatchesSorted(player, season)); | |||||
| } catch (NoSuchElementException e) { | |||||
| removeAll(); | |||||
| add(playerNavigation.getValidationLabel()); | |||||
| } | |||||
| } | |||||
| private void configureHeaderLabels(Player player, Season season) { | |||||
| ComponentUtils.configurePlayerLabel(headerLabelLayout, player, season); | |||||
| } | |||||
| } | |||||