internal void Load(string fileName = "") { if (string.IsNullOrEmpty(fileName)) { fileName = _appData.Configuration.LastSavedFileName; } _logger.LogMessage($"Loading game from file {fileName}."); if (File.Exists(fileName)) { try { _eventBus.PublishClearAllCardsRequest(); var gameFile = JsonConvert.DeserializeObject <GameFile>(File.ReadAllText(fileName)); _zoneButtons = gameFile.ZoneButtons; var game = _appData.Game; game.ClearAllCardsLists(); gameFile.CopyTo(game); game.FileName = fileName; game.OnEncounterSetsChanged(); for (var index = 0; index < gameFile.DeckIds.Count && index < game.Players.Count; index++) { game.Players[index].DeckId = gameFile.DeckIds[index]; if (!string.IsNullOrEmpty(game.Players[index].DeckId)) { try { _loadingStatusService.ReportPlayerStatus(game.Players[index].ID, Status.LoadingCards); _cardLoadService.LoadPlayer(game.Players[index]); } catch (Exception ex) { _logger.LogException(ex, $"Error loading player {game.Players[index].ID}."); _loadingStatusService.ReportPlayerStatus(game.Players[index].ID, Status.Error); } } } game.OnPlayersChanged(); _appData.OnGameChanged(); _logger.LogMessage($"Finished reading game file: {fileName}."); } catch (Exception ex) { // if there's an error, we don't care- just use the existing game _logger.LogException(ex, $"Error reading game file: {fileName}."); } } }
private void LoadAllPlayerCards() { var worker = new BackgroundWorker(); worker.DoWork += (x, y) => { foreach (var player in _appData.Game.Players) { if (!string.IsNullOrEmpty(player.DeckId)) { _loadingStatusService.ReportPlayerStatus(player.ID, Status.LoadingCards); try { LoadPlayerCards(player); _loadingStatusService.ReportPlayerStatus(player.ID, Status.Finished); } catch (Exception ex) { _logger.LogException(ex, $"Error loading player cards for player {player.ID}"); _loadingStatusService.ReportPlayerStatus(player.ID, Status.Error); } } } }; worker.RunWorkerAsync(); }