public async Task AddPlayersFromFile(string fileText, ObservableCollection <UserPlanet> playersPlanets, DateTime fileCreationDate) { var planetLocalization = "0:0"; var isGalaxyAndSystemReaded = false; var positionLine = ""; var errorCount = 0; var stringReader = new StringReader(fileText); while (true) { _actualLine = stringReader.ReadLine(); if (_actualLine != null && _actualLine.Contains("System ") && !isGalaxyAndSystemReaded) { planetLocalization = await readPlanetLocalization(_actualLine); isGalaxyAndSystemReaded = true; var tempUserPlanet = new UserPlanet("temp", planetLocalization, fileCreationDate); await DeleteOldSolarSystems(tempUserPlanet, playersPlanets); } if (isGalaxyAndSystemReaded) { positionLine = GetLineWithPosition(stringReader, positionLine); } if (_startToReadUserData) { var userName = readUserName(_actualLine); var position = GetPositionFromLine(positionLine); var userPlanet = new UserPlanet(userName, planetLocalization, position, fileCreationDate) { PlanetId = 1 }; lock (playersPlanets) { playersPlanets.Add(userPlanet); } _startToReadUserData = false; } if (_actualLine == null) { errorCount++; if (errorCount > 10) { break; } } } }
private async Task DeleteOldSolarSystems(UserPlanet tempUserPlanet, ObservableCollection <UserPlanet> playersPlanets) { lock (playersPlanets) { var solarSystemsToRemove = playersPlanets.Where(x => x.Galaxy == tempUserPlanet.Galaxy && x.SolarSystem == tempUserPlanet.SolarSystem).ToList(); foreach (var item in solarSystemsToRemove) { playersPlanets.Remove(item); } } }
private DateTime GetSolarSystemCreationDate(UserPlanet tempUserPlanet, ObservableCollection <UserPlanet> playersPlanets) { try { var solarSystemCreationDate = playersPlanets.First(x => x.Galaxy == tempUserPlanet.Galaxy && x.SolarSystem == tempUserPlanet.SolarSystem).CreationDate; return(solarSystemCreationDate); } catch (ArgumentNullException) { return(new DateTime(5, 1, 1, 1, 1, 1)); } catch (InvalidOperationException) { return(new DateTime(5, 1, 1, 1, 1, 1)); } }