public void DeleteLocalSaveIfPresent(Game game)
        {
            string saveFilePath = null;

            lock (LocalSaveFiles)
            {
                if (LocalSaveFiles.ContainsKey(game.Id))
                {
                    saveFilePath = LocalSaveFiles[game.Id].PathOnDisk;
                    LocalSaveFiles.Remove(game.Id);
                }
            }

            UpdateCurrentTurnCount();

            if (saveFilePath == null)
            {
                saveFilePath = CreateLocalSaveFilePath(game);
            }

            try
            {
                if (File.Exists(saveFilePath))
                {
                    File.Delete(saveFilePath);
                }
            }
            catch (Exception exc)
            {
                Log.Error("Deleting local save file.", exc);
            }
        }
Esempio n. 2
0
        public void SubmitGameSaveToServer(Game game, LocalGameSave localSave)
        {
            AddNewSaveTransfer(game);

            var progressStream = LoadSaveIntoProgressStream(localSave);
            progressStream.BytesRead += (sender, args) => OnGameSaveUploadBytesRead(game.Id, args);

            App.GameHub.UploadSave(progressStream)
                .ContinueWith(t =>
                {
                    var saveTransfer = CurrentSaveTransfers.FirstOrDefault(st => st.GameId == game.Id);
                    if (saveTransfer != null)
                    {
                        if (t.IsFaulted)
                        {
                            Log.Error($"Uploading save for game #{game.Id}", t.Exception);
                            saveTransfer.IsFailed = true;
                            RetrySaveTransferAfterWait(saveTransfer, () => SubmitGameSaveToServer(game, localSave));
                        }
                        else
                        {
                            UpdateSaveTransfer(saveTransfer, 100);

                            _localSaveManager.ArchiveFile(localSave);

                            SaveUploadResult uploadResult = t.Result;
                            if (uploadResult != null)
                            {
                                App.ToastMaker.ShowToast("Error Submitting Turn", uploadResult.Message);
                            }
                        }
                    }
                });
        }
Esempio n. 3
0
 public void DownloadSaveIfAvailable(Game game)
 {
     if (HasAvailableGameSave(game)
         && IsGameSaveNewerThanDownloadedVersion(game))
     {
         DownloadGameSave(game);
     }
 }
 public void AddLocalSave(Game game, string localFilePath)
 {
     lock (LocalSaveFiles)
     {
         LocalSaveFiles[game.Id] = new LocalSaveFile
         {
             GameId = game.Id,
             PathOnDisk = localFilePath,
             DownloadedAt = DateTime.UtcNow
         };
         UpdateCurrentTurnCount();
     }
 }
Esempio n. 5
0
 private void DownloadSave(Game game, string localSavePath, Uri onlineSaveUrl)
 {
     var webClient = new WebClient();
     webClient.DownloadProgressChanged +=
         (sender, args) => OnFileTransferProgressChanged(game.Id, args.ProgressPercentage);
     webClient.DownloadFileCompleted +=
         (sender, args) => OnFileTransferCompleted(game, localSavePath, args.Error);
     webClient.DownloadFileAsync(onlineSaveUrl, localSavePath);
 }
Esempio n. 6
0
        private void DownloadGameSave(Game game)
        {
            Uri onlineSaveUrl = CreateSaveDownloadUri(game);
            string localSavePath = _localSaveManager.CreateLocalSaveFilePath(game);

            DownloadSave(game, localSavePath, onlineSaveUrl);
        }
Esempio n. 7
0
        public void AttemptToDownloadGameSave(Game game)
        {
            if (!IsDownloadingGameSave(game))
            {
                AddNewSaveTransfer(game);

                DownloadGameSave(game);
            }
        }
Esempio n. 8
0
 private bool HasAvailableGameSave(Game game)
 {
     return game.LastTurn != null && game.LastTurn.SaveId.HasValue;
 }
Esempio n. 9
0
 private Uri CreateSaveDownloadUri(Game game)
 {
     return new Uri(string.Format("https://mrobot.blob.core.windows.net/saves/{0}/{1}",
         game.LastTurn.SaveId, GameManager.CreateSaveName(game)));
 }
Esempio n. 10
0
 private void AddNewSaveTransfer(Game game)
 {
     var transfer = CreateNewSaveTransfer(game);
     Application.Current.Dispatcher.Invoke(() => CurrentSaveTransfers.Add(transfer));
 }
Esempio n. 11
0
 public static string CreateSaveName(Game game)
 {
     return $"(MR) {game.Name}{SavedGameExtension}";
 }
Esempio n. 12
0
        private bool ShouldCloseGameAfterBecauseOfLastSave(Game game)
        {
            bool result = false;

            if (App.SyncedSettings.AutoCloseCivCondition == AutoCloseCivSettings.NewSaveDetectedNoOtherSaves)
            {
                lock (_games)
                {
                    result = _games.Any(g => g.Key != game.Id && g.Value.Type == game.Type);
                }
            }

            return result;
        }
Esempio n. 13
0
 private void CloseCivIfNecessary(Game game)
 {
     if (ShouldAlwaysCloseGameAfterSave() || ShouldCloseGameAfterBecauseOfLastSave(game))
     {
         CloseCiv(game.Type);
     }
 }
Esempio n. 14
0
 private Task GetDataForGameAsync(Game game)
 {
     return GetDataForGamesAsync(new List<Game> { game });
 }
Esempio n. 15
0
        private bool IsDownloadingGameSave(Game game)
        {
            var existingTransfer = GetExistingTransfer(game.Id);
            if (existingTransfer != null)
            {
                if (existingTransfer.IsFailed)
                {
                    existingTransfer.IsFailed = false;
                    existingTransfer.ProgressPercentage = 0;
                }
                else
                {
                    Log.DebugFormat("Already downloading Game #{0}", game.Id);
                    return true;
                }
            }

            return false;
        }
Esempio n. 16
0
        private bool IsGameSaveNewerThanDownloadedVersion(Game game)
        {
            LocalSaveFile localSave = _localSaveManager.GetLocalSaveFile(game.Id);

            return localSave == null || game.LastTurn.LastModifiedSave > localSave.DownloadedAt;
        }
Esempio n. 17
0
 private static SaveTransfer CreateNewSaveTransfer(Game game)
 {
     var transfer = new SaveTransfer
     {
         Game = game,
         IconVisual = Application.Current.FindResource("TransfersIcon") as Visual
     };
     return transfer;
 }
Esempio n. 18
0
        private void OnFileTransferCompleted(Game game, string localFilePath, Exception exception)
        {
            if (exception == null)
            {
                UpdateSaveTransfer(game.Id, 100);

                _localSaveManager.AddLocalSave(game, localFilePath);
            }
            else
            {
                Log.Error("Failed to transfer save.", exception);

                var existingTransfer = GetExistingTransfer(game.Id);
                if (existingTransfer != null)
                {
                    existingTransfer.IsFailed = true;
                    RetrySaveTransferAfterWait(existingTransfer, () => DownloadSaveIfAvailable(game));
                }
            }
        }
 public string CreateLocalSaveFilePath(Game game)
 {
     return Path.Combine(_savesDirectoryPath, GameManager.CreateSaveName(game));
 }