2024-10-03 01:42:04 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ProjectGasolineTanker.Drawings;
|
|
|
|
|
using ProjectGasolineTanker.MovementStratg;
|
|
|
|
|
|
|
|
|
|
namespace ProjectGasolineTanker.Generic
|
|
|
|
|
{
|
|
|
|
|
internal class TruckGenericStorage
|
|
|
|
|
{
|
2024-10-03 01:44:06 +04:00
|
|
|
|
//Словарь (хранилище)
|
2024-10-03 01:42:04 +04:00
|
|
|
|
readonly Dictionary<string, TruckGenericCollection<DrawingTruck, DrawingObjectTruck>> _TruckStorages;
|
2024-10-03 01:44:06 +04:00
|
|
|
|
//Возвращение списка названий наборов
|
2024-10-03 01:42:04 +04:00
|
|
|
|
public List<string> Keys => _TruckStorages.Keys.ToList();
|
2024-10-03 01:44:06 +04:00
|
|
|
|
//Ширина окна отрисовки
|
2024-10-03 01:42:04 +04:00
|
|
|
|
private readonly int _pictureWidth;
|
2024-10-03 01:44:06 +04:00
|
|
|
|
//Высота окна отрисовки
|
2024-10-03 01:42:04 +04:00
|
|
|
|
private readonly int _pictureHeight;
|
|
|
|
|
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
|
|
|
// Разделитель для записей коллекции данных в файл
|
|
|
|
|
private readonly char _separatorRecords = ';';
|
|
|
|
|
// Разделитель для записи информации по объекту в файл
|
|
|
|
|
private static readonly char _separatorForObject = ':';
|
|
|
|
|
|
2024-10-03 01:42:04 +04:00
|
|
|
|
public TruckGenericStorage(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
|
|
|
|
_TruckStorages = new Dictionary<string, TruckGenericCollection<DrawingTruck, DrawingObjectTruck>>();
|
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// Добавление набора
|
2024-10-03 01:42:04 +04:00
|
|
|
|
public void AddSet(string name)
|
|
|
|
|
{
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// TODO Прописать логику для добавления
|
|
|
|
|
// проверка, что нет набора с таким именем
|
2024-10-03 01:42:04 +04:00
|
|
|
|
foreach (string nameStorage in Keys)
|
|
|
|
|
{
|
|
|
|
|
if (nameStorage == name)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Набор с заданным именем уже занят", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_TruckStorages.Add(name, new TruckGenericCollection<DrawingTruck, DrawingObjectTruck>(_pictureWidth, _pictureHeight));
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// Удаление набора
|
2024-10-03 01:42:04 +04:00
|
|
|
|
public void DelSet(string name)
|
|
|
|
|
{
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// TODO Прописать логику для удаления
|
2024-10-03 01:42:04 +04:00
|
|
|
|
if (_TruckStorages.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
_TruckStorages.Remove(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// Доступ к набору
|
2024-10-03 01:42:04 +04:00
|
|
|
|
public TruckGenericCollection<DrawingTruck, DrawingObjectTruck>? this[string ind]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2024-10-03 01:43:12 +04:00
|
|
|
|
// TODO Продумать логику получения набора
|
2024-10-03 01:42:04 +04:00
|
|
|
|
if (_TruckStorages.ContainsKey(ind))
|
|
|
|
|
{
|
|
|
|
|
return _TruckStorages[ind];
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-03 01:44:06 +04:00
|
|
|
|
|
|
|
|
|
public bool SaveData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (StreamWriter sw = File.CreateText(filename))
|
|
|
|
|
{
|
|
|
|
|
sw.WriteLine($"TruckStorage");
|
|
|
|
|
foreach (var record in _TruckStorages)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder records = new();
|
|
|
|
|
foreach (DrawingTruck? elem in record.Value.GetTruck)
|
|
|
|
|
{
|
|
|
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
|
|
|
}
|
|
|
|
|
sw.WriteLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Загрузка информации по автомобилям в хранилище из файла
|
|
|
|
|
public bool LoadData(string filename)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (StreamReader sr = File.OpenText(filename))
|
|
|
|
|
{
|
|
|
|
|
// 1-ая строка
|
|
|
|
|
string? curLine = sr.ReadLine();
|
|
|
|
|
// пустая или не те данные
|
|
|
|
|
if (curLine == null || !curLine.Contains("TruckStorage"))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// очищаем
|
|
|
|
|
_TruckStorages.Clear();
|
|
|
|
|
|
|
|
|
|
// загружаем данные построчно
|
|
|
|
|
curLine = sr.ReadLine();
|
|
|
|
|
while (curLine != null)
|
|
|
|
|
{
|
|
|
|
|
// загружаем запись
|
|
|
|
|
string[] record = curLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
// загружаем набор
|
|
|
|
|
TruckGenericCollection<DrawingTruck, DrawingObjectTruck> collection = new(_pictureWidth, _pictureHeight);
|
|
|
|
|
// record[0] - название набора, record[1] - куча объектов
|
|
|
|
|
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);
|
|
|
|
|
curLine = sr.ReadLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-10-03 01:42:04 +04:00
|
|
|
|
}
|
|
|
|
|
}
|