Skip to content

Commit

Permalink
Generate maps with options
Browse files Browse the repository at this point in the history
Add UI to select options when creating a game with a generated map.
Fixes #1781
  • Loading branch information
Sheikah45 committed Jul 19, 2020
1 parent f6f23b7 commit f47737e
Show file tree
Hide file tree
Showing 19 changed files with 1,248 additions and 91 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/faforever/client/config/CacheConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.faforever.client.config.CacheNames.GLOBAL_LEADERBOARD;
import static com.faforever.client.config.CacheNames.LADDER_1V1_LEADERBOARD;
import static com.faforever.client.config.CacheNames.MAPS;
import static com.faforever.client.config.CacheNames.MAP_GENERATOR;
import static com.faforever.client.config.CacheNames.MAP_PREVIEW;
import static com.faforever.client.config.CacheNames.MODS;
import static com.faforever.client.config.CacheNames.MOD_THUMBNAIL;
Expand All @@ -53,6 +54,7 @@ public CacheManager cacheManager() {
new CaffeineCache(ACHIEVEMENTS, newBuilder().expireAfterWrite(10, MINUTES).build()),
new CaffeineCache(MODS, newBuilder().expireAfterWrite(10, MINUTES).build()),
new CaffeineCache(MAPS, newBuilder().expireAfterWrite(10, MINUTES).build()),
new CaffeineCache(MAP_GENERATOR, newBuilder().expireAfterWrite(10, MINUTES).build()),
new CaffeineCache(GLOBAL_LEADERBOARD, newBuilder().maximumSize(1).expireAfterAccess(5, MINUTES).build()),
new CaffeineCache(LADDER_1V1_LEADERBOARD, newBuilder().maximumSize(1).expireAfterAccess(5, MINUTES).build()),
new CaffeineCache(AVAILABLE_AVATARS, newBuilder().expireAfterAccess(10, MINUTES).build()),
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/faforever/client/config/CacheNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public final class CacheNames {
public static final String LADDER_1V1_LEADERBOARD = "ladder1v1Leaderboard";
public static final String GLOBAL_LEADERBOARD = "globalLeaderboard";
public static final String MAPS = "maps";
public static final String MAP_GENERATOR = "mapGenerator";
public static final String THEME_IMAGES = "themeImages";
public static final String MOD_THUMBNAIL = "modThumbnail";
public static final String COOP_MAPS = "coopMaps";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ public static class MapGenerator {
private String downloadUrlFormat;
private String repoAndOwnerName;
private String queryLatestVersionUrl;
private String queryVersionsUrl;
private int maxSupportedMajorVersion;
private int minSupportedMajorVersion;
}

@Data
Expand Down
69 changes: 46 additions & 23 deletions src/main/java/com/faforever/client/game/CreateGameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialog;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
Expand All @@ -47,18 +48,17 @@
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.util.Callback;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.ref.WeakReference;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -73,10 +73,10 @@
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@RequiredArgsConstructor
@Slf4j
public class CreateGameController implements Controller<Pane> {

private static final int MAX_RATING_LENGTH = 4;
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String STYLE_CLASS_DUAL_LIST_CELL = "create-game-dual-list-cell";
private final MapService mapService;
private final ModService modService;
Expand All @@ -100,6 +100,7 @@ public class CreateGameController implements Controller<Pane> {
public TextField maxRankingTextField;
public ListView<FeaturedMod> featuredModListView;
public ListView<MapBean> mapListView;
public StackPane gamesRoot;
public Pane createGameRoot;
public Button createGameButton;
public Pane mapPreviewPane;
Expand Down Expand Up @@ -148,10 +149,10 @@ public void initialize() {
selectionModel.select(newMapIndex);
mapListView.scrollTo(newMapIndex);
});

Function<FeaturedMod, String> isDefaultModString = mod ->
Objects.equals(mod.getTechnicalName(), KnownFeaturedMod.DEFAULT.getTechnicalName()) ?
" " + i18n.get("game.create.defaultGameTypeMarker") : null;
Objects.equals(mod.getTechnicalName(), KnownFeaturedMod.DEFAULT.getTechnicalName()) ?
" " + i18n.get("game.create.defaultGameTypeMarker") : null;

featuredModListView.setCellFactory(param ->
new DualStringListCell<>(FeaturedMod::getDisplayName, isDefaultModString, STYLE_CLASS_DUAL_LIST_CELL, uiService)
Expand Down Expand Up @@ -256,12 +257,12 @@ private void initModList() {
try {
modService.getActivatedSimAndUIMods().forEach(mod -> modListView.getSelectionModel().select(mod));
} catch (IOException e) {
logger.error("Activated mods could not be loaded", e);
log.error("Activated mods could not be loaded", e);
}
modListView.scrollTo(modListView.getSelectionModel().getSelectedItem());
}

private void initMapSelection() {
protected void initMapSelection() {
filteredMapBeans = new FilteredList<>(
mapService.getInstalledMaps().filtered(mapBean -> mapBean.getType() == Type.SKIRMISH).sorted((o1, o2) -> o1.getDisplayName().compareToIgnoreCase(o2.getDisplayName()))
);
Expand All @@ -271,7 +272,7 @@ private void initMapSelection() {
mapListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> Platform.runLater(() -> setSelectedMap(newValue)));
}

private void setSelectedMap(MapBean newValue) {
protected void setSelectedMap(MapBean newValue) {
JavaFxUtil.assertApplicationThread();

if (newValue == null) {
Expand Down Expand Up @@ -388,22 +389,40 @@ public void onRandomMapButtonClicked() {
}

public void onGenerateMapButtonClicked() {
onGenerateMap();
}

private void onGenerateMap() {
try {
mapGeneratorService.generateMap().thenAccept(mapName -> {
Platform.runLater(() -> {
initMapSelection();
mapListView.getItems().stream()
.filter(mapBean -> mapBean.getFolderName().equalsIgnoreCase(mapName))
.findAny().ifPresent(mapBean -> {
mapListView.getSelectionModel().select(mapBean);
mapListView.scrollTo(mapBean);
setSelectedMap(mapBean);
mapGeneratorService.setGeneratorVersion(mapGeneratorService.queryMaxSupportedVersion());
// Check if generated map is major version 0 which does not support options
if (mapGeneratorService.getGeneratorVersion().compareTo(new ComparableVersion("1")) < 0) {
mapGeneratorService.generateMap().thenAccept(mapName -> {
Platform.runLater(() -> {
initMapSelection();
mapListView.getItems().stream()
.filter(mapBean -> mapBean.getFolderName().equalsIgnoreCase(mapName))
.findAny()
.ifPresent(mapBean -> {
mapListView.getSelectionModel().select(mapBean);
mapListView.scrollTo(mapBean);
setSelectedMap(mapBean);
});
});
});
});
} else {
GenerateMapController generateMapController = uiService.loadFxml("theme/play/generate_map.fxml");

Pane root = generateMapController.getRoot();
generateMapController.setCreateGameController(this);
JFXDialog dialog = uiService.showInDialog(gamesRoot, root, i18n.get("game.generate.dialog"));
generateMapController.setOnCloseButtonClickedListener(dialog::close);

root.requestFocus();
}
} catch (Exception e) {
notificationService.addImmediateErrorNotification(e, "mapGenerator.generationFailed");
logger.error("Map generation failed", e);
log.error("Map generation failed", e);
}
}

Expand All @@ -413,7 +432,7 @@ public void onCreateButtonClicked() {
try {
modService.overrideActivatedMods(modListView.getSelectionModel().getSelectedItems());
} catch (IOException e) {
logger.warn("Activated mods could not be updated", e);
log.warn("Activated mods could not be updated", e);
}

Set<String> simMods = selectedModVersions.stream()
Expand All @@ -429,7 +448,7 @@ public void onCreateButtonClicked() {
onlyForFriendsCheckBox.isSelected() ? GameVisibility.PRIVATE : GameVisibility.PUBLIC);

gameService.hostGame(newGameInfo).exceptionally(throwable -> {
logger.warn("Game could not be hosted", throwable);
log.warn("Game could not be hosted", throwable);
notificationService.addNotification(
new ImmediateErrorNotification(
i18n.get("errorTitle"),
Expand All @@ -447,6 +466,10 @@ public Pane getRoot() {
return createGameRoot;
}

public void setGamesRoot(StackPane root) {
gamesRoot = root;
}

public void onDeselectModsButtonClicked() {
modListView.getSelectionModel().clearSelection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private void onCreateGame(@Nullable String mapFolderName) {
}

CreateGameController createGameController = uiService.loadFxml("theme/play/create_game.fxml");
createGameController.setGamesRoot(gamesRoot);

if (mapFolderName != null && !createGameController.selectMap(mapFolderName)) {
log.warn("Map with folder name '{}' could not be found in map list", mapFolderName);
Expand Down

0 comments on commit f47737e

Please sign in to comment.