2023-10-24 18:58:46 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using RoadTrain.DrawningObjects;
|
|
|
|
|
using RoadTrain.MovementStrategy;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace RoadTrain.Generics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Класс для хранения коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class RoadTrainGenericStorage
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Словарь (хранилище)
|
|
|
|
|
/// </summary>
|
|
|
|
|
readonly Dictionary<string, RoadTrainGenericCollection<DrawningRoadTrain,
|
|
|
|
|
DrawningObjectTrain>> _trainStorages;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий наборов
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<string> Keys => _trainStorages.Keys.ToList();
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ширина окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureWidth;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Высота окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _pictureHeight;
|
2023-11-23 00:09:21 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly char _separatorRecords = ';';
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи информации по объекту в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly char _separatorForObject = ':';
|
|
|
|
|
|
2023-10-24 18:58:46 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pictureWidth"></param>
|
|
|
|
|
/// <param name="pictureHeight"></param>
|
|
|
|
|
public RoadTrainGenericStorage(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
|
|
|
|
_trainStorages = new Dictionary<string,
|
|
|
|
|
RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>>();
|
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название набора</param>
|
|
|
|
|
public void AddSet(string name)
|
|
|
|
|
{
|
|
|
|
|
if (_trainStorages.ContainsKey(name)) return;
|
|
|
|
|
_trainStorages[name] = new RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>(_pictureWidth, _pictureHeight);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название набора</param>
|
|
|
|
|
public void DelSet(string name)
|
|
|
|
|
{
|
|
|
|
|
if (_trainStorages.ContainsKey(name)) _trainStorages.Remove(name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Доступ к набору
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ind"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>?
|
|
|
|
|
this[string ind]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_trainStorages.ContainsKey(ind)) return _trainStorages[ind];
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-23 00:09:21 +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,
|
|
|
|
|
RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>> record in _trainStorages)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder records = new();
|
|
|
|
|
foreach (DrawningRoadTrain? elem in record.Value.GetTrains)
|
|
|
|
|
{
|
|
|
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
|
|
|
}
|
|
|
|
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
|
|
|
}
|
|
|
|
|
if (data.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
string dataStr = data.ToString();
|
|
|
|
|
using (StreamWriter writer = new StreamWriter(filename))
|
|
|
|
|
{
|
|
|
|
|
writer.WriteLine("TrainStorage");
|
|
|
|
|
writer.WriteLine(dataStr);
|
|
|
|
|
}
|
|
|
|
|
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 reader = new StreamReader(filename))
|
|
|
|
|
{
|
|
|
|
|
string checker = reader.ReadLine();
|
|
|
|
|
if (checker == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!checker.StartsWith("TrainStorage"))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_trainStorages.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];
|
|
|
|
|
RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain> collection = new(_pictureWidth, _pictureHeight);
|
|
|
|
|
foreach (string data in strs.Split('|')[1].Split(';').Reverse())
|
|
|
|
|
{
|
|
|
|
|
DrawningRoadTrain? train =
|
|
|
|
|
data?.CreateDrawningTrain(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
|
|
|
if (train != null)
|
|
|
|
|
{
|
|
|
|
|
if (collection + train == -1)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
_trainStorages.Add(name, collection);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-24 18:58:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|