using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ContainerShip.MovementStrategy; using ContainerShip.DrawningObjects; using ContainerShip.Entities; namespace ContainerShip.Generic { internal class ShipsGenericStorage { private static readonly char _separatorForKeyValue = '|'; private readonly char _separatorRecords = ';'; private static readonly char _separatorForObject = ':'; readonly Dictionary> _shipStorages; public List Keys => _shipStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; public ShipsGenericStorage(int pictureWidth, int pictureHeight) { _shipStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string name) { foreach (string nameStorage in Keys) { if (nameStorage == name) { MessageBox.Show("Набор с заданным именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } _shipStorages.Add(name, new ShipsGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name) { if (_shipStorages.ContainsKey(name)) { _shipStorages.Remove(name); } } public bool SaveData(string filename) { if (_shipStorages.Count == 0) { throw new InvalidOperationException("Невалидная операция, нет данных для сохранения"); } if (File.Exists(filename)) { File.Delete(filename); } using (StreamWriter sw = File.CreateText(filename)) { sw.WriteLine($"ShipStorage"); foreach (var record in _shipStorages) { StringBuilder records = new(); if (record.Value.count <= 0) { throw new InvalidOperationException("Невалидная операция, нет данных для сохранения"); } foreach (DrawningShip? elem in record.Value.GetShip) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } sw.WriteLine($"{record.Key}{_separatorForKeyValue}{records}"); } } return true; } public bool LoadData(string filename) { if (!File.Exists(filename)) { throw new FileNotFoundException($"Файл {filename} не найден"); } using (StreamReader sr = File.OpenText(filename)) { string? curLine = sr.ReadLine(); if (curLine == null || curLine.Length == 0 || !curLine.StartsWith("ShipStorage")) { throw new ArgumentException("Неверный формат данных"); } _shipStorages.Clear(); curLine = sr.ReadLine(); if (curLine == null || curLine.Length == 0) { throw new ArgumentException("Нет данных"); } while (curLine != null) { if (!curLine.Contains(_separatorRecords)) { throw new ArgumentException("Коллекция пуста"); } string[] record = curLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); ShipsGenericCollection collection = new(_pictureWidth, _pictureHeight); string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { DrawningShip? ship = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); if (ship != null) { if (collection + ship == -1) { throw new InvalidOperationException("Невалидная операция, ошибка добавления в коллекцию"); } } } _shipStorages.Add(record[0], collection); curLine = sr.ReadLine(); } } return true; } public ShipsGenericCollection? this[string ind] { get { if (_shipStorages.ContainsKey(ind)) { return _shipStorages[ind]; } return null; } } } }