|
@ -3,36 +3,91 @@ package app.views.match.components; |
|
|
import app.data.bean.CalculatedMatch; |
|
|
import app.data.bean.CalculatedMatch; |
|
|
import app.data.entity.Game; |
|
|
import app.data.entity.Game; |
|
|
import app.data.entity.Match; |
|
|
import app.data.entity.Match; |
|
|
|
|
|
import app.data.service.ChessComService; |
|
|
|
|
|
import app.data.service.GameInfoService; |
|
|
|
|
|
import app.data.service.GameService; |
|
|
|
|
|
import app.gameimage.GameImageService; |
|
|
import app.navigation.Navigation; |
|
|
import app.navigation.Navigation; |
|
|
|
|
|
import app.utils.ChessComUtils; |
|
|
import app.utils.EntityComponentUtils; |
|
|
import app.utils.EntityComponentUtils; |
|
|
import app.utils.StringUtils; |
|
|
import app.utils.StringUtils; |
|
|
import app.views.navigation.interfaces.ContentConfigurable; |
|
|
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.html.Div; |
|
|
import com.vaadin.flow.component.html.Div; |
|
|
import com.vaadin.flow.component.html.Label; |
|
|
import com.vaadin.flow.component.html.Label; |
|
|
|
|
|
import com.vaadin.flow.component.icon.Icon; |
|
|
|
|
|
import com.vaadin.flow.component.icon.VaadinIcon; |
|
|
import com.vaadin.flow.component.orderedlayout.FlexComponent; |
|
|
import com.vaadin.flow.component.orderedlayout.FlexComponent; |
|
|
import com.vaadin.flow.component.orderedlayout.FlexLayout; |
|
|
import com.vaadin.flow.component.orderedlayout.FlexLayout; |
|
|
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
|
|
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
|
|
import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
|
|
import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
|
|
|
|
|
import com.vaadin.flow.component.textfield.TextField; |
|
|
|
|
|
import com.vaadin.flow.shared.Registration; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.List; |
|
|
import java.util.NoSuchElementException; |
|
|
import java.util.NoSuchElementException; |
|
|
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
|
public class MatchComponent extends Div implements ContentConfigurable { |
|
|
public class MatchComponent extends Div implements ContentConfigurable { |
|
|
|
|
|
|
|
|
private final Navigation navigation; |
|
|
private final Navigation navigation; |
|
|
|
|
|
private final ChessComService chessComService; |
|
|
|
|
|
private final GameService gameService; |
|
|
|
|
|
private final GameInfoService gameInfoService; |
|
|
|
|
|
private final GameImageService gameImageService; |
|
|
|
|
|
|
|
|
private final VerticalLayout headerLayout = new VerticalLayout(); |
|
|
private final VerticalLayout headerLayout = new VerticalLayout(); |
|
|
private final HorizontalLayout headerPlayersLayout = new HorizontalLayout(); |
|
|
private final HorizontalLayout headerPlayersLayout = new HorizontalLayout(); |
|
|
private final Label headerResultLabel = new Label(); |
|
|
private final Label headerResultLabel = new Label(); |
|
|
|
|
|
|
|
|
private final FlexLayout gamesLayout = new FlexLayout(); |
|
|
private final FlexLayout gamesLayout = new FlexLayout(); |
|
|
|
|
|
|
|
|
public MatchComponent(Navigation navigation) { |
|
|
|
|
|
|
|
|
private final VerticalLayout noGamesLayout = new VerticalLayout(); |
|
|
|
|
|
|
|
|
|
|
|
private final FlexLayout editLayoutWrapper = new FlexLayout(); |
|
|
|
|
|
private final VerticalLayout editLayout = new VerticalLayout(); |
|
|
|
|
|
private final ArrayList<TextField> editTextFields = new ArrayList<>(); |
|
|
|
|
|
private final Button editSubmitButton = new Button("Submit", new Icon(VaadinIcon.CHECK)); |
|
|
|
|
|
private Registration editSubmitButtonRegistration; |
|
|
|
|
|
private final Button editCancelButton = new Button("Cancel", new Icon(VaadinIcon.CLOSE)); |
|
|
|
|
|
private Registration editCancelButtonRegistration; |
|
|
|
|
|
private final Button chessComButton = new Button("Autofill with the latest 6 games between the players", new Icon(VaadinIcon.MAGIC)); |
|
|
|
|
|
private Registration chessComButtonButtonRegistration; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public MatchComponent(Navigation navigation, |
|
|
|
|
|
@Autowired ChessComService chessComService, |
|
|
|
|
|
@Autowired GameService gameService, |
|
|
|
|
|
@Autowired GameInfoService gameInfoService, |
|
|
|
|
|
@Autowired GameImageService gameImageService) { |
|
|
this.navigation = navigation; |
|
|
this.navigation = navigation; |
|
|
|
|
|
this.chessComService = chessComService; |
|
|
|
|
|
this.gameService = gameService; |
|
|
|
|
|
this.gameInfoService = gameInfoService; |
|
|
|
|
|
this.gameImageService = gameImageService; |
|
|
|
|
|
|
|
|
|
|
|
add(headerLayout); |
|
|
|
|
|
|
|
|
add(headerLayout, gamesLayout); |
|
|
|
|
|
|
|
|
|
|
|
configureHeaderLayout(); |
|
|
configureHeaderLayout(); |
|
|
configureGamesLayout(); |
|
|
configureGamesLayout(); |
|
|
|
|
|
configureNoGamesLayout(); |
|
|
|
|
|
configureEditLayout(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void configureNoGamesLayout() { |
|
|
|
|
|
Label label = new Label("No games for this match in the database."); |
|
|
|
|
|
Button button = new Button("Add games", new Icon(VaadinIcon.PLUS)); |
|
|
|
|
|
button.addThemeVariants(ButtonVariant.LUMO_PRIMARY); |
|
|
|
|
|
button.addClickListener(event -> { |
|
|
|
|
|
navigation.setEditFlag(true); |
|
|
|
|
|
remove(noGamesLayout); |
|
|
|
|
|
}); |
|
|
|
|
|
noGamesLayout.add(label, button); |
|
|
|
|
|
noGamesLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void configureHeaderLayout() { |
|
|
private void configureHeaderLayout() { |
|
@ -49,23 +104,112 @@ public class MatchComponent extends Div implements ContentConfigurable { |
|
|
gamesLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
gamesLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void configureEditLayout() { |
|
|
|
|
|
editLayoutWrapper.add(editLayout); |
|
|
|
|
|
editLayoutWrapper.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
|
|
|
|
|
|
|
|
|
editLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
|
|
|
editLayout.setWidth(""); |
|
|
|
|
|
|
|
|
|
|
|
editLayout.addClassName("wrapper"); |
|
|
|
|
|
|
|
|
|
|
|
editLayout.add(chessComButton); |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; i++) { |
|
|
|
|
|
TextField textField = new TextField(); |
|
|
|
|
|
textField.setWidth("23em"); |
|
|
|
|
|
HorizontalLayout horizontalLayout = new HorizontalLayout(new Label(String.format("Game %d:", i + 1)), textField); |
|
|
|
|
|
horizontalLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
|
|
|
horizontalLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
|
|
|
editLayout.add(horizontalLayout); |
|
|
|
|
|
editTextFields.add(textField); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
editSubmitButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY); |
|
|
|
|
|
|
|
|
|
|
|
HorizontalLayout buttonLayout = new HorizontalLayout(); |
|
|
|
|
|
buttonLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
|
|
|
buttonLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
|
|
|
buttonLayout.add(editSubmitButton, editCancelButton); |
|
|
|
|
|
editLayout.add(buttonLayout); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void configureContent() { |
|
|
public void configureContent() { |
|
|
try { |
|
|
try { |
|
|
Match match = navigation.getSelectedMatch().orElseThrow(); |
|
|
Match match = navigation.getSelectedMatch().orElseThrow(); |
|
|
|
|
|
|
|
|
configureHeaderContent(match); |
|
|
configureHeaderContent(match); |
|
|
|
|
|
|
|
|
gamesLayout.removeAll(); |
|
|
|
|
|
for (Game game : match.getGames()) { |
|
|
|
|
|
gamesLayout.add(new GameComponent(game)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
configureMainContent(match); |
|
|
} catch (NoSuchElementException e) { |
|
|
} catch (NoSuchElementException e) { |
|
|
gamesLayout.removeAll(); |
|
|
gamesLayout.removeAll(); |
|
|
add(navigation.getValidationLabel()); |
|
|
add(navigation.getValidationLabel()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void configureMainContent(Match match) { |
|
|
|
|
|
if (navigation.editFlag()) { |
|
|
|
|
|
remove(gamesLayout); |
|
|
|
|
|
add(editLayoutWrapper); |
|
|
|
|
|
configureEditContent(match); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
remove(editLayoutWrapper); |
|
|
|
|
|
configureGamesContent(match); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void configureEditContent(Match match) { |
|
|
|
|
|
List<Game> games = new ArrayList<>(match.getGames()); |
|
|
|
|
|
|
|
|
|
|
|
// clear text fields |
|
|
|
|
|
for (TextField textField : editTextFields) { |
|
|
|
|
|
textField.setValue(""); |
|
|
|
|
|
} |
|
|
|
|
|
// fill text fields; |
|
|
|
|
|
for (int i = 0; i < Math.min(games.size(), 6); i++) |
|
|
|
|
|
editTextFields.get(i).setValue(ChessComUtils.getGameURL(games.get(i))); |
|
|
|
|
|
|
|
|
|
|
|
if (chessComButtonButtonRegistration != null) chessComButtonButtonRegistration.remove(); |
|
|
|
|
|
chessComButtonButtonRegistration = chessComButton.addClickListener(event -> { |
|
|
|
|
|
List<Game> gamesBetweenPlayers = chessComService.getLatestGamesBetweenPlayers(match, 6, 2); |
|
|
|
|
|
for (int i = 0; i < Math.min(gamesBetweenPlayers.size(), 6); i++) { |
|
|
|
|
|
editTextFields.get(i).setValue(ChessComUtils.getGameURL(gamesBetweenPlayers.get(i))); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (editSubmitButtonRegistration != null) editSubmitButtonRegistration.remove(); |
|
|
|
|
|
editSubmitButtonRegistration = editSubmitButton.addClickListener(event -> { |
|
|
|
|
|
match.getGames().clear(); // TODO: delete old games from database as well |
|
|
|
|
|
for (TextField textField : editTextFields) { |
|
|
|
|
|
Optional<Game> game = chessComService.getGame(textField.getValue(), match); // TODO: handle this when Optional is empty! |
|
|
|
|
|
game.ifPresent(value -> match.getGames().add(value)); |
|
|
|
|
|
} |
|
|
|
|
|
match.getGames().forEach(game -> gameInfoService.update(game.getGameInfo())); |
|
|
|
|
|
match.getGames().forEach(gameImageService::createImageIfNotPresent); |
|
|
|
|
|
|
|
|
|
|
|
navigation.setEditFlag(false); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (editCancelButtonRegistration != null) editCancelButtonRegistration.remove(); |
|
|
|
|
|
editCancelButtonRegistration = editCancelButton.addClickListener(event -> |
|
|
|
|
|
UI.getCurrent().navigate(String.format("matchday/%s", navigation.getWildcardParam().replace("edit/", "")))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void configureGamesContent(Match match) { |
|
|
|
|
|
if (match.getGames().isEmpty()) { |
|
|
|
|
|
remove(gamesLayout); |
|
|
|
|
|
add(noGamesLayout); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
remove(noGamesLayout); |
|
|
|
|
|
add(gamesLayout); |
|
|
|
|
|
|
|
|
|
|
|
gamesLayout.removeAll(); |
|
|
|
|
|
for (Game game : match.getGames()) { |
|
|
|
|
|
gamesLayout.add(new GameComponent(game, gameImageService)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private void configureHeaderContent(Match match) { |
|
|
private void configureHeaderContent(Match match) { |
|
|
CalculatedMatch calculatedMatch = navigation.getMatchService().getCalculatedMatch(match); |
|
|
CalculatedMatch calculatedMatch = navigation.getMatchService().getCalculatedMatch(match); |
|
|
|
|
|
|
|
@ -76,7 +220,12 @@ public class MatchComponent extends Div implements ContentConfigurable { |
|
|
headerPlayersLayout.removeAll(); |
|
|
headerPlayersLayout.removeAll(); |
|
|
headerPlayersLayout.add(player1, vs, player2); |
|
|
headerPlayersLayout.add(player1, vs, player2); |
|
|
|
|
|
|
|
|
headerResultLabel.setText(String.format("%s", |
|
|
|
|
|
StringUtils.getResultString("-", calculatedMatch.getScore1(), calculatedMatch.getScore2()))); |
|
|
|
|
|
|
|
|
if (match.getGames().isEmpty() || navigation.editFlag()) { |
|
|
|
|
|
headerLayout.remove(headerResultLabel); |
|
|
|
|
|
} else { |
|
|
|
|
|
headerLayout.add(headerResultLabel); |
|
|
|
|
|
headerResultLabel.setText(String.format("%s", |
|
|
|
|
|
StringUtils.getResultString("-", calculatedMatch.getScore1(), calculatedMatch.getScore2()))); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |