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); } }
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); } } } }); }
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(); } }
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); }
private void DownloadGameSave(Game game) { Uri onlineSaveUrl = CreateSaveDownloadUri(game); string localSavePath = _localSaveManager.CreateLocalSaveFilePath(game); DownloadSave(game, localSavePath, onlineSaveUrl); }
public void AttemptToDownloadGameSave(Game game) { if (!IsDownloadingGameSave(game)) { AddNewSaveTransfer(game); DownloadGameSave(game); } }
private bool HasAvailableGameSave(Game game) { return game.LastTurn != null && game.LastTurn.SaveId.HasValue; }
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))); }
private void AddNewSaveTransfer(Game game) { var transfer = CreateNewSaveTransfer(game); Application.Current.Dispatcher.Invoke(() => CurrentSaveTransfers.Add(transfer)); }
public static string CreateSaveName(Game game) { return $"(MR) {game.Name}{SavedGameExtension}"; }
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; }
private void CloseCivIfNecessary(Game game) { if (ShouldAlwaysCloseGameAfterSave() || ShouldCloseGameAfterBecauseOfLastSave(game)) { CloseCiv(game.Type); } }
private Task GetDataForGameAsync(Game game) { return GetDataForGamesAsync(new List<Game> { game }); }
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; }
private bool IsGameSaveNewerThanDownloadedVersion(Game game) { LocalSaveFile localSave = _localSaveManager.GetLocalSaveFile(game.Id); return localSave == null || game.LastTurn.LastModifiedSave > localSave.DownloadedAt; }
private static SaveTransfer CreateNewSaveTransfer(Game game) { var transfer = new SaveTransfer { Game = game, IconVisual = Application.Current.FindResource("TransfersIcon") as Visual }; return transfer; }
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)); }