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