174 lines
6.7 KiB
C#
174 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Cruiser.Drawing;
|
|
using Cruiser.MovementStrategy;
|
|
|
|
namespace Cruiser.Generics
|
|
{
|
|
internal class CruisersGenericStorage
|
|
{
|
|
/// <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, CarsGenericCollection<DrawingCruiser, DrawningObjectCar>> _cruiserStorages;
|
|
/// <summary>
|
|
/// Возвращение списка названий наборов
|
|
/// </summary>
|
|
public List<string> Keys => _cruiserStorages.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 CruisersGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_cruiserStorages = new Dictionary<string, CarsGenericCollection<DrawingCruiser, DrawningObjectCar>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
/// <summary>
|
|
/// Добавление набора
|
|
/// </summary>
|
|
/// <param name="name">Название набора</param>
|
|
public void AddSet(string name)
|
|
{
|
|
if (_cruiserStorages.ContainsKey(name)) return;
|
|
_cruiserStorages[name] = new CarsGenericCollection<DrawingCruiser, DrawningObjectCar>(_pictureWidth, _pictureHeight);
|
|
}
|
|
/// <summary>
|
|
/// Удаление набора
|
|
/// </summary>
|
|
/// <param name="name">Название набора</param>
|
|
public void DelSet(string name)
|
|
{
|
|
if (!_cruiserStorages.ContainsKey(name)) return;
|
|
_cruiserStorages.Remove(name);
|
|
}
|
|
/// <summary>
|
|
/// Доступ к набору
|
|
/// </summary>
|
|
/// <param name="ind"></param>
|
|
/// <returns></returns>
|
|
public CarsGenericCollection<DrawingCruiser, DrawningObjectCar>?
|
|
this[string ind]
|
|
{
|
|
get
|
|
{
|
|
if (_cruiserStorages.ContainsKey(ind)) return _cruiserStorages[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, CarsGenericCollection<DrawingCruiser, DrawningObjectCar>> record in _cruiserStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawingCruiser? elem in record.Value.GetCruisers)
|
|
{
|
|
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("CruiserStorage");
|
|
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("CruiserStorage"))
|
|
{
|
|
return false;
|
|
}
|
|
_cruiserStorages.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];
|
|
CarsGenericCollection<DrawingCruiser, DrawningObjectCar> collection = new(_pictureWidth, _pictureHeight);
|
|
foreach (string data in strs.Split('|')[1].Split(';').Reverse())
|
|
{
|
|
DrawingCruiser? cruiser = data?.CreateDrawingCruiser(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (cruiser != null)
|
|
{
|
|
if (collection + cruiser == false)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|
|
_cruiserStorages.Add(name, collection);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|