<commit_before>formatDate = -> moment().format("HH:mm:ss")

isModalShowing = -> $(".modal, .large-modal").length > 0


angular.module('gamEvolve.game.undo', ['gamEvolve.model.undo'])
.controller "UndoCtrl", ($scope, $window, undo, currentGame, cache, gameConverter, WillChangeLocalVersionEvent, GameVersionPublishedEvent) -> 
  currentLocalVersion = null
  initialVersionHasBeenCached = false

  # Bring canUndo() and canRedo() into scope
  $scope.canUndo = -> undo.canUndo()
  $scope.canRedo = -> undo.canRedo()
  $scope.getStatusMessage = -> currentGame.statusMessage

  $scope.undo = -> 
    if not undo.canUndo() then return 

    [currentGame.localVersion, currentGame.version] = undo.undo()
    currentLocalVersion = currentGame.localVersion
    WillChangeLocalVersionEvent.send()
    currentGame.setHasUnpublishedChanges()

  $scope.redo = -> 
    if not undo.canRedo() then return 

    [currentGame.localVersion, currentGame.version] = undo.redo()
    currentLocalVersion = currentGame.localVersion
    WillChangeLocalVersionEvent.send()
    currentGame.setHasUnpublishedChanges()

  onGameVersionPublished = ->
    currentGame.setStatusMessage("Published at #{moment().format("HH:mm:ss")}")
    currentGame.clearHasUnpublishedChanges()
  
  unsubcribeOnGameVersionPublished = GameVersionPublishedEvent.listen(onGameVersionPublished)
  $scope.$on("$destroy", unsubcribeOnGameVersionPublished)

  onUpdateCurrentGame = ->
    if not currentGame.version then return 

    # If this the first time the code is loaded (ie. the controller just started)
    if not currentLocalVersion
      initialVersionHasBeenCached = false

      # Check if code exists in offline cache
      try 
        cachedCode = cache.load(currentGame.info.id)
        # Remove hash keys to get good comparaison
        gameConverter.removeHashKeys(cachedCode)
        gameConverter.removeHashKeys(currentGame.version)

        if cachedCode and not _.isEqual(currentGame.version, cachedCode)
          if window.confirm("You have some changes saved offline. Restore your offline version?")
            # Put the old version as the first in the undo stack<commit_msg>Corrected comparaison  between local and server code<commit_after>formatDate = -> moment().format("HH:mm:ss")

isModalShowing = -> $(".modal, .large-modal").length > 0


angular.module('gamEvolve.game.undo', ['gamEvolve.model.undo'])
.controller "UndoCtrl", ($scope, $window, undo, currentGame, cache, gameConverter, WillChangeLocalVersionEvent, GameVersionPublishedEvent) -> 
  currentLocalVersion = null
  initialVersionHasBeenCached = false

  # Bring canUndo() and canRedo() into scope
  $scope.canUndo = -> undo.canUndo()
  $scope.canRedo = -> undo.canRedo()
  $scope.getStatusMessage = -> currentGame.statusMessage

  $scope.undo = -> 
    if not undo.canUndo() then return 

    [currentGame.localVersion, currentGame.version] = undo.undo()
    currentLocalVersion = currentGame.localVersion
    WillChangeLocalVersionEvent.send()
    currentGame.setHasUnpublishedChanges()

  $scope.redo = -> 
    if not undo.canRedo() then return 

    [currentGame.localVersion, currentGame.version] = undo.redo()
    currentLocalVersion = currentGame.localVersion
    WillChangeLocalVersionEvent.send()
    currentGame.setHasUnpublishedChanges()

  onGameVersionPublished = ->
    currentGame.setStatusMessage("Published at #{moment().format("HH:mm:ss")}")
    currentGame.clearHasUnpublishedChanges()
  
  unsubcribeOnGameVersionPublished = GameVersionPublishedEvent.listen(onGameVersionPublished)
  $scope.$on("$destroy", unsubcribeOnGameVersionPublished)

  localCodeIsNewer = (localCode, serverCode) ->
    # For some odd reason _.isEqual() is flagging some equal code as inequal
    localCode.versionNumber == serverCode.versionNumber and RW.makePatches(localCode, serverCode).length > 0

  onUpdateCurrentGame = ->
    if not currentGame.version then return 

    # If this the first time the code is loaded (ie. the controller just started)
    if not currentLocalVersion
      initialVersionHasBeenCached = false

      # Check if code exists in offline cache
      try 
        cachedCode = cache.load(currentGame.info.id)
        # Remove hash keys to get good comparaison
        gameConverter.removeHashKeys(cachedCode)
        gameConverter.removeHashKeys(currentGame.version)

        if cachedCode and localCodeIsNewer(cachedCode, currentGame.version)
          if window.confirm("You have some changes saved offline. Restore your offline version?")
            # Put the old version as the first in the undo stack
{'fraction_non_alphanumeric': 0.09691533581273244, 'fraction_numerical': 0.0006563115292058631, 'mean_word_length': 4.1313131313131315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1780973', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1251, 'n_words': 307}