196 lines
7.0 KiB
C#
196 lines
7.0 KiB
C#
using WarmlyShip.DrawningObjects;
|
|
using WarmlyShip.MovementStrategy;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace WarmlyShip.Generics
|
|
{
|
|
/// <summary>
|
|
/// Класс для хранения коллекции
|
|
/// </summary>
|
|
internal class ShipsGenericStorage
|
|
{
|
|
/// <summary>
|
|
/// Словарь (хранилище)
|
|
/// </summary>
|
|
readonly Dictionary<string, ShipsGenericCollection<DrawningShip,
|
|
DrawningObjectShip>> _shipStorages;
|
|
|
|
/// <summary>
|
|
/// Возвращение списка названий наборов
|
|
/// </summary>
|
|
public List<string> Keys => _shipStorages.Keys.ToList();
|
|
|
|
/// <summary>
|
|
/// Ширина окна отрисовки
|
|
/// </summary>
|
|
private readonly int _pictureWidth;
|
|
|
|
/// <summary>
|
|
/// Высота окна отрисовки
|
|
/// </summary>
|
|
private readonly int _pictureHeight;
|
|
|
|
/// <summary>
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
/// </summary>
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
/// <summary>
|
|
/// Разделитель для записей коллекции данных в файл
|
|
/// </summary>
|
|
private readonly char _separatorRecords = ';';
|
|
/// <summary>
|
|
/// Разделитель для записи информации по объекту в файл
|
|
/// </summary>
|
|
private static readonly char _separatorForObject = ':';
|
|
|
|
/// <summary>
|
|
/// Конструктор
|
|
/// </summary>
|
|
/// <param name="pictureWidth"></param>
|
|
/// <param name="pictureHeight"></param>
|
|
public ShipsGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_shipStorages = new Dictionary<string, ShipsGenericCollection<DrawningShip, DrawningObjectShip>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
|
|
/// <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, ShipsGenericCollection<DrawningShip, DrawningObjectShip>> record in _shipStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawningShip? elem in record.Value.GetShips)
|
|
{
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
}
|
|
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
|
|
}
|
|
|
|
if (data.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
string toWrite = $"ShipStorage{Environment.NewLine}{data}";
|
|
var strs = toWrite.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
using (StreamWriter sw = new(filename))
|
|
{
|
|
foreach (var str in strs)
|
|
{
|
|
sw.WriteLine(str);
|
|
}
|
|
}
|
|
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 sr = new(filename))
|
|
{
|
|
string str = sr.ReadLine();
|
|
var strs = str.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
if (strs == null || strs.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (!strs[0].StartsWith("ShipStorage"))
|
|
{
|
|
return false;
|
|
}
|
|
_shipStorages.Clear();
|
|
do
|
|
{
|
|
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 2)
|
|
{
|
|
str = sr.ReadLine();
|
|
continue;
|
|
}
|
|
ShipsGenericCollection<DrawningShip, DrawningObjectShip> collection = new(_pictureWidth, _pictureHeight);
|
|
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
DrawningShip? ship =
|
|
elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (ship != null)
|
|
{
|
|
if (!(collection + ship))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
_shipStorages.Add(record[0], collection);
|
|
|
|
str = sr.ReadLine();
|
|
} while (str != null);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавление набора
|
|
/// </summary>
|
|
/// <param name="name">Название набора</param>
|
|
public void AddSet(string name)
|
|
{
|
|
_shipStorages.Add(name, new ShipsGenericCollection<DrawningShip, DrawningObjectShip>(_pictureWidth, _pictureHeight));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление набора
|
|
/// </summary>
|
|
/// <param name="name">Название набора</param>
|
|
public void DelSet(string name)
|
|
{
|
|
if (!_shipStorages.ContainsKey(name))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_shipStorages.Remove(name);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Доступ к набору
|
|
/// </summary>
|
|
/// <param name="ind"></param>
|
|
/// <returns></returns>
|
|
public ShipsGenericCollection<DrawningShip, DrawningObjectShip>? this[string ind]
|
|
{
|
|
get
|
|
{
|
|
if (_shipStorages.ContainsKey(ind))
|
|
{
|
|
return _shipStorages[ind];
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|