PIbd-23-Kondratev-N.D.-Gaso.../GasolineTanker/ProjectGasolineTanker/Generic/TruckGenericStorage.cs

160 lines
6.4 KiB
C#
Raw Permalink Normal View History

2024-10-03 01:42:04 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-10-03 01:46:41 +04:00
using System.Xml.Linq;
2024-10-03 01:42:04 +04:00
using ProjectGasolineTanker.Drawings;
2024-10-03 01:46:41 +04:00
using ProjectGasolineTanker.Generics;
2024-10-03 01:42:04 +04:00
using ProjectGasolineTanker.MovementStratg;
namespace ProjectGasolineTanker.Generic
{
internal class TruckGenericStorage
{
2024-10-03 01:44:06 +04:00
//Словарь (хранилище)
2024-10-03 01:46:41 +04:00
readonly Dictionary<TruckCollectionInfo, TruckGenericCollection<DrawingTruck, DrawingObjectTruck>> _TruckStorages;
2024-10-03 01:44:06 +04:00
//Возвращение списка названий наборов
2024-10-03 01:46:41 +04:00
public List<TruckCollectionInfo> 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)
{
2024-10-03 01:46:41 +04:00
_TruckStorages = new Dictionary<TruckCollectionInfo, TruckGenericCollection<DrawingTruck, DrawingObjectTruck>>();
2024-10-03 01:42:04 +04:00
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
2024-10-03 01:46:41 +04:00
// Добавление набора
2024-10-03 01:42:04 +04:00
public void AddSet(string name)
{
2024-10-03 01:46:41 +04:00
TruckCollectionInfo set = new TruckCollectionInfo(name, string.Empty);
if (_TruckStorages.ContainsKey(set))
return;
_TruckStorages.Add(set, new TruckGenericCollection<DrawingTruck, DrawingObjectTruck>(_pictureWidth, _pictureHeight));
2024-10-03 01:42:04 +04:00
}
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:46:41 +04:00
TruckCollectionInfo set = new TruckCollectionInfo(name, string.Empty);
2024-10-03 01:44:06 +04:00
// TODO Прописать логику для удаления
2024-10-03 01:46:41 +04:00
if (!_TruckStorages.ContainsKey(set))
2024-10-03 01:42:04 +04:00
{
2024-10-03 01:46:41 +04:00
return;
2024-10-03 01:42:04 +04:00
}
2024-10-03 01:46:41 +04:00
_TruckStorages.Remove(set);
2024-10-03 01:42:04 +04:00
}
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:46:41 +04:00
TruckCollectionInfo set = new TruckCollectionInfo(ind, string.Empty);
2024-10-03 01:43:12 +04:00
// TODO Продумать логику получения набора
2024-10-03 01:46:41 +04:00
if (!_TruckStorages.ContainsKey(set))
2024-10-03 01:42:04 +04:00
{
2024-10-03 01:46:41 +04:00
return null;
2024-10-03 01:42:04 +04:00
}
2024-10-03 01:46:41 +04:00
return _TruckStorages[set];
2024-10-03 01:42:04 +04:00
}
}
2024-10-03 01:44:06 +04:00
2024-10-03 01:45:35 +04:00
public void SaveData(string filename)
2024-10-03 01:44:06 +04:00
{
if (File.Exists(filename))
{
File.Delete(filename);
}
2024-10-03 01:45:35 +04:00
StringBuilder data = new();
2024-10-03 01:46:41 +04:00
foreach (KeyValuePair<TruckCollectionInfo,
2024-10-03 01:45:35 +04:00
TruckGenericCollection<DrawingTruck, DrawingObjectTruck>> record in _TruckStorages)
2024-10-03 01:44:06 +04:00
{
2024-10-03 01:45:35 +04:00
StringBuilder records = new();
foreach (DrawingTruck? elem in record.Value.GetTruck)
2024-10-03 01:44:06 +04:00
{
2024-10-03 01:45:35 +04:00
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
2024-10-03 01:44:06 +04:00
}
2024-10-03 01:46:41 +04:00
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
2024-10-03 01:44:06 +04:00
}
2024-10-03 01:45:35 +04:00
if (data.Length == 0)
{
throw new Exception("Невалиданя операция, нет данных для сохранения");
}
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new
2024-10-03 01:46:41 +04:00
UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}");
2024-10-03 01:45:35 +04:00
fs.Write(info, 0, info.Length);
return;
2024-10-03 01:44:06 +04:00
}
2024-10-03 01:45:35 +04:00
public void LoadData(string filename)
2024-10-03 01:44:06 +04:00
{
if (!File.Exists(filename))
{
2024-10-03 01:45:35 +04:00
throw new Exception("Файл не найден");
2024-10-03 01:44:06 +04:00
}
2024-10-03 01:45:35 +04:00
string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open))
2024-10-03 01:44:06 +04:00
{
2024-10-03 01:45:35 +04:00
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new(true);
while (fs.Read(b, 0, b.Length) > 0)
2024-10-03 01:44:06 +04:00
{
2024-10-03 01:45:35 +04:00
bufferTextFromFile += temp.GetString(b);
2024-10-03 01:44:06 +04:00
}
2024-10-03 01:45:35 +04:00
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
throw new Exception("Нет данных для загрузки");
}
if (!strs[0].StartsWith("TruckStorage"))
{
//если нет такой записи, то это не те данные
throw new Exception("Неверный формат данных");
}
_TruckStorages.Clear();
foreach (string data in strs)
{
string[] record = data.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
2024-10-03 01:44:06 +04:00
{
2024-10-03 01:45:35 +04:00
continue;
}
TruckGenericCollection<DrawingTruck, DrawingObjectTruck>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
2024-10-03 01:46:41 +04:00
DrawingTruck? Truck =
2024-10-03 01:45:35 +04:00
elem?.CreateDrawingTruck(_separatorForObject, _pictureWidth, _pictureHeight);
2024-10-03 01:46:41 +04:00
if (Truck != null)
2024-10-03 01:44:06 +04:00
{
2024-10-03 01:46:41 +04:00
if ((collection + Truck)==-1)
2024-10-03 01:44:06 +04:00
{
2024-10-03 01:45:35 +04:00
throw new Exception("Ошибка добавления в коллекцию");
2024-10-03 01:44:06 +04:00
}
}
}
2024-10-03 01:46:41 +04:00
_TruckStorages.Add(new TruckCollectionInfo(record[0],
string.Empty), collection);
2024-10-03 01:44:06 +04:00
}
}
2024-10-03 01:42:04 +04:00
}
}