net.sf.colossus.server
Class GameSaving

java.lang.Object
  extended by net.sf.colossus.server.GameSaving

public class GameSaving
extends java.lang.Object


Field Summary
private  java.util.List<java.lang.String> autoGeneratedFiles
          List of filenames that has been created by AutoSave.
private  GameServerSide game
           
private static java.util.logging.Logger LOGGER
           
private  Options options
           
private  org.jdom.Element phaseStartSnapshot
          snapshot of game data (caretaker, players, legions, ...) at the last "commit point", initially those are taken only at start of a phase.
 
Constructor Summary
GameSaving(GameServerSide game, Options options)
           
 
Method Summary
private  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 document
private  void addSnapshotData(org.jdom.Element saveGameRoot, org.jdom.Element commitDataRoot)
           
private  java.lang.String automaticFilenameHandling(java.lang.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 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.
private  org.jdom.Element createSavegameContent()
          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 element
private  void ensureSavesDirectory()
          Ensure that saves/ directory in Colossus-home exists, or create it.
private  java.lang.String 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
private  java.lang.String notnull(java.lang.String in)
          Helper method, returns "null" if given string is null; used by dumpLegion.
private  void saveGame(java.lang.String filename, boolean autoSave)
          High-level method to save a file.
(package private)  void saveGameWithErrorHandling(java.lang.String filename, boolean autoSave)
          Call saveGame in a try-catch block.
private  void takeSnapshotAtBeginOfPhase()
          Take a new snapshot of the data (basic game data, players with legions, and history) at the begin of a phase.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

private static final java.util.logging.Logger LOGGER

game

private final GameServerSide game

options

private final Options options

phaseStartSnapshot

private org.jdom.Element phaseStartSnapshot
snapshot 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.


autoGeneratedFiles

private final java.util.List<java.lang.String> 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 Detail

GameSaving

public GameSaving(GameServerSide game,
                  Options options)
Method Detail

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

private java.lang.String notnull(java.lang.String in)
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

private org.jdom.Element dumpLegion(LegionServerSide legion,
                                    boolean inBattle)
Dump the given legion to an XML element

Parameters:
legion - For which legion to dump the data
inBattle - 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

private java.lang.String 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

private void ensureSavesDirectory()
                           throws java.io.IOException
Ensure that saves/ directory in Colossus-home exists, or create it. Throws IOException if creation fails.

Throws:
java.io.IOException - if the saves directory does not exist and creation fails

automaticFilenameHandling

private java.lang.String automaticFilenameHandling(java.lang.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. 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-Save
autoSave - Whether or not this was triggered by autosave
keep - How many autosave files to keep, 0 for "keep all"
Returns:
The automatically generated file name

saveGame

private void saveGame(java.lang.String filename,
                      boolean autoSave)
               throws java.io.IOException
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-Save
autoSave - Whether or not this is autoSave
Throws:
java.io.IOException - if the saves directory (for autosave or File-Save) does not exist and creation fails

saveGameWithErrorHandling

void saveGameWithErrorHandling(java.lang.String filename,
                               boolean autoSave)
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 create
autoSave - True if this was triggered by autoSave