Class GameSaving

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

public class GameSaving extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final List<String>
    List of filenames that has been created by AutoSave.
    private String
    Store timestamp of first created autosave file here; iscFile will be generated with same timestamp
    private 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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 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 element
    private 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-PHASE
    private String
     
    private String
    Helper method, returns "null" if given string is null; used by dumpLegion.
    private void
    saveGame(String filename, boolean autoSave)
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LOGGER

      private static final 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.
    • firstAutosavefileTimestamp

      private String firstAutosavefileTimestamp
      Store timestamp of first created autosave file here; iscFile will be generated with same timestamp
    • iscmName

      private String iscmName
    • autoGeneratedFiles

      private final List<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 Details

  • 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

      private String notnull(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 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 IOException
      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

      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. 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(String filename, boolean autoSave) throws 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:
      IOException - if the saves directory (for autosave or File-Save) does not exist and creation fails
    • saveGameWithErrorHandling

      void saveGameWithErrorHandling(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
    • makeIscName

      private String makeIscName()
    • createIscmFile

      public PrintWriter 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