164 lines
6.2 KiB
C#
164 lines
6.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using DumpTruck.DrawingObjects;
|
|
using DumpTruck.MovementStrategy;
|
|
|
|
namespace DumpTruck.Generics
|
|
{
|
|
/// <summary>
|
|
/// Класс для хранения коллекции
|
|
/// </summary>
|
|
internal class TrucksGenericStorage
|
|
{
|
|
/// <summary>
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
/// </summary>
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
/// <summary>
|
|
/// Разделитель для записей коллекции данных в файл
|
|
/// </summary>
|
|
private readonly char _separatorRecords = ';';
|
|
/// <summary>
|
|
/// Разделитель для записи информации по объекту в файл
|
|
/// </summary>
|
|
private static readonly char _separatorForObject = ':';
|
|
/// <summary>
|
|
/// Словарь (хранилище)
|
|
/// </summary>
|
|
readonly Dictionary<string, TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>> _truckStorages;
|
|
/// <summary>
|
|
/// Возвращение списка названий наборов
|
|
/// </summary>
|
|
public List<string> Keys => _truckStorages.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 TrucksGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_truckStorages = new Dictionary<string, TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
/// <summary>
|
|
/// Добавление набора
|
|
/// </summary>
|
|
/// <param name="name">Название набора</param>
|
|
public void AddSet(string name)
|
|
{
|
|
if (!_truckStorages.ContainsKey(name)) _truckStorages.Add(name, new TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>(_pictureWidth, _pictureHeight));
|
|
}
|
|
/// <summary>
|
|
/// Удаление набора
|
|
/// </summary>
|
|
/// <param name="name">Название набора</param>
|
|
public void DelSet(string name)
|
|
{
|
|
if (_truckStorages.ContainsKey(name)) _truckStorages.Remove(name);
|
|
}
|
|
/// <summary>
|
|
/// Доступ к набору
|
|
/// </summary>
|
|
/// <param name="ind"></param>
|
|
/// <returns></returns>
|
|
public TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>?
|
|
this[string ind]
|
|
{
|
|
get
|
|
{
|
|
if (_truckStorages.ContainsKey(ind)) return _truckStorages[ind];
|
|
return null;
|
|
}
|
|
}
|
|
/// <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, TrucksGenericCollection<DrawingTruck, DrawningObjectTruck>> record in _truckStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawingTruck? elem in record.Value.GetTrucks)
|
|
{
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
}
|
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
}
|
|
if (data.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
using StreamWriter sw = new(filename);
|
|
sw.Write($"TruckStorage{Environment.NewLine}{data}");
|
|
return true;
|
|
}
|
|
/// <summary>
|
|
/// Загрузка информации по автомобилям в хранилище из файла
|
|
/// </summary>
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
|
public bool LoadData(string filename)
|
|
{
|
|
if (!File.Exists(filename))
|
|
{
|
|
return false;
|
|
}
|
|
using (StreamReader sr = new(filename))
|
|
{
|
|
string str = sr.ReadLine();
|
|
if (str == null || str.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (!str.StartsWith("TruckStorage"))
|
|
{
|
|
return false;
|
|
}
|
|
_truckStorages.Clear();
|
|
while ((str = sr.ReadLine()) != null)
|
|
{
|
|
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 2)
|
|
{
|
|
continue;
|
|
}
|
|
TrucksGenericCollection < DrawingTruck, DrawningObjectTruck > collection = new(_pictureWidth, _pictureHeight);
|
|
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
DrawingTruck? truck = elem?.CreateDrawingTruck(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (truck != null)
|
|
{
|
|
if (collection + truck == -1)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
_truckStorages.Add(record[0], collection);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|