2023-11-24 11:41:50 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Cruiser.Drawing;
|
|
|
|
|
using Cruiser.MovementStrategy;
|
2023-12-18 15:13:53 +04:00
|
|
|
|
using System.IO;
|
2023-11-24 11:41:50 +04:00
|
|
|
|
|
|
|
|
|
namespace Cruiser.Generics
|
|
|
|
|
{
|
|
|
|
|
internal class CruisersGenericStorage
|
|
|
|
|
{
|
2023-12-03 15:02:10 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly char _separatorRecords = ';';
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи информации по объекту в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly char _separatorForObject = ':';
|
|
|
|
|
|
2023-11-24 11:41:50 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Словарь (хранилище)
|
|
|
|
|
/// </summary>
|
2023-12-03 15:02:10 +04:00
|
|
|
|
readonly Dictionary<string, CarsGenericCollection<DrawingCruiser, DrawningObjectCar>> _cruiserStorages;
|
2023-11-24 11:41:50 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий наборов
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<string> Keys => _cruiserStorages.Keys.ToList();
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ширина окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Высота окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pictureWidth"></param>
|
|
|
|
|
/// <param name="pictureHeight"></param>
|
|
|
|
|
public CruisersGenericStorage(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
2023-12-03 15:02:10 +04:00
|
|
|
|
_cruiserStorages = new Dictionary<string, CarsGenericCollection<DrawingCruiser, DrawningObjectCar>>();
|
2023-11-24 11:41:50 +04:00
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название набора</param>
|
|
|
|
|
public void AddSet(string name)
|
|
|
|
|
{
|
|
|
|
|
if (_cruiserStorages.ContainsKey(name)) return;
|
|
|
|
|
_cruiserStorages[name] = new CarsGenericCollection<DrawingCruiser, DrawningObjectCar>(_pictureWidth, _pictureHeight);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название набора</param>
|
|
|
|
|
public void DelSet(string name)
|
|
|
|
|
{
|
|
|
|
|
if (!_cruiserStorages.ContainsKey(name)) return;
|
|
|
|
|
_cruiserStorages.Remove(name);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Доступ к набору
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ind"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public CarsGenericCollection<DrawingCruiser, DrawningObjectCar>?
|
|
|
|
|
this[string ind]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_cruiserStorages.ContainsKey(ind)) return _cruiserStorages[ind];
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-03 15:02:10 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение информации по автомобилям в хранилище в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
|
|
|
|
public bool SaveData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
StringBuilder data = new();
|
|
|
|
|
foreach (KeyValuePair<string, CarsGenericCollection<DrawingCruiser, DrawningObjectCar>> record in _cruiserStorages)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder records = new();
|
|
|
|
|
foreach (DrawingCruiser? elem in record.Value.GetCruisers)
|
|
|
|
|
{
|
|
|
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
|
|
|
}
|
|
|
|
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
|
|
|
}
|
|
|
|
|
if (data.Length == 0)
|
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
throw new Exception("Невалиданя операция, нет данных для сохранения");
|
2023-12-03 15:02:10 +04:00
|
|
|
|
}
|
|
|
|
|
string dataStr = data.ToString();
|
|
|
|
|
using (StreamWriter writer = new StreamWriter(filename))
|
|
|
|
|
{
|
|
|
|
|
writer.WriteLine("CruiserStorage");
|
|
|
|
|
writer.WriteLine(dataStr);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации по крейсеру в хранилище из файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
|
|
|
|
public bool LoadData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
throw new FileNotFoundException("Файл не найден");
|
2023-12-03 15:02:10 +04:00
|
|
|
|
}
|
|
|
|
|
using (StreamReader reader = new StreamReader(filename))
|
|
|
|
|
{
|
|
|
|
|
string checker = reader.ReadLine();
|
|
|
|
|
if (checker == null)
|
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
throw new NullReferenceException("Нет данных для загрузки");
|
2023-12-03 15:02:10 +04:00
|
|
|
|
}
|
|
|
|
|
if (!checker.StartsWith("CruiserStorage"))
|
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
throw new FormatException("Неверный формат данных");
|
2023-12-03 15:02:10 +04:00
|
|
|
|
}
|
|
|
|
|
_cruiserStorages.Clear();
|
|
|
|
|
string strs;
|
|
|
|
|
bool firstinit = true;
|
|
|
|
|
while ((strs = reader.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
if (strs == null && firstinit)
|
|
|
|
|
return false;
|
|
|
|
|
if (strs == null)
|
|
|
|
|
break;
|
|
|
|
|
if (strs == string.Empty)
|
|
|
|
|
break;
|
|
|
|
|
firstinit = false;
|
|
|
|
|
string name = strs.Split('|')[0];
|
|
|
|
|
CarsGenericCollection<DrawingCruiser, DrawningObjectCar> collection = new(_pictureWidth, _pictureHeight);
|
|
|
|
|
foreach (string data in strs.Split('|')[1].Split(';').Reverse())
|
|
|
|
|
{
|
|
|
|
|
DrawingCruiser? cruiser = data?.CreateDrawingCruiser(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
|
|
|
if (cruiser != null)
|
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
try
|
2023-12-03 15:02:10 +04:00
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
bool? tmp = collection + cruiser;
|
|
|
|
|
}
|
|
|
|
|
catch (ApplicationException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException($"Ошибка добавления в коллекцию: {ex.Message}");
|
2023-12-03 15:02:10 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
_cruiserStorages.Add(name, collection);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-24 11:41:50 +04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|