Skip to content

Commit

Permalink
start game file directly via parameter (#12862)
Browse files Browse the repository at this point in the history
* start game file directly via parameter

Examples arguments (for gradle run use pattern "run --args='<arguments>'":
'triplea.start=lobby' to start lobby login window
'triplea.game=<save file>' to start saved game as local game
'triplea.game=<game file>' to start new game as local game (only full path supported)
'triplea.game=<save file> triplea.start=pbem' to start saved game as PBEM game

HeadedGameRunner.java
- handling of main-arguments reworked, especially new one 'triplea.start' via parsing in new method setPropertiesFromArgs and handling in method showMainFrame
MainFrame.java
- new method startGameDirectly()
- rename field mainFrame to mainJFrame to call method MainPanelBuilder.getMainPanelLaunchAction() with MainPanel

CliProperties.java
- add constants for TRIPLEA_START (triplea.start)
Constants.java
- adding @NonNls to some constants
HeadedServerSetupModel.java
- typos
MainPanelBuilder.java
- extract new public static method getMainPanelLaunchAction to allow call from MainFrame
ServerModel.java
- typos

* fix checkstyleMain due to star imports '.*'

---------

Co-authored-by: RogerCooper <[email protected]>
  • Loading branch information
frigoref and RogerCooper committed Aug 25, 2024
1 parent 4e98143 commit 41bc340
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
/** A collection of all CLI related constants. */
public class CliProperties {
@NonNls public static final String TRIPLEA_GAME = "triplea.game";
@NonNls public static final String TRIPLEA_SERVER = "triplea.server";
@NonNls public static final String TRIPLEA_CLIENT = "triplea.client";
@NonNls public static final String TRIPLEA_SERVER = "triplea.server"; // true/false
@NonNls public static final String TRIPLEA_CLIENT = "triplea.client"; // true/false
@NonNls public static final String TRIPLEA_HOST = "triplea.host";
@NonNls public static final String TRIPLEA_PORT = "triplea.port";
@NonNls public static final String TRIPLEA_NAME = "triplea.name";
@NonNls public static final String TRIPLEA_START = "triplea.start";
@NonNls public static final String SERVER_PASSWORD = "triplea.server.password";
@NonNls public static final String LOBBY_URI = "triplea.lobby.uri";
@NonNls public static final String LOBBY_GAME_COMMENTS = "triplea.lobby.game.comments";
@NonNls public static final String MAP_FOLDER = "triplea.map.folder";
@NonNls public static final String TRIPLEA_MAP_DOWNLOAD = "triplea.map.download";
@NonNls public static final String TRIPLEA_MAP_DOWNLOAD_PREFIX = "triplea:";

@NonNls public static final String TRIPLEA_START_LOCAL = "local";
@NonNls public static final String TRIPLEA_START_PBF = "pbf";
@NonNls public static final String TRIPLEA_START_PBEM = "pbem";
@NonNls public static final String TRIPLEA_START_LOBBY = "lobby";

private CliProperties() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ private void setPlayerEnabled(final String playerName, final boolean enabled) {
playersEnabledListing.put(playerName, enabled);
if (launchAction.shouldMinimizeExpensiveAiUse()) {
// we do not want the host bot to actually play, so set to null if enabled,
// and set to weak ai if disabled
// and set to weak AI if disabled
if (enabled) {
playersToNodeListing.put(playerName, null);
} else {
Expand Down Expand Up @@ -484,7 +484,7 @@ private Map<String, PlayerTypes.Type> getLocalPlayerTypes() {
}

final Map<String, PlayerTypes.Type> localPlayerMappings = new HashMap<>();
// local player default = humans (for bots = weak ai)
// local player default = humans (for bots = weak AI)
final PlayerTypes.Type defaultLocalType = launchAction.getDefaultLocalPlayerType();
for (final Map.Entry<String, String> entry : playersToNodeListing.entrySet()) {
final String player = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ private void selectGameFile() {
}
}

private void gameSelected(final Path gameFile) {
public void gameSelected(final Path gameFile) {
BackgroundTaskRunner.runInBackground(
"Loading map...",
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,49 +210,49 @@ public interface Constants {
String SUPPORT_RULE_NAME_OLD = SUPPORT_ATTACHMENT_PREFIX + OLD_ART_RULE_NAME;
String SUPPORT_RULE_NAME_OLD_TEMP_FIRST =
SUPPORT_ATTACHMENT_PREFIX + OLD_ART_RULE_NAME + "TempFirst";
String EDIT_MODE = "EditMode";
@NonNls String EDIT_MODE = "EditMode";
// by default this is 0, but for lhtr, it is 1
String ORIGINAL_OWNER = "originalOwner";
@NonNls String ORIGINAL_OWNER = "originalOwner";
// unit types
String UNIT_TYPE_INFANTRY = "infantry";
String UNIT_TYPE_INF = "inf";
String UNIT_TYPE_MOTORIZED = "motorized";
String UNIT_TYPE_ARMOUR = "armour";
String UNIT_TYPE_TRANSPORT = "transport";
String UNIT_TYPE_SUBMARINE = "submarine";
String UNIT_TYPE_BATTLESHIP = "battleship";
String UNIT_TYPE_MARINE = "marine";
String UNIT_TYPE_CARRIER = "carrier";
String UNIT_TYPE_FIGHTER = "fighter";
String UNIT_TYPE_BOMBER = "bomber";
String UNIT_TYPE_FACTORY = "factory";
String UNIT_TYPE_AAGUN = "aaGun";
String UNIT_TYPE_ARTILLERY = "artillery";
String UNIT_TYPE_DESTROYER = "destroyer";
String SMALL_MAP_FILENAME = "smallMap";
String[] SMALL_MAP_EXTENSIONS = {"jpeg", "jpg", "png"};
String MAP_NAME = "mapName";
@NonNls String UNIT_TYPE_INFANTRY = "infantry";
@NonNls String UNIT_TYPE_INF = "inf";
@NonNls String UNIT_TYPE_MOTORIZED = "motorized";
@NonNls String UNIT_TYPE_ARMOUR = "armour";
@NonNls String UNIT_TYPE_TRANSPORT = "transport";
@NonNls String UNIT_TYPE_SUBMARINE = "submarine";
@NonNls String UNIT_TYPE_BATTLESHIP = "battleship";
@NonNls String UNIT_TYPE_MARINE = "marine";
@NonNls String UNIT_TYPE_CARRIER = "carrier";
@NonNls String UNIT_TYPE_FIGHTER = "fighter";
@NonNls String UNIT_TYPE_BOMBER = "bomber";
@NonNls String UNIT_TYPE_FACTORY = "factory";
@NonNls String UNIT_TYPE_AAGUN = "aaGun";
@NonNls String UNIT_TYPE_ARTILLERY = "artillery";
@NonNls String UNIT_TYPE_DESTROYER = "destroyer";
@NonNls String SMALL_MAP_FILENAME = "smallMap";
@NonNls String[] SMALL_MAP_EXTENSIONS = {"jpeg", "jpg", "png"};
@NonNls String MAP_NAME = "mapName";

// relationships stuff
String RELATIONSHIP_TYPE_SELF = "self_relation";
String RELATIONSHIP_TYPE_NULL = "null_relation";
String RELATIONSHIP_TYPE_DEFAULT_ALLIED = "default_allied_relation";
String RELATIONSHIP_TYPE_DEFAULT_WAR = "default_war_relation";
String RELATIONSHIP_CONDITION_ANY_NEUTRAL = "anyNeutral";
String RELATIONSHIP_CONDITION_ANY = "any";
String RELATIONSHIP_CONDITION_ANY_ALLIED = "anyAllied";
String RELATIONSHIP_CONDITION_ANY_WAR = "anyWar";
String RELATIONSHIP_ARCHETYPE_NEUTRAL = "neutral";
String RELATIONSHIP_ARCHETYPE_WAR = "war";
String RELATIONSHIP_ARCHETYPE_ALLIED = "allied";
String RELATIONSHIP_PROPERTY_DEFAULT = "default";
String RELATIONSHIP_PROPERTY_TRUE = "true";
String RELATIONSHIP_PROPERTY_FALSE = "false";
String PROPERTY_TRUE = "true";
String PROPERTY_FALSE = "false";
@NonNls String RELATIONSHIP_TYPE_SELF = "self_relation";
@NonNls String RELATIONSHIP_TYPE_NULL = "null_relation";
@NonNls String RELATIONSHIP_TYPE_DEFAULT_ALLIED = "default_allied_relation";
@NonNls String RELATIONSHIP_TYPE_DEFAULT_WAR = "default_war_relation";
@NonNls String RELATIONSHIP_CONDITION_ANY_NEUTRAL = "anyNeutral";
@NonNls String RELATIONSHIP_CONDITION_ANY = "any";
@NonNls String RELATIONSHIP_CONDITION_ANY_ALLIED = "anyAllied";
@NonNls String RELATIONSHIP_CONDITION_ANY_WAR = "anyWar";
@NonNls String RELATIONSHIP_ARCHETYPE_NEUTRAL = "neutral";
@NonNls String RELATIONSHIP_ARCHETYPE_WAR = "war";
@NonNls String RELATIONSHIP_ARCHETYPE_ALLIED = "allied";
@NonNls String RELATIONSHIP_PROPERTY_DEFAULT = "default";
@NonNls String RELATIONSHIP_PROPERTY_TRUE = "true";
@NonNls String RELATIONSHIP_PROPERTY_FALSE = "false";
@NonNls String PROPERTY_TRUE = "true";
@NonNls String PROPERTY_FALSE = "false";

String CONSTRUCTION_TYPE_FACTORY = "factory";
String CONSTRUCTION_TYPE_STRUCTURE = "structure";
@NonNls String CONSTRUCTION_TYPE_FACTORY = "factory";
@NonNls String CONSTRUCTION_TYPE_STRUCTURE = "structure";

static String getIncomePercentageFor(final GamePlayer gamePlayer) {
return gamePlayer.getName() + " Income Percentage";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public void showPbem() {
}

/**
* Starts the game server and displays the game start screen afterwards, awaiting remote game
* clients.
* Starts the game server and then displays the game start screen, awaiting remote game clients.
*/
public ServerModel showServer() {
final ServerModel serverModel = new ServerModel(gameSelectorModel, new HeadedLaunchAction(ui));
Expand All @@ -81,7 +80,7 @@ private void onServerMessengerCreated(final ServerModel serverModel) {
() -> {
setGameTypePanel(new ServerSetupPanel(serverModel, gameSelectorModel));

// for whatever reason, the server window is showing very very small, causing the nation
// for whatever reason, the server window is showing very, very small, causing the nation
// info to be cut and requiring scroll bars
final int x = Math.max(ui.getPreferredSize().width, 800);
final int y = Math.max(ui.getPreferredSize().height, 660);
Expand All @@ -92,7 +91,7 @@ private void onServerMessengerCreated(final ServerModel serverModel) {

/**
* A method that establishes a connection to a remote game and displays the game start screen
* afterwards if the connection was successfully established.
* after the connection was successfully established.
*/
public void showClient() {
Preconditions.checkState(!SwingUtilities.isEventDispatchThread());
Expand Down Expand Up @@ -123,7 +122,8 @@ private void setGameTypePanel(final SetupPanel panel) {
/**
* Executes a login sequence prompting the user for their lobby username+password and sends it to
* server. If successful the user is presented with the lobby frame. Failure cases are handled and
* user is presented with another try or they can abort. In the abort case this method is a no-op.
* user is presented with another try, or they can abort. In the abort case this method is a
* no-op.
*/
public void login() {
new LobbyLogin(ui)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import games.strategy.engine.framework.startup.ui.panels.main.game.selector.GameSelectorPanel;
import games.strategy.engine.framework.ui.background.WaitWindow;
import java.util.Optional;
import java.util.function.Consumer;
import javax.swing.JOptionPane;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.triplea.game.startup.SetupModel;
import org.triplea.java.ThreadRunner;

Expand All @@ -31,11 +33,7 @@ public MainPanel buildMainPanel(
new MainPanel(
quitAction,
gameSelectorPanel,
uiPanel -> {
final var setupPanel = headedServerSetupModel.getPanel();
setupPanel.getLauncher().ifPresent(launcher -> launch(uiPanel, launcher));
setupPanel.postStartGame();
},
getMainPanelLaunchAction(headedServerSetupModel),
Optional.ofNullable(headedServerSetupModel.getPanel())
.map(SetupModel::getChatModel)
.orElse(null),
Expand All @@ -45,7 +43,16 @@ public MainPanel buildMainPanel(
return mainPanel;
}

private void launch(MainPanel uiPanel, ILauncher launcher) {
public static @NotNull Consumer<MainPanel> getMainPanelLaunchAction(
HeadedServerSetupModel headedServerSetupModel) {
return uiPanel -> {
final var setupPanel = headedServerSetupModel.getPanel();
setupPanel.getLauncher().ifPresent(launcher -> launch(uiPanel, launcher));
setupPanel.postStartGame();
};
}

private static void launch(MainPanel uiPanel, ILauncher launcher) {
final WaitWindow gameLoadingWindow = new WaitWindow();
final var frame = JOptionPane.getFrameForComponent(uiPanel);
gameLoadingWindow.setLocationRelativeTo(frame);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.triplea.game.client.HeadedGameRunner;
Expand All @@ -26,35 +27,35 @@ public class MainFrame {

private static MainFrame instance;

private final JFrame mainFrame;
private final JFrame mainJFrame;
private final MainPanel mainPanel;
private final List<Runnable> quitActions = new ArrayList<>();

private MainFrame(
final HeadedServerSetupModel headedServerSetupModel,
final GameSelectorModel gameSelectorModel) {
mainFrame =
mainJFrame =
JFrameBuilder.builder()
.title("TripleA")
.iconImage(EngineImageLoader.loadFrameIcon())
.windowClosedAction(HeadedGameRunner::exitGameIfNoWindowsVisible)
.build();
BackgroundTaskRunner.setMainFrame(mainFrame);
BackgroundTaskRunner.setMainFrame(mainJFrame);

LookAndFeelSwingFrameListener.register(mainFrame);
LookAndFeelSwingFrameListener.register(mainJFrame);

final Runnable quitAction =
() -> {
quitActions.forEach(Runnable::run);
mainFrame.dispatchEvent(new WindowEvent(mainFrame, WindowEvent.WINDOW_CLOSING));
mainJFrame.dispatchEvent(new WindowEvent(mainJFrame, WindowEvent.WINDOW_CLOSING));
};

mainPanel =
new MainPanelBuilder(quitAction).buildMainPanel(headedServerSetupModel, gameSelectorModel);
mainFrame.add(mainPanel);
mainFrame.pack();
mainJFrame.add(mainPanel);
mainJFrame.pack();

headedServerSetupModel.setUi(mainFrame);
headedServerSetupModel.setUi(mainJFrame);
}

public static void buildMainFrame(
Expand All @@ -67,14 +68,20 @@ public static void buildMainFrame(
public static void show() {
SwingUtilities.invokeLater(
() -> {
instance.mainFrame.requestFocus();
instance.mainFrame.toFront();
instance.mainFrame.setVisible(true);
instance.mainJFrame.requestFocus();
instance.mainJFrame.toFront();
instance.mainJFrame.setVisible(true);
});
}

public static void startGameDirectly(final HeadedServerSetupModel headedServerSetupModel) {
Consumer<MainPanel> mainPanelLaunchAction =
MainPanelBuilder.getMainPanelLaunchAction(headedServerSetupModel);
SwingUtilities.invokeLater(() -> mainPanelLaunchAction.accept(instance.mainPanel));
}

public static void hide() {
SwingUtilities.invokeLater(() -> instance.mainFrame.setVisible(false));
SwingUtilities.invokeLater(() -> instance.mainJFrame.setVisible(false));
}

public static void addQuitAction(final Runnable onQuitAction) {
Expand Down
Loading

0 comments on commit 41bc340

Please sign in to comment.