public static SaveGameData FromGameWorld(GameWorld gameWorld) { var saveGameData = new SaveGameData { BossesDefeated = new List<string>(gameWorld.BossesDefeated), CharacterName = gameWorld.Player.Name, Class = gameWorld.Player.Class.Name, CurrentDungeonLevel = gameWorld.CurrentDungeonLevel, Id = gameWorld.SaveGameId, Level = gameWorld.Player.Level, Gold = gameWorld.Player.Gold, Experience = gameWorld.Player.Experience, Race = gameWorld.Player.Race.Name, Stats = gameWorld.Player.BaseStats, TotalNumberOfMonstersDefeated = gameWorld.TotalNumberOfMonstersDefeated, Weapon = gameWorld.Player.Weapon, Inventory = gameWorld.Player.Inventory.ToList(), SpellBook = gameWorld.Player.SpellBook.ToList() }; return saveGameData; }
private void Persist(SaveGameData[] savedGames) { using (var writer = _fileAccess.CreateWriter()) { _serializer.Serialize(writer, savedGames); } }
public void SaveGame(SaveGameData saveGameData) { var index = _savedGames.Select((x, i) => new { SavedGameData = x, Index = i }).First(x => x.SavedGameData.Id == saveGameData.Id).Index; _savedGames[index] = saveGameData; Persist(_savedGames); }
private void Persist(SaveGameData[] savedGames) { var i = 0; while (true) { try { using (var writer = _fileAccess.CreateWriter()) { _serializer.Serialize(writer, savedGames); return; } } catch { Thread.Sleep(1); if (++i > 10) { throw; } } } }