Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// addWorldToHistory.js
/*global adventurejs A*/

/**
 * Save the current world state for UNDO.
 * @method adventurejs#addWorldToHistory
 * @memberOf adventurejs
 * @param {Object} world
 */
adventurejs.addWorldToHistory = function Adventurejs_addWorldToHistory(world) {
  this.game.log(
    "L1044",
    "log",
    "high",
    "adventurejs.worldSave > Attempting to save the world.",
    "CopyOperations"
  );

  this.game.world_history.unshift(world);

  if (this.settings.max_undos < this.game.world_history.length) {
    // setting to null not really necessary
    // but doubling down for garbage collection
    // because these saves represent a fair amount of resources
    this.game.world_history[this.game.world_history.length - 1] = null;
    this.game.world_history.pop();
  }

  this.game.log(
    "L1045",
    "log",
    "high",
    "adventurejs.worldSave > Saved the world.",
    "CopyOperations"
  );
  return;
};

// TODO: can I use proxy to intercept and record all changes?
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
// not supported in IE11 - https://caniuse.com/#search=proxy