Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: change test files to XML #387

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Binary file added 1835_20210331_1936_Ulli_Hegemann.rails
Binary file not shown.
11,906 changes: 11,906 additions & 0 deletions 1835_20210331_2116_Ulli_Hegemann.xml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ dependencies {
implementation 'org.apache.commons:commons-text:1.9'
// https://www.jetbrains.com/help/idea/annotating-source-code.html
implementation 'org.jetbrains:annotations:20.1.0'
// https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream
implementation group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.16'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.easytesting:fest-assert-core:2.0M10'
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/rails/common/GameData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.sf.rails.common;

import java.util.ArrayList;
import java.util.List;

public class GameData {
Expand All @@ -14,7 +15,7 @@ private GameData(GameInfo game, GameOptionsSet gameOptions, List<String> players

this.game = game;
this.gameOptions = gameOptions;
this.players = players;
this.players = new ArrayList<>(players);
}

public static GameData create(GameInfo game, GameOptionsSet.Builder gameOptions, List<String> players) {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/net/sf/rails/game/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import net.sf.rails.game.state.*;
import net.sf.rails.game.state.Currency;
import net.sf.rails.ui.swing.GameUIManager;
import net.sf.rails.util.GameLoader;
import net.sf.rails.util.GameSaver;
import net.sf.rails.util.Util;
import net.sf.rails.util.*;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -907,9 +905,16 @@ private boolean processGameActions(GameAction gameAction) {
ChangeStack changeStack = getRoot().getStateManager().getChangeStack();
int index = gameAction.getmoveStackIndex();
switch (gameAction.getMode()) {
case SAVE:
result = save(gameAction);
case XML_SAVE: {
IGameSaver gameSaver = new XmlGameSaver(getRoot().getGameData(), executedActions.view());
result = save(gameAction, gameSaver);
break;
}
case SAVE: {
IGameSaver gameSaver = new GameSaver(getRoot().getGameData(), executedActions.view());
result = save(gameAction, gameSaver);
break;
}
case RELOAD:
result = reload(gameAction);
break;
Expand Down Expand Up @@ -1035,8 +1040,7 @@ protected void recoverySave() {
}
}

protected boolean save(GameAction saveAction) {
GameSaver gameSaver = new GameSaver(getRoot().getGameData(), executedActions.view());
protected boolean save(GameAction saveAction, IGameSaver gameSaver) {
File file = new File(saveAction.getFilepath());
try {
gameSaver.saveGame(file);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/rails/game/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void initPlayers(List<String> playerNames, Bank bank) {
playerIndex,
player.getId()));
}
this.playerNames = Collections.unmodifiableMap(playerNamesBuilder);
this.playerNames = new HashMap<>(playerNamesBuilder);

ReportBuffer.add(this, LocalText.getText("PlayerCash", cashText));
ReportBuffer.add(this, LocalText.getText("BankHas", Bank.format(this, bank.getCash())));
Expand Down
104 changes: 79 additions & 25 deletions src/main/java/net/sf/rails/ui/swing/StatusWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.swing.filechooser.FileFilter;

import net.sf.rails.util.Util;
import net.sf.rails.util.XmlGameLoader;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -63,8 +64,10 @@ public class StatusWindow extends JFrame implements ActionListener, KeyListener,
protected static final String NEW_CMD = "New";

protected static final String LOAD_CMD = "Load";
protected static final String XML_LOAD_CMD = "XML-Load";

protected static final String SAVE_CMD = "Save";
protected static final String XML_SAVE_CMD = "XML-Save";

protected static final String RELOAD_CMD = "Reload";

Expand Down Expand Up @@ -152,6 +155,14 @@ public void initMenu() {
actionMenuItem.setPossibleAction(new GameAction(gameUIManager.getRoot(), GameAction.Mode.LOAD));
fileMenu.add(actionMenuItem);

actionMenuItem = new ActionMenuItem(XML_LOAD_CMD);
actionMenuItem.setActionCommand(XML_LOAD_CMD);
actionMenuItem.addActionListener(this);
actionMenuItem.setEnabled(true);
actionMenuItem.setPossibleAction(new GameAction(gameUIManager.getRoot(), GameAction.Mode.LOAD));
fileMenu.add(actionMenuItem);


actionMenuItem = new ActionMenuItem(LocalText.getText("SAVE"));
actionMenuItem.setActionCommand(SAVE_CMD);
actionMenuItem.setMnemonic(KeyEvent.VK_S);
Expand All @@ -161,6 +172,13 @@ public void initMenu() {
actionMenuItem.setPossibleAction(new GameAction(gameUIManager.getRoot(), GameAction.Mode.SAVE));
fileMenu.add(actionMenuItem);

actionMenuItem = new ActionMenuItem(XML_SAVE_CMD);
actionMenuItem.setActionCommand(XML_SAVE_CMD);
actionMenuItem.addActionListener(this);
actionMenuItem.setEnabled(true);
actionMenuItem.setPossibleAction(new GameAction(gameUIManager.getRoot(), GameAction.Mode.XML_SAVE));
fileMenu.add(actionMenuItem);

actionMenuItem = new ActionMenuItem(LocalText.getText("Reload"));
actionMenuItem.setActionCommand(RELOAD_CMD);
actionMenuItem.setMnemonic(KeyEvent.VK_R);
Expand Down Expand Up @@ -611,12 +629,14 @@ public void updateStatus(boolean myTurn) {
toFront();
}

public void disableButtons () {
public void disableButtons() {
passButton.setEnabled(false);
autopassButton.setEnabled(false);
}

/** Stub, may be overridden in game-specific subclasses */
/**
* Stub, may be overridden in game-specific subclasses
*/
protected boolean updateGameSpecificSettings() {
return false;
}
Expand Down Expand Up @@ -673,9 +693,9 @@ public void actionPerformed(ActionEvent actor) {

} else if (command.equals(QUIT_CMD)) {
gameUIManager.terminate();
} else if ( command.equals(NEW_CMD) ) {
} else if (command.equals(NEW_CMD)) {
// TODO
} else if ( command.equals(LOAD_CMD) ) {
} else if (command.equals(LOAD_CMD)) {
// TODO: does this really belong here?
String saveDirectory = Config.get("save.directory");
JFileChooser jfc = new JFileChooser();
Expand Down Expand Up @@ -705,6 +725,36 @@ public String getDescription() {
GameLoader.loadAndStartGame(selectedFile);
}).start();
}
} else if (command.equals(XML_LOAD_CMD)) {
// TODO: does this really belong here?
String saveDirectory = Config.get("save.directory");
JFileChooser jfc = new JFileChooser();
jfc.setCurrentDirectory(new File(saveDirectory));
jfc.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
// TODO: need to filter like GameSetupController.isOurs() does
return true;
}

@Override
public String getDescription() {
return null;
}
});

if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
// close the existing game

final File selectedFile = jfc.getSelectedFile();
//start in new thread so that swing thread is not used for game setup
new Thread(() -> {
// close the existing game (which ironically will include us
gameUIManager.closeGame();
// start the new game
XmlGameLoader.loadAndStartGame(selectedFile);
}).start();
}
} else if (command.equals(REPORT_CMD)) {
gameUIManager.reportWindow.setVisible(((JMenuItem) actor.getSource()).isSelected());
gameUIManager.reportWindow.scrollDown();
Expand All @@ -718,28 +768,29 @@ public String getDescription() {
gameUIManager.autoSaveLoadGame();
} else if (command.equals(SAVESTATUS_CMD)) {
gameUIManager.saveGameStatus();
} else if ( command.equals("Save Logs")) {
} else if (command.equals("Save Logs")) {
gameUIManager.saveLogs();
} else if (executedAction == null) {
;
} else if (executedAction instanceof GameAction) {
switch (((GameAction) executedAction).getMode()) {
case SAVE:
gameUIManager.saveGame((GameAction) executedAction);
break;
case RELOAD:
gameUIManager.reloadGame((GameAction) executedAction);
break;
case EXPORT:
gameUIManager.exportGame((GameAction) executedAction);
break;
default:
process(executedAction);
break;
case XML_SAVE:
case SAVE:
gameUIManager.saveGame((GameAction) executedAction);
break;
case RELOAD:
gameUIManager.reloadGame((GameAction) executedAction);
break;
case EXPORT:
gameUIManager.exportGame((GameAction) executedAction);
break;
default:
process(executedAction);
break;
}
} else {
// Unknown action, let UIManager catch it
process (executedAction);
process(executedAction);
}
}

Expand All @@ -760,7 +811,7 @@ public boolean processImmediateAction() {
// so that it's not going to loop.
DiscardTrain nextAction = (DiscardTrain) immediateAction;
immediateAction = null;
gameUIManager.discardTrains (nextAction);
gameUIManager.discardTrains(nextAction);
}
return true;
}
Expand Down Expand Up @@ -835,7 +886,7 @@ public void endOfGame() {
gameUIManager.orWindow.finish();
}

public Player getCurrentPlayer () {
public Player getCurrentPlayer() {
return gameUIManager.getCurrentPlayer();
}

Expand All @@ -860,7 +911,7 @@ public void endOfGameReport() {
List<String> gameReport = gm.getGameReport();
Collections.reverse(gameReport);
StringBuilder report = new StringBuilder();
for (String s:gameReport) {
for (String s : gameReport) {
report.insert(0, s + "\n");
JOptionPane.showMessageDialog(this,
report,
Expand All @@ -872,16 +923,19 @@ public void endOfGameReport() {
}

@Override
public void keyReleased(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {}
public void keyTyped(KeyEvent e) {
}

public void updatePlayerOrder(List<String> newPlayerNames) {
gameStatus.updatePlayerOrder(newPlayerNames);
gameStatus.updatePlayerOrder(newPlayerNames);
}


Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/sf/rails/util/GameIOData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.sf.rails.util;

import java.util.ArrayList;
import java.util.List;

import rails.game.action.PossibleAction;
Expand All @@ -23,7 +24,7 @@ class GameIOData {
this.version = version;
this.date = date;
this.fileVersionID = fileVersionID;
this.actions = actions;
this.actions = new ArrayList<>(actions);
}

GameIOData() {}
Expand Down Expand Up @@ -62,7 +63,7 @@ long getFileVersionID() {
}

void setActions(List<PossibleAction> actions) {
this.actions = actions;
this.actions = new ArrayList<>(actions);
}

List<PossibleAction> getActions() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/rails/util/GameSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* GameLoader is responsible to load a saved Rails game
*/
public class GameSaver {
public class GameSaver implements IGameSaver {

private static final Logger log = LoggerFactory.getLogger(GameSaver.class);

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/sf/rails/util/IGameSaver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.sf.rails.util;

import java.io.File;
import java.io.IOException;

public interface IGameSaver {
void saveGame(File file) throws IOException;
}
35 changes: 35 additions & 0 deletions src/main/java/net/sf/rails/util/XmlConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.sf.rails.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.RegexFileFilter;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

public class XmlConverter {
public static void main(String[] args) {
Collection<File> railsFiles = FileUtils.listFiles(
new File("src/test/resources"),
new RegexFileFilter("(.+).rails"),
DirectoryFileFilter.DIRECTORY
);

for (File file : railsFiles) {
System.out.println(file.toString());

GameLoader loader = new GameLoader();

loader.createFromFile(file);

XmlGameSaver saver = new XmlGameSaver(loader);

try {
saver.saveGame(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Loading