Package net.sf.colossus.server
Class GameSaving
java.lang.Object
net.sf.colossus.server.GameSaving
-
Field Summary
FieldsModifier and TypeFieldDescriptionList of filenames that has been created by AutoSave.private String
Store timestamp of first created autosave file here; iscFile will be generated with same timestampprivate final GameServerSide
private String
private static final Logger
private final Options
private org.jdom.Element
snapshot of game data (caretaker, players, legions, ...) at the last "commit point", initially those are taken only at start of a phase. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addBasicData
(org.jdom.Element root) Adds the basic data: variant info, turn number, current player, current phase, and caretaker.private void
addBattleData
(org.jdom.Element root) private void
addPlayerData
(org.jdom.Element root) Adds the data for all players and their legions to an XML documentprivate void
addSnapshotData
(org.jdom.Element saveGameRoot, org.jdom.Element commitDataRoot) private String
automaticFilenameHandling
(String filename, boolean autoSave, int keep) Produce one "automatically generated file name" for saving games, including directory handling: 1) Creates the save game directory if it does not exist yet, including error handling.void
When a commit point is reached (typically, one phase is "Done" and a new phase begins), 1) take new snapshot of overall game state, player, legion, caretaker data 2) flush the so far redoLog data to the history, 3) clear the redoLog data.Prepare/create the file for the internal spectator client, so that later commit messages in the spectators ClientHandler upon "commit" can store messages to that file (and remove from re-send queue).private org.jdom.Element
Create the whole content that will be written to the save game file.private org.jdom.Element
dumpLegion
(LegionServerSide legion, boolean inBattle) Dump the given legion to an XML elementprivate void
Ensure that saves/ directory in Colossus-home exists, or create it.private String
Generate the filename for autosaving (or just "Save" where one does specify file name either) according to the pattern: DIRECTORY/snap TIMESTAMP TURN-PLAYER-PHASEprivate String
private String
Helper method, returns "null" if given string is null; used by dumpLegion.private void
High-level method to save a file.(package private) void
saveGameWithErrorHandling
(String filename, boolean autoSave) Call saveGame in a try-catch block.private void
Take a new snapshot of the data (basic game data, players with legions, and history) at the begin of a phase.
-
Field Details
-
LOGGER
-
game
-
options
-
phaseStartSnapshot
private org.jdom.Element phaseStartSnapshotsnapshot of game data (caretaker, players, legions, ...) at the last "commit point", initially those are taken only at start of a phase. (Later this might be also after each completed engagement/battle). Savegame contains then this snapshot plus the redo-Data which was additionally done after that. -
firstAutosavefileTimestamp
Store timestamp of first created autosave file here; iscFile will be generated with same timestamp -
iscmName
-
autoGeneratedFiles
List of filenames that has been created by AutoSave. If option "keep max N autosave files" is set, when N+1th file was created, first from this list will be deleted and so on.
-
-
Constructor Details
-
GameSaving
-
-
Method Details
-
takeSnapshotAtBeginOfPhase
private void takeSnapshotAtBeginOfPhase()Take a new snapshot of the data (basic game data, players with legions, and history) at the begin of a phase. At every point of time there is always one such latest snapshot in this.phaseStartSnapshot. -
commitPointReached
public void commitPointReached()When a commit point is reached (typically, one phase is "Done" and a new phase begins), 1) take new snapshot of overall game state, player, legion, caretaker data 2) flush the so far redoLog data to the history, 3) clear the redoLog data. -
addSnapshotData
private void addSnapshotData(org.jdom.Element saveGameRoot, org.jdom.Element commitDataRoot) -
createSavegameContent
private org.jdom.Element createSavegameContent()Create the whole content that will be written to the save game file. Takes the last phaseStartSnapshot plus redo-Data plus battle data plus data files.- Returns:
- The "ColossusSnapshot" root element containing all information
-
addBasicData
private void addBasicData(org.jdom.Element root) Adds the basic data: variant info, turn number, current player, current phase, and caretaker.- Parameters:
root
- The document root to which to add all the data
-
notnull
Helper method, returns "null" if given string is null; used by dumpLegion.- Parameters:
in
- the string to "null"ify if needed- Returns:
- "null" or the string itself
-
dumpLegion
Dump the given legion to an XML element- Parameters:
legion
- For which legion to dump the datainBattle
- Whether this legion is currently involved into an ongoing battle (i.e. battle data needs to be dumped too)- Returns:
- An XML Element with all Legion data
-
addPlayerData
private void addPlayerData(org.jdom.Element root) Adds the data for all players and their legions to an XML document- Parameters:
root
- The document root to which to add the data
-
addBattleData
private void addBattleData(org.jdom.Element root) -
makeAutosaveFileName
Generate the filename for autosaving (or just "Save" where one does specify file name either) according to the pattern: DIRECTORY/snap TIMESTAMP TURN-PLAYER-PHASE- Returns:
- The file name/path, including directory
-
ensureSavesDirectory
Ensure that saves/ directory in Colossus-home exists, or create it. Throws IOException if creation fails.- Throws:
IOException
- if the saves directory does not exist and creation fails
-
automaticFilenameHandling
Produce one "automatically generated file name" for saving games, including directory handling: 1) Creates the save game directory if it does not exist yet, including error handling. 2) Generates an "automatic" file name (both for autoSave and File-Save) 3) if it is autosave and the option to keep only a limited number of autosave files, add it to the list of autosave file names- Parameters:
filename
- User specified filename, null for autosave or File-SaveautoSave
- Whether or not this was triggered by autosavekeep
- How many autosave files to keep, 0 for "keep all"- Returns:
- The automatically generated file name
-
saveGame
High-level method to save a file. Used for all three cases: Auto-save, User specified file and File-Save (without specified file name).- Parameters:
filename
- user specified filename, null for auto-save or File-SaveautoSave
- Whether or not this is autoSave- Throws:
IOException
- if the saves directory (for autosave or File-Save) does not exist and creation fails
-
saveGameWithErrorHandling
Call saveGame in a try-catch block. If any exception is caught, log it, show an error dialog, and additionally if this was triggered by autosave, disable the autosave from now on.- Parameters:
filename
- The name of the file to createautoSave
- True if this was triggered by autoSave
-
makeIscName
-
createIscmFile
Prepare/create the file for the internal spectator client, so that later commit messages in the spectators ClientHandler upon "commit" can store messages to that file (and remove from re-send queue).- Returns:
- A PrintWriter for that file, or null if creation failed
-