2023-12-02 00:46:08 +04:00
|
|
|
|
using WarmlyShip.DrawningObjects;
|
|
|
|
|
using WarmlyShip.MovementStrategy;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace WarmlyShip.Generics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Класс для хранения коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class ShipsGenericStorage
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Словарь (хранилище)
|
|
|
|
|
/// </summary>
|
2023-12-30 13:14:20 +04:00
|
|
|
|
readonly Dictionary<ShipsCollectionInfo, ShipsGenericCollection<DrawningShip,
|
2023-12-02 00:46:08 +04:00
|
|
|
|
DrawningObjectShip>> _shipStorages;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий наборов
|
|
|
|
|
/// </summary>
|
2023-12-30 13:14:20 +04:00
|
|
|
|
public List<ShipsCollectionInfo> Keys => _shipStorages.Keys.ToList();
|
2023-12-02 00:46:08 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ширина окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Высота окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
|
2023-12-15 20:50:00 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly char _separatorRecords = ';';
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи информации по объекту в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly char _separatorForObject = ':';
|
|
|
|
|
|
2023-12-02 00:46:08 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pictureWidth"></param>
|
|
|
|
|
/// <param name="pictureHeight"></param>
|
|
|
|
|
public ShipsGenericStorage(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
2023-12-30 13:14:20 +04:00
|
|
|
|
_shipStorages = new Dictionary<ShipsCollectionInfo, ShipsGenericCollection<DrawningShip, DrawningObjectShip>>();
|
2023-12-02 00:46:08 +04:00
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 20:50:00 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение информации по кораблям в хранилище в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
2023-12-26 23:00:14 +04:00
|
|
|
|
public void SaveData(string filename)
|
2023-12-15 20:50:00 +04:00
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
StringBuilder data = new();
|
2023-12-30 13:14:20 +04:00
|
|
|
|
foreach (KeyValuePair<ShipsCollectionInfo, ShipsGenericCollection<DrawningShip, DrawningObjectShip>> record in _shipStorages)
|
2023-12-15 20:50:00 +04:00
|
|
|
|
{
|
|
|
|
|
StringBuilder records = new();
|
|
|
|
|
foreach (DrawningShip? elem in record.Value.GetShips)
|
|
|
|
|
{
|
|
|
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
|
|
|
}
|
2023-12-30 13:14:20 +04:00
|
|
|
|
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
|
2023-12-15 20:50:00 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.Length == 0)
|
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
throw new IOException("Невалидная операция, нет данных для сохранения");
|
2023-12-15 20:50:00 +04:00
|
|
|
|
}
|
|
|
|
|
string toWrite = $"ShipStorage{Environment.NewLine}{data}";
|
|
|
|
|
var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
using (StreamWriter sw = new(filename))
|
|
|
|
|
{
|
|
|
|
|
foreach (var str in strs)
|
|
|
|
|
{
|
|
|
|
|
sw.WriteLine(str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации по кораблям в хранилище из файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
2023-12-26 23:00:14 +04:00
|
|
|
|
public void LoadData(string filename)
|
2023-12-15 20:50:00 +04:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
throw new IOException("Файл не найден");
|
2023-12-15 20:50:00 +04:00
|
|
|
|
}
|
|
|
|
|
using (StreamReader sr = new(filename))
|
|
|
|
|
{
|
|
|
|
|
string str = sr.ReadLine();
|
|
|
|
|
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (strs == null || strs.Length == 0)
|
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
throw new IOException("Нет данных для загрузки");
|
2023-12-15 20:50:00 +04:00
|
|
|
|
}
|
|
|
|
|
if (!strs[0].StartsWith("ShipStorage"))
|
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
throw new IOException("Неверный формат данных");
|
2023-12-15 20:50:00 +04:00
|
|
|
|
}
|
|
|
|
|
_shipStorages.Clear();
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (record.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
str = sr.ReadLine();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ShipsGenericCollection<DrawningShip, DrawningObjectShip> 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))
|
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
throw new IOException("Ошибка добавления в коллекцию");
|
2023-12-15 20:50:00 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-30 13:14:20 +04:00
|
|
|
|
_shipStorages.Add(new ShipsCollectionInfo(record[0], string.Empty), collection);
|
2023-12-15 20:50:00 +04:00
|
|
|
|
|
|
|
|
|
str = sr.ReadLine();
|
|
|
|
|
} while (str != null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 00:46:08 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название набора</param>
|
|
|
|
|
public void AddSet(string name)
|
|
|
|
|
{
|
2023-12-30 13:14:20 +04:00
|
|
|
|
_shipStorages.Add(new ShipsCollectionInfo(name, string.Empty), new ShipsGenericCollection<DrawningShip, DrawningObjectShip>(_pictureWidth, _pictureHeight));
|
2023-12-02 00:46:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название набора</param>
|
|
|
|
|
public void DelSet(string name)
|
|
|
|
|
{
|
2023-12-30 13:14:20 +04:00
|
|
|
|
if (!_shipStorages.ContainsKey(new ShipsCollectionInfo(name, string.Empty)))
|
2023-12-02 00:46:08 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-30 13:14:20 +04:00
|
|
|
|
_shipStorages.Remove(new ShipsCollectionInfo(name, string.Empty));
|
2023-12-02 00:46:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Доступ к набору
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ind"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ShipsGenericCollection<DrawningShip, DrawningObjectShip>? this[string ind]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-12-30 13:14:20 +04:00
|
|
|
|
ShipsCollectionInfo indObj = new ShipsCollectionInfo(ind, string.Empty);
|
|
|
|
|
if (_shipStorages.ContainsKey(indObj))
|
2023-12-02 00:46:08 +04:00
|
|
|
|
{
|
2023-12-30 13:14:20 +04:00
|
|
|
|
return _shipStorages[indObj];
|
2023-12-02 00:46:08 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|