129 lines
5.6 KiB
C#
129 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using ProjectAirplaneWithRadar.DrawningObjects;
|
|
using ProjectAirplaneWithRadar.MovementStrategy;
|
|
using ProjectAirplaneWithRadar.Generics;
|
|
|
|
namespace ProjectAirplaneWithRadar.Generics
|
|
{
|
|
internal class AirplanesGenericStorage
|
|
{
|
|
readonly Dictionary<AirplaneCollectionInfo, AirplanesGenericCollection<DrawningAirplane,DrawningObjectAirplane>> _airplanesStorages;
|
|
public List<AirplaneCollectionInfo> Keys => _airplanesStorages.Keys.ToList();
|
|
private readonly int _pictureWidth;
|
|
private readonly int _pictureHeight;
|
|
private static readonly char _separatorForKeyValue = '|';
|
|
private readonly char _separatorRecords = ';';
|
|
private static readonly char _separatorForObject = ':';
|
|
public AirplanesGenericStorage(int pictureWidth, int pictureHeight)
|
|
{
|
|
_airplanesStorages = new Dictionary<AirplaneCollectionInfo,AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
public bool SaveData(string filename)
|
|
{
|
|
if (File.Exists(filename))
|
|
{
|
|
File.Delete(filename);
|
|
}
|
|
StringBuilder data = new();
|
|
foreach (KeyValuePair<AirplaneCollectionInfo,
|
|
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplanesStorages)
|
|
{
|
|
StringBuilder records = new();
|
|
foreach (DrawningAirplane? elem in record.Value.GetAirplanes)
|
|
{
|
|
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
|
|
}
|
|
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
|
|
}
|
|
if (data.Length == 0)
|
|
{
|
|
throw new Exception("Невалиданя операция, нет данных для сохранения");
|
|
}
|
|
string toWrite = $"AirplanesStorage{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;
|
|
}
|
|
public bool LoadData(string filename)
|
|
{
|
|
if (!File.Exists(filename))
|
|
{
|
|
throw new IOException("Файл не найден");
|
|
}
|
|
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)
|
|
{
|
|
throw new IOException("Нет данных для загрузки");
|
|
}
|
|
if (!strs[0].StartsWith("AirplanesStorage"))
|
|
{
|
|
throw new IOException("Неверный формат данных");
|
|
}
|
|
_airplanesStorages.Clear();
|
|
do
|
|
{
|
|
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 2)
|
|
{
|
|
str = sr.ReadLine();
|
|
continue;
|
|
}
|
|
AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>
|
|
collection = new(_pictureWidth, _pictureHeight);
|
|
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
DrawningAirplane? airplane =
|
|
elem?.CreateDrawningAirplane(_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (airplane != null)
|
|
{
|
|
if (!(collection + airplane))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
_airplanesStorages.Add(new AirplaneCollectionInfo (record[0], string.Empty), collection);
|
|
str = sr.ReadLine();
|
|
} while (str != null);
|
|
}
|
|
return true;
|
|
}
|
|
public void AddSet(string name)
|
|
{
|
|
_airplanesStorages.Add(new AirplaneCollectionInfo(name, string.Empty), new AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
|
|
}
|
|
public void DelSet(string name)
|
|
{
|
|
if (!_airplanesStorages.ContainsKey(new AirplaneCollectionInfo(name, string.Empty)))
|
|
return;
|
|
_airplanesStorages.Remove(new AirplaneCollectionInfo(name, string.Empty));
|
|
}
|
|
public AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>?this[string ind]
|
|
{
|
|
get
|
|
{
|
|
AirplaneCollectionInfo indObj = new AirplaneCollectionInfo(ind, string.Empty);
|
|
if (_airplanesStorages.ContainsKey(indObj))
|
|
return _airplanesStorages[indObj];
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|