2024-04-30 11:26:54 +04:00
|
|
|
|
using ProjectStormtrooper.Drawnings;
|
2024-05-09 22:08:07 +04:00
|
|
|
|
using ProjectStormtrooper.Exceptions;
|
2024-04-30 11:26:54 +04:00
|
|
|
|
using System;
|
2024-04-16 10:33:52 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ProjectStormtrooper.CollectionGenericObjects;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Класс-хранилище коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public class StorageCollection<T>
|
2024-04-30 11:26:54 +04:00
|
|
|
|
where T : DrawningStormtrooperBase
|
2024-04-16 10:33:52 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Словарь(хранилище) с коллекциями
|
|
|
|
|
/// </summary>
|
2024-05-16 21:57:38 +04:00
|
|
|
|
readonly Dictionary<CollectionInfo, ICollectionGenericObjects<T>> _storages;
|
2024-04-16 10:33:52 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий коллекций
|
|
|
|
|
/// </summary>
|
2024-05-16 21:57:38 +04:00
|
|
|
|
public List<CollectionInfo> Keys => _storages.Keys.ToList();
|
2024-04-16 10:33:52 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public StorageCollection()
|
|
|
|
|
{
|
2024-05-16 21:57:38 +04:00
|
|
|
|
_storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
|
2024-04-16 10:33:52 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление коллекции в хранилище
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
/// <param name="collectionType">Тип коллекции</param>
|
|
|
|
|
public void AddCollection(string name, CollectionType collectionType)
|
|
|
|
|
{
|
|
|
|
|
// TODO проверка, что name не пустой и нет в словаре записи с таким ключом
|
|
|
|
|
// TODO Прописать логику для добавления
|
2024-05-16 21:57:38 +04:00
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty);
|
|
|
|
|
if (_storages.ContainsKey(collectionInfo)) return;
|
|
|
|
|
if (collectionType == CollectionType.None) return;
|
|
|
|
|
else if (collectionType == CollectionType.Massive)
|
|
|
|
|
_storages[collectionInfo] = new MassiveGenericObjects<T>();
|
|
|
|
|
else if (collectionType == CollectionType.List)
|
|
|
|
|
_storages[collectionInfo] = new ListGenericObjects<T>();
|
2024-04-16 10:33:52 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
public void DelCollection(string name)
|
|
|
|
|
{
|
|
|
|
|
// TODO Прописать логику для удаления коллекции
|
2024-05-16 21:57:38 +04:00
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
|
|
|
|
|
if (_storages.ContainsKey(collectionInfo))
|
2024-04-16 10:33:52 +04:00
|
|
|
|
{
|
2024-05-16 21:57:38 +04:00
|
|
|
|
_storages.Remove(collectionInfo);
|
2024-04-16 10:33:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Доступ к коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ICollectionGenericObjects<T>? this[string name]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// TODO Продумать логику получения объекта
|
2024-05-16 21:57:38 +04:00
|
|
|
|
CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty);
|
|
|
|
|
if (_storages.ContainsKey(collectionInfo))
|
|
|
|
|
return _storages[collectionInfo];
|
2024-04-16 10:33:52 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-04-30 11:26:54 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ключевое слово, с которого должен начинаться файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _collectionKey = "CollectionsStorage";
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorForKeyValue = "|";
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorItems = ";";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение информации по штурмовику в хранилище в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
2024-05-09 22:08:07 +04:00
|
|
|
|
|
|
|
|
|
public void SaveData(string filename)
|
2024-04-30 11:26:54 +04:00
|
|
|
|
{
|
|
|
|
|
if (_storages.Count == 0)
|
|
|
|
|
{
|
2024-05-09 22:08:07 +04:00
|
|
|
|
throw new Exception("В хранилище отсутствует коллекция для сохранения");
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
using (StreamWriter writer = new StreamWriter(filename))
|
|
|
|
|
{
|
|
|
|
|
writer.Write(_collectionKey);
|
2024-05-16 21:57:38 +04:00
|
|
|
|
foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
|
2024-04-30 11:26:54 +04:00
|
|
|
|
{
|
|
|
|
|
writer.Write(Environment.NewLine);
|
|
|
|
|
if (value.Value.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
writer.Write(value.Key);
|
|
|
|
|
writer.Write(_separatorForKeyValue);
|
|
|
|
|
writer.Write(value.Value.GetCollectionType);
|
|
|
|
|
writer.Write(_separatorForKeyValue);
|
|
|
|
|
writer.Write(value.Value.MaxCount);
|
|
|
|
|
writer.Write(_separatorForKeyValue);
|
|
|
|
|
foreach (T? item in value.Value.GetItems())
|
|
|
|
|
{
|
|
|
|
|
string data = item?.GetDataForSave() ?? string.Empty;
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
writer.Write(data);
|
|
|
|
|
writer.Write(_separatorItems);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-09 22:08:07 +04:00
|
|
|
|
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации по штурмовику в хранилище из файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
2024-05-09 22:08:07 +04:00
|
|
|
|
|
|
|
|
|
public void LoadData(string filename)
|
2024-04-30 11:26:54 +04:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2024-05-09 22:08:07 +04:00
|
|
|
|
throw new Exception("Файл не существует");
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
|
|
|
|
using (StreamReader fs = File.OpenText(filename))
|
|
|
|
|
{
|
|
|
|
|
string str = fs.ReadLine();
|
|
|
|
|
if (str == null || str.Length == 0)
|
|
|
|
|
{
|
2024-05-09 22:08:07 +04:00
|
|
|
|
throw new Exception("В файле нет данных");
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
|
|
|
|
if (!str.StartsWith(_collectionKey))
|
|
|
|
|
{
|
2024-05-09 22:08:07 +04:00
|
|
|
|
throw new Exception("В файле неверные данные");
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
|
|
|
|
_storages.Clear();
|
|
|
|
|
string strs = "";
|
|
|
|
|
while ((strs = fs.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
2024-05-16 21:57:38 +04:00
|
|
|
|
if (record.Length != 3)
|
2024-04-30 11:26:54 +04:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-05-16 21:57:38 +04:00
|
|
|
|
CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ??
|
|
|
|
|
throw new Exception("Не удалось определить информацию коллекции: " + record[0]);
|
|
|
|
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionInfo.CollectionType) ??
|
|
|
|
|
throw new Exception("Не удалось создать коллекцию");
|
2024-04-30 11:26:54 +04:00
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
2024-05-09 22:08:07 +04:00
|
|
|
|
throw new Exception("Не удалось создать коллекцию");
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
2024-05-16 21:57:38 +04:00
|
|
|
|
collection.MaxCount = Convert.ToInt32(record[1]);
|
|
|
|
|
string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
2024-04-30 11:26:54 +04:00
|
|
|
|
foreach (string elem in set)
|
|
|
|
|
{
|
|
|
|
|
if (elem?.CreateDrawningStormtrooper() is T stormtrooper)
|
|
|
|
|
{
|
2024-05-09 22:08:07 +04:00
|
|
|
|
try
|
2024-04-30 11:26:54 +04:00
|
|
|
|
{
|
2024-05-09 22:08:07 +04:00
|
|
|
|
if (collection.Insert(stormtrooper) == -1)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Объект не удалось добавить в коллекцию" + record[3]);
|
|
|
|
|
}
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
2024-05-09 22:08:07 +04:00
|
|
|
|
catch(CollectionOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Коллекция переполнена", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-16 21:57:38 +04:00
|
|
|
|
_storages.Add(collectionInfo, collection);
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
2024-05-09 22:08:07 +04:00
|
|
|
|
|
2024-04-30 11:26:54 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание коллекции по типу
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collectionType"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static ICollectionGenericObjects<T>?
|
|
|
|
|
CreateCollection(CollectionType collectionType)
|
|
|
|
|
{
|
|
|
|
|
return collectionType switch
|
|
|
|
|
{
|
|
|
|
|
CollectionType.Massive => new MassiveGenericObjects<T>(),
|
|
|
|
|
CollectionType.List => new ListGenericObjects<T>(),
|
|
|
|
|
_ => null,
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-04-16 10:33:52 +04:00
|
|
|
|
}
|